112 lines
4.0 KiB
TypeScript
112 lines
4.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { Readable } from "node:stream";
|
|
import { test } from "bun:test";
|
|
|
|
import { handleWorkbenchLaunchHttp } from "./server-workbench-launch-http.ts";
|
|
|
|
test("workbench launch records OTel stage for Project Management link write 404", async () => {
|
|
const originalFetch = globalThis.fetch;
|
|
const request = jsonRequest({
|
|
projectId: "project_hwlab_v03",
|
|
taskRef: "mdtodo:hwlab-v03-mdtodo:file_demo:t4_open",
|
|
sessionId: "ses_launch_otel",
|
|
conversationId: "cnv_launch_otel"
|
|
});
|
|
const response = jsonResponse();
|
|
const fetchCalls = [];
|
|
globalThis.fetch = async (url, init) => {
|
|
fetchCalls.push({ url: String(url), method: init?.method, body: init?.body });
|
|
return new Response(JSON.stringify({ error: { code: "not_found", message: "Project link route was not found." } }), {
|
|
status: 404,
|
|
headers: { "content-type": "application/json" }
|
|
});
|
|
};
|
|
try {
|
|
await handleWorkbenchLaunchHttp(request, response, {
|
|
env: { HWLAB_PROJECT_MANAGEMENT_URL: "http://project-management.test" },
|
|
accessController: {
|
|
async authenticate() {
|
|
return { ok: true, authMethod: "web-session", actor: { id: "usr_launch_otel", role: "admin" } };
|
|
},
|
|
async recordAgentSessionOwner(input) {
|
|
return {
|
|
id: input.sessionId,
|
|
ownerUserId: input.ownerUserId,
|
|
ownerRole: input.ownerRole,
|
|
projectId: input.projectId,
|
|
conversationId: input.conversationId,
|
|
threadId: input.threadId,
|
|
traceId: input.traceId,
|
|
status: input.status,
|
|
session: input.session,
|
|
createdAt: "2026-06-25T10:00:00.000Z",
|
|
updatedAt: "2026-06-25T10:00:00.000Z"
|
|
};
|
|
}
|
|
},
|
|
runtimeStore: {
|
|
async writeWorkbenchFacts() {
|
|
return { ok: true };
|
|
}
|
|
}
|
|
});
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
|
|
assert.equal(response.statusCode, 404);
|
|
assert.equal(fetchCalls.length, 1);
|
|
const body = JSON.parse(response.body);
|
|
assert.equal(body.error.code, "not_found");
|
|
const context = request.hwlabHttpRequestContext;
|
|
assert.equal(context.route, "/v1/workbench/launches");
|
|
assert.equal(context.lastHttpError.code, "not_found");
|
|
assert.equal(context.lastHttpError.category, "workbench_launch");
|
|
assert.equal(context.lastHttpError.layer, "workbench.launch.project_link_write");
|
|
assert.equal(context.otelAttributes["workbench.launch.stage"], "project_link_write");
|
|
assert.equal(context.otelAttributes["workbench.launch.result"], "failed");
|
|
assert.equal(context.otelAttributes["workbench.launch.upstream_status"], 404);
|
|
assert.equal(context.otelAttributes["workbench.launch.session_id"], "ses_launch_otel");
|
|
assert.equal(context.otelAttributes["workbench.launch.task_ref"], "mdtodo:hwlab-v03-mdtodo:file_demo:t4_open");
|
|
assert.match(context.otelAttributes["workbench.launch.task_ref_hash"], /^[0-9a-f]{24}$/u);
|
|
assert.equal(context.otelAttributes["workbench.launch.values_redacted"], true);
|
|
});
|
|
|
|
function jsonRequest(body) {
|
|
const request = Readable.from([JSON.stringify(body)]);
|
|
request.method = "POST";
|
|
request.url = "/v1/workbench/launches";
|
|
request.headers = { "content-type": "application/json" };
|
|
request.hwlabHttpRequestContext = {
|
|
requestId: "req_launch_otel",
|
|
startedAtMs: Date.now(),
|
|
traceId: "11111111111111111111111111111111",
|
|
parentSpanId: null,
|
|
spanId: "2222222222222222",
|
|
traceparent: "00-11111111111111111111111111111111-2222222222222222-01",
|
|
method: "POST",
|
|
route: "/v1/workbench/launches",
|
|
valuesPrinted: false
|
|
};
|
|
return request;
|
|
}
|
|
|
|
function jsonResponse() {
|
|
return {
|
|
headersSent: false,
|
|
writableEnded: false,
|
|
statusCode: 0,
|
|
headers: {},
|
|
body: "",
|
|
writeHead(statusCode, headers) {
|
|
this.statusCode = statusCode;
|
|
this.headers = headers;
|
|
this.headersSent = true;
|
|
},
|
|
end(chunk) {
|
|
this.body += String(chunk ?? "");
|
|
this.writableEnded = true;
|
|
}
|
|
};
|
|
}
|