Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(test): deflake visionCli describeImageViaCli tests — drive child events from spawnImpl microtask instead of racing a fixed 10ms sleep

Adam Eivy1819123c90e2
diff --git a/server/services/visionCli.test.js b/server/services/visionCli.test.js
index 404bff4295..154c74f846 100644
--- a/server/services/visionCli.test.js
+++ b/server/services/visionCli.test.js
@@ -72,10 +72,23 @@ function makeFakeChild() {
return child;
}
+// A spawnImpl double that runs `emit(child)` on the next microtask after it's
+// invoked. describeImageViaCli attaches its stdout/close/error listeners
+// SYNCHRONOUSLY right after spawnImpl() returns (no await between), so a
+// microtask scheduled here always fires after the listeners exist — regardless
+// of how long the preceding async mkdtemp/writeFile took. This replaces a
+// fixed `setTimeout(10ms)` race that hung to the 10s test timeout on slow CI.
+function spawnEmitting(child, emit) {
+ return vi.fn(() => { queueMicrotask(() => emit(child)); return child; });
+}
+
describe('describeImageViaCli', () => {
it('returns the trimmed stdout text in the API-compatible shape on exit 0', async () => {
const child = makeFakeChild();
- const spawnImpl = vi.fn(() => child);
+ const spawnImpl = spawnEmitting(child, (c) => {
+ c.stdout.emit('data', Buffer.from(' a woman in a red cloak '));
+ c.emit('close', 0);
+ });
const promise = describeImageViaCli({
provider: { id: 'codex', command: 'codex', args: [] },
dataUrl: PNG_DATA_URL,
@@ -83,10 +96,6 @@ describe('describeImageViaCli', () => {
model: 'gpt-5',
spawnImpl,
});
- // Let the mkdtemp/writeFile awaits settle before emitting process events.
- await new Promise((r) => setTimeout(r, 10));
- child.stdout.emit('data', Buffer.from(' a woman in a red cloak '));
- child.emit('close', 0);
const result = await promise;
expect(result).toEqual({
text: 'a woman in a red cloak', finishReason: null, usage: null, reasoning: '',
@@ -96,15 +105,6 @@ describe('describeImageViaCli', () => {
it('strips the codex session transcript down to the assistant reply', async () => {
const child = makeFakeChild();
- const spawnImpl = vi.fn(() => child);
- const promise = describeImageViaCli({
- provider: { id: 'codex', command: 'codex', args: [] },
- dataUrl: PNG_DATA_URL,
- prompt: 'caption',
- model: 'gpt-5',
- spawnImpl,
- });
- await new Promise((r) => setTimeout(r, 10));
// A realistic codex exec transcript: banner … \ncodex\n<reply>\ntokens used …
const transcript = [
'OpenAI Codex v0.141.0',
@@ -115,22 +115,23 @@ describe('describeImageViaCli', () => {
'a woman in a red cloak, bust shot',
'tokens used: 1234',
].join('\n');
- child.stdout.emit('data', Buffer.from(transcript));
- child.emit('close', 0);
- const result = await promise;
- expect(result.text).toBe('a woman in a red cloak, bust shot');
- });
-
- it('extracts the assistant reply when codex emits it AFTER the tokens-used footer', async () => {
- const child = makeFakeChild();
- const spawnImpl = vi.fn(() => child);
+ const spawnImpl = spawnEmitting(child, (c) => {
+ c.stdout.emit('data', Buffer.from(transcript));
+ c.emit('close', 0);
+ });
const promise = describeImageViaCli({
provider: { id: 'codex', command: 'codex', args: [] },
dataUrl: PNG_DATA_URL,
prompt: 'caption',
+ model: 'gpt-5',
spawnImpl,
});
- await new Promise((r) => setTimeout(r, 10));
+ const result = await promise;
+ expect(result.text).toBe('a woman in a red cloak, bust shot');
+ });
+
+ it('extracts the assistant reply when codex emits it AFTER the tokens-used footer', async () => {
+ const child = makeFakeChild();
// Newer codex format: the final reply follows the `tokens used\n<count>` footer.
const transcript = [
'OpenAI Codex v0.141.0',
@@ -140,38 +141,46 @@ describe('describeImageViaCli', () => {
'1234',
'{"boxes":[{"x":0,"y":0,"w":0.5,"h":1}]}',
].join('\n');
- child.stdout.emit('data', Buffer.from(transcript));
- child.emit('close', 0);
+ const spawnImpl = spawnEmitting(child, (c) => {
+ c.stdout.emit('data', Buffer.from(transcript));
+ c.emit('close', 0);
+ });
+ const promise = describeImageViaCli({
+ provider: { id: 'codex', command: 'codex', args: [] },
+ dataUrl: PNG_DATA_URL,
+ prompt: 'caption',
+ spawnImpl,
+ });
const result = await promise;
expect(result.text).toBe('{"boxes":[{"x":0,"y":0,"w":0.5,"h":1}]}');
});
it('rejects with a tail of stderr on a non-zero exit', async () => {
const child = makeFakeChild();
- const spawnImpl = vi.fn(() => child);
+ const spawnImpl = spawnEmitting(child, (c) => {
+ c.stderr.emit('data', Buffer.from('vision unavailable'));
+ c.emit('close', 1);
+ });
const promise = describeImageViaCli({
provider: { id: 'claude-code', command: 'claude', args: [] },
dataUrl: PNG_DATA_URL,
prompt: 'caption',
spawnImpl,
});
- await new Promise((r) => setTimeout(r, 10));
- child.stderr.emit('data', Buffer.from('vision unavailable'));
- child.emit('close', 1);
await expect(promise).rejects.toThrow(/exited 1.*vision unavailable/s);
});
it('rejects when the process fails to spawn', async () => {
const child = makeFakeChild();
- const spawnImpl = vi.fn(() => child);
+ const spawnImpl = spawnEmitting(child, (c) => {
+ c.emit('error', new Error('ENOENT'));
+ });
const promise = describeImageViaCli({
provider: { id: 'codex', command: 'codex', args: [] },
dataUrl: PNG_DATA_URL,
prompt: 'p',
spawnImpl,
});
- await new Promise((r) => setTimeout(r, 10));
- child.emit('error', new Error('ENOENT'));
await expect(promise).rejects.toThrow(/Failed to spawn codex.*ENOENT/s);
});