671 lines
26 KiB
JavaScript
671 lines
26 KiB
JavaScript
// Repository request-shape tests: the templates catalogue and the validate
|
|
// command, driven through the real transport against a fake WebSocket so the
|
|
// exact frames sent (and the error-envelope handling) are proven, not assumed.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { PipelinesClient, ProtocolError, NotConnectedError } from "../src/client.js";
|
|
import { PipelinesRepository, SHELL_OUTPUT_LIMIT, nativeChatMessageId, newChatOperationId } from "../src/repository.js";
|
|
import { validateServerUrl } from "../src/serverUrl.js";
|
|
|
|
const VALID = validateServerUrl("ws://host:8080/connect");
|
|
|
|
/** The same minimal fake the client tests use (test files are not importable). */
|
|
class FakeWebSocket {
|
|
static instances = [];
|
|
|
|
constructor(url) {
|
|
this.url = url;
|
|
this.sent = [];
|
|
this.onopen = null;
|
|
this.onmessage = null;
|
|
this.onclose = null;
|
|
this.onerror = null;
|
|
this.reply = null;
|
|
FakeWebSocket.instances.push(this);
|
|
}
|
|
|
|
send(data) {
|
|
const frame = JSON.parse(data);
|
|
this.sent.push(frame);
|
|
const response = this.reply?.(frame);
|
|
if (response !== undefined) {
|
|
this.onmessage?.({ data: JSON.stringify(response) });
|
|
}
|
|
}
|
|
|
|
close(code, reason) {
|
|
this.onclose?.({ code, reason });
|
|
}
|
|
|
|
open() {
|
|
this.onopen?.({});
|
|
}
|
|
}
|
|
|
|
function connected() {
|
|
FakeWebSocket.instances = [];
|
|
const client = new PipelinesClient({ WebSocketImpl: FakeWebSocket });
|
|
const repo = new PipelinesRepository(client);
|
|
client.connect(VALID);
|
|
const ws = FakeWebSocket.instances.at(-1);
|
|
ws.open();
|
|
return { repo, ws, client };
|
|
}
|
|
|
|
/** The last frame without the client-generated id, for an exact shape assertion. */
|
|
function lastFrame(ws) {
|
|
const { id, ...frame } = ws.sent.at(-1);
|
|
assert.match(id, /^web-\d+$/);
|
|
return frame;
|
|
}
|
|
|
|
test("listTemplates sends exactly {type:'list_templates'} and returns the catalogue", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({
|
|
id: frame.id,
|
|
type: "templates",
|
|
templates: [{ key: "task", name: "Complete a task" }],
|
|
});
|
|
|
|
const templates = await repo.listTemplates();
|
|
assert.deepEqual(lastFrame(ws), { type: "list_templates" });
|
|
assert.equal(templates.length, 1);
|
|
assert.equal(templates[0].key, "task");
|
|
});
|
|
|
|
test("listTemplates returns [] when the reply carries no templates", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "templates" });
|
|
assert.deepEqual(await repo.listTemplates(), []);
|
|
});
|
|
|
|
test("validate sends exactly {type:'validate', source} and returns response.graph", async () => {
|
|
const { repo, ws } = connected();
|
|
const graph = { stages: [{ key: "plan" }] };
|
|
ws.reply = (frame) => ({ id: frame.id, type: "validated", graph });
|
|
|
|
const result = await repo.validate("the source");
|
|
assert.deepEqual(lastFrame(ws), { type: "validate", source: "the source" });
|
|
assert.deepEqual(result, graph);
|
|
});
|
|
|
|
test("validate rejects a server error envelope with the server's text", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "unexpected token" });
|
|
await assert.rejects(() => repo.validate("bad source"), {
|
|
name: "ProtocolError",
|
|
message: "unexpected token",
|
|
});
|
|
});
|
|
|
|
test("validate throws when the reply has no graph", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "validated" });
|
|
await assert.rejects(() => repo.validate("empty"), ProtocolError);
|
|
});
|
|
|
|
/** The new pipeline detail a `submitted` reply carries in these tests. */
|
|
const SUBMITTED_DETAIL = { id: 91, name: "Release helper", version: 1 };
|
|
|
|
test("submit sends exactly {type:'submit',name,source,create_only:true} and returns the detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "submitted", detail: SUBMITTED_DETAIL });
|
|
|
|
const detail = await repo.submit("Release helper", "the source");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "submit",
|
|
name: "Release helper",
|
|
source: "the source",
|
|
create_only: true,
|
|
});
|
|
assert.deepEqual(detail, SUBMITTED_DETAIL);
|
|
});
|
|
|
|
test("submit trims the name before sending it", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "submitted", detail: SUBMITTED_DETAIL });
|
|
|
|
await repo.submit(" Release helper ", "the source");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "submit",
|
|
name: "Release helper",
|
|
source: "the source",
|
|
create_only: true,
|
|
});
|
|
});
|
|
|
|
test("submit refuses a blank name without sending anything", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = () => ({ id: "unused", type: "submitted", detail: SUBMITTED_DETAIL });
|
|
|
|
await assert.rejects(() => repo.submit(" ", "the source"), {
|
|
name: "ProtocolError",
|
|
message: "Enter a pipeline name.",
|
|
});
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
|
|
test("submit rejects a server error envelope with the server's own text", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: 'a pipeline named "Release helper" already exists' });
|
|
|
|
await assert.rejects(() => repo.submit("Release helper", "the source"), {
|
|
name: "ProtocolError",
|
|
message: 'a pipeline named "Release helper" already exists',
|
|
});
|
|
});
|
|
|
|
test("submit throws when the reply has no pipeline detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "submitted", name: "Release helper" });
|
|
|
|
await assert.rejects(() => repo.submit("Release helper", "the source"), {
|
|
name: "ProtocolError",
|
|
message: "The server returned no pipeline detail.",
|
|
});
|
|
});
|
|
|
|
test("search sends exactly {type:'search',search:{query,kind:'',limit:0}} and returns search_results", async () => {
|
|
const { repo, ws } = connected();
|
|
const payload = { query: "release", results: [{ kind: "run" }], cap: 50, scanned_steps: 3 };
|
|
ws.reply = (frame) => ({ id: frame.id, type: "search_results", search_results: payload });
|
|
|
|
const reply = await repo.search("release");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "search",
|
|
search: { query: "release", kind: "", limit: 0 },
|
|
});
|
|
assert.deepEqual(reply, payload);
|
|
});
|
|
|
|
test("search sends the selected kind verbatim and never a limit above the cap", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "search_results", search_results: { results: [] } });
|
|
|
|
await repo.search("release", "run");
|
|
const { search } = lastFrame(ws);
|
|
assert.deepEqual(search, { query: "release", kind: "run", limit: 0 });
|
|
// limit 0 asks for the server's own default (SearchDefaultLimit 50); the client
|
|
// never asks for more than SearchMaxLimit (200), let alone sends a larger value.
|
|
assert.ok(search.limit <= 200);
|
|
});
|
|
|
|
test("search returns null when an older server replies without search_results", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "pong" });
|
|
assert.equal(await repo.search("release"), null);
|
|
});
|
|
|
|
test("search surfaces an explicit server error with the server's own text", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "unknown command: search" });
|
|
await assert.rejects(() => repo.search("release"), {
|
|
name: "ProtocolError",
|
|
message: "unknown command: search",
|
|
});
|
|
});
|
|
/** The run detail a run.action reply carries in these tests. */
|
|
const RUN_DETAIL = { id: 440, status: "failed" };
|
|
|
|
test("decideRun sends exactly {type:'decide_run',run_id,option,feedback} and returns the run detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: RUN_DETAIL });
|
|
|
|
const detail = await repo.decideRun(440, { stage: "review", option: "approve", feedback: " yes " });
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "decide_run",
|
|
run_id: 440,
|
|
stage: "review",
|
|
option: "approve",
|
|
feedback: "yes",
|
|
});
|
|
assert.deepEqual(detail, RUN_DETAIL);
|
|
});
|
|
|
|
test("decideRun omits a blank stage and blank feedback", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: RUN_DETAIL });
|
|
|
|
await repo.decideRun(12, { stage: " ", option: "publish", feedback: " " });
|
|
assert.deepEqual(lastFrame(ws), { type: "decide_run", run_id: 12, option: "publish" });
|
|
});
|
|
|
|
test("cancelRun sends exactly {type:'cancel_run',run_id} and returns the run detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: RUN_DETAIL });
|
|
|
|
const detail = await repo.cancelRun(440);
|
|
assert.deepEqual(lastFrame(ws), { type: "cancel_run", run_id: 440 });
|
|
assert.deepEqual(detail, RUN_DETAIL);
|
|
});
|
|
|
|
test("retryRun always sends request_key and omits retry_mode for the default resume", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run_started", run_detail: RUN_DETAIL });
|
|
|
|
await repo.retryRun(440, { requestKey: "key-1" });
|
|
assert.deepEqual(lastFrame(ws), { type: "retry_run", run_id: 440, request_key: "key-1" });
|
|
});
|
|
|
|
test("retryRun sends retry_mode only for an explicit restart", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run_started", run_detail: RUN_DETAIL });
|
|
|
|
await repo.retryRun(440, { mode: "restart", requestKey: "key-2" });
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "retry_run",
|
|
run_id: 440,
|
|
request_key: "key-2",
|
|
retry_mode: "restart",
|
|
});
|
|
});
|
|
|
|
test("setRunTitle sends exactly {type:'set_run_title',run_id,title} and returns the run detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: RUN_DETAIL });
|
|
|
|
const detail = await repo.setRunTitle(440, "New title");
|
|
assert.deepEqual(lastFrame(ws), { type: "set_run_title", run_id: 440, title: "New title" });
|
|
assert.deepEqual(detail, RUN_DETAIL);
|
|
});
|
|
|
|
test("subscribeRuns sends exactly {type:'subscribe',workspace:''} with no run_ids", async () => {
|
|
const { repo, ws } = connected();
|
|
const id = repo.subscribeRuns();
|
|
assert.match(id, /^web-\d+$/);
|
|
assert.deepEqual(lastFrame(ws), { type: "subscribe", workspace: "" });
|
|
});
|
|
|
|
test("subscribeRuns includes run_ids only when ids are given and sends the workspace verbatim", () => {
|
|
const { repo, ws } = connected();
|
|
repo.subscribeRuns("", [3, 4]);
|
|
assert.deepEqual(lastFrame(ws), { type: "subscribe", workspace: "", run_ids: [3, 4] });
|
|
repo.subscribeRuns("/tmp/ws", []);
|
|
assert.deepEqual(lastFrame(ws), { type: "subscribe", workspace: "/tmp/ws" });
|
|
});
|
|
|
|
test("subscribeRuns is send-only: it returns the id and never awaits a reply", () => {
|
|
const { repo, ws } = connected();
|
|
// No reply is scripted; a send-only call must not hang or reject.
|
|
const id = repo.subscribeRuns();
|
|
assert.equal(ws.sent.at(-1).id, id);
|
|
});
|
|
|
|
test("unsubscribeRuns sends exactly {type:'unsubscribe'} and is send-only", () => {
|
|
const { repo, ws } = connected();
|
|
const id = repo.unsubscribeRuns();
|
|
assert.match(id, /^web-\d+$/);
|
|
assert.deepEqual(lastFrame(ws), { type: "unsubscribe" });
|
|
});
|
|
|
|
test("the run watch commands are refused before connect", () => {
|
|
FakeWebSocket.instances = [];
|
|
const client = new PipelinesClient({ WebSocketImpl: FakeWebSocket });
|
|
const repo = new PipelinesRepository(client);
|
|
assert.throws(() => repo.subscribeRuns(), NotConnectedError);
|
|
assert.throws(() => repo.unsubscribeRuns(), NotConnectedError);
|
|
});
|
|
|
|
test("each run action rejects a server error envelope instead of returning a detail", async () => {
|
|
const calls = [
|
|
(repo) => () => repo.decideRun(440, { option: "approve" }),
|
|
(repo) => () => repo.cancelRun(440),
|
|
(repo) => () => repo.retryRun(440, { requestKey: "key-3" }),
|
|
(repo) => () => repo.setRunTitle(440, ""),
|
|
];
|
|
for (const makeCall of calls) {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "a title with usable text is required" });
|
|
await assert.rejects(makeCall(repo), {
|
|
name: "ProtocolError",
|
|
message: "a title with usable text is required",
|
|
});
|
|
}
|
|
});
|
|
|
|
test("a run action throws when the reply has no run detail", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run" });
|
|
await assert.rejects(() => repo.cancelRun(7), ProtocolError);
|
|
});
|
|
|
|
// ---- The composer's own commands (C8/C9) ----------------------------------
|
|
|
|
test("run retries preserve one admission key and can reconcile without execution", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run", run_detail: { id: 42 } });
|
|
const intent = { pipelineId: 7, version: 1, prompt: "task", workspace: "/work", requestKey: "same-intent" };
|
|
await repo.runPipeline(intent);
|
|
await repo.runPipeline(intent);
|
|
assert.deepEqual(ws.sent.map((frame) => frame.request_key), ["same-intent", "same-intent"]);
|
|
assert.equal((await repo.runByRequestKey("same-intent")).id, 42);
|
|
assert.equal(lastFrame(ws).type, "get_run");
|
|
assert.equal(lastFrame(ws).request_key, "same-intent");
|
|
ws.reply = (frame) => ({ id: frame.id, type: "run" });
|
|
assert.equal(await repo.runByRequestKey("not-admitted"), null);
|
|
});
|
|
|
|
test("chatPrompt trims the text, sends the operation and native message ids, and omits files when empty", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt" });
|
|
|
|
const queued = await repo.chatPrompt("s1", " hello ", [], "op-1");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_prompt",
|
|
chat_prompt: {
|
|
session_id: "s1",
|
|
text: "hello",
|
|
operation_id: "op-1",
|
|
native_message_id: "msg_op1",
|
|
},
|
|
});
|
|
assert.equal(queued, false);
|
|
});
|
|
|
|
test("chatPrompt includes files when present and reads chat_queued", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt", chat_queued: true });
|
|
|
|
const queued = await repo.chatPrompt("s1", "hi", ["a.txt", "b.txt"], "op-2");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_prompt",
|
|
chat_prompt: {
|
|
session_id: "s1",
|
|
text: "hi",
|
|
operation_id: "op-2",
|
|
native_message_id: "msg_op2",
|
|
files: ["a.txt", "b.txt"],
|
|
},
|
|
});
|
|
assert.equal(queued, true);
|
|
});
|
|
|
|
test("chatModels and switchChatModel use the session-scoped wire shapes", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => {
|
|
if (frame.type === "chat_models") {
|
|
return { id: frame.id, type: "chat_models", chat_models: [{ id: "m1", provider: "p1", name: "Model One" }] };
|
|
}
|
|
return { id: frame.id, type: "chat_model", chat_model: frame.chat_model };
|
|
};
|
|
const models = await repo.chatModels("s1");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_models",
|
|
chat_query: { session_id: "s1" },
|
|
});
|
|
assert.deepEqual(models, [{ id: "m1", provider: "p1", name: "Model One" }]);
|
|
|
|
const selected = await repo.switchChatModel("s1", "p1", "m1");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_model",
|
|
chat_model: { session_id: "s1", provider: "p1", model: "m1" },
|
|
});
|
|
assert.equal(selected.model, "m1");
|
|
});
|
|
|
|
test("chatPrompt sends browser attachment bytes with their name and mime", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_prompt" });
|
|
await repo.chatPrompt("s1", "see attached", [], "op-3", [
|
|
{ name: "note.txt", mime: "text/plain", data: "aGVsbG8=" },
|
|
]);
|
|
assert.deepEqual(lastFrame(ws).chat_prompt.attachments, [
|
|
{ name: "note.txt", mime: "text/plain", data: "aGVsbG8=" },
|
|
]);
|
|
});
|
|
|
|
test("chatPrompt refuses a blank session or blank text without sending", async () => {
|
|
const { repo, ws } = connected();
|
|
await assert.rejects(() => repo.chatPrompt(" ", "hi", [], "op"), ProtocolError);
|
|
await assert.rejects(() => repo.chatPrompt("s1", " ", [], "op"), ProtocolError);
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
|
|
test("chatInterrupt sends the session as chat_query (not a chat_interrupt body)", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_interrupt" });
|
|
|
|
await repo.chatInterrupt("s1");
|
|
assert.deepEqual(lastFrame(ws), { type: "chat_interrupt", chat_query: { session_id: "s1" } });
|
|
});
|
|
|
|
test("chatDirectories sends exactly {type:'chat_directories'} and returns the list", async () => {
|
|
const { repo, ws } = connected();
|
|
const directories = ["/home/operator", "/repo/one", "/repo/two"];
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_directories", chat_directories: directories });
|
|
|
|
assert.deepEqual(await repo.chatDirectories(), directories);
|
|
assert.deepEqual(lastFrame(ws), { type: "chat_directories" });
|
|
});
|
|
|
|
test("chatDirectories returns [] when the reply carries no directories", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_directories" });
|
|
assert.deepEqual(await repo.chatDirectories(), []);
|
|
});
|
|
|
|
test("chatDirectories surfaces a server refusal as a ProtocolError", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "unknown command: chat_directories" });
|
|
await assert.rejects(() => repo.chatDirectories(), {
|
|
name: "ProtocolError",
|
|
message: "unknown command: chat_directories",
|
|
});
|
|
});
|
|
|
|
test("chatCreate sends home as ~ and carries the exact title/directory/operation_id body", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_create", chat_session: { id: "ses_new" } });
|
|
|
|
await repo.chatCreate("New chat", "~", "op-4");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_create",
|
|
chat_create: { title: "New chat", directory: "~", operation_id: "op-4" },
|
|
});
|
|
});
|
|
|
|
test("chatCreate sends title/directory/operation_id and returns chat_session", async () => {
|
|
const { repo, ws } = connected();
|
|
const created = { id: "ses_new", title: "New chat" };
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_create", chat_session: created });
|
|
|
|
const session = await repo.chatCreate("New chat", "/repo", "op-3");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_create",
|
|
chat_create: { title: "New chat", directory: "/repo", operation_id: "op-3" },
|
|
});
|
|
assert.deepEqual(session, created);
|
|
});
|
|
|
|
test("chatCreate sends a preloaded behavior brief when provided", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_create", chat_session: { id: "s1" } });
|
|
await repo.chatCreate("New chat", "/repo", "op-5", { instructions: "be careful" });
|
|
assert.equal(lastFrame(ws).chat_create.instructions, "be careful");
|
|
});
|
|
|
|
test("chatCreate throws when the reply carries no chat_session", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_create" });
|
|
await assert.rejects(() => repo.chatCreate("t", "/d", "op"), ProtocolError);
|
|
});
|
|
|
|
test("nativeChatMessageId mirrors the Android id: msg_ + alphanumerics, capped", () => {
|
|
assert.equal(nativeChatMessageId("web-chat-abc-123"), "msg_webchatabc123");
|
|
assert.equal(nativeChatMessageId(""), "");
|
|
assert.equal(nativeChatMessageId("a".repeat(300)).length, "msg_".length + 120);
|
|
assert.match(newChatOperationId(), /^web-chat-[a-z0-9]+-[a-z0-9]+$/);
|
|
});
|
|
|
|
test("subscribeChat sends exactly {type:'chat_subscribe',chat_subscribe:{session_id}}", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_subscribe" });
|
|
|
|
await repo.subscribeChat("ses_eae413f50ffeLQDExlg8cYnscN");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_subscribe",
|
|
chat_subscribe: { session_id: "ses_eae413f50ffeLQDExlg8cYnscN" },
|
|
});
|
|
});
|
|
|
|
test("subscribeChat rejects a blank session id without sending anything", async () => {
|
|
const { repo, ws } = connected();
|
|
await assert.rejects(() => repo.subscribeChat(" "), ProtocolError);
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
|
|
test("subscribeChat surfaces a server refusal as a ProtocolError", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "error", error: "unknown command: chat_subscribe" });
|
|
await assert.rejects(() => repo.subscribeChat("ses_1"), {
|
|
name: "ProtocolError",
|
|
message: "unknown command: chat_subscribe",
|
|
});
|
|
});
|
|
|
|
// ---- The approval reply (C10) ---------------------------------------------
|
|
|
|
test("chatReply sends exactly {type:'chat_reply', chat_reply:{session_id,request_id,decision,client}}", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_reply" });
|
|
|
|
await repo.chatReply("s1", "req-1", "once");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_reply",
|
|
chat_reply: { session_id: "s1", request_id: "req-1", decision: "once", client: "Web" },
|
|
});
|
|
});
|
|
|
|
test("chatReply includes message only when non-blank", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_reply" });
|
|
|
|
await repo.chatReply("s1", "req-1", "always", { message: " looks fine " });
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_reply",
|
|
chat_reply: {
|
|
session_id: "s1",
|
|
request_id: "req-1",
|
|
decision: "always",
|
|
client: "Web",
|
|
message: "looks fine",
|
|
},
|
|
});
|
|
|
|
await repo.chatReply("s1", "req-1", "reject", { message: " " });
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_reply",
|
|
chat_reply: { session_id: "s1", request_id: "req-1", decision: "reject", client: "Web" },
|
|
});
|
|
});
|
|
|
|
test("chatReply refuses blank ids or a decision outside once/always/reject without sending", async () => {
|
|
const { repo, ws } = connected();
|
|
await assert.rejects(() => repo.chatReply(" ", "req-1", "once"), ProtocolError);
|
|
await assert.rejects(() => repo.chatReply("s1", " ", "once"), ProtocolError);
|
|
await assert.rejects(() => repo.chatReply("s1", "req-1", "maybe"), ProtocolError);
|
|
await assert.rejects(() => repo.chatReply("s1", "req-1", ""), ProtocolError);
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
// --- C11: background shells -------------------------------------------------
|
|
|
|
test("chatShells sends the session as chat_query and returns the list", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({
|
|
id: frame.id,
|
|
type: "chat_shells",
|
|
chat_shells: [{ id: "sh1", status: "running", command: "go test ./..." }],
|
|
});
|
|
|
|
const shells = await repo.chatShells("ses_eae413f50ffeLQDExlg8cYnscN");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_shells",
|
|
chat_query: { session_id: "ses_eae413f50ffeLQDExlg8cYnscN" },
|
|
});
|
|
assert.deepEqual(shells, [{ id: "sh1", status: "running", command: "go test ./..." }]);
|
|
});
|
|
|
|
test("chatShells returns [] when the reply carries none", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_shells" });
|
|
assert.deepEqual(await repo.chatShells("ses_1"), []);
|
|
});
|
|
|
|
test("chatShells refuses a blank session id without sending anything", async () => {
|
|
const { repo, ws } = connected();
|
|
await assert.rejects(() => repo.chatShells(" "), {
|
|
name: "ProtocolError",
|
|
message: "Select a chat session first.",
|
|
});
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
|
|
test("chatShellOutput sends chat_shell_output_query with cursor 0 and the 64 KiB limit", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({
|
|
id: frame.id,
|
|
type: "chat_shell_output",
|
|
chat_shell_output: { shell: { id: "sh1", status: "running" }, output: "hello", cursor: 5, size: 5 },
|
|
});
|
|
|
|
const page = await repo.chatShellOutput("ses_1", "sh1");
|
|
assert.deepEqual(lastFrame(ws), {
|
|
type: "chat_shell_output",
|
|
chat_shell_output_query: { session_id: "ses_1", shell_id: "sh1", cursor: 0, limit: 64 << 10 },
|
|
});
|
|
assert.equal(SHELL_OUTPUT_LIMIT, 65536);
|
|
assert.equal(page.output, "hello");
|
|
});
|
|
|
|
test("a refresh always rereads from cursor 0, never appending", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({
|
|
id: frame.id,
|
|
type: "chat_shell_output",
|
|
chat_shell_output: { shell: { id: "sh1", status: "running" }, output: "full page", truncated: true },
|
|
});
|
|
await repo.chatShellOutput("ses_1", "sh1", { cursor: 0, limit: SHELL_OUTPUT_LIMIT });
|
|
await repo.chatShellOutput("ses_1", "sh1", { cursor: 0, limit: SHELL_OUTPUT_LIMIT });
|
|
const frames = ws.sent.filter((frame) => frame.type === "chat_shell_output");
|
|
assert.equal(frames.length, 2);
|
|
for (const frame of frames) {
|
|
assert.equal(frame.chat_shell_output_query.cursor, 0);
|
|
assert.equal(frame.chat_shell_output_query.limit, 65536);
|
|
}
|
|
});
|
|
|
|
test("chatShellOutput refuses blank ids and out-of-bounds reads", async () => {
|
|
const { repo, ws } = connected();
|
|
await assert.rejects(() => repo.chatShellOutput("", "sh1"), {
|
|
name: "ProtocolError",
|
|
message: "Select a shell first.",
|
|
});
|
|
await assert.rejects(() => repo.chatShellOutput("ses_1", " "), {
|
|
name: "ProtocolError",
|
|
message: "Select a shell first.",
|
|
});
|
|
await assert.rejects(() => repo.chatShellOutput("ses_1", "sh1", { cursor: -1 }), {
|
|
name: "ProtocolError",
|
|
message: "Shell output bounds are invalid.",
|
|
});
|
|
await assert.rejects(() => repo.chatShellOutput("ses_1", "sh1", { limit: 0 }), {
|
|
name: "ProtocolError",
|
|
message: "Shell output bounds are invalid.",
|
|
});
|
|
await assert.rejects(() => repo.chatShellOutput("ses_1", "sh1", { limit: SHELL_OUTPUT_LIMIT + 1 }), {
|
|
name: "ProtocolError",
|
|
message: "Shell output bounds are invalid.",
|
|
});
|
|
assert.equal(ws.sent.length, 0);
|
|
});
|
|
|
|
test("chatShellOutput throws when the reply carries no page", async () => {
|
|
const { repo, ws } = connected();
|
|
ws.reply = (frame) => ({ id: frame.id, type: "chat_shell_output" });
|
|
await assert.rejects(() => repo.chatShellOutput("ses_1", "sh1"), {
|
|
name: "ProtocolError",
|
|
message: "The server returned no shell output page.",
|
|
});
|
|
});
|