Skip to content

test: add test utilities and mock infrastructure#146

Merged
jakebromberg merged 5 commits intomainfrom
test/01-test-utilities
Mar 20, 2026
Merged

test: add test utilities and mock infrastructure#146
jakebromberg merged 5 commits intomainfrom
test/01-test-utilities

Conversation

@jakebromberg
Copy link
Copy Markdown
Member

@jakebromberg jakebromberg commented Feb 5, 2026

Closes #246

Summary

Lay the foundation for the test suite by adding shared infrastructure that all subsequent test PRs depend on:

  • lib/test-utils/component-harness.ts — Redux-wrapped hook rendering harness (createHookWrapper) so hook tests get a real store without boilerplate
  • lib/test-utils/fixtures.ts — Test data factories and shared MOCK_USERS for consistent test identities
  • lib/test-utils/index.ts — Barrel export for clean imports
  • Auth and bin-api mocks — Reusable mock setup for organization-utils, organization-config, and utilities
  • e2e/fixtures/auth.fixture.ts — Playwright-style login/logout helpers with port discovery for e2e tests

Test plan

  • Run test suite to verify utilities work

Part 1 of 26 — Test infrastructure. Merge before all other test PRs.

Note: E2E tests depend on #271 and WXYC/Backend-Service#223. E2E will pass once those merge.

Note: Merge this PR before #148 — both modify lib/test-utils/fixtures.ts.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Feb 5, 2026

Deploying wxyc-dj with  Cloudflare Pages  Cloudflare Pages

Latest commit: f760520
Status: ✅  Deploy successful!
Preview URL: https://196f8edc.dj-site.pages.dev
Branch Preview URL: https://test-01-test-utilities.dj-site.pages.dev

View logs

@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch from 71e645b to 3904412 Compare February 5, 2026 21:26
@jakebromberg jakebromberg marked this pull request as ready for review February 6, 2026 00:55
Copy link
Copy Markdown
Collaborator

@JacksonMeade JacksonMeade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testing infrastructure is extremely clean, I'm a big fan.

There are several places where (I assume, since I never write comments like this) the AI has, in my opinion, over-commented the code, adding unnecessary commentary that could easily become outdated as the codebase drifts.

Hidden in this PR, however, is the total removal of the organization client, a feature of the design that I wish had been made explicit. This is a huge change to the way roles work, and not a decision we have discussed. That is blocking for me until it is justified.

