Files
LivingDexTracker/tests/unit/combinedDataRepository.test.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

118 lines
4.4 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
type Call = { method: string; args: unknown[] };
type TableQuery = { table: string; calls: Call[] };
/**
* Minimal recording stand-in for a Supabase query builder.
*
* Every chained call is recorded and returns the builder, and the builder is thenable so
* `await query` / `await query.range(...)` resolve like a real PostgREST response. Returning
* an empty data set keeps the repository's paging loops to a single iteration.
*/
function createSupabaseStub() {
const queries: TableQuery[] = [];
const from = (table: string) => {
const record: TableQuery = { table, calls: [] };
queries.push(record);
const builder: Record<string, unknown> = new Proxy(
{},
{
get(_target, prop: string) {
if (prop === 'then') {
return (resolve: (value: unknown) => unknown) =>
resolve({ data: [], error: null, count: 0 });
}
return (...args: unknown[]) => {
record.calls.push({ method: prop, args });
return builder;
};
}
}
);
return builder;
};
return { supabase: { from } as never, queries };
}
const queryFor = (queries: TableQuery[], table: string) => queries.filter((q) => q.table === table);
const hasCall = (q: TableQuery, method: string, args: unknown[]) =>
q.calls.some((c) => c.method === method && JSON.stringify(c.args) === JSON.stringify(args));
const mentionsIsDefaultForm = (q: TableQuery) =>
q.calls.some((c) => JSON.stringify(c.args).includes('isDefaultForm'));
describe('CombinedDataRepository base-form filtering', () => {
it('filters to default forms only when the form dex toggle is off', async () => {
const { supabase, queries } = createSupabaseStub();
const repo = new CombinedDataRepository(supabase, 'user-1', null);
await repo.findAllCombinedData('user-1', false, '', '', []);
const [entries] = queryFor(queries, 'pokedex_entries');
expect(entries).toBeDefined();
expect(hasCall(entries, 'eq', ['isDefaultForm', true])).toBe(true);
});
it('applies no form filter at all when the form dex toggle is on', async () => {
const { supabase, queries } = createSupabaseStub();
const repo = new CombinedDataRepository(supabase, 'user-1', null);
await repo.findAllCombinedData('user-1', true, '', '', []);
const [entries] = queryFor(queries, 'pokedex_entries');
expect(mentionsIsDefaultForm(entries)).toBe(false);
});
it('filters dex-scoped queries to default forms when the form dex toggle is off', async () => {
const { supabase, queries } = createSupabaseStub();
const repo = new CombinedDataRepository(supabase, 'user-1', null);
await repo.findAllCombinedData('user-1', false, '', '', ['black-unova']);
const [dexEntries] = queryFor(queries, 'game_pokedex_entry_details');
expect(dexEntries).toBeDefined();
expect(hasCall(dexEntries, 'eq', ['isDefaultForm', true])).toBe(true);
});
it('counts with the same default-form filter the listing uses', async () => {
const { supabase, queries } = createSupabaseStub();
const repo = new CombinedDataRepository(supabase, 'user-1', null);
await repo.countCombinedData(false, '', '', []);
const [entries] = queryFor(queries, 'pokedex_entries');
expect(hasCall(entries, 'eq', ['isDefaultForm', true])).toBe(true);
});
/**
* Regression guard. game_pokedex_entries is seeded from `form IS NULL` rows, so a default
* form that has a NAME (e.g. Rotom "Lightbulb", Basculin "Red-striped") is absent from the
* game dex tables and can only reach a game-scoped form dex through this supplement query.
*
* Switching this filter to `isDefaultForm` looks like a tidy-up, but it silently drops
* those rows: base Rotom disappeared from the Black form dex while its five appliance
* forms remained. Keep it keyed on `form` - excludeIds already dedupes whatever the dex
* table does list.
*/
it('supplements game forms by form name, never by isDefaultForm', async () => {
const { supabase, queries } = createSupabaseStub();
const repo = new CombinedDataRepository(supabase, 'user-1', null);
await repo.findAllCombinedData('user-1', true, '', 'Black', ['black-unova']);
const supplements = queryFor(queries, 'pokedex_entries');
expect(supplements.length).toBeGreaterThan(0);
const supplement = supplements[0];
expect(hasCall(supplement, 'not', ['form', 'is', null])).toBe(true);
expect(mentionsIsDefaultForm(supplement)).toBe(false);
});
});