Skip to content

Commit df778ab

Browse files
committed
test(cli, spec): pin bundled-only plugin packaging and the native-addon refusal (red)
Claude-Session: https://claude.ai/code/session_01L6bhgWLCWkh3MaLj1qTLCe Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4a00b2b commit df778ab

2 files changed

Lines changed: 518 additions & 0 deletions

File tree

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* `os plugin build` packs one strategy, `bundled`, and refuses a native addon
5+
* (ADR-0025 v2 §3.3; M1 of #27123).
6+
*
7+
* Driven through the real door, `PluginBuild.run([dir])`, over a plugin
8+
* project on disk with its own `node_modules`, so esbuild resolves exactly
9+
* what an author's tree would hand it. Every refusal case asserts the three
10+
* things a refusal owes: exit 1, the refused subject named, and NO artifact
11+
* on disk — a build that prints a refusal but still writes the `.osplugin`
12+
* would hand `os plugin sign` the very artifact it refused.
13+
*
14+
* `manifest-deps` (the retired second strategy) reaches this door through
15+
* `ManifestSchema`, which `@objectstack/cli` resolves from
16+
* `@objectstack/spec`'s `dist/` (no source alias), so that case reads the
17+
* built spec.
18+
*/
19+
20+
import { describe, it, expect, afterAll, afterEach, vi } from 'vitest';
21+
import { existsSync } from 'node:fs';
22+
import { mkdtemp, rm, mkdir, writeFile, readFile } from 'node:fs/promises';
23+
import { tmpdir } from 'node:os';
24+
import { dirname, join } from 'node:path';
25+
import { MANIFEST_FILENAME, readTarGz } from '../src/utils/osplugin.js';
26+
import PluginBuild from '../src/commands/plugin/build.js';
27+
28+
const ID = 'com.acme.bundled';
29+
const ARTIFACT = `${ID}-1.0.0.osplugin`;
30+
31+
const dirs: string[] = [];
32+
afterEach(() => vi.restoreAllMocks());
33+
afterAll(async () => {
34+
for (const d of dirs) await rm(d, { recursive: true, force: true });
35+
});
36+
37+
/** Write `files` (project-relative path → contents) into a fresh plugin project. */
38+
async function project(manifest: Record<string, unknown>, files: Record<string, string>): Promise<string> {
39+
const dir = await mkdtemp(join(tmpdir(), 'osplugin-bundled-'));
40+
dirs.push(dir);
41+
await writeFile(
42+
join(dir, MANIFEST_FILENAME),
43+
JSON.stringify({ id: ID, name: 'Bundled', version: '1.0.0', type: 'plugin', runtime: 'node', ...manifest }),
44+
);
45+
for (const [rel, contents] of Object.entries(files)) {
46+
await mkdir(dirname(join(dir, rel)), { recursive: true });
47+
await writeFile(join(dir, rel), contents);
48+
}
49+
return dir;
50+
}
51+
52+
/** Run the build door; the exit code is oclif's, `undefined` when it finished. */
53+
async function build(dir: string): Promise<{ exit: number | undefined; printed: string }> {
54+
const lines: string[] = [];
55+
vi.spyOn(console, 'log').mockImplementation((...a: unknown[]) => void lines.push(a.join(' ')));
56+
let exit: number | undefined;
57+
try {
58+
await PluginBuild.run([dir]);
59+
} catch (err) {
60+
exit = (err as { oclif?: { exit?: number } }).oclif?.exit;
61+
if (typeof exit !== 'number') throw err;
62+
}
63+
return { exit, printed: lines.join('\n') };
64+
}
65+
66+
/** The refusal lines (`✗`), the channel a refusal speaks on. */
67+
const refusals = (printed: string): string => printed.split('\n').filter((l) => l.includes('✗')).join('\n');
68+
69+
const PURE_DEP = {
70+
'node_modules/pure-dep/package.json': JSON.stringify({ name: 'pure-dep', version: '1.0.0', main: 'index.js' }),
71+
'node_modules/pure-dep/index.js': `module.exports = 'PURE_DEP_INLINED';\n`,
72+
};
73+
74+
describe('os plugin build — `bundled` is the one packaging strategy', () => {
75+
for (const packaging of ['bundled', undefined] as const) {
76+
it(`packaging ${packaging === undefined ? 'unset' : `'${packaging}'`}: the dependency is inlined and no package.json or lockfile is packed`, async () => {
77+
const dir = await project(packaging === undefined ? {} : { packaging }, {
78+
...PURE_DEP,
79+
'package.json': JSON.stringify({ name: 'demo', dependencies: { 'pure-dep': '1.0.0' } }),
80+
'pnpm-lock.yaml': `lockfileVersion: '9.0'\n`,
81+
'src/index.ts': `import pure from 'pure-dep';\nexport const value = pure;\n`,
82+
});
83+
84+
const { exit } = await build(dir);
85+
86+
expect(exit).toBeUndefined();
87+
const files = readTarGz(new Uint8Array(await readFile(join(dir, ARTIFACT))));
88+
const paths = files.map((f) => f.path).sort();
89+
expect(paths).toEqual(['SIGNATURE', 'dist/index.mjs', MANIFEST_FILENAME].sort());
90+
const bundle = Buffer.from(files.find((f) => f.path === 'dist/index.mjs')!.data).toString('utf8');
91+
expect(bundle).toContain('PURE_DEP_INLINED');
92+
expect(bundle).not.toMatch(/from\s*["']pure-dep["']|require\(\s*["']pure-dep["']\s*\)/);
93+
});
94+
}
95+
96+
it("refuses the retired `packaging: 'manifest-deps'` at the manifest: exit 1, the key named, no artifact", async () => {
97+
const dir = await project({ packaging: 'manifest-deps' }, {
98+
...PURE_DEP,
99+
'package.json': JSON.stringify({ name: 'demo', dependencies: { 'pure-dep': '1.0.0' } }),
100+
'src/index.ts': `import pure from 'pure-dep';\nexport const value = pure;\n`,
101+
});
102+
103+
const { exit, printed } = await build(dir);
104+
105+
expect(exit).toBe(1);
106+
expect(printed).toContain(`${MANIFEST_FILENAME} is invalid`);
107+
expect(refusals(printed)).toMatch(/packaging/);
108+
expect(printed).toMatch(/manifest-deps/);
109+
expect(existsSync(join(dir, ARTIFACT))).toBe(false);
110+
});
111+
});
112+
113+
describe('os plugin build — a native addon is refused (ADR-0025 v2 §3.3)', () => {
114+
it('refuses a `.node` file the entry imports, naming the file and its importer', async () => {
115+
const dir = await project({}, {
116+
'src/native/addon.node': 'not really a binary',
117+
'src/index.ts': `import addon from './native/addon.node';\nexport const value = addon;\n`,
118+
});
119+
120+
const { exit, printed } = await build(dir);
121+
122+
expect(exit).toBe(1);
123+
const refused = refusals(printed);
124+
expect(refused).toMatch(/native addon/i);
125+
expect(printed).toContain('src/native/addon.node');
126+
expect(printed).toContain('src/index.ts');
127+
expect(existsSync(join(dir, ARTIFACT))).toBe(false);
128+
});
129+
130+
it('refuses a dependency whose entry IS a `.node` file, naming the package', async () => {
131+
const dir = await project({}, {
132+
'node_modules/main-node/package.json': JSON.stringify({ name: 'main-node', version: '1.0.0', main: 'addon.node' }),
133+
'node_modules/main-node/addon.node': 'not really a binary',
134+
'src/index.ts': `import addon from 'main-node';\nexport const value = addon;\n`,
135+
});
136+
137+
const { exit, printed } = await build(dir);
138+
139+
expect(exit).toBe(1);
140+
expect(refusals(printed)).toMatch(/native addon/i);
141+
expect(printed).toContain('node_modules/main-node/addon.node');
142+
expect(existsSync(join(dir, ARTIFACT))).toBe(false);
143+
});
144+
145+
// The common shape in the wild: the package's JS is bundleable, and the
146+
// binary is located at RUN time (`bindings`, `node-gyp-build`), so esbuild
147+
// never sees a `.node` import — the package itself is what gives it away.
148+
const nativePackages = [
149+
{
150+
why: 'declares a node-gyp build (`binding.gyp`)',
151+
files: { 'binding.gyp': `{ 'targets': [] }\n` },
152+
manifest: {},
153+
},
154+
{
155+
why: 'declares a node-gyp build (`gypfile: true`)',
156+
files: {},
157+
manifest: { gypfile: true },
158+
},
159+
{
160+
why: 'ships a compiled `.node` binary',
161+
files: { 'prebuilds/linux-x64/fake.node': 'not really a binary' },
162+
manifest: {},
163+
},
164+
];
165+
for (const { why, files, manifest } of nativePackages) {
166+
it(`refuses a bundled dependency that ${why}, naming the package`, async () => {
167+
const pkgFiles: Record<string, string> = {};
168+
for (const [rel, contents] of Object.entries(files)) pkgFiles[`node_modules/fake-native/${rel}`] = contents;
169+
const dir = await project({}, {
170+
...PURE_DEP,
171+
...pkgFiles,
172+
'node_modules/fake-native/package.json': JSON.stringify({ name: 'fake-native', version: '1.0.0', main: 'index.js', ...manifest }),
173+
'node_modules/fake-native/index.js':
174+
`const path = require('path');\nmodule.exports = require(path.join(__dirname, 'build', 'Release', 'fake.node'));\n`,
175+
'src/index.ts': `import fake from 'fake-native';\nimport pure from 'pure-dep';\nexport const value = [fake, pure];\n`,
176+
});
177+
178+
const { exit, printed } = await build(dir);
179+
180+
expect(exit).toBe(1);
181+
expect(refusals(printed)).toMatch(/native addon/i);
182+
expect(printed).toContain('fake-native');
183+
// Only the native package is named: the pure one it sits beside is not.
184+
expect(printed).not.toContain('pure-dep');
185+
expect(existsSync(join(dir, ARTIFACT))).toBe(false);
186+
});
187+
}
188+
189+
it('refuses a `.node` file under `assets/`, naming it', async () => {
190+
const dir = await project({}, {
191+
'assets/icon.svg': '<svg/>',
192+
'assets/bin/addon.node': 'not really a binary',
193+
'src/index.ts': `export const value = 1;\n`,
194+
});
195+
196+
const { exit, printed } = await build(dir);
197+
198+
expect(exit).toBe(1);
199+
expect(refusals(printed)).toMatch(/native addon/i);
200+
expect(printed).toContain('assets/bin/addon.node');
201+
expect(printed).not.toContain('assets/icon.svg');
202+
expect(existsSync(join(dir, ARTIFACT))).toBe(false);
203+
});
204+
});

0 commit comments

Comments
 (0)