mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-14 10:52:17 +03:00
Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.
One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.
Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.
287 lines
8.5 KiB
TypeScript
287 lines
8.5 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-claude-rendering-"));
|
|
const previousDataDir = process.env.DATA_DIR;
|
|
process.env.DATA_DIR = TEST_DATA_DIR;
|
|
|
|
const { openaiResponsesToOpenAIResponse } =
|
|
await import("../../open-sse/translator/response/openai-responses.ts");
|
|
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
|
|
const { createSSETransformStreamWithLogger } = await import("../../open-sse/utils/stream.ts");
|
|
const { resetDbInstance } = await import("../../src/lib/db/core.ts");
|
|
|
|
test.after(() => {
|
|
resetDbInstance();
|
|
if (previousDataDir === undefined) delete process.env.DATA_DIR;
|
|
else process.env.DATA_DIR = previousDataDir;
|
|
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
});
|
|
|
|
test("Responses->Chat: output_item.done emits arguments when no delta chunks were sent", () => {
|
|
const state = {
|
|
started: true,
|
|
chatId: "chatcmpl-test",
|
|
created: 1234567890,
|
|
toolCallIndex: 0,
|
|
finishReasonSent: false,
|
|
currentToolCallId: "call_abc",
|
|
currentToolCallArgsBuffer: "",
|
|
};
|
|
|
|
const chunk = {
|
|
type: "response.output_item.done",
|
|
item: {
|
|
type: "function_call",
|
|
call_id: "call_abc",
|
|
name: "search_tasks",
|
|
status: "completed",
|
|
arguments: '{"query":"select:TaskCreate,TaskUpdate","max_results":10}',
|
|
},
|
|
};
|
|
|
|
const result = openaiResponsesToOpenAIResponse(chunk, state);
|
|
|
|
assert.ok(result);
|
|
assert.equal(
|
|
result.choices[0].delta.tool_calls[0].function.arguments,
|
|
'{"query":"select:TaskCreate,TaskUpdate","max_results":10}'
|
|
);
|
|
assert.equal(state.toolCallIndex, 1);
|
|
});
|
|
|
|
test("Responses->Chat: buffered argument deltas emit once at output_item.done", () => {
|
|
const state = {
|
|
started: true,
|
|
chatId: "chatcmpl-test",
|
|
created: 1234567890,
|
|
toolCallIndex: 0,
|
|
finishReasonSent: false,
|
|
currentToolCallId: "call_abc",
|
|
currentToolCallArgsBuffer: "",
|
|
};
|
|
|
|
const deltaResult = openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.function_call_arguments.delta",
|
|
delta: '{"query":"search"}',
|
|
},
|
|
state
|
|
);
|
|
|
|
assert.equal(deltaResult, null);
|
|
|
|
const chunk = {
|
|
type: "response.output_item.done",
|
|
item: {
|
|
type: "function_call",
|
|
call_id: "call_abc",
|
|
name: "search",
|
|
status: "completed",
|
|
arguments: '{"query":"search"}',
|
|
},
|
|
};
|
|
|
|
const result = openaiResponsesToOpenAIResponse(chunk, state);
|
|
|
|
assert.ok(result);
|
|
assert.equal(result.choices[0].delta.tool_calls[0].function.arguments, '{"query":"search"}');
|
|
assert.equal(state.toolCallIndex, 1);
|
|
});
|
|
|
|
test("Responses->Chat: empty-name tool call is deferred until done provides a valid name", () => {
|
|
const state = {
|
|
started: true,
|
|
chatId: "chatcmpl-test",
|
|
created: 1234567890,
|
|
toolCallIndex: 0,
|
|
finishReasonSent: false,
|
|
currentToolCallArgsBuffer: "",
|
|
currentToolCallDeferred: false,
|
|
};
|
|
|
|
const added = openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.output_item.added",
|
|
item: { type: "function_call", call_id: "call_deferred", name: " " },
|
|
},
|
|
state
|
|
);
|
|
assert.equal(added, null);
|
|
|
|
const delta = openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.function_call_arguments.delta",
|
|
delta: '{"query":"deferred"}',
|
|
},
|
|
state
|
|
);
|
|
assert.equal(delta, null);
|
|
|
|
const done = openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.output_item.done",
|
|
item: {
|
|
type: "function_call",
|
|
call_id: "call_deferred",
|
|
name: "search_tasks",
|
|
arguments: '{"query":"deferred"}',
|
|
},
|
|
},
|
|
state
|
|
);
|
|
|
|
assert.ok(done);
|
|
assert.equal(done.choices[0].delta.tool_calls[0].function.name, "search_tasks");
|
|
assert.equal(done.choices[0].delta.tool_calls[0].function.arguments, '{"query":"deferred"}');
|
|
});
|
|
|
|
test("Responses->Chat: empty-name tool call is dropped when done still has no valid name", () => {
|
|
const state = {
|
|
started: true,
|
|
chatId: "chatcmpl-test",
|
|
created: 1234567890,
|
|
toolCallIndex: 0,
|
|
finishReasonSent: false,
|
|
currentToolCallArgsBuffer: "",
|
|
currentToolCallDeferred: false,
|
|
};
|
|
|
|
openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.output_item.added",
|
|
item: { type: "function_call", call_id: "call_empty", name: "" },
|
|
},
|
|
state
|
|
);
|
|
|
|
const done = openaiResponsesToOpenAIResponse(
|
|
{
|
|
type: "response.output_item.done",
|
|
item: {
|
|
type: "function_call",
|
|
call_id: "call_empty",
|
|
name: " ",
|
|
arguments: '{"ignored":true}',
|
|
},
|
|
},
|
|
state
|
|
);
|
|
|
|
assert.equal(done, null);
|
|
assert.equal(state.toolCallIndex, 0);
|
|
});
|
|
|
|
test("Claude->Responses: {event,data} items bypass sanitization in translate mode", async () => {
|
|
// Regression test: when translating Claude-format (GLM) to Responses API for Codex CLI,
|
|
// the sanitizer was stripping {event,data} items to {"object":"chat.completion.chunk"},
|
|
// losing all content and the critical response.completed event.
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder();
|
|
|
|
// Create stream translating claude → openai-responses (same path as GLM via Codex CLI)
|
|
const stream = createSSETransformStreamWithLogger(
|
|
FORMATS.CLAUDE,
|
|
FORMATS.OPENAI_RESPONSES,
|
|
"glm",
|
|
null,
|
|
null,
|
|
"glm-5.1",
|
|
"conn-test",
|
|
{ messages: [{ role: "user", content: "hi" }] },
|
|
null,
|
|
null
|
|
);
|
|
|
|
const writer = stream.writable.getWriter();
|
|
// Simulate Claude-format SSE from GLM
|
|
await writer.write(
|
|
encoder.encode(
|
|
'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_test","type":"message","role":"assistant","model":"glm-5.1","content":[],"stop_reason":null,"usage":{"input_tokens":10,"output_tokens":0}}}\n\n'
|
|
)
|
|
);
|
|
await writer.write(
|
|
encoder.encode(
|
|
'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}\n\n'
|
|
)
|
|
);
|
|
await writer.write(
|
|
encoder.encode(
|
|
'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}}\n\n'
|
|
)
|
|
);
|
|
await writer.write(
|
|
encoder.encode(
|
|
'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}\n\n'
|
|
)
|
|
);
|
|
await writer.write(encoder.encode('event: message_stop\ndata: {"type":"message_stop"}\n\n'));
|
|
await writer.close();
|
|
|
|
const reader = stream.readable.getReader();
|
|
let output = "";
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
output += decoder.decode(value, { stream: true });
|
|
}
|
|
output += decoder.decode();
|
|
|
|
// Must emit Responses API events (not sanitized chat.completion.chunk objects)
|
|
assert.match(output, /event: response\.created/);
|
|
assert.match(output, /event: response\.output_text\.delta/);
|
|
assert.match(output, /event: response\.completed/);
|
|
assert.match(output, /"delta":"hello"/);
|
|
assert.match(output, /"status":"completed"/);
|
|
|
|
// Must NOT contain sanitized empty chunks
|
|
assert.doesNotMatch(output, /data: \{"object":"chat\.completion\.chunk"\}\n\n/);
|
|
});
|
|
|
|
test("Responses->Claude: translated Claude SSE is not sanitized into empty OpenAI chunks", async () => {
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder();
|
|
const stream = createSSETransformStreamWithLogger(
|
|
FORMATS.OPENAI_RESPONSES,
|
|
FORMATS.CLAUDE,
|
|
"codex",
|
|
null,
|
|
null,
|
|
"gpt-5.4",
|
|
"conn-test",
|
|
{ messages: [{ role: "user", content: "hi" }] },
|
|
null,
|
|
null
|
|
);
|
|
|
|
const writer = stream.writable.getWriter();
|
|
await writer.write(
|
|
encoder.encode('data: {"type":"response.output_text.delta","delta":"hello"}\n\n')
|
|
);
|
|
await writer.write(
|
|
encoder.encode(
|
|
'data: {"type":"response.completed","response":{"usage":{"input_tokens":12,"output_tokens":3}}}\n\n'
|
|
)
|
|
);
|
|
await writer.close();
|
|
|
|
const reader = stream.readable.getReader();
|
|
let output = "";
|
|
while (true) {
|
|
const { value, done } = await reader.read();
|
|
if (done) break;
|
|
output += decoder.decode(value, { stream: true });
|
|
}
|
|
output += decoder.decode();
|
|
|
|
assert.match(output, /event: message_start/);
|
|
assert.match(output, /event: content_block_start/);
|
|
assert.match(output, /event: content_block_delta/);
|
|
assert.match(output, /event: message_delta/);
|
|
assert.match(output, /event: message_stop/);
|
|
assert.doesNotMatch(output, /data: \{"object":"chat\.completion\.chunk"\}\n\n/);
|
|
});
|