import { SheetFailure, confirmSheetAssetUpload, listSheetAssets, parseSaveAnswer, registerSheetAsset, uploadSheetDrawing, type FaunosteoClient, } from '../src' /** * Call shapes and failure folding of the drawing accessors (V1-2.6 Lot B). The * same functions run against real Storage and PostgREST in * tests/sandbox/sheet-editor-assets.sandbox.ts. */ const SHEET = '22222222-2222-4222-8222-222222222222' const SHA = 'b'.repeat(64) function stub(answers: { rpc?: unknown; rows?: unknown; upload?: unknown } = {}) { const calls: unknown[][] = [] const settle = (error: unknown, data: unknown = null) => ({ data, error: error ?? null, status: error ? 400 : 200 }) const chain = { select: () => chain, eq: () => chain, then: (r: (v: unknown) => unknown) => r(settle(null, answers.rows ?? [])) } const client = { from: (table: string) => (calls.push(['from', table]), chain), rpc: async (name: string, args: unknown) => (calls.push([name, args]), settle(answers.rpc, 'id-1')), storage: { from: (bucket: string) => ({ upload: async (path: string, body: unknown, options: unknown) => ( calls.push(['upload', bucket, path, body, options]), { data: null, error: answers.upload ?? null } ), }), }, } as unknown as FaunosteoClient return { client, calls } } async function kindOf(promise: Promise) { try { await promise return 'no failure' } catch (error) { return error instanceof SheetFailure ? error.kind : `other: ${String(error)}` } } describe('uploadSheetDrawing', () => { const bytes = new Uint8Array([1, 2, 3]) const failing = (error: unknown) => stub({ upload: error }).client it('uploads .svg as image/svg+xml, never upserting', async () => { const { client, calls } = stub() expect(await uploadSheetDrawing(client, SHA, bytes)).toBe('uploaded') expect(calls).toEqual([ ['upload', 'anatomical-sheet-assets', `${SHA}.svg`, bytes, { contentType: 'image/svg+xml', upsert: false }], ]) }) it('reads an existing object on HTTP 409 or HTTP 400 with body statusCode "409"', async () => { for (const error of [ { name: 'StorageApiError', status: 409, statusCode: '409' }, { name: 'StorageApiError', status: 400, statusCode: '409' }, ]) expect(await uploadSheetDrawing(failing(error), SHA, bytes)).toBe('present') }) it('reads a policy refusal on HTTP 403 or HTTP 400 with body statusCode "403"', async () => { for (const error of [ { name: 'StorageApiError', status: 403, statusCode: '403' }, { name: 'StorageApiError', status: 400, statusCode: '403' }, ]) expect(await kindOf(uploadSheetDrawing(failing(error), SHA, bytes))).toBe('upload.pending_other') }) it('keeps any other refusal apart and a bad hash out of the network', async () => { const kind = (error: unknown) => kindOf(uploadSheetDrawing(failing(error), SHA, bytes)) expect(await kind({ name: 'StorageApiError', status: 400, statusCode: '404' })).toBe('unavailable') expect(await kind({ name: 'StorageApiError', status: 401 })).toBe('session_expired') expect(await kind({ name: 'StorageApiError', status: 429 })).toBe('rate_limited') expect(await kind({ name: 'StorageUnknownError' })).toBe('network') const { client, calls } = stub() expect(await kindOf(uploadSheetDrawing(client, 'xyz', bytes))).toBe('invalid_input') expect(calls).toEqual([]) }) }) describe('register, list, confirm', () => { it('registers with the RPC arguments and answers the asset id', async () => { const { client, calls } = stub() const args = { sheet_id: SHEET, sha256: SHA, byte_size: 12, file_name: 'd.svg' } expect(await registerSheetAsset(client, args)).toBe('id-1') expect(calls).toEqual([['register_sheet_asset', args]]) }) it('folds server kinds through the existing reader', async () => { const refused = stub({ rpc: { code: 'P0001', message: 'upload_missing' } }).client expect(await kindOf(confirmSheetAssetUpload(refused, SHEET))).toBe('upload_missing') expect(await kindOf(registerSheetAsset(stub({ rpc: { code: '42501', message: 'rights.capability' } }).client, { sheet_id: SHEET, sha256: SHA, byte_size: 1, file_name: 'd.svg' }))).toBe('rights.capability') }) it('lists the rows of a sheet and refuses a malformed id', async () => { const rows = [{ id: 'a', sha256: SHA, byte_size: 3, uploaded_at: null }] const { client, calls } = stub({ rows }) expect(await listSheetAssets(client, SHEET)).toEqual(rows) expect(calls).toEqual([['from', 'anatomical_sheet_assets']]) expect(await kindOf(listSheetAssets(client, 'nope'))).toBe('not_found') }) }) describe('parseSaveAnswer', () => { it('reads a positive integer revision', () => { expect(parseSaveAnswer({ revision: 4 })).toEqual({ revision: 4 }) }) it('answers unavailable for anything else', () => { for (const value of [null, 4, {}, { revision: 0 }, { revision: -1 }, { revision: 1.5 }, { revision: '2' }]) expect(() => parseSaveAnswer(value)).toThrow(expect.objectContaining({ kind: 'unavailable' })) }) })