|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36887 — the published wire schema of `GET /meta/types` |
| 5 | + * (`GetMetaTypesResponseSchema`) declares every member `getMetaTypes()` |
| 6 | + * serves on an entry. |
| 7 | + * |
| 8 | + * A real answer, sent through JSON as the wire sends it, is parsed with the |
| 9 | + * schema. `z.object` strips a key it does not declare, so "every served key |
| 10 | + * survives the parse" is the strict reading: a member the server sends and the |
| 11 | + * schema omits shows up as a missing key. Both arms of the listing are read — |
| 12 | + * registry types and a plugin type with no registry entry — and a type with a |
| 13 | + * plugin-registered action, so `actions` is on the wire. |
| 14 | + * |
| 15 | + * Controls: the same check reports a served key the schema does not declare, |
| 16 | + * and a malformed `form` is refused, so neither pin passes vacuously. |
| 17 | + */ |
| 18 | +import { describe, expect, it } from 'vitest'; |
| 19 | +// `./protocol.js` reaches `import('@objectstack/lint')`; pay its first load at |
| 20 | +// collection, never inside a clocked case. |
| 21 | +import '@objectstack/lint'; |
| 22 | +import { |
| 23 | + assertEngineDeleteDispatch, |
| 24 | + assertEngineUpdateDispatch, |
| 25 | + assertEngineFindOnePredicate, |
| 26 | + type EngineFindOneQueryInput, |
| 27 | +} from '@objectstack/metadata-core'; |
| 28 | +import { DEFAULT_METADATA_TYPE_REGISTRY, registerMetadataTypeActions } from '@objectstack/spec/kernel'; |
| 29 | +import { GetMetaTypesResponseSchema } from '@objectstack/spec/api'; |
| 30 | +import { ObjectStackProtocolImplementation } from './protocol.js'; |
| 31 | + |
| 32 | +/** Every registry type, plus `webhook`: a plugin kind with no registry entry, served by the synthesised arm. */ |
| 33 | +const SERVED_TYPES = [...new Set(DEFAULT_METADATA_TYPE_REGISTRY.map((e) => e.type)), 'webhook']; |
| 34 | + |
| 35 | +function makeProtocol() { |
| 36 | + const engine: any = { |
| 37 | + async findOne(object: string, query?: EngineFindOneQueryInput) { |
| 38 | + assertEngineFindOnePredicate(object, query); return null; |
| 39 | + }, |
| 40 | + async find() { return []; }, |
| 41 | + async insert() { return { id: 'unused' }; }, |
| 42 | + async update(_t: string, data: Record<string, unknown>, opts?: Record<string, unknown>) { |
| 43 | + assertEngineUpdateDispatch(data, opts); |
| 44 | + return { id: null }; |
| 45 | + }, |
| 46 | + async delete(_t: string, opts?: Record<string, unknown>) { |
| 47 | + assertEngineDeleteDispatch(opts); |
| 48 | + return { deleted: 1 }; |
| 49 | + }, |
| 50 | + async count() { return 0; }, |
| 51 | + async transaction(fn: (ctx: unknown) => Promise<unknown>) { return fn(undefined); }, |
| 52 | + async execute() { return {}; }, |
| 53 | + async getObjectSchema() { return undefined; }, |
| 54 | + registry: { |
| 55 | + getRegisteredTypes: () => [...SERVED_TYPES], |
| 56 | + registerItem: () => {}, |
| 57 | + registerObject: () => {}, |
| 58 | + unregisterItem: () => {}, |
| 59 | + listItems: () => [], |
| 60 | + getItem: () => undefined, |
| 61 | + getArtifactItem: () => undefined, |
| 62 | + }, |
| 63 | + }; |
| 64 | + return new ObjectStackProtocolImplementation(engine, () => new Map(), undefined) as any; |
| 65 | +} |
| 66 | + |
| 67 | +registerMetadataTypeActions('datasource', [ |
| 68 | + { |
| 69 | + name: 'test_connection', |
| 70 | + label: 'Test connection', |
| 71 | + type: 'api', |
| 72 | + target: '/api/v1/datasources/${ctx.recordId}/test', |
| 73 | + } as any, |
| 74 | +]); |
| 75 | + |
| 76 | +/** The answer as a client receives it: through JSON, so an `undefined` member is absent. */ |
| 77 | +async function wireAnswer(): Promise<any> { |
| 78 | + return JSON.parse(JSON.stringify(await makeProtocol().getMetaTypes())); |
| 79 | +} |
| 80 | + |
| 81 | +/** `type.key` for every member a served entry carries and the parsed entry lost. */ |
| 82 | +function undeclaredMembers(wire: any, parsed: any): string[] { |
| 83 | + const lost: string[] = []; |
| 84 | + wire.entries.forEach((entry: any, i: number) => { |
| 85 | + for (const key of Object.keys(entry)) { |
| 86 | + if (!Object.prototype.hasOwnProperty.call(parsed.entries[i], key)) lost.push(`${entry.type}.${key}`); |
| 87 | + } |
| 88 | + }); |
| 89 | + return lost; |
| 90 | +} |
| 91 | + |
| 92 | +describe('GetMetaTypesResponseSchema declares what GET /meta/types serves (objectstack#36887)', () => { |
| 93 | + it('a real answer parses, and no served member is stripped', async () => { |
| 94 | + const wire = await wireAnswer(); |
| 95 | + const result = GetMetaTypesResponseSchema.safeParse(wire); |
| 96 | + expect(result.success, JSON.stringify(result.error?.issues?.slice(0, 3))).toBe(true); |
| 97 | + expect(undeclaredMembers(wire, result.data)).toEqual([]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('the members the card names are on the wire, so the parse above read them', async () => { |
| 101 | + const { entries } = await wireAnswer(); |
| 102 | + const byType = new Map<string, any>(entries.map((e: any) => [e.type, e])); |
| 103 | + expect(byType.get('webhook'), 'the synthesised arm is served').toBeDefined(); |
| 104 | + expect(entries.every((e: any) => e.schemaId === e.type)).toBe(true); |
| 105 | + expect(entries.filter((e: any) => e.schema !== undefined).length).toBeGreaterThan(0); |
| 106 | + expect(byType.get('hook').form.sections.length).toBeGreaterThan(0); |
| 107 | + expect(byType.get('hook').list.columns.length).toBeGreaterThan(0); |
| 108 | + expect(byType.get('datasource').actions.map((a: any) => a.name)).toContain('test_connection'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('control — a served member the schema does not declare is reported', async () => { |
| 112 | + const wire = await wireAnswer(); |
| 113 | + wire.entries[0].notDeclared = true; |
| 114 | + const parsed = GetMetaTypesResponseSchema.parse(wire); |
| 115 | + expect(undeclaredMembers(wire, parsed)).toEqual([`${wire.entries[0].type}.notDeclared`]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('control — a malformed form is refused, so `form` is typed, not `unknown`', async () => { |
| 119 | + const wire = await wireAnswer(); |
| 120 | + const hook = wire.entries.find((e: any) => e.type === 'hook'); |
| 121 | + hook.form = { ...hook.form, sections: 'not a list' }; |
| 122 | + expect(GetMetaTypesResponseSchema.safeParse(wire).success).toBe(false); |
| 123 | + }); |
| 124 | +}); |
0 commit comments