Files
LivingDexTracker/tests/bdd/globalTeardown.ts
T
Josh Creek 4af33709a3 test: make the suite's assertions falsifiable and its state isolated
Several assertions could not fail:

- "the catch update remains saved" was `caught.isChecked() || notes.includes(...)`
  shared by two scenarios, so either half satisfied both. Split into two steps
  that each assert the outcome their own scenario is about.
- The token-refresh check read a global counter with `> 0` and asserted an upload
  had happened `some(...)`, both already satisfied by the preceding scenario. It
  now asserts exactly one refresh, ordered before the upload.
- The box step ignored its box argument and asserted on the first N entries on
  the page; it now scopes to that box and checks its full contents.
- The filter step asserted on whichever entry was first after filtering; it now
  records the caught entry beforehand and names it, and checks the filter did not
  exclude everything.
- The empty-state precondition asserted emptiness instead of establishing it,
  which a fresh user satisfies for free.
- Offline coverage was `caches.keys().length > 0`. It now checks the precache
  contract: one workbox cache holding the shell and a revisioned web manifest,
  with _app/immutable assets cached without a revision query. The scenario that
  claimed to test a trailing slash did not; it is replaced with real offline
  client-side navigation.

The mock provider kept recorded requests, its refresh counter and the
fail-uploads switch in one process-wide object that only one step reset, so
scenario order was load-bearing and the failing-upload scenario poisoned
everything after it. An auto fixture now resets it per scenario, and the mock no
longer records its own control-plane calls. That reset is why the suite stays on
a single worker, which is now documented.

Coverage was gated at 90% per file over an allowlist of exactly the five files
that had tests, so new code was invisible to it permanently. It now measures all
of src/lib with global thresholds at the measured baseline, and no longer runs
the unit tests twice.

Also: a global teardown removes the users each run creates, the Supabase wrapper
distinguishes a stopped stack from a broken CLI call and detects an unseeded
database, the sign-in rate limit is raised above what one serial run needs, and
the integration suite no longer falls back to a hard-coded anon key that would
mask a misconfigured run.

The password-reset scenarios are renamed to what they actually cover: following a
real recovery link bounces to /signin, because the browser client persists no
cookies and so cannot keep the session it parses out of the URL. The helper for
the real flow is left in place and the gap is documented.
2026-09-13 17:38:42 +01:00

49 lines
1.6 KiB
TypeScript

/**
* Deletes the users each BDD run provisions, so repeated local runs do not need a full
* `supabase db reset` to stay clean. Pokédexes, catch records and integrations follow via the
* schema's cascades. Only the suite's own synthetic addresses are touched.
*/
const SUPABASE_URL = process.env.TEST_SUPABASE_URL ?? 'http://127.0.0.1:54321';
const OWNED_EMAIL = /^(bdd|other|integration)-.*@example\.test$/;
type AdminUser = { id: string; email?: string };
async function adminRequest(path: string, init: RequestInit = {}) {
const key = process.env.E2E_SERVICE_ROLE_KEY;
if (!key) return null;
return fetch(`${SUPABASE_URL}${path}`, {
...init,
headers: {
apikey: key,
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
...(init.headers ?? {})
}
});
}
export default async function globalTeardown() {
const listed = await adminRequest('/auth/v1/admin/users?per_page=1000');
if (!listed) {
console.warn('Skipping BDD teardown: E2E_SERVICE_ROLE_KEY is not set.');
return;
}
if (!listed.ok) {
console.warn(`Skipping BDD teardown: unable to list users (${listed.status}).`);
return;
}
const { users = [] } = (await listed.json()) as { users?: AdminUser[] };
const disposable = users.filter((user) => user.email && OWNED_EMAIL.test(user.email));
let failures = 0;
for (const user of disposable) {
const deleted = await adminRequest(`/auth/v1/admin/users/${user.id}`, { method: 'DELETE' });
if (!deleted?.ok) failures++;
}
console.log(
`BDD teardown removed ${disposable.length - failures} of ${disposable.length} test users.`
);
}