8e5ce8494b
Add the background-tasks bar to an open conversation: heading
"Background tasks" plus "{n} running", shown only while at least one
shell is running. Rows are capped at 3 (BACKGROUND_TASK_VISIBLE_ROWS)
and the list scrolls beyond; each row is a play glyph, "Running", a
one-line shellCommandSummary and "Details" while selected. Tapping a
row toggles its detail pane ("Command"/"Task identity"/"Status:
running" plus the bounded output: spinner, error + "Retry",
"Output unavailable for this task.", "No output yet.", or the
monospace output and "Output is bounded to 64 KiB." with "Refresh").
Fold shell frames without ever marking the turn running, merge a blank
command from the previous frame, list only running shells (an exited
shell is never a task) and drop/relearn shells on reconnect via
chat_shells. Refresh always rereads the bounded 64 KiB page from cursor
0. Update PARITY.md C11 with its captures and the replayed-vs-real note.
Checks: node --check on every touched file; cd web && node --test test/
(456 pass).
500 lines
18 KiB
JavaScript
500 lines
18 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 } 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("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("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("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("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 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",
|
|
});
|
|
});
|
|
|
|
// --- 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.",
|
|
});
|
|
});
|