diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5d0906b..bd76c03 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,7 +78,21 @@ jobs: with: ref: ${{ github.event.pull_request.head.sha }} fetch-depth: 0 + - uses: actions/setup-node@v7.1.0 + with: + node-version-file: .node-version + cache: npm + cache-dependency-path: e2e/package-lock.json - run: scripts/e2e env: BUILD_SHA: ${{ github.event.pull_request.head.sha }} E2E_BASE_REF: origin/${{ github.base_ref }} + - if: failure() + uses: actions/upload-artifact@v4 + with: + name: playwright-${{ github.event.pull_request.head.sha }} + path: | + e2e/test-results/ + e2e/playwright-report/ + if-no-files-found: ignore + retention-days: 14 diff --git a/.gitignore b/.gitignore index 8910c17..8194385 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,11 @@ web/node_modules/ web/dist/ web/tsconfig.tsbuildinfo +# Browser tests +e2e/node_modules/ +e2e/test-results/ +e2e/playwright-report/ + # Internal docs plan.md internal_docs/ diff --git a/README.md b/README.md index 135d042..954a2e0 100644 --- a/README.md +++ b/README.md @@ -322,7 +322,7 @@ docker buildx build --platform linux/amd64 -t vimmary:local . | `scripts/setup` | installs the toolchain (Go, golangci-lint, govulncheck, Node, npm dependencies); runs at session start | | `scripts/test` | Go tests with the race detector, the storage and service tests against Postgres + pgvector (`TEST_DATABASE_URL`, or `compose.test.yaml`), the frontend tests | | `scripts/lint` | gofmt, go vet, golangci-lint, govulncheck, the migration rules (`scripts/check-migrations.sh`), the script tests, the frontend type check and build | -| `scripts/e2e` | the built image with pgvector and the fixture data: `/version` and the loopback `/healthz` report the SHA, the API and the frontend answer; upgrade from the image of `main` and back | +| `scripts/e2e` | the built image with pgvector and the fixture data: `/version` and the loopback `/healthz` report the SHA, the API and the frontend answer, the Playwright tests in `e2e/` pass; upgrade from the image of `main` and back | CI runs `test`, `lint` and `e2e` on the head commit of every pull request, and `review` (Claude, rules in `REVIEW.md`); all four are required checks. diff --git a/e2e/package-lock.json b/e2e/package-lock.json new file mode 100644 index 0000000..6c7a345 --- /dev/null +++ b/e2e/package-lock.json @@ -0,0 +1,58 @@ +{ + "name": "vimmary-e2e", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "vimmary-e2e", + "devDependencies": { + "@playwright/test": "1.64.1" + } + }, + "node_modules/@playwright/test": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.64.1.tgz", + "integrity": "sha512-IaYGml/nSOiZWROfPtuIL7neuu9qDOZ3TNvNljUIrhDMxiGTgPjzj/g9p9RxpS2tSepPDRY+UwAaEeiKtUvIAg==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.64.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.1.tgz", + "integrity": "sha512-HCrVhHr5QQuwYoswEUGTb7gGp522yflbv/ST8nF4sFWGuLjQCt17Q/EubpGGSNKYn1tsTCPZxqHhHCfVURu7UA==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.64.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, + "node_modules/playwright-core": { + "version": "1.64.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.64.1.tgz", + "integrity": "sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + } + } +} diff --git a/e2e/package.json b/e2e/package.json new file mode 100644 index 0000000..583eb8e --- /dev/null +++ b/e2e/package.json @@ -0,0 +1,11 @@ +{ + "name": "vimmary-e2e", + "private": true, + "type": "module", + "scripts": { + "test": "playwright test" + }, + "devDependencies": { + "@playwright/test": "1.64.1" + } +} diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts new file mode 100644 index 0000000..4f4aa84 --- /dev/null +++ b/e2e/playwright.config.ts @@ -0,0 +1,23 @@ +// Browser tests against a running vimmary with the preview fixtures and +// without an auth block. scripts/e2e starts the instance and sets +// E2E_BASE_URL; see that script. +import { defineConfig, devices } from "@playwright/test"; + +const baseURL = process.env.E2E_BASE_URL; +if (!baseURL) { + throw new Error("E2E_BASE_URL is not set; run the tests through scripts/e2e"); +} + +export default defineConfig({ + testDir: "tests", + outputDir: "test-results", + forbidOnly: !!process.env.CI, + retries: 0, + reporter: process.env.CI ? [["list"], ["html", { open: "never" }]] : "list", + use: { + baseURL, + trace: "retain-on-failure", + screenshot: "only-on-failure", + }, + projects: [{ name: "chromium", use: { ...devices["Desktop Chrome"] } }], +}); diff --git a/e2e/tests/fixtures.ts b/e2e/tests/fixtures.ts new file mode 100644 index 0000000..2c4f2fb --- /dev/null +++ b/e2e/tests/fixtures.ts @@ -0,0 +1,18 @@ +import { test as base, expect } from "@playwright/test"; + +// Every test fails on an uncaught exception in the page. Console messages +// about failed resource loads do not count: the fixture thumbnails point at +// YouTube IDs that do not exist. +export const test = base.extend<{ pageErrors: Error[] }>({ + pageErrors: [ + async ({ page }, use) => { + const errors: Error[] = []; + page.on("pageerror", (err) => errors.push(err)); + await use(errors); + expect(errors.map((e) => e.message)).toEqual([]); + }, + { auto: true }, + ], +}); + +export { expect }; diff --git a/e2e/tests/library.spec.ts b/e2e/tests/library.spec.ts new file mode 100644 index 0000000..6f49506 --- /dev/null +++ b/e2e/tests/library.spec.ts @@ -0,0 +1,56 @@ +import { test, expect } from "./fixtures.ts"; + +// The preview fixtures: internal/storage/fixtures/preview.json. +const videoTitles = [ + "Fixture: Postgres indexes in fifteen minutes", + "Fixture: Context cancellation in Go", + "Fixture: Sourdough starter from scratch", + "Fixture: Vortrag über Wärmepumpen", +]; + +test("the library lists the fixture videos and no podcast episodes", async ({ page }) => { + await page.goto("/"); + for (const title of videoTitles) { + await expect(page.getByRole("heading", { name: title, level: 3 })).toBeVisible(); + } + await expect(page.getByRole("heading", { name: /Fixture Weekly/ })).toHaveCount(0); +}); + +test("opening a video shows its summary", async ({ page }) => { + await page.goto("/"); + await page.getByRole("link", { name: /Context cancellation in Go/ }).click(); + await expect(page).toHaveURL(/\/video\/[0-9a-f-]{36}$/); + await expect( + page.getByRole("heading", { name: "Fixture: Context cancellation in Go", level: 1 }), + ).toBeVisible(); + await expect(page.getByRole("heading", { name: "Overview", level: 2 })).toBeVisible(); +}); + +test("search narrows the library to the matching video", async ({ page }) => { + await page.goto("/"); + await page.getByRole("textbox", { name: "Search summaries" }).fill("sourdough"); + await page.keyboard.press("Enter"); + await expect(page).toHaveURL(/\?q=sourdough$/); + await expect( + page.getByRole("heading", { name: "Fixture: Sourdough starter from scratch" }), + ).toBeVisible(); + await expect(page.getByRole("heading", { name: videoTitles[0] })).toHaveCount(0); +}); + +for (const [path, heading] of [ + ["/inbox", "Inbox"], + ["/channels", "Channels & shows"], + ["/stats", "Stats"], + ["/settings", "Settings"], +] as const) { + test(`${path} renders`, async ({ page }) => { + await page.goto(path); + await expect(page.getByRole("heading", { name: heading, level: 1 })).toBeVisible(); + }); +} + +test("without application login the nav offers no sign-out", async ({ page }) => { + await page.goto("/"); + await expect(page.getByRole("navigation").getByRole("link", { name: "Settings" })).toBeVisible(); + await expect(page.getByRole("button", { name: "Sign out" })).toHaveCount(0); +}); diff --git a/scripts/e2e b/scripts/e2e index 333bb04..9c9b69a 100755 --- a/scripts/e2e +++ b/scripts/e2e @@ -1,8 +1,9 @@ #!/usr/bin/env bash # End-to-end checks against a running vimmary. # -# With E2E_BASE_URL set, the HTTP checks run against that instance (a session -# starts the app natively with seed_fixtures: true and sets it). +# With E2E_BASE_URL set, the HTTP checks and the Playwright tests in e2e/ run +# against that instance (a session starts the app natively with +# seed_fixtures: true and sets it). # # Otherwise the script builds the production image of the working tree as # vimmary-local: and runs the upgrade test with compose.e2e.yaml: @@ -10,7 +11,8 @@ # 1. the image of the base (E2E_BASE_REF, default origin/main) on an empty # database, which seeds the fixtures once the base knows seed_fixtures; # 2. the new image on that database: /version reports BUILD_SHA inside the -# container and on the published port, the HTTP checks pass; +# container and on the published port, the HTTP checks and the +# Playwright tests pass; # 3. the base image again on the migrated schema, because a rollback resets # only the image. # @@ -50,8 +52,17 @@ http_checks() { echo "ok / serves the frontend" } +# browser_tests : the Playwright tests in e2e/. On failure the +# traces and screenshots are in e2e/test-results. +browser_tests() { + step "browser tests (e2e/)" + ensure_playwright + E2E_BASE_URL="${1%/}" playwright test +} + if [[ -n "${E2E_BASE_URL:-}" ]]; then http_checks "$E2E_BASE_URL" + browser_tests "$E2E_BASE_URL" echo echo "e2e ok: ${E2E_BASE_URL}" exit 0 @@ -115,6 +126,7 @@ fi step "2. new image" start "vimmary-local:${BUILD_SHA}" "$BUILD_SHA" http_checks "$base_url" +browser_tests "$base_url" if [[ "$upgrade" == 1 ]]; then step "3. base image on the migrated schema" diff --git a/scripts/lib/common.sh b/scripts/lib/common.sh index 8d9225a..b008d03 100644 --- a/scripts/lib/common.sh +++ b/scripts/lib/common.sh @@ -26,3 +26,25 @@ ensure_npm_deps() { is_ci() { [[ "${CI:-}" == "true" ]]; } step() { printf '\n==> %s\n' "$*"; } + +# playwright : the Playwright CLI of e2e/, with browsers in Playwright's +# default cache. A cloud session presets PLAYWRIGHT_BROWSERS_PATH to a browser +# build that need not match e2e/'s Playwright version, and +# PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD, which would keep the matching one out. +playwright() { + (cd e2e && env -u PLAYWRIGHT_BROWSERS_PATH -u PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD \ + ./node_modules/.bin/playwright "$@") +} + +# ensure_playwright: e2e/'s npm dependencies and the Chromium build of its +# Playwright version, with the system libraries it needs on Linux. +ensure_playwright() { + if [[ ! -f e2e/node_modules/.package-lock.json || e2e/package-lock.json -nt e2e/node_modules/.package-lock.json ]]; then + (cd e2e && npm ci --no-audit --no-fund --loglevel=error) + fi + if [[ "$(uname -s)" == Linux ]]; then + playwright install --with-deps chromium + else + playwright install chromium + fi +} diff --git a/scripts/setup b/scripts/setup index 00240c1..96fc0f0 100755 --- a/scripts/setup +++ b/scripts/setup @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Toolchain and dependencies for a session or a CI job: Go (the version in # go.mod), golangci-lint, govulncheck, Node (.node-version), the npm -# dependencies of web/, and jq. Runs from the SessionStart hook in +# dependencies of web/ and e2e/, Playwright's Chromium, and jq. Runs from the SessionStart hook in # .claude/settings.json; quiet when everything is in place. # # On Linux as root it installs a missing Go and a missing or mismatched Node; @@ -95,6 +95,13 @@ if [[ ! -f web/node_modules/.package-lock.json || web/package-lock.json -nt web/ (cd web && npm ci --no-audit --no-fund --loglevel=error) >&2 fi +# Playwright for the browser tests in e2e/ +# shellcheck source=scripts/lib/common.sh +source scripts/lib/common.sh +if ! ensure_playwright >/tmp/vimmary-playwright.log 2>&1; then + say "Playwright browser install failed (log: /tmp/vimmary-playwright.log)" +fi + # jq (hooks read their input with it) if ! command -v jq >/dev/null 2>&1 && $linux && $root && command -v apt-get >/dev/null 2>&1; then say "installing jq"