mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 04:09:23 +08:00
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
buildCancelTurn,
|
|
buildResumeTurn,
|
|
buildSubmitUserReply,
|
|
buildSubscribeTurn,
|
|
} from "../contracts/parse/turn-command";
|
|
import { parseTurnEvent } from "../contracts/parse/turn-event";
|
|
|
|
const eventTypes = [
|
|
"stage_start",
|
|
"stage_end",
|
|
"thinking",
|
|
"observation",
|
|
"content",
|
|
"tool_call",
|
|
"tool_result",
|
|
"progress",
|
|
"sources",
|
|
"result",
|
|
"error",
|
|
"session",
|
|
"session_meta",
|
|
"wait_for_input",
|
|
"done",
|
|
] as const;
|
|
|
|
test("every v2 stream event kind crosses the validated boundary", () => {
|
|
for (const [index, type] of eventTypes.entries()) {
|
|
const parsed = parseTurnEvent(
|
|
JSON.stringify({
|
|
type,
|
|
turn_id: "turn-1",
|
|
seq: index,
|
|
timestamp: 100 + index,
|
|
content: "safe",
|
|
metadata: { future_field: true },
|
|
}),
|
|
);
|
|
assert.equal(parsed.ok, true, type);
|
|
if (parsed.ok) assert.equal(parsed.value.type, type);
|
|
}
|
|
});
|
|
|
|
test("active turn information is validated and heartbeats are quarantined", () => {
|
|
const active = parseTurnEvent({
|
|
type: "active_turn_info",
|
|
turn_id: "turn-1",
|
|
status: "waiting_input",
|
|
protocol_version: "2.0",
|
|
});
|
|
assert.equal(active.ok, true);
|
|
|
|
for (const heartbeat of ["", "heartbeat", '{"type":"ping"}', '{"type":"pong"}']) {
|
|
const parsed = parseTurnEvent(heartbeat);
|
|
assert.deepEqual(parsed.ok ? null : parsed.reason, "heartbeat");
|
|
}
|
|
});
|
|
|
|
test("invalid, incomplete, future, and version-mismatched frames are rejected", () => {
|
|
const missing = parseTurnEvent({ type: "content", turn_id: "turn-1", timestamp: 1 });
|
|
assert.deepEqual(missing.ok ? null : missing.reason, "invalid");
|
|
|
|
const badSeq = parseTurnEvent({
|
|
type: "content",
|
|
turn_id: "turn-1",
|
|
seq: -1,
|
|
timestamp: 1,
|
|
});
|
|
assert.deepEqual(badSeq.ok ? null : badSeq.reason, "invalid");
|
|
|
|
const future = parseTurnEvent({
|
|
type: "telepathy",
|
|
turn_id: "turn-1",
|
|
seq: 1,
|
|
timestamp: 1,
|
|
});
|
|
assert.deepEqual(future.ok ? null : future.reason, "unsupported");
|
|
|
|
const version = parseTurnEvent({
|
|
type: "content",
|
|
protocol_version: "3.0",
|
|
turn_id: "turn-1",
|
|
seq: 1,
|
|
timestamp: 1,
|
|
});
|
|
assert.deepEqual(version.ok ? null : version.reason, "unsupported");
|
|
});
|
|
|
|
test("protocol diagnostics never echo content, metadata, or credentials", () => {
|
|
const parsed = parseTurnEvent({
|
|
type: "future_event",
|
|
content: "private prompt",
|
|
metadata: { token: "secret-token" },
|
|
password: "hunter2",
|
|
});
|
|
assert.equal(parsed.ok, false);
|
|
if (parsed.ok) return;
|
|
assert.doesNotMatch(parsed.diagnostic, /private prompt|secret-token|hunter2|content|metadata|password/);
|
|
});
|
|
|
|
test("command builders enforce IDs, sequence bounds, replies, and v2 versioning", () => {
|
|
assert.deepEqual(buildSubscribeTurn({ turnId: " turn-1 ", afterSeq: 7 }), {
|
|
type: "subscribe_turn",
|
|
turn_id: "turn-1",
|
|
after_seq: 7,
|
|
protocol_version: "2.0",
|
|
});
|
|
assert.equal(buildResumeTurn({ turnId: "turn-1", afterSeq: 9 }).seq, 9);
|
|
assert.equal(buildCancelTurn("turn-1").type, "cancel_turn");
|
|
assert.equal(buildSubmitUserReply({ turnId: "turn-1", text: "yes" }).text, "yes");
|
|
|
|
assert.throws(() => buildSubscribeTurn({ turnId: " ", afterSeq: 0 }), /turn_id/);
|
|
assert.throws(() => buildSubscribeTurn({ turnId: "turn-1", afterSeq: 1.5 }), /after_seq/);
|
|
assert.throws(() => buildSubmitUserReply({ turnId: "turn-1" }), /requires/);
|
|
});
|