Files
order-notification-automation/tests/assistant/dsh-events.test.cjs
T

128 lines
5.3 KiB
JavaScript

'use strict';
const test = require('node:test'),
assert = require('node:assert/strict');
const events = () => require('../../src/modules/assistant/dsh-events.cjs');
test('incremental NDJSON accepts split UTF8, CRLF and final unterminated line without leaking native payloads', () => {
const { createDecoder } = events(),
seen = [],
decoder = createDecoder((e) => seen.push(e));
const text = [
{ type: 'session', sessionId: 'PRIVATE_SESSION', cwd: 'C:/PRIVATE_PATH' },
{ type: 'status', phase: 'turn_start', sessionId: 'PRIVATE_SESSION' },
{ type: 'thinking', text: 'PRIVATE_THINKING' },
{ type: 'status', phase: 'step_start', step: 1, secret: 'PRIVATE_STATUS' },
{ type: 'text', text: '{"reply":"unvalidated"}' },
{ type: 'status', phase: 'turn_end', reason: { kind: 'completed' } },
{ type: 'final', text: '{"reply":"中文","actions":[]}' },
]
.map(JSON.stringify)
.join('\r\n');
const bytes = Buffer.from(text);
for (let i = 0; i < bytes.length; i++) decoder.write(bytes.subarray(i, i + 1));
assert.equal(decoder.end(), '{"reply":"中文","actions":[]}');
assert.deepEqual(
seen.map((e) => e.data.phase),
['turn_start', 'step_start', 'turn_end'],
);
assert.doesNotMatch(JSON.stringify(seen), /PRIVATE_|unvalidated|中文/);
});
test('native tools, unknown events, invalid statuses, duplicate final and incomplete results fail closed', () => {
const { createDecoder } = events();
for (const event of [
{ type: 'tool_call', name: 'shell' },
{ type: 'tool_result', result: 'private' },
{ type: 'error', message: 'PRIVATE_PROVIDER' },
{ type: 'unknown' },
{ type: 'status', phase: 'new_phase' },
{ type: 'status', phase: 'turn_start', version: 2 },
]) {
const decoder = createDecoder();
assert.throws(
() => {
decoder.write(Buffer.from(JSON.stringify(event) + '\n'));
decoder.end();
},
(e) => e.status === 502 && !e.message.includes('PRIVATE_PROVIDER'),
);
}
for (const output of [
'{"type":"final","text":"partial"}',
'{"type":"final","reason":{"kind":"completed"},"text":"ok"}\n{"type":"final","reason":{"kind":"completed"},"text":"again"}',
'{"type":"status","phase":"turn_end","reason":{"kind":"cancelled"}}\n{"type":"final","reason":{"kind":"completed"},"text":"ok"}',
]) {
const decoder = createDecoder();
assert.throws(() => {
decoder.write(Buffer.from(output));
decoder.end();
});
}
const decoder = createDecoder();
assert.throws(() => decoder.write(Buffer.from('x'.repeat(1024 * 1024 + 1))), { status: 502 });
});
test('terminal turn_end only permits its final and never permits renewed model activity', () => {
const { createDecoder } = events(),
terminal = { type: 'status', phase: 'turn_end', reason: { kind: 'completed' } },
final = { type: 'final', text: 'checked' };
for (const later of [
{ type: 'status', phase: 'turn_start' },
{ type: 'status', phase: 'step_start', step: 2 },
{ type: 'status', phase: 'step_end', step: 2 },
{ type: 'text', text: 'unfinished response' },
{ type: 'thinking', text: 'unfinished reasoning' },
{ type: 'session', sessionId: 'later-session' },
]) {
const seen = [],
decoder = createDecoder((event) => seen.push(event));
decoder.write(Buffer.from(JSON.stringify(terminal) + '\n'));
assert.throws(
() => decoder.write(Buffer.from([later, final].map(JSON.stringify).join('\n') + '\n')),
{ status: 502 },
later.type + ':' + (later.phase || ''),
);
assert.deepEqual(seen, [{ kind: 'model', data: { phase: 'turn_end' } }]);
}
const completed = createDecoder();
completed.write(Buffer.from([terminal, final].map(JSON.stringify).join('\n') + '\n'));
assert.throws(() => completed.write(Buffer.from('{"type":"text","text":"extra response"}\n')), {
status: 502,
});
});
test('runtime publishes sanitized native status before child exit and rejects a forbidden tool immediately', async (t) => {
const fs = require('node:fs/promises'),
os = require('node:os'),
path = require('node:path'),
{ invoke, environment } = require('../../src/modules/assistant/dsh-runtime.cjs'),
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-progress-'));
t.after(() => fs.rm(dir, { recursive: true, force: true }));
const file = path.join(dir, 'child.cjs'),
controller = new AbortController(),
seen = [];
await fs.writeFile(
file,
"process.stdout.write(JSON.stringify({type:'status',phase:'step_start',step:1,path:'PRIVATE_PATH',text:'PRIVATE_JSON'})+'\\n');setInterval(()=>{},1000);",
);
await assert.rejects(
invoke(file, [], {
cwd: dir,
env: environment(dir),
ndjson: true,
signal: controller.signal,
timeout: 3000,
onEvent: (event) => {
seen.push(event);
controller.abort();
},
}),
{ status: 499 },
);
assert.deepEqual(seen, [{ kind: 'model', data: { phase: 'step_start', step: 1 } }]);
await fs.writeFile(
file,
"process.stdout.write(JSON.stringify({type:'tool_call',tool:'shell',input:'PRIVATE_ARGS'})+'\\n');setInterval(()=>{},1000);",
);
await assert.rejects(
invoke(file, [], { cwd: dir, env: environment(dir), ndjson: true, timeout: 3000 }),
(error) => error.status === 502 && !error.message.includes('PRIVATE_ARGS'),
);
});