Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

tests: add e2e ui tests (#3104)

Mohamed Bassemfcdbdd13d99d
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index ef582f20..b0bc5835 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -80,6 +80,10 @@ jobs:
# working-directory: packages/plugins
# run: pnpm test
+ - name: Install Playwright Chromium
+ working-directory: packages/e2e_tests
+ run: pnpm exec playwright install --with-deps chromium
+
- name: E2E Tests
working-directory: packages/e2e_tests
run: pnpm test
diff --git a/packages/e2e_tests/package.json b/packages/e2e_tests/package.json
index 75d7682b..72788c47 100644
--- a/packages/e2e_tests/package.json
+++ b/packages/e2e_tests/package.json
@@ -11,6 +11,7 @@
"lint": "oxlint --ignore-path=../../.gitignore --ignore-pattern='setup/docker-logs/**' .",
"lint:fix": "oxlint --ignore-path=../../.gitignore --ignore-pattern='setup/docker-logs/**' . --fix",
"test": "vitest run",
+ "test:web": "vitest run tests/web",
"test:watch": "vitest",
"test:no-build": "E2E_TEST_NO_BUILD=1 vitest run"
},
@@ -28,6 +29,7 @@
"@karakeep/tsconfig": "workspace:^0.1.0",
"@types/adm-zip": "^0.5.7",
"adm-zip": "^0.6.1",
+ "playwright": "^1.59.1",
"vite-tsconfig-paths": "^6.1.1",
"vitest": "^4.1.11"
}
diff --git a/packages/e2e_tests/tests/web/bookmarks.test.ts b/packages/e2e_tests/tests/web/bookmarks.test.ts
new file mode 100644
index 00000000..2627cf90
--- /dev/null
+++ b/packages/e2e_tests/tests/web/bookmarks.test.ts
@@ -0,0 +1,101 @@
+import type { Page } from "playwright/test";
+import { expect } from "playwright/test";
+
+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
+
+import { createWebTestUser, it, signIn } from "../../utils/browser";
+import { waitUntil } from "../../utils/general";
+
+async function seedBookmark(page: Page) {
+ const { trpc, email } = await createWebTestUser();
+ await trpc.bookmarks.createBookmark.mutate({
+ type: BookmarkTypes.TEXT,
+ text: "A bookmark for the weekend",
+ });
+ await signIn(page, email);
+ const bookmark = page.locator("[data-bookmark-index]");
+ await expect(bookmark).toHaveCount(1);
+ return bookmark;
+}
+
+it("saves a link and finds it through search", async ({ page }) => {
+ const { trpc, email } = await createWebTestUser();
+ await signIn(page, email);
+ await page
+ .getByPlaceholder("Paste a link, write a note or drop an image…")
+ .fill("http://nginx/hello.html");
+ await page.getByRole("button", { name: /^Save/ }).click();
+ const cards = page.locator("[data-bookmark-index]");
+ await expect(cards).toHaveCount(1);
+
+ // Crawling and search indexing happen asynchronously in the workers.
+ await waitUntil(
+ async () => {
+ const result = await trpc.bookmarks.searchBookmarks.query({
+ text: "My test title",
+ });
+ return result.bookmarks.some(
+ (bookmark) =>
+ bookmark.content.type === BookmarkTypes.LINK &&
+ bookmark.content.url === "http://nginx/hello.html",
+ );
+ },
+ "Saved bookmark is crawled and searchable",
+ 30000,
+ );
+
+ const search = page.getByPlaceholder("Search titles, text, URLs, and tags…");
+ await search.fill("My test title");
+ await search.press("Enter");
+ await expect(page).toHaveURL(/\/dashboard\/search\?/);
+ await expect(cards.filter({ hasText: "My test title" })).toBeVisible();
+ await expect(cards).toHaveCount(1);
+});
+
+it("favorites a bookmark and shows it in Favourites", async ({ page }) => {
+ const bookmark = await seedBookmark(page);
+ await bookmark.hover();
+ await bookmark.getByRole("button", { name: "Favorite", exact: true }).click();
+ await expect(
+ bookmark.getByRole("button", { name: "Unfavorite", exact: true }),
+ ).toBeVisible();
+ await page.getByRole("link", { name: /Favourites/ }).click();
+ await expect(page).toHaveURL(/\/dashboard\/favourites$/);
+ await expect(bookmark).toHaveText(/A bookmark for the weekend/);
+ await page.reload();
+ await bookmark.hover();
+ await expect(
+ bookmark.getByRole("button", { name: "Unfavorite", exact: true }),
+ ).toBeVisible();
+});
+
+it("archives a bookmark and shows it in Archive", async ({ page }) => {
+ const bookmark = await seedBookmark(page);
+ await bookmark.hover();
+ await bookmark.getByRole("button", { name: "Archive", exact: true }).click();
+ await expect(bookmark).toHaveCount(0);
+ await page.getByRole("link", { name: "Archive", exact: true }).click();
+ await expect(page).toHaveURL(/\/dashboard\/archive$/);
+ await expect(bookmark).toHaveText(/A bookmark for the weekend/);
+ await page.reload();
+ await bookmark.hover();
+ await expect(
+ bookmark.getByRole("button", { name: "Un-archive", exact: true }),
+ ).toBeVisible();
+});
+
+it("deletes a bookmark after confirmation", async ({ page }) => {
+ const bookmark = await seedBookmark(page);
+ await bookmark.locator('button[aria-haspopup="menu"]').click();
+ await page.getByRole("menuitem", { name: "Delete", exact: true }).click();
+ await page
+ .getByRole("dialog", { name: "Delete Bookmark?" })
+ .getByRole("button", { name: "Delete", exact: true })
+ .click();
+ await expect(bookmark).toHaveCount(0);
+ await page.reload();
+ await expect(
+ page.getByPlaceholder("Paste a link, write a note or drop an image…"),
+ ).toBeVisible();
+ await expect(bookmark).toHaveCount(0);
+});
diff --git a/packages/e2e_tests/tests/web/lists.test.ts b/packages/e2e_tests/tests/web/lists.test.ts
new file mode 100644
index 00000000..aa53d455
--- /dev/null
+++ b/packages/e2e_tests/tests/web/lists.test.ts
@@ -0,0 +1,52 @@
+import { expect } from "playwright/test";
+
+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
+
+import { createWebTestUser, it, signIn } from "../../utils/browser";
+
+it("adds a bookmark through the list dialog and finds it in the list", async ({
+ page,
+}) => {
+ const { trpc, email } = await createWebTestUser();
+ const list = await trpc.lists.create.mutate({
+ name: "Weekend reading",
+ icon: "📚",
+ });
+ await trpc.bookmarks.createBookmark.mutate({
+ type: BookmarkTypes.TEXT,
+ text: "Read this over the weekend",
+ });
+ await trpc.bookmarks.createBookmark.mutate({
+ type: BookmarkTypes.TEXT,
+ text: "Keep this outside the list",
+ });
+ await signIn(page, email);
+
+ const cards = page.locator("[data-bookmark-index]").filter({ visible: true });
+ const bookmark = cards.filter({ hasText: "Read this over the weekend" });
+ await bookmark.locator('button[aria-haspopup="menu"]').click();
+ await page
+ .getByRole("menuitem", { name: "Manage Lists", exact: true })
+ .click();
+ const dialog = page.getByRole("dialog", { name: "Manage Lists" });
+ await dialog.getByRole("combobox").click();
+ await page.getByRole("option", { name: /Weekend reading/ }).click();
+ await expect(dialog.getByRole("listitem")).toContainText("Weekend reading");
+ // Both the footer button and the corner icon are named Close.
+ await dialog
+ .getByRole("button", { name: "Close", exact: true })
+ .first()
+ .click();
+ await expect(dialog).not.toBeVisible();
+
+ await page.getByRole("link", { name: /Weekend reading/ }).click();
+ await expect(page).toHaveURL(new RegExp(`/dashboard/lists/${list.id}$`));
+ await expect(
+ page.getByRole("heading", { name: "Weekend reading" }),
+ ).toBeVisible();
+ await expect(bookmark).toBeVisible();
+ await expect(cards).toHaveCount(1);
+ await page.reload();
+ await expect(bookmark).toBeVisible();
+ await expect(cards).toHaveCount(1);
+});
diff --git a/packages/e2e_tests/tests/web/previews.test.ts b/packages/e2e_tests/tests/web/previews.test.ts
new file mode 100644
index 00000000..9526a992
--- /dev/null
+++ b/packages/e2e_tests/tests/web/previews.test.ts
@@ -0,0 +1,62 @@
+import type { Page } from "playwright/test";
+import { expect } from "playwright/test";
+
+import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
+
+import { createWebTestUser, it, signIn } from "../../utils/browser";
+
+async function seedPreview(page: Page) {
+ const { trpc, email } = await createWebTestUser();
+ const bookmark = await trpc.bookmarks.createBookmark.mutate({
+ type: BookmarkTypes.TEXT,
+ title: "Preview test bookmark",
+ text: "# Weekend plans\n\nRead a book and take a walk.",
+ note: "Bring the paperback",
+ });
+ await signIn(page, email);
+ return `/dashboard/preview/${bookmark.id}`;
+}
+
+it("opens and closes a bookmark preview modal", async ({ page }) => {
+ const previewPath = await seedPreview(page);
+ const card = page.locator("[data-bookmark-index]");
+ // Cards have both a timestamp link and an expand link to the preview.
+ await card.locator(`a[href="${previewPath}"]`).last().click();
+ await expect(page).toHaveURL(new RegExp(`${previewPath}$`));
+ const dialog = page.getByRole("dialog");
+ await expect(dialog).toBeVisible();
+ await expect(
+ dialog.getByRole("heading", { name: "Weekend plans" }),
+ ).toBeVisible();
+ await expect(
+ dialog
+ .getByText("Read a book and take a walk.", { exact: true })
+ .filter({ visible: true }),
+ ).toBeVisible();
+ await expect(
+ dialog.getByPlaceholder("Write some notes ...").filter({ visible: true }),
+ ).toHaveValue("Bring the paperback");
+
+ await page.keyboard.press("Escape");
+ await expect(dialog).not.toBeVisible();
+ await expect(page).toHaveURL(/\/dashboard\/bookmarks$/);
+ await expect(card).toBeVisible();
+});
+
+it("loads a bookmark preview directly as a full page", async ({ page }) => {
+ const previewPath = await seedPreview(page);
+ await page.goto(previewPath);
+ await expect(page).toHaveURL(new RegExp(`${previewPath}$`));
+ await expect(
+ page.getByRole("heading", { name: "Weekend plans" }),
+ ).toBeVisible();
+ await expect(page.getByRole("dialog")).toHaveCount(0);
+ await expect(
+ page
+ .getByText("Read a book and take a walk.", { exact: true })
+ .filter({ visible: true }),
+ ).toBeVisible();
+ await expect(
+ page.getByPlaceholder("Write some notes ...").filter({ visible: true }),
+ ).toHaveValue("Bring the paperback");
+});
diff --git a/packages/e2e_tests/tests/web/signup.test.ts b/packages/e2e_tests/tests/web/signup.test.ts
new file mode 100644
index 00000000..ba842fa9
--- /dev/null
+++ b/packages/e2e_tests/tests/web/signup.test.ts
@@ -0,0 +1,26 @@
+import { randomUUID } from "node:crypto";
+import { expect } from "playwright/test";
+
+import { it, signIn } from "../../utils/browser";
+
+it("signs up and can sign back in with the new account", async ({ page }) => {
+ const email = `signup-${randomUUID()}@example.com`;
+ await page.goto("/signin");
+ await page.getByRole("link", { name: "Sign up", exact: true }).click();
+ await page.getByLabel("Full Name", { exact: true }).fill("Signup Test User");
+ await page.getByLabel("Email", { exact: true }).fill(email);
+ await page.getByLabel("Password", { exact: true }).fill("test1234");
+ await page.getByLabel("Confirm Password", { exact: true }).fill("test1234");
+ await page.getByRole("button", { name: "Sign up", exact: true }).click();
+ await expect(page).toHaveURL(/\/dashboard\/bookmarks$/);
+ await expect(
+ page.getByPlaceholder("Paste a link, write a note or drop an image…"),
+ ).toBeVisible();
+
+ // Remove the automatic signup session to verify the saved credentials.
+ await page.context().clearCookies();
+ await signIn(page, email);
+ await expect(
+ page.getByPlaceholder("Paste a link, write a note or drop an image…"),
+ ).toBeVisible();
+});
diff --git a/packages/e2e_tests/utils/browser.ts b/packages/e2e_tests/utils/browser.ts
new file mode 100644
index 00000000..41478dd3
--- /dev/null
+++ b/packages/e2e_tests/utils/browser.ts
@@ -0,0 +1,42 @@
+import type { Page } from "playwright/test";
+import { chromium, expect } from "playwright/test";
+import { inject, it as base } from "vitest";
+
+import { createTestUser } from "./api";
+import { getTrpcClient } from "./trpc";
+
+export const it = base.extend<{ page: Page }>({
+ // Vitest requires destructuring, even when no other fixtures are needed.
+ // oxlint-disable-next-line no-empty-pattern
+ page: async ({}, use) => {
+ const browser = await chromium.launch();
+ try {
+ const page = await browser.newPage({
+ baseURL: `http://localhost:${inject("karakeepPort")}`,
+ viewport: { width: 1440, height: 900 },
+ locale: "en-US",
+ });
+ page.setDefaultTimeout(10000);
+ await use(page);
+ } finally {
+ await browser.close();
+ }
+ },
+});
+
+export async function signIn(page: Page, email: string) {
+ await page.goto("/signin");
+ await page.getByLabel("Email", { exact: true }).fill(email);
+ await page.getByLabel("Password", { exact: true }).fill("test1234");
+ await page.getByRole("button", { name: "Sign In", exact: true }).click();
+ await expect(page).toHaveURL(/\/dashboard\/bookmarks$/);
+}
+
+export async function createWebTestUser() {
+ const trpc = getTrpcClient(await createTestUser());
+ const { email } = await trpc.users.whoami.query();
+ if (!email) {
+ throw new Error("The test user must have an email address");
+ }
+ return { trpc, email };
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 722c7425..16d6f507 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1312,6 +1312,9 @@ importers:
adm-zip:
specifier: ^0.6.1
version: 0.6.1
+ playwright:
+ specifier: ^1.59.1
+ version: 1.59.1
vite-tsconfig-paths:
specifier: ^6.1.1