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, M7 and M8 raise (`raise exception ''`, 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', '20261103100000_sheet_draft_last_save.sql', ] async function raisedCodes(): Promise { const codes = new Set() 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(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') }) })