Files
LivingDexTracker/tests/integration/databaseIntegrity.integration.test.ts
T
Josh Creek de39dc78ea test: replace ad-hoc tests with a layered suite and CI workflow
Splits testing into five layers so a failure points at the responsible one:

- tests/unit    isolated utility, repository and service tests
- tests/data    validates the tracked Pokémon, game, region and dex files
- tests/integration  schema, views, constraints, RLS and repositories
- tests/bdd     executable Gherkin for user-visible behaviour
- tests/build   service worker and manifest artifacts per build variant

Replaces the two Playwright specs in client-test/ and the two Vitest files in
test/. Adds a GitHub Actions workflow running the layers as separate jobs, a
mock OAuth provider server so the Drive and Dropbox scenarios never touch real
accounts, and a wrapper that reads the local Supabase keys from
`supabase status` rather than hard-coding them.

Extracts the pure formatting helpers out of PokedexExportService so they can be
unit tested, and makes the provider endpoints configurable so the mock server
can stand in for Google and Dropbox.
2026-09-13 17:38:41 +01:00

115 lines
4.0 KiB
TypeScript

import { createClient } from '@supabase/supabase-js';
import { beforeAll, describe, expect, it } from 'vitest';
const url = process.env.TEST_SUPABASE_URL ?? 'http://127.0.0.1:54321';
const anonKey = process.env.TEST_SUPABASE_ANON_KEY;
const serviceKey = process.env.E2E_SERVICE_ROLE_KEY ?? process.env.SUPABASE_SERVICE_ROLE_KEY;
describe('database integrity and ownership', () => {
beforeAll(() => {
if (!anonKey || !serviceKey) {
throw new Error('Integration tests require TEST_SUPABASE_ANON_KEY and E2E_SERVICE_ROLE_KEY');
}
});
it('enforces catch-record uniqueness and cascades records when a Pokédex is deleted', async () => {
const admin = createClient(url, serviceKey!);
const email = `integration-cascade-${Date.now()}@example.test`;
const { data: created, error: userError } = await admin.auth.admin.createUser({
email,
password: 'Integration123!',
email_confirm: true
});
expect(userError).toBeNull();
const userId = created.user!.id;
const { data: dex, error: dexError } = await admin
.from('pokedexes')
.insert({ userId, name: 'Cascade', isLivingDex: true })
.select('id')
.single();
expect(dexError).toBeNull();
const { data: pokemon } = await admin
.from('pokemon')
.select('id')
.order('id')
.limit(1)
.single();
const record = { userId, pokedexId: dex!.id, pokemonId: pokemon!.id, caught: true };
expect((await admin.from('catch_records').insert(record)).error).toBeNull();
expect((await admin.from('catch_records').insert(record)).error?.code).toBe('23505');
expect((await admin.from('pokedexes').delete().eq('id', dex!.id)).error).toBeNull();
const { count } = await admin
.from('catch_records')
.select('*', { count: 'exact', head: true })
.eq('pokedexId', dex!.id);
expect(count).toBe(0);
});
it("does not disclose another user's Pokédex through row-level security", async () => {
const admin = createClient(url, serviceKey!);
const stamp = Date.now();
const password = 'Integration123!';
const firstEmail = `integration-owner-${stamp}@example.test`;
const secondEmail = `integration-other-${stamp}@example.test`;
const first = await admin.auth.admin.createUser({
email: firstEmail,
password,
email_confirm: true
});
const second = await admin.auth.admin.createUser({
email: secondEmail,
password,
email_confirm: true
});
expect(first.error).toBeNull();
expect(second.error).toBeNull();
const { data: dex } = await admin
.from('pokedexes')
.insert({ userId: first.data.user!.id, name: 'Owner only', isLivingDex: true })
.select('id')
.single();
const other = createClient(url, anonKey!);
expect(
(await other.auth.signInWithPassword({ email: secondEmail, password })).error
).toBeNull();
const { data, error } = await other.from('pokedexes').select('id').eq('id', dex!.id);
expect(error).toBeNull();
expect(data).toEqual([]);
});
it('keeps mapping membership unique for each Pokédex', async () => {
const admin = createClient(url, serviceKey!);
const email = `integration-mapping-${Date.now()}@example.test`;
const created = await admin.auth.admin.createUser({
email,
password: 'Integration123!',
email_confirm: true
});
const { data: dex } = await admin
.from('pokedexes')
.insert({ userId: created.data.user!.id, name: 'Unique mapping', isLivingDex: true })
.select('id')
.single();
const { data: pokemon } = await admin
.from('pokemon')
.select('id')
.order('id')
.limit(1)
.single();
const mapping = { pokedexId: dex!.id, pokemonId: pokemon!.id };
expect((await admin.from('pokedex_pokemon_mapping').insert(mapping)).error).toBeNull();
expect((await admin.from('pokedex_pokemon_mapping').insert(mapping)).error?.code).toBe('23505');
});
it('rejects mapping rows that reference missing records', async () => {
const admin = createClient(url, serviceKey!);
const { error } = await admin.from('pokedex_pokemon_mapping').insert({
pokedexId: '00000000-0000-0000-0000-000000000000',
pokemonId: -1
});
expect(error?.code).toBe('23503');
});
});