import assert from 'assert'; import * as fs from 'fs/promises'; import * as os from 'os'; import path from 'path'; import { deleteSchema, isValidSchemaPayload, listTables, loadSchema, mergeSchema, newSchemaWrapper, saveSchema, schemaFile, updateSchemaWrapper, } from '../src/service/sharedTableSchema'; import { SynthOSConfig } from '../src/init'; import { FsStorageProvider } from '../src/storage'; function makeConfig(pagesFolder: string): SynthOSConfig { return { localFolder: '.synthos', pagesFolder, requiredPagesFolders: [], defaultPagesFolders: [], defaultScriptsFolders: [], defaultThemesFolders: [], staticFilesFolders: [], serviceConnectorsFolders: [], requiredPages: [], storageProvider: new FsStorageProvider(), debug: false, debugPageUpdates: false, }; } const SAMPLE_SCHEMA: Record = { type: 'object', properties: { first_name: { type: 'string' }, last_name: { type: 'string' }, }, required: ['first_name'], }; describe('sharedTableSchema — pure helpers', () => { describe('schemaFile', () => { it('returns /.schema.json', () => { assert.strictEqual(schemaFile('/p/shared', 'employees'), path.join('/p/shared', 'employees.schema.json')); }); }); describe('isValidSchemaPayload', () => { it('accepts plain objects', () => { assert.strictEqual(isValidSchemaPayload({ type: 'object' }), true); }); it('rejects arrays, null, primitives', () => { assert.strictEqual(isValidSchemaPayload([]), false); assert.strictEqual(isValidSchemaPayload(null), false); assert.strictEqual(isValidSchemaPayload('schema'), false); assert.strictEqual(isValidSchemaPayload(42), false); }); }); describe('newSchemaWrapper', () => { it('initializes version, timestamps, schema, definedBy', () => { const w = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z', 'builder'); assert.strictEqual(w.version, 1); assert.strictEqual(w.createdAt, '2026-04-25T00:00:00.000Z'); assert.strictEqual(w.updatedAt, '2026-04-25T00:00:00.000Z'); assert.strictEqual(w.definedBy, 'builder'); assert.deepStrictEqual(w.schema, SAMPLE_SCHEMA); }); it('omits definedBy when not provided', () => { const w = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z'); assert.strictEqual(w.definedBy, undefined); }); }); describe('updateSchemaWrapper', () => { it('preserves createdAt, advances updatedAt', () => { const orig = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z', 'page-a'); const updated = updateSchemaWrapper(orig, { type: 'object' }, '2026-04-26T00:00:00.000Z', 'page-b'); assert.strictEqual(updated.createdAt, '2026-04-25T00:00:00.000Z'); assert.strictEqual(updated.updatedAt, '2026-04-26T00:00:00.000Z'); assert.strictEqual(updated.definedBy, 'page-b'); }); it('keeps existing definedBy when no override given', () => { const orig = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z', 'page-a'); const updated = updateSchemaWrapper(orig, { type: 'object' }, '2026-04-26T00:00:00.000Z'); assert.strictEqual(updated.definedBy, 'page-a'); }); }); describe('mergeSchema', () => { it('replace mode wins outright', () => { const { merged, conflicts } = mergeSchema(SAMPLE_SCHEMA, { type: 'object', properties: { name: { type: 'string' } } }, 'replace'); assert.deepStrictEqual(conflicts, []); assert.deepStrictEqual(merged, { type: 'object', properties: { name: { type: 'string' } } }); }); it('additive: adds new fields, keeps existing', () => { const incoming: Record = { type: 'object', properties: { phone: { type: 'string' }, }, required: ['phone'], }; const { merged, conflicts } = mergeSchema(SAMPLE_SCHEMA, incoming, 'additive'); assert.deepStrictEqual(conflicts, []); const props = (merged.properties as Record); assert.ok(props.first_name); assert.ok(props.last_name); assert.ok(props.phone); assert.deepStrictEqual(merged.required, ['first_name', 'phone']); }); it('additive: existing wins on overlap when types match', () => { const incoming: Record = { type: 'object', properties: { first_name: { type: 'string', description: 'changed' }, }, }; const { merged, conflicts } = mergeSchema(SAMPLE_SCHEMA, incoming, 'additive'); assert.deepStrictEqual(conflicts, []); const fn = (merged.properties as Record).first_name; assert.strictEqual(fn.description, undefined, 'existing definition should be preserved'); }); it('additive: type conflicts produce 409-style result with merged === existing', () => { const incoming: Record = { type: 'object', properties: { first_name: { type: 'number' }, }, }; const { merged, conflicts } = mergeSchema(SAMPLE_SCHEMA, incoming, 'additive'); assert.strictEqual(conflicts.length, 1); assert.strictEqual(conflicts[0].field, 'first_name'); assert.strictEqual(merged, SAMPLE_SCHEMA); }); it('additive: undefined existing returns incoming as-is', () => { const incoming: Record = { type: 'object', properties: { x: { type: 'string' } } }; const { merged, conflicts } = mergeSchema(undefined, incoming, 'additive'); assert.deepStrictEqual(conflicts, []); assert.deepStrictEqual(merged, incoming); }); }); }); describe('sharedTableSchema — IO + listing', () => { let tmpDir: string; let config: SynthOSConfig; let parent: string; beforeEach(async () => { tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'synthos-schema-test-')); parent = path.join(tmpDir, 'shared'); config = makeConfig(tmpDir); await fs.mkdir(parent, { recursive: true }); }); afterEach(async () => { await fs.rm(tmpDir, { recursive: true, force: true }); }); it('saveSchema + loadSchema roundtrip', async () => { const wrapper = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z', 'builder'); await saveSchema(config, parent, 'employees', wrapper); const loaded = await loadSchema(config, parent, 'employees'); assert.deepStrictEqual(loaded, wrapper); }); it('loadSchema returns undefined for missing sidecar', async () => { const loaded = await loadSchema(config, parent, 'ghost'); assert.strictEqual(loaded, undefined); }); it('loadSchema returns undefined for malformed sidecar', async () => { await fs.writeFile(path.join(parent, 'bad.schema.json'), 'not-json'); const loaded = await loadSchema(config, parent, 'bad'); assert.strictEqual(loaded, undefined); }); it('deleteSchema removes the sidecar; idempotent on missing', async () => { const wrapper = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z'); await saveSchema(config, parent, 'employees', wrapper); await deleteSchema(config, parent, 'employees'); assert.strictEqual(await loadSchema(config, parent, 'employees'), undefined); // Second delete should not throw await deleteSchema(config, parent, 'employees'); }); describe('listTables', () => { it('lists subfolder tables with record count', async () => { const empFolder = path.join(parent, 'employees'); await fs.mkdir(empFolder, { recursive: true }); await fs.writeFile(path.join(empFolder, 'a.json'), '{}'); await fs.writeFile(path.join(empFolder, 'b.json'), '{}'); const tables = await listTables(config, parent); assert.deepStrictEqual(tables, [ { name: 'employees', hasSchema: false, recordCount: 2 }, ]); }); it('lists schema-only tables (no records yet)', async () => { const wrapper = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z'); await saveSchema(config, parent, 'locations', wrapper); const tables = await listTables(config, parent); assert.deepStrictEqual(tables, [ { name: 'locations', hasSchema: true, recordCount: 0 }, ]); }); it('combines records + schema entries', async () => { const empFolder = path.join(parent, 'employees'); await fs.mkdir(empFolder, { recursive: true }); await fs.writeFile(path.join(empFolder, 'a.json'), '{}'); const wrapper = newSchemaWrapper(SAMPLE_SCHEMA, '2026-04-25T00:00:00.000Z'); await saveSchema(config, parent, 'employees', wrapper); const tables = await listTables(config, parent); assert.deepStrictEqual(tables, [ { name: 'employees', hasSchema: true, recordCount: 1 }, ]); }); it('respects reserved set', async () => { const filesFolder = path.join(parent, 'files'); await fs.mkdir(filesFolder, { recursive: true }); const empFolder = path.join(parent, 'employees'); await fs.mkdir(empFolder, { recursive: true }); const tables = await listTables(config, parent, new Set(['files'])); assert.strictEqual(tables.length, 1); assert.strictEqual(tables[0].name, 'employees'); }); it('returns [] for missing namespace', async () => { const tables = await listTables(config, path.join(tmpDir, 'doesnotexist')); assert.deepStrictEqual(tables, []); }); }); });