Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fix readiness checks for partially written test files (#5532)

Nick Gomez55803eaba2d0
diff --git a/packages/app/tests/stress/okf-recommended-skill-install.e2e.ts b/packages/app/tests/stress/okf-recommended-skill-install.e2e.ts
index d19968cb5..c16f1fb2e 100644
--- a/packages/app/tests/stress/okf-recommended-skill-install.e2e.ts
+++ b/packages/app/tests/stress/okf-recommended-skill-install.e2e.ts
@@ -51,8 +51,8 @@ test('installs the recommended skill through the real endpoint and recognizes it
'SKILL.md',
);
await expect.poll(() => existsSync(installedSkill), { timeout: 15_000 }).toBe(true);
- expect(readFileSync(installedSkill, 'utf-8')).toContain('name: okf-knowledge-base');
await expect(card).toContainText('Installed');
+ expect(readFileSync(installedSkill, 'utf-8')).toContain('name: okf-knowledge-base');
await page.reload();
await waitForSettingsPanel(page, 'settings-plugin-okf');
diff --git a/packages/cli/tests/integration/_helpers/state-publication-preload.cjs b/packages/cli/tests/integration/_helpers/state-publication-preload.cjs
new file mode 100644
index 000000000..b0c80999c
--- /dev/null
+++ b/packages/cli/tests/integration/_helpers/state-publication-preload.cjs
@@ -0,0 +1,27 @@
+const fs = require('node:fs');
+const { syncBuiltinESMExports } = require('node:module');
+
+const stateFile = process.env.OK_TEST_STATE_FILE;
+const witnessFile = process.env.OK_TEST_STATE_WITNESS;
+if (!stateFile || !witnessFile) throw new Error('State publication observation paths are required');
+
+const originalOpen = fs.openSync;
+const originalWrite = fs.writeFileSync;
+const originalRead = fs.readFileSync;
+const matches = (file) => file === stateFile || file === `${stateFile}.pending`;
+
+fs.writeFileSync = (file, data, options) => {
+ if (!matches(file) || typeof data !== 'string') return originalWrite(file, data, options);
+ const encoding = typeof options === 'string' ? options : (options?.encoding ?? 'utf8');
+ return originalWrite(file, Buffer.from(data, encoding), options);
+};
+
+fs.openSync = (...args) => {
+ const fd = Reflect.apply(originalOpen, fs, args);
+ if (!matches(args[0]) || args[1] !== 'w') return fd;
+ const contents = fs.existsSync(stateFile) ? originalRead(stateFile, 'utf8') : null;
+ originalWrite(witnessFile, JSON.stringify(contents));
+ return fd;
+};
+
+syncBuiltinESMExports();
diff --git a/packages/cli/tests/integration/detached-spawn-lifetime.test.ts b/packages/cli/tests/integration/detached-spawn-lifetime.test.ts
index 359396e2e..617e14314 100644
--- a/packages/cli/tests/integration/detached-spawn-lifetime.test.ts
+++ b/packages/cli/tests/integration/detached-spawn-lifetime.test.ts
@@ -4,6 +4,7 @@ import { request as httpRequest } from 'node:http';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { setTimeout as wait } from 'node:timers/promises';
+import { fileURLToPath } from 'node:url';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
function isProcessAlive(pid: number): boolean {
@@ -57,6 +58,10 @@ describe('detached spawn lifetime (A3 / D-003)', () => {
it('grandchild survives parent exit AND keeps serving HTTP for ≥5s', async () => {
const grandchildScript = join(testDir, 'grandchild.mjs');
const stateFile = join(testDir, 'grandchild.state.json');
+ const witnessFile = join(testDir, 'grandchild.publication.json');
+ const publicationPreload = fileURLToPath(
+ new URL('./_helpers/state-publication-preload.cjs', import.meta.url),
+ );
const mcpSurrogateScript = join(testDir, 'mcp-surrogate.mjs');
writeFileSync(
@@ -64,7 +69,7 @@ describe('detached spawn lifetime (A3 / D-003)', () => {
`
import { createServer } from 'node:http';
import { setTimeout as wait } from 'node:timers/promises';
-import { writeFileSync } from 'node:fs';
+import { renameSync, writeFileSync } from 'node:fs';
const server = createServer((_req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
@@ -73,7 +78,9 @@ const server = createServer((_req, res) => {
server.listen(0, '127.0.0.1', () => {
const addr = server.address();
if (typeof addr !== 'object' || addr === null) process.exit(2);
- writeFileSync(${JSON.stringify(stateFile)}, JSON.stringify({ pid: process.pid, port: addr.port }));
+ const pendingStateFile = ${JSON.stringify(`${stateFile}.pending`)};
+ writeFileSync(pendingStateFile, JSON.stringify({ pid: process.pid, port: addr.port }));
+ renameSync(pendingStateFile, ${JSON.stringify(stateFile)});
});
await wait(30_000);
@@ -87,9 +94,14 @@ process.exit(0);
mcpSurrogateScript,
`
import { spawn } from 'node:child_process';
-const child = spawn('node', [${JSON.stringify(grandchildScript)}], {
+const child = spawn('node', ['--require', ${JSON.stringify(publicationPreload)}, ${JSON.stringify(grandchildScript)}], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
+ env: {
+ ...process.env,
+ OK_TEST_STATE_FILE: ${JSON.stringify(stateFile)},
+ OK_TEST_STATE_WITNESS: ${JSON.stringify(witnessFile)},
+ },
});
child.unref();
setTimeout(() => process.exit(0), 300);
@@ -120,6 +132,9 @@ setTimeout(() => process.exit(0), 300);
expect(state.pid).toBeGreaterThan(0);
expect(state.port).toBeGreaterThan(0);
+ const observed: unknown = JSON.parse(readFileSync(witnessFile, 'utf-8'));
+ expect(observed).toBeNull();
+
if (mcpPid !== undefined) {
expect(isProcessAlive(mcpPid)).toBe(false);
}
diff --git a/packages/server/src/acp/browser-mcp.test.ts b/packages/server/src/acp/browser-mcp.test.ts
index 70cc6a55a..751d085af 100644
--- a/packages/server/src/acp/browser-mcp.test.ts
+++ b/packages/server/src/acp/browser-mcp.test.ts
@@ -1,5 +1,6 @@
import { spawn, spawnSync } from 'node:child_process';
import {
+ appendFileSync,
existsSync,
mkdirSync,
mkdtempSync,
@@ -498,8 +499,12 @@ describe.skipIf(process.platform === 'win32')('the browser server relay', () =>
child.stdin.write(`${typeof message === 'string' ? message : JSON.stringify(message)}\n`);
}
const deadline = Date.now() + 10_000;
- while (!settled() && Date.now() < deadline) await new Promise((r) => setTimeout(r, 25));
- child.stdin.end();
+ try {
+ while (!settled() && Date.now() < deadline) await new Promise((r) => setTimeout(r, 25));
+ } finally {
+ child.stdin.end();
+ await exited;
+ }
expect(await exited).toBe(0);
return stdout;
}
@@ -518,11 +523,38 @@ describe.skipIf(process.platform === 'win32')('the browser server relay', () =>
const receivedLines = (path: string): Array<Record<string, unknown>> =>
existsSync(path)
? readFileSync(path, 'utf8')
- .trim()
.split('\n')
+ .slice(0, -1)
.map((line) => JSON.parse(line) as Record<string, unknown>)
: [];
+ test('reads no receive records from an empty file', () => {
+ const received = join(tmp(), 'received.ndjson');
+ writeFileSync(received, '');
+ expect(receivedLines(received)).toEqual([]);
+ appendFileSync(received, '{"id":1}\n');
+ expect(receivedLines(received)).toEqual([{ id: 1 }]);
+ });
+
+ test.each([
+ { stage: 'empty tail', tail: '', rest: '{"id":2}' },
+ { stage: 'partial JSON', tail: '{"id":', rest: '2}' },
+ { stage: 'unterminated JSON', tail: '{"id":2}', rest: '' },
+ ])('reads only complete receive records with $stage', ({ tail, rest }) => {
+ const received = join(tmp(), 'received.ndjson');
+ writeFileSync(received, `{"id":1}\n${tail}`);
+ expect(() => receivedLines(received)).not.toThrow();
+ expect(receivedLines(received)).toEqual([{ id: 1 }]);
+ appendFileSync(received, `${rest}\n`);
+ expect(receivedLines(received)).toEqual([{ id: 1 }, { id: 2 }]);
+ });
+
+ test('reports malformed completed receive records', () => {
+ const received = join(tmp(), 'received.ndjson');
+ writeFileSync(received, 'not-json\n');
+ expect(() => receivedLines(received)).toThrow(SyntaxError);
+ });
+
test("answers the server's roots request with the chat's files folder instead of the agent's roots", async () => {
const root = tmp();
const npm = join(root, 'npm');