|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0146 D10, #28727] `os build` asks the `__c` extension-field |
| 5 | + * reservation that `os validate`, the save, restore and publish doors and the |
| 6 | + * marketplace install ask, and refuses the build with the rows `os validate` |
| 7 | + * prints: same rows, same exit code, and NOTHING written to `dist/`. |
| 8 | + * |
| 9 | + * ## The defect, measured before the fix |
| 10 | + * |
| 11 | + * On a plain package declaring a field `x__c` on its own object (case (a)), |
| 12 | + * and on a base's paired `.custom` extending a base object with a field |
| 13 | + * `region` that lacks the suffix (case (b)), `os build --json` exited 0 and |
| 14 | + * wrote `dist/objectstack.json`, while `os validate --json` exited 1 with one |
| 15 | + * `VALIDATION_ERROR` row each. The marketplace install and the save and |
| 16 | + * publish doors refused that artifact later with the same judge. |
| 17 | + * |
| 18 | + * ## One judge, one walk |
| 19 | + * |
| 20 | + * `compile.ts` makes the call `validate.ts` makes |
| 21 | + * (`findExtensionFieldSuffixRefusals`, over `judgeExtensionFieldSuffix`), so |
| 22 | + * the pins below assert EQUALITY with what `os validate` answers for the same |
| 23 | + * config, run in the same process, rather than a copy of the judge's words. |
| 24 | + * Which packages the walk judges is pinned by its own file |
| 25 | + * (`src/utils/extension-field-suffix.test.ts`). Both commands run in-process |
| 26 | + * (`Command.run`), not as spawned CLIs. |
| 27 | + */ |
| 28 | + |
| 29 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 30 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 31 | +import { tmpdir } from 'node:os'; |
| 32 | +import { join, resolve } from 'node:path'; |
| 33 | +import { fileURLToPath } from 'node:url'; |
| 34 | +import Compile from '../src/commands/compile'; |
| 35 | +import Validate from '../src/commands/validate'; |
| 36 | +import { defineStackSource, linkSpec } from './helpers/define-stack-fixture.js'; |
| 37 | + |
| 38 | +const CLI_ROOT = resolve(fileURLToPath(import.meta.url), '..', '..'); |
| 39 | +const BASE = 'com.acme.crm'; |
| 40 | + |
| 41 | +interface Run { |
| 42 | + /** The exit code `this.exit(n)` threw, or 0 when the command returned. */ |
| 43 | + code: number; |
| 44 | + stdout: string; |
| 45 | +} |
| 46 | + |
| 47 | +/** Runs one command in-process; stdout captured ANSI-free, the write callback honoured. */ |
| 48 | +async function run(command: typeof Compile | typeof Validate, argv: string[]): Promise<Run> { |
| 49 | + const chunks: string[] = []; |
| 50 | + const spy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 51 | + chunks.push(String(chunk)); |
| 52 | + const done = rest.find((a) => typeof a === 'function') as ((e?: Error | null) => void) | undefined; |
| 53 | + done?.(null); |
| 54 | + return true; |
| 55 | + }) as never); |
| 56 | + const exitCodeBefore = process.exitCode; |
| 57 | + let code = 0; |
| 58 | + try { |
| 59 | + await command.run(argv, { root: CLI_ROOT }); |
| 60 | + } catch (error) { |
| 61 | + const exit = (error as { oclif?: { exit?: unknown } }).oclif?.exit; |
| 62 | + if (typeof exit !== 'number') throw error; |
| 63 | + code = exit; |
| 64 | + } finally { |
| 65 | + spy.mockRestore(); |
| 66 | + process.exitCode = exitCodeBefore; |
| 67 | + } |
| 68 | + // eslint-disable-next-line no-control-regex |
| 69 | + return { code, stdout: chunks.join('').replace(/\u001b\[[0-9;]*m/g, '') }; |
| 70 | +} |
| 71 | + |
| 72 | +const json = (r: Run, label: string): Record<string, any> => { |
| 73 | + try { |
| 74 | + return JSON.parse(r.stdout) as Record<string, any>; |
| 75 | + } catch { |
| 76 | + throw new Error(`${label}: stdout was not one JSON document (exit ${r.code})\n${r.stdout}`); |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +/** Case (a): a plain package declaring `x__c` on its own object. */ |
| 81 | +const plainOwnField = (): Record<string, unknown> => ({ |
| 82 | + manifest: { id: 'com.acme.partner', name: 'partner', version: '1.0.0', type: 'app', namespace: 'partner' }, |
| 83 | + objects: [ |
| 84 | + { |
| 85 | + name: 'partner_ticket', |
| 86 | + label: 'Ticket', |
| 87 | + sharingModel: 'private', |
| 88 | + fields: { title: { type: 'text', label: 'Title' }, x__c: { type: 'text', label: 'X' } }, |
| 89 | + }, |
| 90 | + ], |
| 91 | +}); |
| 92 | + |
| 93 | +/** The base's paired `.custom` extending the base's `crm_account` with one field named `field`. */ |
| 94 | +const pairExtension = (field: string): Record<string, unknown> => ({ |
| 95 | + manifest: { |
| 96 | + id: `${BASE}.custom`, name: 'ext', version: '1.0.0', type: 'app', namespace: 'crm_custom', |
| 97 | + dependencies: { [BASE]: '^1.0.0' }, |
| 98 | + }, |
| 99 | + objectExtensions: [{ extend: 'crm_account', fields: { [field]: { type: 'text', label: 'Region' } } }], |
| 100 | +}); |
| 101 | + |
| 102 | +let root = ''; |
| 103 | +const fixtures: Record<string, { config: string; dist: string; artifact: string }> = {}; |
| 104 | + |
| 105 | +beforeAll(() => { |
| 106 | + root = mkdtempSync(join(tmpdir(), 'os-build-extension-field-suffix-')); |
| 107 | + const make = (label: string, s: Record<string, unknown>) => { |
| 108 | + const dir = join(root, label); |
| 109 | + mkdirSync(dir, { recursive: true }); |
| 110 | + // `strict: false`: the content is the door's to judge, not the producer's. |
| 111 | + writeFileSync(join(dir, 'objectstack.config.ts'), defineStackSource(s, { strict: false })); |
| 112 | + linkSpec(dir); |
| 113 | + fixtures[label] = { |
| 114 | + config: join(dir, 'objectstack.config.ts'), |
| 115 | + dist: join(dir, 'dist'), |
| 116 | + artifact: join(dir, 'dist', 'objectstack.json'), |
| 117 | + }; |
| 118 | + }; |
| 119 | + make('plainOwnField', plainOwnField()); |
| 120 | + make('pairPlain', pairExtension('region')); |
| 121 | + make('pairSuffixed', pairExtension('region__c')); |
| 122 | +}); |
| 123 | + |
| 124 | +afterAll(() => { |
| 125 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 126 | +}); |
| 127 | + |
| 128 | +const build = (label: string, ...flags: string[]) => |
| 129 | + run(Compile, [fixtures[label].config, '--output', fixtures[label].artifact, ...flags]); |
| 130 | +const validate = (label: string, ...flags: string[]) => run(Validate, [fixtures[label].config, ...flags]); |
| 131 | + |
| 132 | +describe('#28727 — os build refuses what os validate refuses under the `__c` reservation, and writes nothing', () => { |
| 133 | + it.each([ |
| 134 | + ['plainOwnField', 'reserved_outside_pair', 'objects'], |
| 135 | + ['pairPlain', 'unsuffixed_in_pair', 'objectExtensions'], |
| 136 | + ])('THE PIN (%s): --json exits as validate does, with validate\'s rows, and dist/ stays empty', async (label, rule, at) => { |
| 137 | + const validated = await validate(label, '--json'); |
| 138 | + expect(validated.code, validated.stdout).toBe(1); |
| 139 | + const expected = json(validated, `validate ${label}`).errors as Array<Record<string, unknown>>; |
| 140 | + // Anti-vacuity: the judge refuses this config, under the case it names. |
| 141 | + expect(expected.map((e) => [e.code, e.rule])).toEqual([['VALIDATION_ERROR', rule]]); |
| 142 | + expect(String(expected[0]!.path).startsWith(at)).toBe(true); |
| 143 | + |
| 144 | + const built = await build(label, '--json'); |
| 145 | + const payload = json(built, `build ${label}`); |
| 146 | + expect(built.code).toBe(validated.code); |
| 147 | + expect(payload.success).toBe(false); |
| 148 | + expect(payload.errors).toEqual(expected); |
| 149 | + expect(existsSync(fixtures[label].dist), 'os build wrote into dist/ for an artifact the doors refuse').toBe(false); |
| 150 | + }, 60_000); |
| 151 | + |
| 152 | + it('the text face exits 1 and prints the refusal block os validate prints', async () => { |
| 153 | + const validated = await validate('pairPlain'); |
| 154 | + expect(validated.code).toBe(1); |
| 155 | + const block = validated.stdout.slice(validated.stdout.indexOf('✗ 1 field breaks the')).trimEnd(); |
| 156 | + expect(block).toContain('objectExtensions[0].fields.region [VALIDATION_ERROR]'); |
| 157 | + |
| 158 | + const built = await build('pairPlain'); |
| 159 | + expect(built.code).toBe(1); |
| 160 | + expect(built.stdout).toContain(block); |
| 161 | + expect(built.stdout).not.toContain('Build complete'); |
| 162 | + expect(existsSync(fixtures.pairPlain.dist)).toBe(false); |
| 163 | + }, 60_000); |
| 164 | + |
| 165 | + it('CONTROL: the base\'s paired .custom extending with `region__c` builds, and validates clean', async () => { |
| 166 | + expect((await validate('pairSuffixed', '--json')).code).toBe(0); |
| 167 | + const built = await build('pairSuffixed', '--json'); |
| 168 | + const payload = json(built, 'build pairSuffixed'); |
| 169 | + expect(payload.success, JSON.stringify(payload.errors ?? payload.error)).toBe(true); |
| 170 | + expect(built.code).toBe(0); |
| 171 | + const artifact = JSON.parse(readFileSync(fixtures.pairSuffixed.artifact, 'utf8')) as Record<string, any>; |
| 172 | + expect(Object.keys(artifact.objectExtensions[0].fields)).toEqual(['region__c']); |
| 173 | + }, 60_000); |
| 174 | +}); |
0 commit comments