|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// GOLDEN REGRESSION — a stored connector entry whose package-relative ref is |
| 4 | +// refused (#28594) never stops the platform from booting. |
| 5 | +// |
| 6 | +// ## Why a restart |
| 7 | +// |
| 8 | +// `stored-connector-package-ref.dogfood.test.ts` saves its entries into a |
| 9 | +// RUNNING kernel, where the reconcile that refuses them is the post-boot one. |
| 10 | +// A row already at rest takes the other road: ObjectQL's `start()` hydrates |
| 11 | +// `sys_metadata` into the registry, and the automation service's own |
| 12 | +// `start()` reconcile reads that registry. That reconcile treats a |
| 13 | +// configuration fault as fatal, so an entry the upgrade newly refuses (one |
| 14 | +// that worked the day before) would refuse the next boot of every |
| 15 | +// environment holding it. |
| 16 | +// |
| 17 | +// ## What this file pins |
| 18 | +// |
| 19 | +// Two showcase boots over one database file. The first saves, through the |
| 20 | +// metadata door, an environment-wide `openapi` entry naming a |
| 21 | +// package-relative file, the same entry in an `authored` package, an |
| 22 | +// environment-wide stdio `mcp` entry, and a control carrying its document |
| 23 | +// inline. The second boot: |
| 24 | +// |
| 25 | +// 1. starts (no throw); |
| 26 | +// 2. does not list either refused entry, and its boot record names each |
| 27 | +// entry and its ref; |
| 28 | +// 3. lists the stored inline control `ready` with its operation (the rows |
| 29 | +// were hydrated, so the refusal above was read at boot, not skipped); |
| 30 | +// 4. lists the showcase's own file-ref and stdio entries `ready` (control). |
| 31 | +// |
| 32 | +// Boots fixture stacks of its own (a database file), so it stays out of |
| 33 | +// `SHARED_SHOWCASE`. |
| 34 | + |
| 35 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 36 | +import { tmpdir } from 'node:os'; |
| 37 | +import { join } from 'node:path'; |
| 38 | + |
| 39 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 40 | +import { type VerifyStack } from '@objectstack/verify'; |
| 41 | +import { bootShowcase } from './showcase-boot.js'; |
| 42 | + |
| 43 | +const FILE_REF = './src/system/connectors/status-openapi.json'; |
| 44 | +const ENV_FILE = 'srr_env_file'; |
| 45 | +const ENV_CWD = 'srr_env_cwd'; |
| 46 | +const AUTHORED_FILE = 'srr_authored_file'; |
| 47 | +/** An `authored` package created through the package door. */ |
| 48 | +const AUTHORED_PACKAGE_ID = 'com.dogfood.storedrestart'; |
| 49 | +const INLINE = 'srr_inline'; |
| 50 | +const HOST_FILE_ENTRY = 'showcase_status_openapi'; |
| 51 | +const HOST_STDIO_ENTRY = 'showcase_mcp_tools'; |
| 52 | + |
| 53 | +/** A one-operation OpenAPI 3 document; nothing is dispatched, so its server answers nothing. */ |
| 54 | +const DOCUMENT = { |
| 55 | + openapi: '3.0.3', |
| 56 | + info: { title: 'Stored inline document', version: '1.0.0' }, |
| 57 | + servers: [{ url: 'http://127.0.0.1:9/' }], |
| 58 | + paths: { '/v1/things': { get: { operationId: 'GetThings', responses: { '200': { description: 'ok' } } } } }, |
| 59 | +}; |
| 60 | + |
| 61 | +const openapi = (name: string, spec: unknown) => ({ |
| 62 | + name, |
| 63 | + label: `Stored ${name}`, |
| 64 | + type: 'api', |
| 65 | + provider: 'openapi', |
| 66 | + providerConfig: { spec, baseUrl: 'http://127.0.0.1:9/' }, |
| 67 | +}); |
| 68 | + |
| 69 | +type Answer = { status: number; body: any }; |
| 70 | + |
| 71 | +async function json(res: Response): Promise<Answer> { |
| 72 | + return { status: res.status, body: await res.json().catch(() => null) }; |
| 73 | +} |
| 74 | + |
| 75 | +/** Every line the process writes while `run` is in flight — the kernel logger writes to the streams. */ |
| 76 | +async function captureOutput<T>(run: () => Promise<T>): Promise<{ value: T; lines: string[] }> { |
| 77 | + const lines: string[] = []; |
| 78 | + const stdout = process.stdout.write.bind(process.stdout); |
| 79 | + const stderr = process.stderr.write.bind(process.stderr); |
| 80 | + const warn = console.warn; |
| 81 | + const error = console.error; |
| 82 | + (process.stdout as any).write = (chunk: unknown, ...rest: any[]) => { lines.push(String(chunk)); return stdout(chunk as any, ...rest); }; |
| 83 | + (process.stderr as any).write = (chunk: unknown, ...rest: any[]) => { lines.push(String(chunk)); return stderr(chunk as any, ...rest); }; |
| 84 | + console.warn = (...args: unknown[]) => { lines.push(args.map(String).join(' ')); warn(...args); }; |
| 85 | + console.error = (...args: unknown[]) => { lines.push(args.map(String).join(' ')); error(...args); }; |
| 86 | + try { |
| 87 | + return { value: await run(), lines }; |
| 88 | + } finally { |
| 89 | + (process.stdout as any).write = stdout; |
| 90 | + (process.stderr as any).write = stderr; |
| 91 | + console.warn = warn; |
| 92 | + console.error = error; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +const listed = (answer: Answer): any[] => answer.body?.data?.connectors ?? []; |
| 97 | +const entryOf = (answer: Answer, name: string): any => listed(answer).find((c: any) => c?.name === name); |
| 98 | +const shown = (answer: Answer, name: string): string => { |
| 99 | + const c = entryOf(answer, name); |
| 100 | + return c === undefined |
| 101 | + ? `'${name}' not listed` |
| 102 | + : JSON.stringify({ name: c.name, state: c.state, actions: c.actions?.map((a: any) => a.key) }); |
| 103 | +}; |
| 104 | + |
| 105 | +/** Read the listing until every `names` entry lists `ready`, or the window closes. */ |
| 106 | +async function settle(stack: VerifyStack, token: string, names: readonly string[]): Promise<Answer> { |
| 107 | + const deadline = Date.now() + 15_000; |
| 108 | + let answer: Answer; |
| 109 | + do { |
| 110 | + answer = await json(await stack.apiAs(token, 'GET', '/automation/connectors')); |
| 111 | + if (names.every((n) => entryOf(answer, n)?.state === 'ready')) break; |
| 112 | + await new Promise((r) => setTimeout(r, 100)); |
| 113 | + } while (Date.now() < deadline); |
| 114 | + return answer; |
| 115 | +} |
| 116 | + |
| 117 | +describe('dogfood: a stored connector entry refused its package-relative ref does not stop the next boot', () => { |
| 118 | + let dir: string; |
| 119 | + const saves: Record<string, Answer> = {}; |
| 120 | + let before: Answer; |
| 121 | + let bootError: unknown; |
| 122 | + let after: Answer; |
| 123 | + let bootLines: string[] = []; |
| 124 | + let second: VerifyStack | undefined; |
| 125 | + |
| 126 | + beforeAll(async () => { |
| 127 | + dir = mkdtempSync(join(tmpdir(), 'dogfood-stored-connector-restart-')); |
| 128 | + const databaseFile = join(dir, 'os.db'); |
| 129 | + |
| 130 | + const first = await bootShowcase({ databaseFile }); |
| 131 | + try { |
| 132 | + const token = await first.signIn(); |
| 133 | + saves[ENV_FILE] = await json(await first.apiAs(token, 'PUT', `/meta/connector/${ENV_FILE}`, openapi(ENV_FILE, FILE_REF))); |
| 134 | + saves[ENV_CWD] = await json(await first.apiAs(token, 'PUT', `/meta/connector/${ENV_CWD}`, { |
| 135 | + name: ENV_CWD, |
| 136 | + label: 'Stored stdio', |
| 137 | + type: 'api', |
| 138 | + provider: 'mcp', |
| 139 | + providerConfig: { transport: { kind: 'stdio', command: 'node', args: ['./scripts/mcp-fixture.mjs'] } }, |
| 140 | + })); |
| 141 | + saves.package = await json(await first.apiAs(token, 'POST', '/packages', { |
| 142 | + id: AUTHORED_PACKAGE_ID, name: 'Stored connector restart', version: '1.0.0', type: 'app', |
| 143 | + })); |
| 144 | + saves[AUTHORED_FILE] = await json(await first.apiAs( |
| 145 | + token, 'PUT', `/meta/connector/${AUTHORED_FILE}?package=${AUTHORED_PACKAGE_ID}`, openapi(AUTHORED_FILE, FILE_REF), |
| 146 | + )); |
| 147 | + saves[INLINE] = await json(await first.apiAs(token, 'PUT', `/meta/connector/${INLINE}`, openapi(INLINE, DOCUMENT))); |
| 148 | + before = await settle(first, token, [INLINE]); |
| 149 | + } finally { |
| 150 | + await first.stop(); |
| 151 | + } |
| 152 | + |
| 153 | + const captured = await captureOutput(async () => { |
| 154 | + try { |
| 155 | + second = await bootShowcase({ databaseFile }); |
| 156 | + } catch (err) { |
| 157 | + bootError = err; |
| 158 | + return undefined; |
| 159 | + } |
| 160 | + const token = await second.signIn(); |
| 161 | + return settle(second, token, [INLINE, HOST_FILE_ENTRY, HOST_STDIO_ENTRY]); |
| 162 | + }); |
| 163 | + bootLines = captured.lines; |
| 164 | + if (captured.value) after = captured.value; |
| 165 | + }, 300_000); |
| 166 | + |
| 167 | + afterAll(async () => { |
| 168 | + await second?.stop?.(); |
| 169 | + if (dir) rmSync(dir, { recursive: true, force: true }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('precondition: the first boot accepts every save and lists the inline control', () => { |
| 173 | + for (const [name, answer] of Object.entries(saves)) { |
| 174 | + expect(answer.status, `${name}: ${JSON.stringify(answer.body).slice(0, 400)}`).toBeLessThan(300); |
| 175 | + } |
| 176 | + expect(entryOf(before, INLINE)?.state, shown(before, INLINE)).toBe('ready'); |
| 177 | + expect(entryOf(before, ENV_FILE), shown(before, ENV_FILE)).toBeUndefined(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('the second boot over the same database starts', () => { |
| 181 | + expect(bootError, bootError instanceof Error ? bootError.message.slice(0, 600) : String(bootError)).toBeUndefined(); |
| 182 | + }); |
| 183 | + |
| 184 | + it.each([ |
| 185 | + [ENV_FILE, `file ref '${FILE_REF}'`], |
| 186 | + [ENV_CWD, 'path ref \'.\''], |
| 187 | + [AUTHORED_FILE, `file ref '${FILE_REF}'`], |
| 188 | + ])('%s is not listed after the restart, and the boot record names it and its ref', (name, ref) => { |
| 189 | + expect(entryOf(after, name), shown(after, name)).toBeUndefined(); |
| 190 | + const record = bootLines.find((l) => l.includes(`connector instance '${name}'`) && l.includes('is refused')); |
| 191 | + expect(record, `no boot record refuses '${name}'`).toBeDefined(); |
| 192 | + expect(record).toContain(ref); |
| 193 | + }); |
| 194 | + |
| 195 | + it('controls: the stored inline entry, and the showcase\'s own file-ref and stdio entries, list ready after the restart', () => { |
| 196 | + expect(entryOf(after, INLINE)?.state, shown(after, INLINE)).toBe('ready'); |
| 197 | + expect(entryOf(after, INLINE).actions.map((a: any) => a.key)).toEqual(['GetThings']); |
| 198 | + expect(entryOf(after, HOST_FILE_ENTRY)?.state, shown(after, HOST_FILE_ENTRY)).toBe('ready'); |
| 199 | + expect(entryOf(after, HOST_STDIO_ENTRY)?.state, shown(after, HOST_STDIO_ENTRY)).toBe('ready'); |
| 200 | + }); |
| 201 | +}); |
0 commit comments