Spaces:
Runtime error
Runtime error
File size: 12,938 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | /**
* tests/unit/ui/comboFlowModel.test.ts
*
* TDD for `comboFlowModel` β the pure reducer/model core of Tela B.
* Run: node --import tsx/esm --test tests/unit/ui/comboFlowModel.test.ts
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
classifyFailKind,
reduceComboEvent,
comboRunToFlow,
type ComboRunModel,
type ComboEventInput,
} from "../../../src/app/(dashboard)/dashboard/combos/live/comboFlowModel.ts";
import { FLOW_EDGE_COLORS } from "../../../src/shared/components/flow/edgeStyles.ts";
// ββ helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function mkAttempt(
targetIndex: number,
strategy = "priority",
provider = `prov${targetIndex}`,
model = `model${targetIndex}`
): ComboEventInput {
return {
comboName: "test-combo",
targetIndex,
provider,
model,
type: "attempt",
strategy,
timestamp: 1000 + targetIndex,
};
}
function mkFailed(
targetIndex: number,
error: string,
provider = `prov${targetIndex}`,
model = `model${targetIndex}`
): ComboEventInput {
return {
comboName: "test-combo",
targetIndex,
provider,
model,
type: "failed",
error,
latencyMs: 100,
timestamp: 2000 + targetIndex,
};
}
function mkSucceeded(
targetIndex: number,
provider = `prov${targetIndex}`,
model = `model${targetIndex}`
): ComboEventInput {
return {
comboName: "test-combo",
targetIndex,
provider,
model,
type: "succeeded",
latencyMs: 42,
timestamp: 3000 + targetIndex,
};
}
// Full 6-event cascade:
// attempt(0) β failed(0,"429 rate limited") β attempt(1) β failed(1,"circuit open") β attempt(2) β succeeded(2)
function buildFullRun(): ComboRunModel {
let run: ComboRunModel | null = null;
run = reduceComboEvent(run, mkAttempt(0));
run = reduceComboEvent(run, mkFailed(0, "429 rate limited"));
run = reduceComboEvent(run, mkAttempt(1));
run = reduceComboEvent(run, mkFailed(1, "circuit open"));
run = reduceComboEvent(run, mkAttempt(2));
run = reduceComboEvent(run, mkSucceeded(2));
return run;
}
// ββ classifyFailKind ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("classifyFailKind", () => {
it("returns undefined when error is absent", () => {
assert.equal(classifyFailKind(undefined), undefined);
assert.equal(classifyFailKind(""), undefined);
});
it("returns 'rate-limit' for 429 messages", () => {
assert.equal(classifyFailKind("429 Too Many Requests"), "rate-limit");
assert.equal(classifyFailKind("upstream rate limit exceeded"), "rate-limit");
assert.equal(classifyFailKind("RATE_LIMIT_EXCEEDED"), "rate-limit");
});
it("returns 'circuit-open' for circuit breaker messages", () => {
assert.equal(classifyFailKind("circuit open"), "circuit-open");
assert.equal(classifyFailKind("Provider circuit breaker is open"), "circuit-open");
assert.equal(classifyFailKind("CIRCUIT_OPEN"), "circuit-open");
});
it("returns 'cooldown' for cooldown messages", () => {
assert.equal(classifyFailKind("connection cooldown active"), "cooldown");
assert.equal(classifyFailKind("cooldown period"), "cooldown");
});
it("returns 'other' for unrecognized error strings", () => {
assert.equal(classifyFailKind("Internal server error"), "other");
assert.equal(classifyFailKind("timeout"), "other");
assert.equal(classifyFailKind("some unknown failure"), "other");
});
it("circuit-open takes precedence over rate-limit when both match", () => {
// edge: a message with both circuit and rate β circuit wins by regex ordering
const result = classifyFailKind("circuit open after 429");
assert.equal(result, "circuit-open");
});
});
// ββ reduceComboEvent ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("reduceComboEvent β basic events", () => {
it("creates a new run on first attempt event (null input)", () => {
const run = reduceComboEvent(null, mkAttempt(0));
assert.equal(run.comboName, "test-combo");
assert.equal(run.strategy, "priority");
assert.equal(run.outcome, "running");
assert.ok(run.startedAt > 0);
assert.equal(run.finishedAt, undefined);
assert.equal(run.targets.length, 1);
assert.equal(run.targets[0].targetIndex, 0);
assert.equal(run.targets[0].provider, "prov0");
assert.equal(run.targets[0].model, "model0");
assert.equal(run.targets[0].state, "attempting");
});
it("marks target as failed and sets failKind on failed event", () => {
let run = reduceComboEvent(null, mkAttempt(0));
run = reduceComboEvent(run, mkFailed(0, "429 rate limited"));
assert.equal(run.targets[0].state, "failed");
assert.equal(run.targets[0].failKind, "rate-limit");
assert.equal(run.targets[0].error, "429 rate limited");
assert.equal(run.outcome, "running"); // not done yet
});
it("marks target as succeeded and sets outcome + finishedAt on succeeded event", () => {
let run = reduceComboEvent(null, mkAttempt(0));
run = reduceComboEvent(run, mkSucceeded(0));
assert.equal(run.targets[0].state, "succeeded");
assert.equal(run.targets[0].latencyMs, 42);
assert.equal(run.outcome, "succeeded");
assert.ok(run.finishedAt != null);
});
});
describe("reduceComboEvent β full cascade (3 targets)", () => {
it("produces 3 targets ordered by targetIndex", () => {
const run = buildFullRun();
assert.equal(run.targets.length, 3);
assert.equal(run.targets[0].targetIndex, 0);
assert.equal(run.targets[1].targetIndex, 1);
assert.equal(run.targets[2].targetIndex, 2);
});
it("states are [failed, failed, succeeded]", () => {
const run = buildFullRun();
assert.equal(run.targets[0].state, "failed");
assert.equal(run.targets[1].state, "failed");
assert.equal(run.targets[2].state, "succeeded");
});
it("failKinds are [rate-limit, circuit-open, undefined]", () => {
const run = buildFullRun();
assert.equal(run.targets[0].failKind, "rate-limit");
assert.equal(run.targets[1].failKind, "circuit-open");
assert.equal(run.targets[2].failKind, undefined);
});
it("outcome is 'succeeded' and finishedAt is set", () => {
const run = buildFullRun();
assert.equal(run.outcome, "succeeded");
assert.ok(run.finishedAt != null);
});
it("strategy is set from the attempt payload", () => {
const run = buildFullRun();
assert.equal(run.strategy, "priority");
});
});
describe("reduceComboEvent β ordering", () => {
it("keeps targets sorted by targetIndex even if events arrive out of order", () => {
// Unusual but defensive: attempt(2) then attempt(0)
let run = reduceComboEvent(null, mkAttempt(2));
run = reduceComboEvent(run, mkAttempt(0));
assert.equal(run.targets[0].targetIndex, 0);
assert.equal(run.targets[1].targetIndex, 2);
});
it("idempotently applies a repeated attempt for the same target", () => {
let run = reduceComboEvent(null, mkAttempt(0));
run = reduceComboEvent(run, mkAttempt(0)); // duplicate
assert.equal(run.targets.length, 1);
assert.equal(run.targets[0].state, "attempting");
});
});
describe("reduceComboEvent β comboName key", () => {
it("ignores events for a different comboName when run already exists", () => {
let run = reduceComboEvent(null, mkAttempt(0));
// Event for a different combo β should be ignored, run returned unchanged
const alienEvent: ComboEventInput = {
comboName: "other-combo",
targetIndex: 99,
provider: "alien",
model: "alien",
type: "failed",
error: "some error",
timestamp: 9999,
};
run = reduceComboEvent(run, alienEvent);
assert.equal(run.targets.length, 1);
assert.equal(run.comboName, "test-combo");
});
});
// ββ comboRunToFlow ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("comboRunToFlow", () => {
it("returns N+3 nodes for N targets (request + strategy + N targets + response)", () => {
const run = buildFullRun();
const { nodes } = comboRunToFlow(run);
// 3 targets β 1 request + 1 strategy + 3 targets + 1 response = 6 nodes
assert.equal(nodes.length, 6, `expected 6 nodes, got ${nodes.length}`);
});
it("node types: first=request, second=strategy, N middle=target, last=response", () => {
const run = buildFullRun();
const { nodes } = comboRunToFlow(run);
assert.equal(nodes[0].type, "request");
assert.equal(nodes[1].type, "strategy");
assert.equal(nodes[2].type, "target");
assert.equal(nodes[3].type, "target");
assert.equal(nodes[4].type, "target");
assert.equal(nodes[5].type, "response");
});
it("target nodes carry provider, model, state, failKind in data", () => {
const run = buildFullRun();
const { nodes } = comboRunToFlow(run);
const t0 = nodes[2];
assert.equal((t0.data as Record<string, unknown>).provider, "prov0");
assert.equal((t0.data as Record<string, unknown>).model, "model0");
assert.equal((t0.data as Record<string, unknown>).state, "failed");
assert.equal((t0.data as Record<string, unknown>).failKind, "rate-limit");
const t2 = nodes[4];
assert.equal((t2.data as Record<string, unknown>).state, "succeeded");
assert.equal((t2.data as Record<string, unknown>).failKind, undefined);
});
it("strategy node carries the strategy name in data", () => {
const run = buildFullRun();
const { nodes } = comboRunToFlow(run);
assert.equal((nodes[1].data as Record<string, unknown>).strategy, "priority");
assert.equal((nodes[1].data as Record<string, unknown>).targetCount, 3);
});
it("edges: N+3 sequential edges (one per node-to-node link)", () => {
const run = buildFullRun();
const { edges } = comboRunToFlow(run);
// 6 nodes β 5 edges: requestβstrategy, strategyβt0, t0βt1, t1βt2, t2βresponse
assert.equal(edges.length, 5, `expected 5 edges, got ${edges.length}`);
});
it("each edge connects consecutive nodes in order", () => {
const run = buildFullRun();
const { nodes, edges } = comboRunToFlow(run);
for (let i = 0; i < edges.length; i++) {
assert.equal(edges[i].source, nodes[i].id, `edge[${i}].source mismatch`);
assert.equal(edges[i].target, nodes[i + 1].id, `edge[${i}].target mismatch`);
}
});
it("failed edges are styled with error color", () => {
const run = buildFullRun();
const { edges } = comboRunToFlow(run);
// edge[2] is strategyβt0 (failed)β¦ actually edges are: [0]reqβstrat, [1]stratβt0, [2]t0βt1, [3]t1βt2, [4]t2βresp
// edge for t0 (failed): index 1 (strategyβt0)
// The edge going INTO t0 should reflect t0 state
const t0IncomingEdge = edges[1]; // strategyβtarget0
const style = t0IncomingEdge.style as Record<string, unknown> | undefined;
assert.ok(style != null, "edge should have style");
assert.equal(style.stroke, FLOW_EDGE_COLORS.error, "failed target edge should be error color");
});
it("succeeded edge is styled with active/green color", () => {
const run = buildFullRun();
const { edges } = comboRunToFlow(run);
// edge[4] is t2βresponse (t2 succeeded)
const t2ResponseEdge = edges[4];
const style = t2ResponseEdge.style as Record<string, unknown> | undefined;
assert.ok(style != null);
assert.equal(
style.stroke,
FLOW_EDGE_COLORS.active,
"succeeded target edge should be active/green color"
);
});
it("idle/attempting edges are styled with idle or last-used color (not error/green)", () => {
// A run with one target still attempting
let run = reduceComboEvent(null, mkAttempt(0));
const { edges } = comboRunToFlow(run);
// edge[1] = strategyβt0 (attempting)
const attemptingEdge = edges[1];
const style = attemptingEdge.style as Record<string, unknown> | undefined;
assert.ok(style != null);
assert.notEqual(style.stroke, FLOW_EDGE_COLORS.error);
assert.notEqual(style.stroke, FLOW_EDGE_COLORS.active);
});
it("produces deterministic node IDs", () => {
const run = buildFullRun();
const { nodes } = comboRunToFlow(run);
assert.equal(nodes[0].id, "request");
assert.equal(nodes[1].id, "strategy");
assert.equal(nodes[2].id, "target-0");
assert.equal(nodes[3].id, "target-1");
assert.equal(nodes[4].id, "target-2");
assert.equal(nodes[5].id, "response");
});
});
|