import assert from 'node:assert/strict'; import path from 'node:path'; import { test } from 'node:test'; import { EditorSchemas, bundleSchema } from '../src/language/editorSchemas'; import { readSchemas } from '../src/language/schemaCheck'; const schemas = readSchemas(path.resolve(__dirname, '..', '..', 'schemas')); /** Every "$ref" in a bundled schema must point inside it. */ function resolveAll(schema: object): void { const visit = (node: unknown): void => { if (Array.isArray(node)) return node.forEach(visit); if (!node || typeof node !== 'object') return; for (const [key, value] of Object.entries(node)) { if (key === '$ref') { assert.match(String(value), /^#/, `local ref: ${value}`); let target: unknown = schema; for (const part of String(value).slice(1).split('/').filter(Boolean)) { target = (target as Record)[part.replace(/~1/g, '/').replace(/~0/g, '~')]; assert.ok(target !== undefined, `${value} resolves`); } } else visit(value); } }; visit(schema); } test('each schema bundles into one self-contained document', () => { for (const name of Object.keys(schemas)) { const bundled = bundleSchema(schemas, name); resolveAll(bundled); const text = JSON.stringify(bundled); assert.ok(!text.includes('"prefixItems"'), `${name}: no prefixItems`); assert.ok(!text.includes('"$schema"'), `${name}: no $schema`); } }); test('points become draft-07 tuples', () => { const architecture = bundleSchema(schemas, 'architecture.schema.json') as { $defs: Record }> }; const point = architecture.$defs['file:common.schema.json'].$defs.point as Record; assert.deepEqual(point.items, [{ type: 'number' }, { type: 'number' }]); assert.equal(point.additionalItems, false); }); test('a diagram gets the schema of its diagram_type, a decision none', () => { const editor = new EditorSchemas(schemas); assert.equal(editor.forSource('diagram', 'schema_version: 1\ndiagram_type: sequence\n')?.key, 'sequence.schema.json'); assert.equal(editor.forSource('diagram', 'diagram_type: "workflow"\n')?.key, 'workflow.schema.json'); assert.equal(editor.forSource('diagram', 'meta:\n title: x\n')?.key, 'archify.schema.json'); assert.equal(editor.forSource('diagram', 'diagram_type: poster\n')?.key, 'archify.schema.json'); assert.equal(editor.forSource('registry', '')?.key, 'archify-registry.schema.json'); assert.equal(editor.forSource('decision', ''), undefined); });