test(comments): cover reader comment auth in e2e via a Clerk dev instance#156
test(comments): cover reader comment auth in e2e via a Clerk dev instance#156axross wants to merge 1 commit into
Conversation
…ance The reader-side Clerk sign-in and comment-submit journeys were catalogued in e2e/scenarios.md but left untested. Wire the suite to a Clerk development instance with @clerk/testing: a Playwright globalSetup fetches a testing token when the dev keys are present, and a signInAsReader helper establishes a reader session through a server-side sign-in ticket — so it needs no OAuth redirect and works on the GitHub-OAuth-only instance. Add the post.comments.sign-in and post.comments.submit tests, gated on NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY so unconfigured runs (local, PR CI, forked previews) skip them and stay green. Pass the dev keys and a +clerk_test reader through the e2e CI job from secrets that resolve to empty until provided, keeping Clerk optional. Refs #155 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UJiXs6YYJdtvmQrAwNvmH2
|
@claude review Generated by Claude Code |
🚀 Preview deploymenthttps://btnopen-pr-156.vercel.app Deployed |
Code review1 important, 0 nits I reviewed this against REVIEW.md's mandatory checks (skill conformance + acceptance criteria) and the code itself. Skill conformance: No violations found against the e2e-testing-guidelines, application-security-requirements, or development-guidelines skills. Verification I ran independently: 1 Important finding: Left inline — three of #155's acceptance criteria (the This review is advisory and does not gate merging. |
Summary
Reader-facing blog-post comments authenticate through Clerk (GitHub OAuth), but the E2E suite never exercised that path — its "authenticated" state is a Payload admin session used only to seed comments. Two reader journeys were already catalogued in
e2e/scenarios.mdbut left untested and marked "needs Clerk test tokens":post.comments.sign-inandpost.comments.submit.This wires the suite to a Clerk development instance via the official
@clerk/testingpackage:globalSetupfetches a Testing Token when the dev keys are present (and is a no-op otherwise), run in the main process so every worker inherits it.signInAsReaderhelper establishes a reader session through a server-side sign-in ticket. This is deliberate: Clerk's helper cannot drive a real GitHub OAuth redirect, and the ticket strategy is independent of enabled first factors, so it works on the GitHub-OAuth-only instance without automating GitHub's login.Everything is gated on
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY, so unconfigured runs (local without setup, PR CI, forked previews) skip the new tests and stay green. The e2e CI job passes the dev keys and a+clerk_testreader from secrets that resolve to empty until provided — keeping Clerk optional, as requested.Why not test real GitHub OAuth: you don't test a third party's login in your own suite; establishing a Clerk session via the testing helpers and asserting the app's behavior is the standard, supported approach and covers all of our code (middleware, provider, CSRF, pending write).
Related issues
Closes #155
Verification
This environment has no Clerk keys, so the new tests skip by design; everything else was run:
npm run format— clean.npm run lint— clean (one pre-existing unrelated warning inscripts/prune-preview-blobs.mjs).npx tsc --noEmit— 0 errors project-wide (covers the e2e sources, whichnext builddoes not).npm run test:unit— 186 passed / 23 suites.npm run build— succeeds (afternpm run migrate:upto create the local schema; an earlier failure was an unmigrated local DB, not the diff).npx playwright test --list— without keys,post.comments.sign-in/post.comments.submitare not defined andpost.comments.unavailable-hiddenstill is; the suite loads with no errors.globalSetupno-op path executed directly without keys — returns cleanly.npx playwright testrun confirmedglobalSetupintegrates into the harness (the run proceeded past it); it could not complete here because the pinned Playwright browser binary (chrome-headless-shell) isn't installed in this environment — CI installs it vianpx playwright install --with-deps chromium.Acceptance criteria status:
unavailable-hidden/draft-no-composerenv-gating behaves as before. (verified via--list)@clerk/testingadded as a devDependency; lint, unit, and build pass. (verified)e2e/scenarios.mdfacet tags for both scenarios match the catalog (posts/may); no catalog/facet errors on load, and themustgate is unaffected. (verified via--list)submittest signs a reader in, submits, sees the pending acknowledgment, and persists apendingcomment with a non-emptyauthorProviderIdthat stays hidden from the public page; thesign-intest shows the affordance. (implemented + type/lint-checked; runtime validation pending keys — see trade-off below)Trade-off: the actual Clerk-authenticated path only runs where the dev keys are configured, so it is not exercised in PR CI. It runs in the post-merge
maine2e job once the secrets are added, or locally with keys in.env.local.Independent review: clean — 0 code defects. The single "Important" item was a REVIEW.md-mandated disclosure that the three ⏳ criteria above can't execute in PR CI without Clerk keys (the deferral by design); it was dispositioned and the thread resolved. The reviewer independently confirmed the
@clerk/testingAPI match, the composerdata-testids, and--listparity on the unconfigured baseline.Risks and breaking changes
globalSetup,.env.example, the e2e CI jobenv,e2e/scenarios.md, and a devDependency. App behavior is unchanged.envadditions are inert when the secrets are unset (they resolve to empty →isClerkAvailablefalse → tests skip).clerkSetup()rejects a production secret key, guarding against pointing the suite at production.Notes for reviewers
e2e/global-setup.ts(whyglobalSetup, not thesetupproject — token propagation across workers) ande2e/helpers/api/clerk.ts(why the server-side ticket over OAuth), then the two tests ine2e/tests/routes/posts/comments.test.ts.emailAddressticket works on the OAuth-only dev instance (expected; validated once keys are provided). If a dev instance restricts it, the fallback is enabling email code on that instance and usingemail_code+ OTP424242— no app change needed.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY/CLERK_SECRET_KEY+ a+clerk_testreader locally, or add them as Actions secrets (and optionally theCLERK_TEST_READER_EMAILvar) for the post-mergemainrun.