Skip to content

Commit 3df38f8

Browse files
committed
test(cli): os validate refuses a foreign _packageId at the item, located
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2fi9zfJhUTubyJYb6V
1 parent d0cde39 commit 3df38f8

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [ADR-0010] `os validate` — the build door an author runs before shipping —
5+
* refuses a collection item whose body carries another package's `_packageId`,
6+
* run for real, located at the item's key.
7+
*
8+
* ## The defect, measured before the fix
9+
*
10+
* A stack whose connector body carried `_packageId: 'com.example.host'` (a
11+
* stamp only the registration writes) validated clean and exited 0; the
12+
* registration door (`ObjectQL.registerApp`) refused it at boot or install
13+
* with `VALIDATION_ERROR` / 400 at `_packageId`. The author heard about it
14+
* from a failed install, not where they wrote it.
15+
*
16+
* ## One judge
17+
*
18+
* Both doors call `judgeCollectionItemOwnerStamp` (`@objectstack/spec/kernel`).
19+
* So the headline pin asserts equality with the judge's own build-door words,
20+
* and with the verdict the registration door returns for the same stack,
21+
* driven through `ObjectQL.registerApp`, rather than a literal copy of the
22+
* words, which would be a second spelling of them.
23+
*
24+
* Integration tier: it spawns the source CLI (`packages/cli/vitest-tiers.ts`).
25+
* Two spawns, one per fixture. The CLI runs through `bin/run-dev.js` (source,
26+
* under `node`), and `@objectstack/spec` resolves through `exports` to `dist/`.
27+
*/
28+
29+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
30+
import { execFile } from 'node:child_process';
31+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
32+
import { tmpdir } from 'node:os';
33+
import { join, resolve } from 'node:path';
34+
import { fileURLToPath } from 'node:url';
35+
import { ObjectQL } from '@objectstack/objectql';
36+
import { judgeCollectionItemOwnerStamp } from '@objectstack/spec/kernel';
37+
import { childEnv } from './helpers/serve-process.js';
38+
import { writeDefineStackConfig } from './helpers/define-stack-fixture.js';
39+
40+
const HERE = resolve(fileURLToPath(import.meta.url), '..');
41+
const CLI = resolve(HERE, '../bin/run-dev.js');
42+
/** A cold spawn of the CLI source entry runs well past vitest's 5 s default. */
43+
const SPAWN_TIMEOUT_MS = 180_000;
44+
45+
const OWNER = 'com.example.ownerstamp';
46+
const FOREIGN = 'com.example.host';
47+
48+
const manifest = {
49+
id: OWNER,
50+
namespace: 'ost',
51+
name: 'Owner stamp door probe',
52+
version: '1.0.0',
53+
type: 'app',
54+
engines: { protocol: '^18' },
55+
};
56+
57+
const connector = (stamp?: string) => ({
58+
name: 'ost_billing',
59+
label: 'Billing',
60+
type: 'api',
61+
provider: 'openapi',
62+
providerConfig: { spec: './specs/billing.json' },
63+
...(stamp === undefined ? {} : { _packageId: stamp }),
64+
});
65+
66+
/** The smallest OpenAPI document the connector's `providerConfig.spec` can name. */
67+
const OPENAPI_DOC = { openapi: '3.0.3', info: { title: 'Billing', version: '1.0.0' }, paths: {} };
68+
69+
const stackOf = (stamp?: string) => ({ manifest, connectors: [connector(stamp)] });
70+
71+
const FIXTURES = {
72+
/** The connector body names another package as its owner. */
73+
stamped: stackOf(FOREIGN),
74+
/** CONTROL: the same body without the stamp. */
75+
unstamped: stackOf(),
76+
} as const;
77+
78+
interface Run {
79+
code: number;
80+
stdout: string;
81+
stderr: string;
82+
}
83+
84+
function runCli(args: string[], cwd: string): Promise<Run> {
85+
return new Promise((resolvePromise) => {
86+
execFile(
87+
process.execPath,
88+
[CLI, ...args],
89+
{ cwd, maxBuffer: 16 * 1024 * 1024, env: childEnv({ NO_COLOR: '1' }) },
90+
(err, stdout, stderr) => {
91+
resolvePromise({
92+
code: err ? (typeof (err as { code?: unknown }).code === 'number' ? (err as unknown as { code: number }).code : 1) : 0,
93+
stdout: String(stdout),
94+
stderr: String(stderr),
95+
});
96+
},
97+
);
98+
});
99+
}
100+
101+
interface ValidateError {
102+
code?: string;
103+
path?: unknown[];
104+
message?: string;
105+
}
106+
107+
/** `os validate --json` on failure: the parse's issues ride `errors`. */
108+
const errorsOf = (stdout: string): ValidateError[] => (JSON.parse(stdout) as { errors?: ValidateError[] }).errors ?? [];
109+
110+
const dirs = new Map<keyof typeof FIXTURES, string>();
111+
112+
beforeAll(() => {
113+
for (const [kind, stack] of Object.entries(FIXTURES) as Array<[keyof typeof FIXTURES, unknown]>) {
114+
const dir = mkdtempSync(join(tmpdir(), `os-owner-stamp-door-${kind}-`));
115+
// `strict: false`: the door's own parse must judge the content, not `defineStack` at load.
116+
writeDefineStackConfig(dir, stack, { strict: false });
117+
// The connector's spec file, so the control's only verdict is the stamp's.
118+
mkdirSync(join(dir, 'specs'));
119+
writeFileSync(join(dir, 'specs', 'billing.json'), JSON.stringify(OPENAPI_DOC));
120+
dirs.set(kind, dir);
121+
}
122+
});
123+
124+
afterAll(() => {
125+
for (const dir of dirs.values()) rmSync(dir, { recursive: true, force: true });
126+
});
127+
128+
describe('os validate refuses a collection item whose body names another package as its owner', () => {
129+
it('exit 1: located at the item\'s `_packageId`, in the judge\'s build words, and the registration door refuses the same body', async () => {
130+
const run = await runCli(['validate', '--json'], dirs.get('stamped')!);
131+
expect(run.code, `expected exit 1:\n${run.stdout}\n${run.stderr}`).toBe(1);
132+
const errors = errorsOf(run.stdout);
133+
expect(errors.map((e) => ({ code: e.code, path: (e.path ?? []).join('.') })))
134+
.toEqual([{ code: 'custom', path: 'connectors.0._packageId' }]);
135+
expect(errors[0].message).toBe(judgeCollectionItemOwnerStamp(connector(FOREIGN), 'connector', 'ost_billing', OWNER, 'build')!.message);
136+
137+
// The same verdict at the registration door, for the same body.
138+
let boot: any;
139+
try {
140+
new ObjectQL().registerApp({ ...manifest, connectors: [connector(FOREIGN)] } as any);
141+
} catch (e) {
142+
boot = e;
143+
}
144+
expect(boot).toMatchObject({ code: 'VALIDATION_ERROR', status: 400 });
145+
expect(boot.issues).toEqual([expect.objectContaining({ path: '_packageId' })]);
146+
expect(boot.message).toBe(judgeCollectionItemOwnerStamp(connector(FOREIGN), 'connector', 'ost_billing', OWNER, 'register')!.message);
147+
}, SPAWN_TIMEOUT_MS);
148+
149+
it('CONTROL: the same body without the stamp exits 0', async () => {
150+
const run = await runCli(['validate', '--json'], dirs.get('unstamped')!);
151+
expect(run.code, `expected exit 0:\n${run.stdout}\n${run.stderr}`).toBe(0);
152+
expect((JSON.parse(run.stdout) as { valid?: boolean }).valid).toBe(true);
153+
}, SPAWN_TIMEOUT_MS);
154+
});

0 commit comments

Comments
 (0)