|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The generated draft is emitted in the ONE authorised `*.object.ts` shape. |
| 5 | + * |
| 6 | + * Director-seat ruling, decision batch #122 item 1, maintainer 「同意」 |
| 7 | + * 2026-09-12, verbatim: |
| 8 | + * |
| 9 | + * > `ObjectSchema.create({ … })` is the one authorised shape for a |
| 10 | + * > `.object.ts` … The factory parses the object against `ObjectSchema` when |
| 11 | + * > the file is evaluated, so an error surfaces where it was written; the typed |
| 12 | + * > literal defers everything to a build the author may never run. |
| 13 | + * |
| 14 | + * `renderObjectSource` wrote the other one — `const X: ServiceObject = { … }` |
| 15 | + * behind an `import type`, closed by `export default X`. Its own docblock calls |
| 16 | + * the output a `*.object.ts`, and `os datasource introspect --out |
| 17 | + * objects/x.object.ts` tells the author to commit it under that suffix, so the |
| 18 | + * bytes really do land where the ruling governs. |
| 19 | + * |
| 20 | + * ## Why this file is not one `toContain` line |
| 21 | + * |
| 22 | + * The failure mode a shape assertion invites is a generator that emits the |
| 23 | + * right-LOOKING call around a definition the factory refuses — the defect moved |
| 24 | + * one layer down, with every string assertion still green. Both halves are |
| 25 | + * therefore pinned separately, and they measure different things: |
| 26 | + * |
| 27 | + * 1. **the form** — the value import, the named export bound to |
| 28 | + * `ObjectSchema.create(`, and the ABSENCE of the refused annotated-literal |
| 29 | + * form. A shape pin that only asserts the new spelling cannot say the old |
| 30 | + * one left. |
| 31 | + * 2. **the round-trip** — the emitted module body is EVALUATED with the real |
| 32 | + * `ObjectSchema` from `@objectstack/spec/data`, which is the same factory |
| 33 | + * call the committed file makes on the author's machine. Nothing is |
| 34 | + * re-spelled here: if `create()` would throw in the author's file, it throws |
| 35 | + * in this test. |
| 36 | + * |
| 37 | + * ## Why the evaluation instrument carries its own negative control |
| 38 | + * |
| 39 | + * An evaluation harness that silently stopped running the factory — a transform |
| 40 | + * that no longer matched, an import that resolved to a stub — would report |
| 41 | + * every draft as valid forever. The last block feeds it a source with one |
| 42 | + * unknown top-level key spliced in and requires it to throw. A round-trip that |
| 43 | + * cannot fail is not a measurement. |
| 44 | + */ |
| 45 | + |
| 46 | +import { describe, it, expect } from 'vitest'; |
| 47 | +import type { IntrospectedSchema } from '@objectstack/spec/contracts'; |
| 48 | +import { ObjectSchema } from '@objectstack/spec/data'; |
| 49 | +import { |
| 50 | + ExternalDatasourceService, |
| 51 | + type DatasourceLike, |
| 52 | +} from '../external-datasource-service.js'; |
| 53 | + |
| 54 | +function remoteSchema(): IntrospectedSchema { |
| 55 | + return { |
| 56 | + dialect: 'postgres', |
| 57 | + introspectedAt: '2026-09-23T00:00:00.000Z', |
| 58 | + tables: { |
| 59 | + 'mart.customers': { |
| 60 | + name: 'mart.customers', |
| 61 | + indexes: [], |
| 62 | + columns: [ |
| 63 | + { name: 'id', type: 'text', nullable: false, primaryKey: true }, |
| 64 | + { name: 'name', type: 'varchar(255)', nullable: true, primaryKey: false }, |
| 65 | + { name: 'signed_up_at', type: 'timestamptz', nullable: true, primaryKey: false }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + }, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +function serviceWith(namespace?: string): ExternalDatasourceService { |
| 73 | + return new ExternalDatasourceService({ |
| 74 | + introspect: async () => remoteSchema(), |
| 75 | + getDatasource: async (name): Promise<DatasourceLike> => ({ name, schemaMode: 'external' }), |
| 76 | + getObject: async () => undefined, |
| 77 | + listObjects: async () => [], |
| 78 | + getNamespace: () => namespace, |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +const draftFor = (namespace?: string) => |
| 83 | + serviceWith(namespace).generateObjectDraft('warehouse', 'customers'); |
| 84 | + |
| 85 | +/** |
| 86 | + * Evaluate the emitted module body and return what its single export is bound |
| 87 | + * to — i.e. run the author's own `ObjectSchema.create(…)` call. |
| 88 | + * |
| 89 | + * The transform is asserted rather than assumed: a silently non-matching |
| 90 | + * `replace` would hand `new Function` a body with no `return` in it, which |
| 91 | + * evaluates to `undefined` and throws nothing at all. |
| 92 | + */ |
| 93 | +function evaluateEmittedModule(source: string): unknown { |
| 94 | + const body = source |
| 95 | + .split('\n') |
| 96 | + .filter((line) => !line.startsWith('import ')) |
| 97 | + .join('\n') |
| 98 | + .replace(/^export const [A-Za-z_$][\w$]* = /m, 'return '); |
| 99 | + expect(body, 'the emitted module has no single named export to evaluate').toContain( |
| 100 | + 'return ObjectSchema.create(', |
| 101 | + ); |
| 102 | + return new Function('ObjectSchema', body)(ObjectSchema) as unknown; |
| 103 | +} |
| 104 | + |
| 105 | +describe('the emitted draft carries the authorised `ObjectSchema.create` shape', () => { |
| 106 | + it('imports the factory as a VALUE and binds a single named export to its call', async () => { |
| 107 | + const draft = await draftFor('wh'); |
| 108 | + |
| 109 | + expect(draft.source).toContain("import { ObjectSchema } from '@objectstack/spec/data';"); |
| 110 | + expect(draft.source).toContain('export const wh_customers = ObjectSchema.create({'); |
| 111 | + // The call is closed as a call, not as a bare object literal. |
| 112 | + expect(draft.source.trimEnd().endsWith('});')).toBe(true); |
| 113 | + }); |
| 114 | + |
| 115 | + it('carries the shape on the no-namespace path too, TODO block and all', async () => { |
| 116 | + const draft = await draftFor(undefined); |
| 117 | + |
| 118 | + // The TODO block renders ABOVE the import; the shape must survive it. |
| 119 | + expect(draft.source).toContain('TODO(namespace)'); |
| 120 | + expect(draft.source).toContain("import { ObjectSchema } from '@objectstack/spec/data';"); |
| 121 | + expect(draft.source).toContain('export const customers = ObjectSchema.create({'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('emits a value import — an `import type` would be elided and the file would throw', async () => { |
| 125 | + const draft = await draftFor('wh'); |
| 126 | + expect(draft.source).not.toContain('import type'); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe('the refused annotated-literal form is absent, by name', () => { |
| 131 | + it.each([ |
| 132 | + ['the `ServiceObject` type annotation', ': ServiceObject = {'], |
| 133 | + ['the type-only spec import', "import type { ServiceObject } from '@objectstack/spec/data';"], |
| 134 | + ['the unexported `const` binding', 'const wh_customers: ServiceObject'], |
| 135 | + ['the default export that closed it', 'export default'], |
| 136 | + ])('%s is gone', async (_label, refused) => { |
| 137 | + const draft = await draftFor('wh'); |
| 138 | + expect(draft.source).not.toContain(refused); |
| 139 | + }); |
| 140 | + |
| 141 | + it('names no `ServiceObject` type at all outside the preserved remote-key note', async () => { |
| 142 | + const draft = await draftFor('wh'); |
| 143 | + const mentions = draft.source |
| 144 | + .split('\n') |
| 145 | + .filter((l) => l.includes('ServiceObject')) |
| 146 | + .map((l) => l.trim()); |
| 147 | + |
| 148 | + // The one survivor is the remote-primary-key tombstone, which explains a |
| 149 | + // SPEC type rather than describing this file's shape. |
| 150 | + expect(mentions).toEqual([ |
| 151 | + "// Preserved as a COMMENT because 'ServiceObject' has no authorable key for a", |
| 152 | + ]); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('round-trip — the factory call inside the emitted file accepts the draft', () => { |
| 157 | + it('evaluates without throwing and yields the definition the draft reports', async () => { |
| 158 | + const draft = await draftFor('wh'); |
| 159 | + const evaluated = evaluateEmittedModule(draft.source) as Record<string, unknown>; |
| 160 | + |
| 161 | + expect(evaluated.name).toBe('wh_customers'); |
| 162 | + expect(evaluated.label).toBe('Customers'); |
| 163 | + expect(evaluated.datasource).toBe('warehouse'); |
| 164 | + expect(evaluated.sharingModel).toBe('private'); |
| 165 | + expect(Object.keys(evaluated.fields as Record<string, unknown>)).toEqual([ |
| 166 | + 'id', |
| 167 | + 'name', |
| 168 | + 'signed_up_at', |
| 169 | + ]); |
| 170 | + expect(evaluated.external).toEqual({ remoteSchema: 'mart', remoteName: 'customers' }); |
| 171 | + }); |
| 172 | + |
| 173 | + it('round-trips the no-namespace draft too — the TODO comment is inert to the factory', async () => { |
| 174 | + const draft = await draftFor(undefined); |
| 175 | + const evaluated = evaluateEmittedModule(draft.source) as Record<string, unknown>; |
| 176 | + expect(evaluated.name).toBe('customers'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('agrees with the structured definition the same draft carries', async () => { |
| 180 | + const draft = await draftFor('wh'); |
| 181 | + const evaluated = evaluateEmittedModule(draft.source); |
| 182 | + // `create()` parses, so the comparison is against the parsed definition — |
| 183 | + // the rendered file and `draft.definition` must describe one object. |
| 184 | + expect(evaluated).toEqual(ObjectSchema.parse(draft.definition)); |
| 185 | + }); |
| 186 | + |
| 187 | + it('NEGATIVE CONTROL — the evaluation really runs the factory', async () => { |
| 188 | + const draft = await draftFor('wh'); |
| 189 | + const poisoned = draft.source.replace( |
| 190 | + " name: 'wh_customers',", |
| 191 | + " name: 'wh_customers',\n workflows: [],", |
| 192 | + ); |
| 193 | + expect(poisoned).not.toBe(draft.source); |
| 194 | + |
| 195 | + expect(() => evaluateEmittedModule(poisoned)).toThrow(/workflows/); |
| 196 | + }); |
| 197 | +}); |
0 commit comments