Files
pikasTech-HWLAB/web/hwlab-cloud-web/scripts/workbench-message-projection-runtime.test.ts
T

85 lines
4.1 KiB
TypeScript

// SPEC: PJ2026-0106050514 Workbench实时运行面 draft-2026-06-30-p0-1297-spec-first; PJ2026-0104010803 Workbench唯一投影 draft-2026-06-18-p0-unique-projection.
// Responsibility: Regression tests for pure Workbench trace/message/projection merge helpers before Pinia integration.
import assert from "node:assert/strict";
import test from "node:test";
import type { AgentChatResultResponse, ChatMessage } from "../src/types/index.ts";
import { clearRunnerTraceTransientDiagnostics, mergeTerminalResultTrace, messageStatusPatchForTerminalMerge, messageTimingPatchForMerge, nonBlockingProjection, normalizeAgentError, projectionFromResult, shouldClearCompletedTurnDiagnostics, terminalMessageTimingPatchForNormalize } from "../src/stores/workbench-message-projection-runtime.ts";
test("terminal result merge preserves runner trace evidence and timing", () => {
const previous: ChatMessage["runnerTrace"] = {
traceId: "trc_1",
sessionId: "ses_1",
events: [{ id: "evt_old", type: "tool" } as never],
eventCount: 1,
projection: { projectionStatus: "blocked", projectionHealth: "degraded", blocker: { code: "old" } },
updatedAt: "2026-01-01T00:00:00.000Z"
};
const result = {
traceId: "trc_1",
sessionId: "ses_1",
status: "completed",
terminal: true,
events: [{ id: "evt_new", type: "finish" }],
eventCount: 1,
startedAt: "2026-01-01T00:00:00.000Z",
finishedAt: "2026-01-01T00:00:05.000Z",
durationMs: 5000,
projectionStatus: "caught-up",
projectionHealth: "healthy",
agentRun: { runId: "run_1", backendProfile: "codex-api" }
} as unknown as AgentChatResultResponse;
const merged = mergeTerminalResultTrace(previous, result);
assert.equal(merged.traceId, "trc_1");
assert.equal(merged.sessionId, "ses_1");
assert.equal(merged.eventCount, 1);
assert.equal(merged.events?.[0]?.id, "evt_old");
assert.equal(merged.terminalEvidence, undefined);
assert.equal(merged.timing?.durationMs, 5000);
assert.equal((merged.agentRun as { runId?: string } | undefined)?.runId, "run_1");
assert.equal(merged.projectionStatus, "caught-up");
});
test("message projection runtime clears only non-blocking completed diagnostics", () => {
const error = normalizeAgentError({ message: "provider failed", diagnostic: { code: "provider_error", source: "provider" } });
assert.equal(error?.message, "provider failed");
assert.equal(error?.diagnostic?.code, "provider_error");
assert.equal(shouldClearCompletedTurnDiagnostics("completed", error), false);
assert.equal(shouldClearCompletedTurnDiagnostics("completed", null), true);
const projection = projectionFromResult({ projectionStatus: "caught-up", projectionHealth: "healthy" } as AgentChatResultResponse);
assert.equal(nonBlockingProjection(projection)?.projectionStatus, "caught-up");
assert.equal(nonBlockingProjection({ projectionStatus: "blocked", projectionHealth: "degraded", blocker: { code: "read_model_gap" } }), null);
const cleared = clearRunnerTraceTransientDiagnostics({ traceId: "trc_1", error, projection, projectionStatus: "caught-up", projectionHealth: "healthy", staleMs: 10, blocker: { code: "old" } });
assert.equal(cleared.error, undefined);
assert.equal(cleared.projection, null);
assert.equal(cleared.blocker, null);
});
test("message timing and terminal status patches stay independent from store state", () => {
const message = {
role: "agent",
status: "running",
startedAt: "2026-01-01T00:00:00.000Z",
timing: { startedAt: "2026-01-01T00:00:00.000Z", valuesRedacted: true }
} as ChatMessage;
const result = {
status: "completed",
terminal: true,
startedAt: "2026-01-01T00:00:00.000Z",
finishedAt: "2026-01-01T00:00:03.500Z"
};
const timingPatch = messageTimingPatchForMerge(message, result);
assert.equal(timingPatch.durationMs, 3500);
assert.equal(timingPatch.finishedAt, "2026-01-01T00:00:03.500Z");
assert.deepEqual(messageStatusPatchForTerminalMerge(message, "completed", true), { status: "completed" });
const terminalPatch = terminalMessageTimingPatchForNormalize(result);
assert.equal(terminalPatch.durationMs, 3500);
});