diff --git a/.gitignore b/.gitignore index bf1bab0..ee3caa8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ # testing /coverage +/test-results # next.js /.next/ diff --git a/A4_HANDOFF.md b/A4_HANDOFF.md new file mode 100644 index 0000000..215f14c --- /dev/null +++ b/A4_HANDOFF.md @@ -0,0 +1,126 @@ +# A4 Handoff — App Shell + +**Status:** In progress. A1, A2, A3 committed. A4 work is uncommitted. + +**Resolved:** The `/api/dev/complete` session bug (see below). With it fixed, all 4 e2e shell tests pass, plus typecheck/lint/17 unit tests. Next session: commit A4 (instructions at the bottom of this file). + +--- + +## What's Done + +- **A1** (commit `9cdf8be`): Foundation scaffold — Next.js 15, shadcn/Radix, Drizzle+Postgres, Vitest, Docker. +- **A2** (commit `d133aff`): Schema core — 6 tables, migration, idempotent seed. +- **A3** (commit `c0e73e2`): Auth — magic-link sign-in, `requirePermission()` chokepoint, role enum. + +## A4 Work (Uncommitted) + +Built for the app shell: + +- `src/components/app/nav.tsx` — Nav (5 tabs mobile / rail desktop). Uses lucide-react icons. +- `src/components/app/state-message.tsx` — Error + empty states. +- `src/components/app/app-layout.tsx` — Shell layout (header, nav, main, footer). +- `src/app/(app)/layout.tsx` — Authenticated route group (redirects to /login if no session). +- `src/app/(app)/schedule/page.tsx` — `src/app/(app)/roster/page.tsx` — `src/app/(app)/training/page.tsx` — `src/app/(app)/announcements/page.tsx` — `src/app/(app)/settings/page.tsx` — 5 shell pages. +- `src/app/api/health/route.ts` — Health check endpoint. +- `e2e/shell.spec.ts` — Playwright e2e test (5 nav tabs at 360px viewport). +- `playwright.config.ts` — Playwright config. +- `package.json` — Added `test:e2e` script. +- `src/app/api/dev/complete/route.ts` — Added try/catch + logging around `signIn`. + +--- + +## The Blocker (RESOLVED): `/api/dev/complete` never set a session cookie + +**Original report:** `GET /api/dev/complete?token=...` appeared to return HTTP 000 / connection +reset. That was a **test-harness artifact** (curl URL parsing/quoting in the handoff's shell +script), not an app bug — the endpoint returns `200 {"ok":true}`. + +**Real symptom:** The complete response carried **no `Set-Cookie` header**, so no session cookie +was established. Every subsequent navigation to `/schedule` (etc.) then 307-redirected to +`/login`, and the two shell e2e tests failed (nav tabs never rendered). The route reported +`{"ok":true}` because `signIn` returned the error-page URL rather than throwing. + +**Root cause:** `[auth][error] UntrustedHost: Host must be trusted.` +- Auth.js v5's `assertConfig()` checks `config.trustHost` **before** `setEnvDefaults()` applies + its env-based default (the code comment says so explicitly). +- `setEnvDefaults()` only derives `trustHost` from `AUTH_URL` / `AUTH_TRUST_HOST` / `VERCEL` / + `CF_PAGES` / `NODE_ENV !== "production"` — **not** from `NEXTAUTH_URL`. +- In `pnpm start` (production, `NODE_ENV=production`) with no `AUTH_URL`, `trustHost` stayed + `false`, so the magic-link callback was rejected and no session cookie was written. +- `pnpm dev` worked (dev mode) and Docker set `NEXTAUTH_URL` — but that var does not set + `trustHost`, so Docker had the same latent bug. + +**Fix:** `src/auth/config.ts` now sets `trustHost: true` explicitly (the documented fix for +`UntrustedHost`; this is a localhost app behind a reverse proxy). Rebuild required for +`pnpm start`. Verified: complete response now sets `authjs.session-token`, `/schedule` returns +200, and all 4 e2e shell tests pass. + +--- + +## How to Test (for next session) + +Start the app and test the complete flow: + +```bash +cd /home/bhursey/code/glenlochmedia + +# Start app (detached, kills itself on exit) +env DATABASE_URL="postgres://media:media@localhost:5433/media" \ + NEXTAUTH_SECRET="e2e-secret-for-shell-tests" \ + AUTO_ACTIVATE_NEW_USERS="true" \ + PORT=3000 \ + pnpm start >/tmp/app.log 2>&1 & +APP_PID=$! + +# Wait for ready +for i in $(seq 1 30); do grep -qiE "Ready" /tmp/app.log && break; sleep 1; done + +# Get a token +TOKEN_URL=$(curl -s -X POST localhost:3000/api/dev/magic-link \ + -H 'Content-Type: application/json' \ + -d '{"email":"e2e-test@example.com"}' \ + | python3 -c "import sys,json; print(json.load(sys.stdin)['url'])") + +# Complete sign-in (this is where HTTP 000 happens) +curl -sv "http://localhost:3000$TOKEN_URL" 2>&1 | grep -iE "set-cookie|HTTP/|error" + +# Cleanup +kill $APP_PID +``` + +**Note:** Use valid emails (with a TLD). Zod's `.email()` rejects `@localhost`. + +--- + +## E2E Test + +```bash +cd /home/bhursey/code/glenlochmedia +pnpm test:e2e --grep shell +``` + +The webServer runs `pnpm db:migrate && pnpm build && pnpm start` on port 3000. + +**Current e2e status:** Both shell tests fail because `signInAs` doesn't establish a session (the complete endpoint resets the connection). Once the complete endpoint works, the e2e tests should pass. + +--- + +## Known Good Commands + +- Typecheck: `pnpm typecheck` (exit 0) +- Lint: `pnpm lint` (exit 0) +- Unit tests: `pnpm test` (17 tests, exit 0) +- Build: `pnpm build` (exit 0) +- DB migrate: `pnpm db:migrate` (needs Postgres on :5433) +- DB seed: `pnpm db:seed` + +--- + +## To Commit A4 (once complete) + +```bash +cd /home/bhursey/code/glenlochmedia +rm -f src/app/api/dev/magic-debug.log # debug file, don't commit +git add -A +git commit -m "A4: app shell (layout, nav, tokens, error/empty states, /api/health)" +``` diff --git a/e2e/shell.spec.ts b/e2e/shell.spec.ts new file mode 100644 index 0000000..877f029 --- /dev/null +++ b/e2e/shell.spec.ts @@ -0,0 +1,76 @@ +import { test, expect, type Page } from "@playwright/test"; + +const BASE = "http://localhost:3000"; +const TAB_LABELS = ["Schedule", "Roster", "Training", "Announcements", "Settings"]; + +/** + * Dev console transport: request a magic link, then complete it in the browser + * context so the Auth.js session cookie is set for the navigation under test. + */ +async function signInAs(page: Page, email: string) { + const res = await page.request.post(`${BASE}/api/dev/magic-link`, { + data: { email }, + }); + expect(res.ok(), "magic-link request should succeed").toBe(true); + const body = (await res.json()) as { url?: string }; + expect(body.url, "magic-link should return a sign-in url").toBeTruthy(); + await page.goto(body.url!); + await expect(page).toHaveURL(/complete/); +} + +test.describe("app shell", () => { + test("renders the shell with 5 nav tabs at a 360px viewport", async ({ page }) => { + await page.setViewportSize({ width: 360, height: 780 }); + await signInAs(page, `e2e-mobile-${Date.now()}@example.com`); + + await page.goto(`${BASE}/schedule`); + await expect(page).toHaveURL(`${BASE}/schedule`); + + // Header + page title. + await expect(page.getByRole("heading", { name: "Schedule", level: 1 })).toBeVisible(); + + // Five nav tabs, one per label. + for (const label of TAB_LABELS) { + await expect(page.getByRole("link", { name: label, exact: true })).toBeVisible(); + } + + // The current tab is marked active. + await expect(page.getByRole("link", { name: "Schedule" })).toHaveAttribute( + "aria-current", + "page", + ); + + // Empty state renders inside the shell. + await expect(page.getByText("Nothing scheduled yet")).toBeVisible(); + }); + + test("shows the desktop rail inside the shell at desktop width", async ({ page }) => { + await page.setViewportSize({ width: 1024, height: 768 }); + await signInAs(page, `e2e-desktop-${Date.now()}@example.com`); + + await page.goto(`${BASE}/roster`); + for (const label of TAB_LABELS) { + await expect(page.getByRole("link", { name: label, exact: true })).toBeVisible(); + } + // Active tab follows the route. + await expect(page.getByRole("link", { name: "Roster" })).toHaveAttribute( + "aria-current", + "page", + ); + }); + + test("redirects unauthenticated users away from the shell", async ({ page }) => { + await page.setViewportSize({ width: 360, height: 780 }); + await page.goto(`${BASE}/schedule`); + await expect(page).toHaveURL(`${BASE}/login`); + }); +}); + +test.describe("health", () => { + test("/api/health returns ok", async ({ request }) => { + const res = await request.get(`${BASE}/api/health`); + expect(res.ok()).toBe(true); + const body = (await res.json()) as { status: string }; + expect(body.status).toBe("ok"); + }); +}); diff --git a/package.json b/package.json index 11f56a0..54ecb1c 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "typecheck": "tsc --noEmit", "test": "vitest run", "test:watch": "vitest", + "test:e2e": "playwright test", "db:generate": "drizzle-kit generate", "db:migrate": "drizzle-kit migrate", "db:seed": "tsx src/db/seed.ts" @@ -33,6 +34,7 @@ }, "devDependencies": { "@eslint/eslintrc": "^3", + "@playwright/test": "^1.65.1", "@tailwindcss/postcss": "^4", "@testing-library/jest-dom": "^7.0.1", "@testing-library/react": "^16.3.3", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..1a2c711 --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,32 @@ +import { defineConfig, devices } from "@playwright/test"; + +/** + * E2E for the app shell. The done-criteria is "passes at 360px viewport", so + * the shell tests pin a mobile viewport. The webServer builds + starts the app + * against the same Postgres the app uses in production. + */ +export default defineConfig({ + testDir: "./e2e", + fullyParallel: true, + retries: process.env.CI ? 2 : 0, + workers: process.env.CI ? 1 : undefined, + reporter: "list", + use: { + baseURL: "http://localhost:3000", + trace: "on-first-retry", + }, + projects: [ + { name: "chromium", use: { ...devices["Desktop Chrome"] } }, + ], + webServer: { + command: "pnpm db:migrate && pnpm build && PORT=3000 pnpm start", + url: "http://localhost:3000", + reuseExistingServer: true, + env: { + DATABASE_URL: "postgres://media:media@localhost:5433/media", + NEXTAUTH_SECRET: "e2e-secret-for-shell-tests", + AUTO_ACTIVATE_NEW_USERS: "true", + }, + timeout: 180_000, + }, +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 90b7c75..ff35f29 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -31,10 +31,10 @@ importers: version: 1.74.0(@types/react@19.3.0)(react@19.1.0) next: specifier: 15.5.29 - version: 15.5.29(@babel/core@7.29.13)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0) + version: 15.5.29(@babel/core@7.29.13)(@playwright/test@1.65.1)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0) next-auth: specifier: 5.0.0-beta.32 - version: 5.0.0-beta.32(next@15.5.29(@babel/core@7.29.13)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0) + version: 5.0.0-beta.32(next@15.5.29(@babel/core@7.29.13)(@playwright/test@1.65.1)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0) postgres: specifier: ^4.1.1 version: 4.1.1 @@ -57,6 +57,9 @@ importers: '@eslint/eslintrc': specifier: ^3 version: 3.3.7 + '@playwright/test': + specifier: ^1.65.1 + version: 1.65.1 '@tailwindcss/postcss': specifier: ^4 version: 4.3.5 @@ -1062,6 +1065,11 @@ packages: '@panva/hkdf@1.2.1': resolution: {integrity: sha512-6oclG6Y3PiDFcoyk8srjLfVKyMfVCKJ27JwNPViuXziFpmdz+MZnZN/aKY0JGXgYuO/VghU0jcOAZgWXZ1Dmrw==} + '@playwright/test@1.65.1': + resolution: {integrity: sha512-y+NCIRm2LvY4slUVWvGh1nujJ8RF/hEisIsq2mtUPgdQC4L701bq5hZGMhrpeXjACvwvO9tj5dJqhvM6TP9lbw==} + engines: {node: '>=20'} + hasBin: true + '@radix-ui/react-compose-refs@1.1.5': resolution: {integrity: sha512-+48PbAAbq3didjJxa+OaWY2ZwgAKsNiRGyeHKszblZMQ+kcpd9pAaT11cMkGEie0vsOi3QdeTE6d5Fe3Gn61kA==} peerDependencies: @@ -3123,6 +3131,16 @@ packages: resolution: {integrity: sha512-nDywThFk1i4BQK4twPQ6TA4RT8bDY96yeuCVBWL3ePARCiEKDRSrNGbFIgUJpLp+XeIR65v8ra7WuJOFUBtkMA==} engines: {node: '>=8'} + playwright-core@1.65.1: + resolution: {integrity: sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==} + engines: {node: '>=20'} + hasBin: true + + playwright@1.65.1: + resolution: {integrity: sha512-vJ1FuoOrNwOKHUIVEvSt3jT1L+wjjDpwhwIstKtfvTh8XIG4FG4bIFuvNdMxi81LGfAhUgUZleVFaOLY4RIUUw==} + engines: {node: '>=20'} + hasBin: true + possible-typed-array-names@1.1.0: resolution: {integrity: sha512-/+5VFTchJDoVj3bhoqi6UeymcD00DAwb1nJwamzPvHEszJ4FpF6SNNbUbOS8yI56qHzdV8eK0qEfOSiodkTdxg==} engines: {node: '>= 0.4'} @@ -4498,6 +4516,10 @@ snapshots: '@panva/hkdf@1.2.1': {} + '@playwright/test@1.65.1': + dependencies: + playwright: 1.65.1 + '@radix-ui/react-compose-refs@1.1.5(@types/react@19.3.0)(react@19.1.0)': dependencies: react: 19.1.0 @@ -6354,13 +6376,13 @@ snapshots: natural-compare@1.4.0: {} - next-auth@5.0.0-beta.32(next@15.5.29(@babel/core@7.29.13)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0): + next-auth@5.0.0-beta.32(next@15.5.29(@babel/core@7.29.13)(@playwright/test@1.65.1)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0))(react@19.1.0): dependencies: '@auth/core': 0.41.3 - next: 15.5.29(@babel/core@7.29.13)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0) + next: 15.5.29(@babel/core@7.29.13)(@playwright/test@1.65.1)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0) react: 19.1.0 - next@15.5.29(@babel/core@7.29.13)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0): + next@15.5.29(@babel/core@7.29.13)(@playwright/test@1.65.1)(@types/node@20.19.43)(react-dom@19.1.0(react@19.1.0))(react@19.1.0): dependencies: '@next/env': 15.5.29 '@swc/helpers': 0.5.15 @@ -6378,6 +6400,7 @@ snapshots: '@next/swc-linux-x64-musl': 15.5.29 '@next/swc-win32-arm64-msvc': 15.5.29 '@next/swc-win32-x64-msvc': 15.5.29 + '@playwright/test': 1.65.1 sharp: 0.35.5(@types/node@20.19.43) transitivePeerDependencies: - '@babel/core' @@ -6555,6 +6578,12 @@ snapshots: dependencies: find-up: 3.0.0 + playwright-core@1.65.1: {} + + playwright@1.65.1: + dependencies: + playwright-core: 1.65.1 + possible-typed-array-names@1.1.0: {} postcss-selector-parser@7.2.1: diff --git a/src/app/(app)/announcements/page.tsx b/src/app/(app)/announcements/page.tsx new file mode 100644 index 0000000..c2edd97 --- /dev/null +++ b/src/app/(app)/announcements/page.tsx @@ -0,0 +1,16 @@ +import { StateMessage } from "@/components/app/state-message"; + +export const metadata = { title: "Announcements" }; + +export default function AnnouncementsPage() { + return ( + <> +
News and notes from the media team.
+The team, their roles, and how to reach them.
+Your upcoming slots and what to do the day of service.
+Church details, timezone, and scheduling rules.
+How-tos and tutorials, with progress tied to your roles.
+{title}
} + {description &&{description}
} +