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.
46 lines
2.0 KiB
TypeScript
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')
|
|
})
|
|
})
|