Files
faunosteo/packages/supabase/tests/sheets.test.ts
T
bchanot c9ace9aff2 feat(supabase): MFA, species, rights, admin and catalogue access layer (V1-2.5 D)
mfa.ts (closed kinds, TOTP list incl. unverified, enrol, verify, unenrol, assurance); sheets.ts wrappers for species, working copy, account lookup, M7 reads, versions list, drawing download, own grants, with shape parsers. Unit 148, sandbox sheets-admin 5/5 x2.
2026-11-02 16:31:50 +01:00

46 lines
2.0 KiB
TypeScript

import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
import { SHEET_FAILURE_KINDS, SheetFailure, classifySheetError } from '../src/sheets'
import { migrationsDir } from './db/harness'
/**
* The closed failure kinds of the sheet workflow (V1-2.1). The census reads
* every closed code M4, M5 and M7 raise (`raise exception '<code>'`, a lowercase
* dotted word; free-text messages are not codes) so a code added on the server
* can never reach a page as `unavailable` for want of a kind.
*/
const SHEET_MIGRATIONS = [
'20261028100000_sheet_access_hardening.sql',
'20261028110000_sheet_review_workflow.sql',
'20261102100000_sheet_admin_reads.sql',
]
async function raisedCodes(): Promise<string[]> {
const codes = new Set<string>()
for (const file of SHEET_MIGRATIONS) {
const sql = await readFile(join(migrationsDir, file), 'utf8')
for (const match of sql.matchAll(/raise exception '([a-z_]+(?:\.[a-z_]+)*)'/g))
codes.add(match[1])
}
return [...codes].sort()
}
describe('sheet failure kinds', () => {
it('names every closed code the sheet migrations raise', async () => {
const kinds = new Set<string>(SHEET_FAILURE_KINDS)
const codes = await raisedCodes()
expect(codes.length).toBeGreaterThan(50)
expect(codes.filter((code) => !kinds.has(code))).toEqual([])
})
it('folds an answer into its kind and fails closed on anything else', () => {
expect(classifySheetError({ code: 'P0001', message: 'view.asset.unknown' })).toBe('view.asset.unknown')
expect(classifySheetError({ code: '42501', message: 'rights.mfa' })).toBe('rights.mfa')
expect(classifySheetError({ code: '40P01', message: 'deadlock detected' })).toBe('conflict_retry')
expect(classifySheetError({ code: '23505', message: 'duplicate key value' })).toBe('unavailable')
expect(classifySheetError({}, 401)).toBe('session_expired')
expect(classifySheetError(new TypeError('fetch failed'))).toBe('network')
expect(classifySheetError(new SheetFailure('draft.exists'))).toBe('draft.exists')
})
})