feat(supabase): regenerated types, row aliases, publishSheetVersion gate, schema test
This commit is contained in:
1 parent
f4ceaf68f9
commit
8cac8b1a68
6 files changed
+510
-8
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import { publishSheetVersion } from '../src'
|
||||
import type { FaunosteoClient } from '../src'
|
||||
|
||||
/**
|
||||
* Stub of the two client calls publishSheetVersion makes. The real RPC is
|
||||
* covered by supabase/tests/005_anatomical_sheets.sql; here the point is the
|
||||
* order of the gate: no RPC call unless the document is publishable.
|
||||
*/
|
||||
function stubClient(content: unknown) {
|
||||
const rpcCalls: unknown[] = []
|
||||
const client = {
|
||||
from: () => ({
|
||||
select: () => ({
|
||||
eq: () => ({
|
||||
single: async () => ({ data: { content }, error: null }),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
rpc: async (name: string, args: unknown) => {
|
||||
rpcCalls.push([name, args])
|
||||
return { data: 'published-id', error: null }
|
||||
},
|
||||
}
|
||||
return { client: client as unknown as FaunosteoClient, rpcCalls }
|
||||
}
|
||||
|
||||
const publishable = {
|
||||
schema_version: 1,
|
||||
species_id: 'equine',
|
||||
name: { fr: 'Test', en: 'Test' },
|
||||
description: {},
|
||||
scope_disclaimer: {},
|
||||
attribution: { authors: [], license: null, notes: null },
|
||||
vertebral_config: { regions: [] },
|
||||
categories: [],
|
||||
views: [
|
||||
{
|
||||
id: 'view-main',
|
||||
kind: 'main',
|
||||
name: { fr: 'Vue', en: 'View' },
|
||||
svg_asset_id: '11111111-1111-4111-8111-111111111111',
|
||||
view_box: null,
|
||||
},
|
||||
],
|
||||
annotations: [],
|
||||
finding_zones: [],
|
||||
}
|
||||
|
||||
describe('publishSheetVersion', () => {
|
||||
it('calls the RPC once the document passes shape and integrity checks', async () => {
|
||||
const { client, rpcCalls } = stubClient(publishable)
|
||||
await expect(publishSheetVersion(client, 'v1')).resolves.toBe(
|
||||
'published-id',
|
||||
)
|
||||
expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]])
|
||||
})
|
||||
|
||||
it('refuses a document with integrity errors before any RPC', async () => {
|
||||
const { client, rpcCalls } = stubClient({
|
||||
...publishable,
|
||||
views: [{ ...publishable.views[0], svg_asset_id: null }],
|
||||
})
|
||||
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow(
|
||||
/view\.asset\.missing/,
|
||||
)
|
||||
expect(rpcCalls).toEqual([])
|
||||
})
|
||||
|
||||
it('refuses a document that does not match the schema', async () => {
|
||||
const { client, rpcCalls } = stubClient({ schema_version: 2 })
|
||||
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow()
|
||||
expect(rpcCalls).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -20,6 +20,50 @@ type ColumnMap = {
|
||||
}
|
||||
|
||||
const expectedColumns = {
|
||||
anatomical_sheet_assets: [
|
||||
'byte_size',
|
||||
'content_type',
|
||||
'created_at',
|
||||
'created_by',
|
||||
'file_name',
|
||||
'id',
|
||||
'sha256',
|
||||
'sheet_id',
|
||||
],
|
||||
anatomical_sheet_versions: [
|
||||
'change_note',
|
||||
'content',
|
||||
'created_at',
|
||||
'created_by',
|
||||
'deprecated_at',
|
||||
'deprecated_by',
|
||||
'document_schema_version',
|
||||
'id',
|
||||
'published_at',
|
||||
'published_by',
|
||||
'sheet_id',
|
||||
'status',
|
||||
'updated_at',
|
||||
'version_number',
|
||||
],
|
||||
anatomical_sheets: [
|
||||
'archived_at',
|
||||
'based_on_sheet_id',
|
||||
'based_on_version_id',
|
||||
'created_at',
|
||||
'creator_user_id',
|
||||
'current_version_id',
|
||||
'deleted_at',
|
||||
'description',
|
||||
'id',
|
||||
'is_official',
|
||||
'name',
|
||||
'slug',
|
||||
'species_id',
|
||||
'status',
|
||||
'updated_at',
|
||||
'visibility',
|
||||
],
|
||||
profile_roles: [
|
||||
'granted_at',
|
||||
'granted_by',
|
||||
@@ -43,6 +87,7 @@ const expectedColumns = {
|
||||
'timezone',
|
||||
'updated_at',
|
||||
],
|
||||
species: ['active', 'created_at', 'id', 'name_en', 'name_fr', 'sort_order'],
|
||||
} as const satisfies ColumnMap
|
||||
|
||||
// Compile-time: a column present in the type but absent from the list is an error.
|
||||
@@ -59,7 +104,26 @@ type FunctionArgs = {
|
||||
string)[]
|
||||
}
|
||||
const expectedFunctionArgs = {
|
||||
archive_anatomical_sheet: ['sheet_id'],
|
||||
create_anatomical_sheet: [
|
||||
'description',
|
||||
'document',
|
||||
'name',
|
||||
'official',
|
||||
'slug',
|
||||
'species_id',
|
||||
'visibility',
|
||||
],
|
||||
create_sheet_draft: ['change_note', 'from_version_id', 'sheet_id'],
|
||||
delete_anatomical_sheet: ['sheet_id'],
|
||||
delete_sheet_asset: ['asset_id'],
|
||||
delete_sheet_version: ['version_id'],
|
||||
deprecate_sheet_version: ['reason', 'version_id'],
|
||||
fork_anatomical_sheet: ['name', 'slug', 'source_version_id', 'visibility'],
|
||||
grant_platform_role: ['role', 'target_user_id'],
|
||||
publish_sheet_version: ['version_id'],
|
||||
register_sheet_asset: ['byte_size', 'file_name', 'sha256', 'sheet_id'],
|
||||
restore_anatomical_sheet: ['sheet_id'],
|
||||
revoke_platform_role: ['reason', 'role', 'target_user_id'],
|
||||
} as const satisfies FunctionArgs
|
||||
|
||||
@@ -78,6 +142,10 @@ interface ColumnRow {
|
||||
interface NameRow {
|
||||
name: string
|
||||
}
|
||||
interface EnumRow {
|
||||
name: string
|
||||
labels: string[]
|
||||
}
|
||||
interface FunctionRow {
|
||||
name: string
|
||||
args: string[]
|
||||
@@ -122,17 +190,23 @@ describe('database.types.ts matches the migrated schema', () => {
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
|
||||
it('lists the enum labels in order', async () => {
|
||||
const { rows } = await db.query<NameRow>(
|
||||
`select e.enumlabel as name
|
||||
it('lists every public enum with its labels in order', async () => {
|
||||
const { rows } = await db.query<EnumRow>(
|
||||
`select t.typname as name,
|
||||
array_agg(e.enumlabel order by e.enumsortorder) as labels
|
||||
from pg_enum e join pg_type t on t.oid = e.enumtypid
|
||||
join pg_namespace n on n.oid = t.typnamespace
|
||||
where n.nspname = 'public' and t.typname = 'platform_role'
|
||||
order by e.enumsortorder`,
|
||||
where n.nspname = 'public'
|
||||
group by t.typname order by t.typname`,
|
||||
)
|
||||
expect(rows.map((r) => r.name)).toEqual([
|
||||
...Constants.public.Enums.platform_role,
|
||||
])
|
||||
const actual = Object.fromEntries(rows.map((r) => [r.name, r.labels]))
|
||||
const expected = Object.fromEntries(
|
||||
Object.entries(Constants.public.Enums).map(([name, labels]) => [
|
||||
name,
|
||||
[...labels],
|
||||
]),
|
||||
)
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
|
||||
it('lists exactly the public functions and their argument names', async () => {
|
||||
|
||||
Reference in new issue
Block a user