diff --git a/package.json b/package.json index 89b8574..b742111 100644 --- a/package.json +++ b/package.json @@ -112,6 +112,7 @@ "cookie-db:refresh": "node scripts/refresh-cookie-database.mjs", "copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/", "dev": "next dev dev --turbo", + "e2e:built": "node scripts/e2e-built.mjs", "dev:generate-importmap": "pnpm dev:payload generate:importmap", "dev:generate-types": "pnpm dev:payload generate:types", "dev:payload": "cross-env PAYLOAD_CONFIG_PATH=./dev/payload.config.ts payload", diff --git a/reference/README.md b/reference/README.md index 4d8a53b..2af9208 100644 --- a/reference/README.md +++ b/reference/README.md @@ -21,13 +21,18 @@ Production check: `pnpm build && pnpm payload run e2e/create-test-admin.ts && AL (Playwright starts the built site on port 3100 itself; another port: `E2E_BASE_URL=http://localhost:3200`, built with the same `NEXT_PUBLIC_SERVER_URL`; a server you started yourself: `E2E_REUSE_SERVER=1`). -Access rules on a built site, both bundlers (a staff collection that a plugin listed after ipalKit adds — unit tests -run unminified code and cannot see what a bundle renames): +Every Playwright run first resets the test admins and clears their stored 2FA attempt counts (`e2e/global-setup.ts`), +so a spec does not fail because of what ran before it. The fresh-site spec needs an EMPTY database: +`E2E_SKIP_ADMIN_SETUP=1 DATABASE_URI= pnpm exec playwright test two-factor-setup`. + +What only a built site shows, with both bundlers — from the plugin repository's root, part of the release gate: ```bash -E2E_LATE_STAFF=1 pnpm build && pnpm payload run e2e/create-test-admin.ts && E2E_LATE_STAFF=1 ALLOW_LOCAL_MEDIA=1 pnpm exec playwright test late-staff -E2E_LATE_STAFF=1 pnpm exec next build --turbopack && pnpm payload run e2e/create-test-admin.ts && E2E_LATE_STAFF=1 ALLOW_LOCAL_MEDIA=1 pnpm exec playwright test late-staff +pnpm e2e:built # webpack, then Turbopack: build with the late-staff fixture, check the public files, run the specs ``` -Without `E2E_LATE_STAFF=1` the fixture adds nothing and the spec is skipped. +It checks the account rules of a staff collection that a plugin listed after ipalKit adds or copies +(`e2e/late-staff-fixture.ts`, on only with `E2E_LATE_STAFF=1`; `e2e/late-staff.spec.ts`) and that no package manifest +is among the public files. Unit tests run unminified code and cannot see what a bundle renames or publishes. +Without `E2E_LATE_STAFF=1` the fixture adds nothing and the spec is skipped. Rebuild with `pnpm build` afterwards. ## What is where | Convention | Files | diff --git a/reference/e2e/global-setup.ts b/reference/e2e/global-setup.ts new file mode 100644 index 0000000..25b06ca --- /dev/null +++ b/reference/e2e/global-setup.ts @@ -0,0 +1,14 @@ +import { execFileSync } from 'node:child_process' + +/** + * Before every Playwright run: the test admins are made (or reset) and their stored 2FA attempt counts cleared + * (e2e/create-test-admin.ts). The count is kept in the database — 5 verify attempts per account in 10 minutes — + * so without this a second run within ten minutes, or one spec run after the whole suite, would find the test + * admin locked out: the result would depend on what ran before. Skipped when the test admin is not configured + * (the specs that need it skip themselves) or with E2E_SKIP_ADMIN_SETUP=1. + */ +export default function globalSetup(): void { + const { E2E_ADMIN_EMAIL, E2E_ADMIN_PASSWORD, E2E_SKIP_ADMIN_SETUP, E2E_TOTP_SECRET } = process.env + if (E2E_SKIP_ADMIN_SETUP === '1' || !E2E_ADMIN_EMAIL || !E2E_ADMIN_PASSWORD || !E2E_TOTP_SECRET) {return} + execFileSync('pnpm', ['payload', 'run', 'e2e/create-test-admin.ts'], { env: { ...process.env, NODE_ENV: 'development' }, stdio: 'inherit' }) +} diff --git a/reference/playwright.config.ts b/reference/playwright.config.ts index 392b003..bc93ea0 100644 --- a/reference/playwright.config.ts +++ b/reference/playwright.config.ts @@ -8,9 +8,10 @@ try { } /** - * Smoke tests against a built, seeded site: pnpm build && pnpm seed && pnpm payload run e2e/create-test-admin.ts. - * Run create-test-admin.ts again before repeating the tests within ten minutes: the 2FA verify limit (5 attempts per - * account in 10 minutes) is stored in the database, and that script clears the test admins' counts. + * Smoke tests against a built, seeded site: pnpm seed && pnpm build. + * The 2FA verify limit (5 attempts per account in 10 minutes) is stored in the database; the global setup resets the + * test admins and clears their counts before every run (e2e/global-setup.ts; E2E_SKIP_ADMIN_SETUP=1 for a run that + * needs an EMPTY database, the fresh-site spec). * E2E_BASE_URL picks the port (default 3100). Playwright starts its own server there and fails when the port is * taken, so another project's dev server is never tested by mistake; E2E_REUSE_SERVER=1 uses a server you started. */ @@ -18,6 +19,8 @@ const baseURL = process.env.E2E_BASE_URL ?? 'http://localhost:3100' export default defineConfig({ forbidOnly: !!process.env.CI, + // makes every run independent of the one before: test admins reset, stored 2FA attempt counts cleared + globalSetup: './e2e/global-setup.ts', projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], reporter: process.env.CI ? 'line' : 'list', retries: process.env.CI ? 1 : 0, diff --git a/scripts/e2e-built.mjs b/scripts/e2e-built.mjs new file mode 100644 index 0000000..aceb254 --- /dev/null +++ b/scripts/e2e-built.mjs @@ -0,0 +1,70 @@ +#!/usr/bin/env node +/** + * pnpm e2e:built — the checks that only a BUILT site can make, with BOTH bundlers. + * + * Unit tests run unminified code. A production bundle renames every function and decides what becomes a public + * asset; two defects of 1.16.7's review lived only there (a rule that told Payload's default by its name; the + * plugin's package.json among a Turbopack build's static files). So for the reference site, once with webpack and + * once with Turbopack: + * 1. production build with the late-staff fixture (E2E_LATE_STAFF=1, reference/e2e/late-staff-fixture.ts); + * 2. no package manifest under .next/static (checked on disk, before anything is started); + * 3. Playwright against `next start`: e2e/late-staff.spec.ts and the smoke test "no package.json is published". + * + * Needs what the reference site's "Production check" needs (reference/README.md): reference/.env with a local, + * seeded database and the E2E_* test admin. The test admins are reset by Playwright's global setup. + * Port: E2E_BASE_URL (default http://localhost:3100), the same value as NEXT_PUBLIC_SERVER_URL in reference/.env. + * Options: --only=webpack | --only=turbopack; --skip-plugin-build (dist and the injected copy are current). + */ +import { spawnSync } from 'node:child_process' +import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' + +const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') +const site = path.join(root, 'reference') +const args = process.argv.slice(2) +const only = args.find((a) => a.startsWith('--only='))?.slice('--only='.length) +const bundlers = ['webpack', 'turbopack'].filter((b) => !only || b === only) +if (!bundlers.length) {throw new Error(`--only must be webpack or turbopack, got "${only}"`)} + +const run = (label, command, commandArgs, cwd, env = {}) => { + console.log(`\n[e2e:built] ${label}`) + const result = spawnSync(command, commandArgs, { cwd, env: { ...process.env, ...env }, stdio: 'inherit' }) + if (result.status !== 0) { + console.error(`\n[e2e:built] ✗ FAILED: ${label}`) + process.exit(result.status ?? 1) + } +} +const walk = (dir) => (existsSync(dir) ? readdirSync(dir).flatMap((name) => { + const file = path.join(dir, name) + return statSync(file).isDirectory() ? walk(file) : [file] +}) : []) + +if (!existsSync(path.join(site, '.env'))) { + console.error('[e2e:built] reference/.env is missing: cp reference/.env.example reference/.env, fill it (local database, E2E_* test admin), seed (reference/README.md).') + process.exit(1) +} +if (!args.includes('--skip-plugin-build')) { + run('build the plugin', 'pnpm', ['build'], root) + run('refresh the plugin copy in the reference site', 'pnpm', ['install', '--ignore-workspace'], site) +} +const fixture = { E2E_LATE_STAFF: '1', NODE_OPTIONS: `${process.env.NODE_OPTIONS ?? ''} --max-old-space-size=3072`.trim() } +const passed = [] +for (const bundler of bundlers) { + run(`${bundler}: production build with the late-staff fixture`, 'pnpm', ['exec', 'next', 'build', `--${bundler}`], site, fixture) + const statics = path.join(site, '.next', 'static') + const files = walk(statics) + if (!files.length) { + console.error(`[e2e:built] ✗ ${bundler}: .next/static is empty — the build did not produce a site`) + process.exit(1) + } + const manifests = files.filter((file) => /package[^/]*\.json$/.test(file) || (file.endsWith('.json') && /"(?:devDependencies|peerDependencies|publishConfig)"\s*:/.test(readFileSync(file, 'utf8')))) + if (manifests.length) { + console.error(`[e2e:built] ✗ ${bundler}: a package manifest is among the PUBLIC files of the build:\n${manifests.map((f) => ` /_next/static/${path.relative(statics, f)}`).join('\n')}`) + process.exit(1) + } + console.log(`[e2e:built] ✓ ${bundler}: no package manifest among ${files.length} public files`) + run(`${bundler}: the built site — late-staff spec and the published-files check`, 'pnpm', ['exec', 'playwright', 'test', 'late-staff', 'smoke', '--grep', 'late staff collection|no package.json is published'], site, { ALLOW_LOCAL_MEDIA: process.env.ALLOW_LOCAL_MEDIA ?? '1', E2E_LATE_STAFF: '1' }) + passed.push(bundler) +} +console.log(`\n[e2e:built] passed: ${passed.join(' and ')}. (The reference site's .next now holds the ${passed.at(-1)} build WITH the fixture: rebuild with \`pnpm build\` there before other checks.)`)