|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0146 D10, second rule] `os validate` asks the field-patch target check — |
| 5 | + * the install door's own judge (`fieldPatchTargetRefusals`), over the walk |
| 6 | + * `utils/field-patch-targets.ts` takes, whose own file pins which targets it |
| 7 | + * reads. What is pinned here is the command: a stack whose object extension |
| 8 | + * patches a field or option value its in-stack target lacks exits 1, each |
| 9 | + * refusal located on `errors`; the same stack patching what exists validates |
| 10 | + * clean. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 14 | +import { execFile } from 'node:child_process'; |
| 15 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 16 | +import { tmpdir } from 'node:os'; |
| 17 | +import { join, resolve } from 'node:path'; |
| 18 | +import { fileURLToPath } from 'node:url'; |
| 19 | +import { childEnv } from './helpers/serve-process.js'; |
| 20 | +import { linkSpec } from './helpers/define-stack-fixture.js'; |
| 21 | + |
| 22 | +const HERE = resolve(fileURLToPath(import.meta.url), '..'); |
| 23 | +const CLI = resolve(HERE, '../bin/run-dev.js'); |
| 24 | +const TSX = resolve(HERE, '../../../node_modules/.bin/tsx'); |
| 25 | + |
| 26 | +const stack = (fieldPatches: string) => ` |
| 27 | +import { defineStack } from '@objectstack/spec'; |
| 28 | +
|
| 29 | +export default defineStack({ |
| 30 | + manifest: { id: 'com.acme.fpt', name: 'fpt', version: '1.0.0', type: 'app', namespace: 'fpt' }, |
| 31 | + objects: [{ |
| 32 | + name: 'fpt_account', |
| 33 | + label: 'Account', |
| 34 | + fields: { |
| 35 | + name: { type: 'text', label: 'Name' }, |
| 36 | + industry: { type: 'select', label: 'Industry', options: [{ value: 'tech', label: 'Tech' }, { value: 'retail', label: 'Retail' }] }, |
| 37 | + }, |
| 38 | + }], |
| 39 | + objectExtensions: [{ extend: 'fpt_account', fieldPatches: ${fieldPatches} }], |
| 40 | +}, { strict: false }); |
| 41 | +`; |
| 42 | + |
| 43 | +interface Run { |
| 44 | + code: number; |
| 45 | + stdout: string; |
| 46 | + stderr: string; |
| 47 | +} |
| 48 | + |
| 49 | +function runValidate(cwd: string): Promise<Run> { |
| 50 | + return new Promise((resolvePromise) => { |
| 51 | + execFile( |
| 52 | + TSX, |
| 53 | + [CLI, 'validate', '--json'], |
| 54 | + // `childEnv()`, never a bulk `process.env` copy (check:cli-test-child-env). |
| 55 | + { cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) }, |
| 56 | + (err, stdout, stderr) => { |
| 57 | + resolvePromise({ |
| 58 | + code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0, |
| 59 | + stdout: String(stdout), |
| 60 | + stderr: String(stderr), |
| 61 | + }); |
| 62 | + }, |
| 63 | + ); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +let missingDir: string; |
| 68 | +let matchingDir: string; |
| 69 | + |
| 70 | +beforeAll(() => { |
| 71 | + missingDir = mkdtempSync(join(tmpdir(), 'os-validate-fpt-missing-')); |
| 72 | + writeFileSync(join(missingDir, 'objectstack.config.ts'), |
| 73 | + stack(`{ sector: { label: 'Sector' }, industry: { options: { finance: { label: 'Finance' } } } }`)); |
| 74 | + linkSpec(missingDir); |
| 75 | + matchingDir = mkdtempSync(join(tmpdir(), 'os-validate-fpt-matching-')); |
| 76 | + writeFileSync(join(matchingDir, 'objectstack.config.ts'), |
| 77 | + stack(`{ industry: { label: 'Sector', options: { tech: { label: 'Technology' } } } }`)); |
| 78 | + linkSpec(matchingDir); |
| 79 | +}); |
| 80 | + |
| 81 | +afterAll(() => { |
| 82 | + rmSync(missingDir, { recursive: true, force: true }); |
| 83 | + rmSync(matchingDir, { recursive: true, force: true }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe('os validate — the field-patch target check (ADR-0146 D10)', () => { |
| 87 | + it('exits 1 with each patch naming a missing field or option value located on errors', async () => { |
| 88 | + const run = await runValidate(missingDir); |
| 89 | + expect(run.code, run.stderr).toBe(1); |
| 90 | + const report = JSON.parse(run.stdout); |
| 91 | + expect(report.valid).toBe(false); |
| 92 | + expect(report.errors.map((e: { path: string }) => e.path)).toEqual([ |
| 93 | + 'objectExtensions[0].fieldPatches.sector', |
| 94 | + 'objectExtensions[0].fieldPatches.industry.options.finance', |
| 95 | + ]); |
| 96 | + expect(report.errors.map((e: { reason: string }) => e.reason)).toEqual(['field', 'option']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('validates clean when the patch names a field and option value the target has', async () => { |
| 100 | + const run = await runValidate(matchingDir); |
| 101 | + expect(run.code, `${run.stdout}\n${run.stderr}`).toBe(0); |
| 102 | + expect(JSON.parse(run.stdout).valid).toBe(true); |
| 103 | + }); |
| 104 | +}); |
0 commit comments