Files
pipelines/web/test/repository.test.mjs
T

107 lines
3.4 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 } 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);
});