|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// GOLDEN REGRESSION — a connector entry an INSTALLED package declares never |
| 4 | +// reads, or launches a process in, the HOST's package root (#28534). |
| 5 | +// |
| 6 | +// ## What was measured before the fix |
| 7 | +// |
| 8 | +// Installed into the showcase through `POST /api/v1/marketplace/install-local`, |
| 9 | +// an inline manifest whose `provider: 'openapi'` connector names |
| 10 | +// `providerConfig.spec: './src/system/connectors/status-openapi.json'` — a file |
| 11 | +// the installed package does not ship, and the showcase does — materialized |
| 12 | +// `ready` on `GET /api/v1/automation/connectors` carrying the showcase's own |
| 13 | +// document: its description and its `getHealth` action. A `provider: 'mcp'` |
| 14 | +// stdio connector naming `./scripts/mcp-fixture.mjs` spawned the showcase's own |
| 15 | +// fixture, its working directory the showcase's root, and listed `ready` with |
| 16 | +// the fixture's `echo_upper` tool. The materializer handed every entry the |
| 17 | +// host's root as `loadPackageFile` / `resolvePackagePath`, whichever package |
| 18 | +// declared it; an installed package has no root on disk at all, since the |
| 19 | +// install keeps its artifact JSON and nothing else. |
| 20 | +// |
| 21 | +// ## What this file pins, on one boot |
| 22 | +// |
| 23 | +// 1. the install answers 200 and hot-loads the package; |
| 24 | +// 2. neither installed entry that names a package-relative ref materializes: |
| 25 | +// both are absent from the listing, and the reconcile's error record names |
| 26 | +// each entry, its ref and the remedy; |
| 27 | +// 3. controls: the installed entry carrying its document inline lists |
| 28 | +// `ready` with that document's operation, and the showcase's own |
| 29 | +// file-ref and stdio entries keep listing `ready` with their own actions. |
| 30 | +// |
| 31 | +// Boots a fixture stack of its own (the install-local plugin mounted), so it |
| 32 | +// stays out of `SHARED_SHOWCASE`. |
| 33 | + |
| 34 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 35 | +import { tmpdir } from 'node:os'; |
| 36 | +import { join } from 'node:path'; |
| 37 | + |
| 38 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 39 | +import { type VerifyStack } from '@objectstack/verify'; |
| 40 | +import { MarketplaceInstallLocalPlugin } from '@objectstack/cloud-connection'; |
| 41 | +import { bootShowcase } from './showcase-boot.js'; |
| 42 | + |
| 43 | +const PACKAGE_ID = 'com.dogfood.pkgref'; |
| 44 | +/** An `openapi` entry naming a file the showcase ships and the installed package does not. */ |
| 45 | +const FILE_REF_ENTRY = 'pkgref_host_file'; |
| 46 | +const FILE_REF = './src/system/connectors/status-openapi.json'; |
| 47 | +/** A stdio `mcp` entry whose working directory the provider asks for with `resolvePackagePath('.')`. */ |
| 48 | +const PATH_REF_ENTRY = 'pkgref_host_cwd'; |
| 49 | +/** Control: an installed `openapi` entry carrying its document inline. */ |
| 50 | +const INLINE_ENTRY = 'pkgref_inline'; |
| 51 | +/** The showcase's own entries — the file-ref and stdio forms of the same two providers. */ |
| 52 | +const HOST_FILE_ENTRY = 'showcase_status_openapi'; |
| 53 | +const HOST_STDIO_ENTRY = 'showcase_mcp_tools'; |
| 54 | + |
| 55 | +/** A one-operation OpenAPI 3 document; nothing is dispatched, so its server answers nothing. */ |
| 56 | +const DOCUMENT = { |
| 57 | + openapi: '3.0.3', |
| 58 | + info: { title: 'Installed inline document', version: '1.0.0' }, |
| 59 | + servers: [{ url: 'http://127.0.0.1:9/' }], |
| 60 | + paths: { '/v1/things': { get: { operationId: 'GetThings', responses: { '200': { description: 'ok' } } } } }, |
| 61 | +}; |
| 62 | + |
| 63 | +/** The install body `os package install` sends for an inline manifest. */ |
| 64 | +const INSTALL_BODY = { |
| 65 | + manifest: { |
| 66 | + id: PACKAGE_ID, |
| 67 | + namespace: 'pkgref', |
| 68 | + name: 'Installed package-relative connector refs', |
| 69 | + version: '1.0.0', |
| 70 | + type: 'app', |
| 71 | + connectors: [ |
| 72 | + { |
| 73 | + name: FILE_REF_ENTRY, |
| 74 | + label: 'Names a host file', |
| 75 | + type: 'api', |
| 76 | + provider: 'openapi', |
| 77 | + providerConfig: { spec: FILE_REF, baseUrl: 'http://127.0.0.1:9/' }, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: PATH_REF_ENTRY, |
| 81 | + label: 'Runs in the host root', |
| 82 | + type: 'api', |
| 83 | + provider: 'mcp', |
| 84 | + providerConfig: { transport: { kind: 'stdio', command: 'node', args: ['./scripts/mcp-fixture.mjs'] } }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: INLINE_ENTRY, |
| 88 | + label: 'Inline document', |
| 89 | + type: 'api', |
| 90 | + provider: 'openapi', |
| 91 | + providerConfig: { spec: DOCUMENT }, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +type Answer = { status: number; body: any }; |
| 98 | + |
| 99 | +async function json(res: Response): Promise<Answer> { |
| 100 | + return { status: res.status, body: await res.json().catch(() => null) }; |
| 101 | +} |
| 102 | + |
| 103 | +/** Every line the process writes while `run` is in flight — the kernel logger writes to the streams. */ |
| 104 | +async function captureOutput<T>(run: () => Promise<T>): Promise<{ value: T; lines: string[] }> { |
| 105 | + const lines: string[] = []; |
| 106 | + const stdout = process.stdout.write.bind(process.stdout); |
| 107 | + const stderr = process.stderr.write.bind(process.stderr); |
| 108 | + const warn = console.warn; |
| 109 | + const error = console.error; |
| 110 | + (process.stdout as any).write = (chunk: unknown, ...rest: any[]) => { lines.push(String(chunk)); return stdout(chunk as any, ...rest); }; |
| 111 | + (process.stderr as any).write = (chunk: unknown, ...rest: any[]) => { lines.push(String(chunk)); return stderr(chunk as any, ...rest); }; |
| 112 | + console.warn = (...args: unknown[]) => { lines.push(args.map(String).join(' ')); warn(...args); }; |
| 113 | + console.error = (...args: unknown[]) => { lines.push(args.map(String).join(' ')); error(...args); }; |
| 114 | + try { |
| 115 | + return { value: await run(), lines }; |
| 116 | + } finally { |
| 117 | + (process.stdout as any).write = stdout; |
| 118 | + (process.stderr as any).write = stderr; |
| 119 | + console.warn = warn; |
| 120 | + console.error = error; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +const listed = (answer: Answer): any[] => answer.body?.data?.connectors ?? []; |
| 125 | +const entryOf = (answer: Answer, name: string): any => listed(answer).find((c: any) => c?.name === name); |
| 126 | +/** What a listing says about one entry — enough for a failure to show whose document it carried. */ |
| 127 | +const shown = (answer: Answer, name: string): string => { |
| 128 | + const c = entryOf(answer, name); |
| 129 | + return c === undefined |
| 130 | + ? `'${name}' not listed` |
| 131 | + : JSON.stringify({ name: c.name, state: c.state, description: c.description, actions: c.actions?.map((a: any) => a.key) }); |
| 132 | +}; |
| 133 | + |
| 134 | +describe('dogfood: an installed package\'s package-relative connector ref is refused, never resolved against the host\'s root', () => { |
| 135 | + let stack: VerifyStack; |
| 136 | + let storageDir: string; |
| 137 | + let install: Answer; |
| 138 | + let connectors: Answer; |
| 139 | + let lines: string[] = []; |
| 140 | + |
| 141 | + beforeAll(async () => { |
| 142 | + storageDir = mkdtempSync(join(tmpdir(), 'dogfood-connector-package-ref-')); |
| 143 | + stack = await bootShowcase({ |
| 144 | + extraPlugins: [new MarketplaceInstallLocalPlugin({ controlPlaneUrl: 'off', storageDir })], |
| 145 | + }); |
| 146 | + const token = await stack.signIn(); |
| 147 | + const captured = await captureOutput(async () => { |
| 148 | + install = await json(await stack.apiAs(token, 'POST', '/marketplace/install-local', INSTALL_BODY)); |
| 149 | + // The reconcile the install's re-sync announces may land after the |
| 150 | + // answer: read the listing until the inline control is in it, or the |
| 151 | + // window closes. The control and the refused entries are judged in the |
| 152 | + // same reconcile run. |
| 153 | + const deadline = Date.now() + 10_000; |
| 154 | + do { |
| 155 | + connectors = await json(await stack.apiAs(token, 'GET', '/automation/connectors')); |
| 156 | + if (entryOf(connectors, INLINE_ENTRY)) break; |
| 157 | + await new Promise((r) => setTimeout(r, 100)); |
| 158 | + } while (Date.now() < deadline); |
| 159 | + }); |
| 160 | + lines = captured.lines; |
| 161 | + }, 300_000); |
| 162 | + |
| 163 | + afterAll(async () => { |
| 164 | + await stack?.stop?.(); |
| 165 | + if (storageDir) rmSync(storageDir, { recursive: true, force: true }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('the install answers 200 and hot-loads the package', () => { |
| 169 | + expect(install.status, JSON.stringify(install.body)).toBe(200); |
| 170 | + expect(install.body?.data?.hotLoaded).toBe(true); |
| 171 | + expect(connectors.status, JSON.stringify(connectors.body)).toBe(200); |
| 172 | + }); |
| 173 | + |
| 174 | + it('controls: the installed inline entry and the showcase\'s own file-ref and stdio entries list ready', () => { |
| 175 | + expect(entryOf(connectors, INLINE_ENTRY)?.state, shown(connectors, INLINE_ENTRY)).toBe('ready'); |
| 176 | + expect(entryOf(connectors, INLINE_ENTRY).actions.map((a: any) => a.key)).toEqual(['GetThings']); |
| 177 | + expect(entryOf(connectors, HOST_FILE_ENTRY)?.state, shown(connectors, HOST_FILE_ENTRY)).toBe('ready'); |
| 178 | + expect(entryOf(connectors, HOST_FILE_ENTRY).actions.map((a: any) => a.key)).toEqual(['getHealth']); |
| 179 | + expect(entryOf(connectors, HOST_STDIO_ENTRY)?.state, shown(connectors, HOST_STDIO_ENTRY)).toBe('ready'); |
| 180 | + }); |
| 181 | + |
| 182 | + it('the installed entry naming a host FILE does not materialize, and the record names it, its ref and the remedy', () => { |
| 183 | + expect(entryOf(connectors, FILE_REF_ENTRY), shown(connectors, FILE_REF_ENTRY)).toBeUndefined(); |
| 184 | + const record = lines.find((l) => l.includes(`connector instance '${FILE_REF_ENTRY}'`) && l.includes(PACKAGE_ID)); |
| 185 | + expect(record, `no reconcile record names '${FILE_REF_ENTRY}' with its package`).toBeDefined(); |
| 186 | + expect(record).toContain(`'${FILE_REF}'`); |
| 187 | + expect(record).toContain('os build'); |
| 188 | + }); |
| 189 | + |
| 190 | + it('the installed entry asking for a host LOCATION does not materialize, and the record names it and its ref', () => { |
| 191 | + expect(entryOf(connectors, PATH_REF_ENTRY), shown(connectors, PATH_REF_ENTRY)).toBeUndefined(); |
| 192 | + const record = lines.find((l) => l.includes(`connector instance '${PATH_REF_ENTRY}'`) && l.includes(PACKAGE_ID)); |
| 193 | + expect(record, `no reconcile record names '${PATH_REF_ENTRY}' with its package`).toBeDefined(); |
| 194 | + expect(record).toContain('path ref \'.\''); |
| 195 | + }); |
| 196 | +}); |
0 commit comments