Comment thread e2e/fixtures/auth.fixture.ts Outdated
async function getAuthServiceBaseUrl(): Promise<string> {
// Check env vars first
const authUrl = process.env.NEXT_PUBLIC_BETTER_AUTH_URL;
if (authUrl) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this replacement? I worry that this is either an artifact of errors while mocking the backend or performing some of the work that now resides in next.config.mjs:

/** @type {import('next').NextConfig} */
const authBaseURL = process.env.NEXT_PUBLIC_BETTER_AUTH_URL || "https://api.wxyc.org/auth";

const nextConfig = {
  reactStrictMode: false,
  // Explicitly set workspace root to silence lockfile warning
  outputFileTracingRoot: import.meta.dirname,
  // Required for OpenNext Cloudflare
  output: "standalone",
  async rewrites() {
    return [
      {
        source: "/auth/:path*",
        destination: `${authBaseURL}/:path*`,
      },
    ];
  },
};

export default nextConfig;

// OpenNext Cloudflare dev initialization
import { initOpenNextCloudflareForDev } from "@opennextjs/cloudflare";
initOpenNextCloudflareForDev();

Comment thread e2e/fixtures/auth.fixture.ts Outdated
Comment thread e2e/fixtures/auth.fixture.ts
Comment thread e2e/fixtures/auth.fixture.ts Outdated
Comment thread lib/test-utils/mocks/auth.ts Outdated
Comment thread lib/test-utils/mocks/auth.ts Outdated
Comment thread lib/test-utils/mocks/bin-api.ts Outdated
Comment thread lib/test-utils/mocks/bin-api.ts Outdated
Comment thread src/hooks/authenticationHooks.ts Outdated
@jakebromberg
Copy link
Copy Markdown
Member Author

Addressed all points in the latest force push:

  1. Organization removal -- Reverted. The org plugin is fully restored; client.ts, server-client.ts, and organization-utils.ts all match main again. The org removal was tracking a change in wxyc-shared (47ad9fb6), but since that's also on a feature branch and not merged, I've pulled it out of this PR entirely. We can discuss the org architecture separately.

  2. Over-commenting -- Removed the vi.mock hoisting notes and verbose @example blocks from bin-api.ts and auth.ts. Comments are now single-line summaries.

  3. Port-finding -- Replaced the hardcoded port array with a PORT_RANGE_START/PORT_RANGE_SIZE loop. The function now throws if all ports are exhausted instead of silently defaulting.

  4. URL replacement -- Added a comment explaining why /auth is stripped: the env var includes the path (e.g., http://localhost:8084/auth), but the fixture needs the bare host for direct HTTP calls to the auth service, bypassing Next.js rewrites.

  5. SES emails -- Opened Prevent SES email sending during test runs #178 to track preventing email sends during test runs.

  6. Sign-out endpoint -- Replaced expireUserSession with revokeUserSessions, which calls /auth/revoke-sessions with a session cookie. Added a doc reference to better-auth's session management docs.

  7. Mock users -- Consolidated into a single MOCK_USERS constant in lib/test-utils/fixtures.ts. Both the e2e fixture and unit test mocks now reference it.

  8. Org mock -- No longer confusing since the org plugin is restored. The organization.getFullOrganization / listMembers mocks correctly reflect the client API.

  9. Auth hooks -- The changes to authenticationHooks.ts were part of the org removal commit, which has been reverted. The file now matches main.

@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch 2 times, most recently from 307e35c to f287868 Compare February 14, 2026 21:42
@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch 2 times, most recently from b3b66a0 to eeec4f9 Compare March 2, 2026 16:11
@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch from eeec4f9 to e0fedc5 Compare March 9, 2026 01:34
@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch from e0fedc5 to ac922ee Compare March 19, 2026 00:13
Jake Bromberg added 3 commits March 19, 2026 10:32
- Add component harness for Redux-wrapped hook testing
- Expand fixtures with test data factories
- Add auth and bin-api mocks for consistent test setup
- Add e2e auth fixture with login/logout helpers and port discovery
- Consolidate mock users into shared MOCK_USERS in fixtures.ts
- Update package.json with test dependencies
The package requires GitHub Packages auth and nothing in this PR imports it.
@jakebromberg jakebromberg force-pushed the test/01-test-utilities branch from ac922ee to 0bd23e8 Compare March 19, 2026 17:32
Jake Bromberg added 2 commits March 19, 2026 16:39
The E2E auth fixture imports fixtures.ts which imports time.ts, and
time.ts had a top-level `import { vi } from "vitest"`. Playwright
cannot load vitest in its CommonJS context, causing the E2E run to
fail with "Vitest cannot be imported in a CommonJS module".

Move the vitest-dependent helpers (mockCurrentTime, restoreRealTime)
into time.vitest.ts and keep time.ts as pure utilities. The barrel
export re-exports time.vitest so existing unit test imports are
unaffected.
The E2E tests reference TEST_USERS.adminReset1 and
TEST_USERS.demotableSm but those keys were missing from
MOCK_USERS in fixtures.ts, causing TypeError at runtime.

Add all four missing seeded users: adminReset1, deletableUser,
promotableUser, and demotableSm.
@jakebromberg jakebromberg merged commit bea0c9d into main Mar 20, 2026
5 checks passed
@jakebromberg jakebromberg deleted the test/01-test-utilities branch March 20, 2026 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add shared test utilities and mock infrastructure

2 participants