droboticflow/tests/frontend/run-logs.test.mjs

60 lines
2.2 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { dedupeRunLogs, selectNodeRunLogs, summarizeNodeRuntime } from "../../src/lib/run-logs.js";
test("dedupes logs by timestamp, level, node and message", () => {
const repeated = { timestamp: "2026-07-10T01:00:00Z", level: "INFO", operator: "train", message: "step 1" };
const distinctNode = { ...repeated, operator: "export" };
assert.deepEqual(dedupeRunLogs([repeated, { ...repeated }, distinctNode]), [repeated, distinctNode]);
});
test("keeps only the newest bounded log entries", () => {
const entries = [0, 1, 2].map((index) => ({
timestamp: `2026-07-10T01:00:0${index}Z`,
level: "INFO",
operator: "train",
message: `step ${index}`,
}));
assert.deepEqual(dedupeRunLogs(entries, 2), entries.slice(1));
});
test("selects live logs for the current node and keeps its persisted logs", () => {
const persisted = { timestamp: "2026-07-10T01:00:00Z", level: "INFO", message: "Starting node execution" };
const live = { timestamp: "2026-07-10T01:00:01Z", level: "INFO", operator: "Train", message: "step 1" };
const other = { ...live, operator: "Export", message: "exporting" };
assert.deepEqual(selectNodeRunLogs([other, live], { nodeId: "train", nodeLabel: "Train", fallback: [persisted] }), [
persisted,
live,
]);
});
test("summarizes the selected node runtime and matching kubernetes job", () => {
const summary = summarizeNodeRuntime(
{
k8sRefs: [
{ nodeId: "prepare", jobName: "prepare-a1", namespace: "droboticflow" },
{ nodeId: "train", attempt: 1, jobName: "train-a1", namespace: "droboticflow" },
],
},
{
nodeId: "train",
status: "running",
startedAt: "2026-07-10T01:00:00Z",
logs: [],
artifacts: [{ id: "checkpoint" }],
resourceSeries: [{ timestamp: "2026-07-10T01:00:01Z" }],
},
[{ message: "step 1" }],
Date.parse("2026-07-10T01:00:05Z"),
);
assert.equal(summary.status, "running");
assert.equal(summary.durationMs, 5000);
assert.equal(summary.logCount, 1);
assert.equal(summary.artifactCount, 1);
assert.equal(summary.resourceCount, 1);
assert.equal(summary.jobRef.jobName, "train-a1");
});