File size: 4,161 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
/**
 * tests/unit/ui/comboFlowModel-breakers.test.ts
 *
 * TDD for U1b — enrichRunWithBreakers: overlays REAL circuit-breaker state
 * (from GET /api/monitoring/health → providerHealth[provider]) onto a combo run's
 * targets, so the cascade can show "CB: OPEN · retry 41s" instead of only the
 * error-string heuristic.
 * Run: node --import tsx/esm --test tests/unit/ui/comboFlowModel-breakers.test.ts
 */

import { describe, it } from "node:test";
import assert from "node:assert/strict";

import {
  enrichRunWithBreakers,
  comboRunToFlow,
  type ComboRunModel,
  type TargetNodeModel,
} from "../../../src/app/(dashboard)/dashboard/combos/live/comboFlowModel.ts";

function mkRun(
  targets: Array<Partial<TargetNodeModel> & { provider: string }>
): ComboRunModel {
  return {
    comboName: "c",
    strategy: "priority",
    outcome: "running",
    startedAt: 0,
    targets: targets.map((t, i) => ({
      targetIndex: i,
      provider: t.provider,
      model: t.model ?? "m",
      state: t.state ?? "idle",
      ...t,
    })),
  };
}

describe("enrichRunWithBreakers", () => {
  it("returns null for a null run", () => {
    assert.equal(enrichRunWithBreakers(null, {}), null);
  });

  it("returns the same run reference when no health map is provided", () => {
    const run = mkRun([{ provider: "openai" }]);
    assert.equal(enrichRunWithBreakers(run, null), run);
    assert.equal(enrichRunWithBreakers(run, undefined), run);
  });

  it("attaches cbState + retryAfterMs for an OPEN provider breaker only", () => {
    const run = mkRun([{ provider: "openai" }, { provider: "anthropic" }]);
    const out = enrichRunWithBreakers(run, {
      openai: { state: "OPEN", retryAfterMs: 41000 },
    });
    assert.ok(out);
    assert.equal(out.targets[0].cbState, "OPEN");
    assert.equal(out.targets[0].cbRetryAfterMs, 41000);
    assert.equal(out.targets[1].cbState, undefined, "healthy/absent provider gets no badge");
  });

  it("does not attach a badge for a CLOSED (healthy) breaker", () => {
    const run = mkRun([{ provider: "openai" }]);
    const out = enrichRunWithBreakers(run, { openai: { state: "CLOSED", retryAfterMs: 0 } });
    assert.equal(out?.targets[0].cbState, undefined);
  });

  it("surfaces HALF_OPEN and DEGRADED states (case-insensitive)", () => {
    const run = mkRun([{ provider: "a" }, { provider: "b" }]);
    const out = enrichRunWithBreakers(run, {
      a: { state: "half_open", retryAfterMs: 5000 },
      b: { state: "DEGRADED" },
    });
    assert.equal(out?.targets[0].cbState, "HALF_OPEN");
    assert.equal(out?.targets[1].cbState, "DEGRADED");
  });

  it("does not mutate the input run (pure)", () => {
    const run = mkRun([{ provider: "openai" }]);
    enrichRunWithBreakers(run, { openai: { state: "OPEN", retryAfterMs: 1 } });
    assert.equal(run.targets[0].cbState, undefined, "original run must be untouched");
  });

  it("strips a stale cbState when the breaker recovers to CLOSED", () => {
    const run = mkRun([{ provider: "openai", cbState: "OPEN", cbRetryAfterMs: 9 }]);
    const out = enrichRunWithBreakers(run, { openai: { state: "CLOSED" } });
    assert.equal(out?.targets[0].cbState, undefined);
    assert.equal(out?.targets[0].cbRetryAfterMs, undefined);
  });

  it("ignores unknown breaker state strings", () => {
    const run = mkRun([{ provider: "openai" }]);
    const out = enrichRunWithBreakers(run, { openai: { state: "WEIRD" } });
    assert.equal(out?.targets[0].cbState, undefined);
  });

  it("comboRunToFlow carries cbState/cbRetryAfterMs into the target node data", () => {
    const run = mkRun([{ provider: "openai" }, { provider: "anthropic" }]);
    const enriched = enrichRunWithBreakers(run, {
      openai: { state: "OPEN", retryAfterMs: 41000 },
    });
    const { nodes } = comboRunToFlow(enriched as ComboRunModel);

    const target0 = nodes.find((n) => n.id === "target-0");
    assert.equal(target0?.data.cbState, "OPEN");
    assert.equal(target0?.data.cbRetryAfterMs, 41000);

    const target1 = nodes.find((n) => n.id === "target-1");
    assert.equal(target1?.data.cbState, undefined, "healthy provider node has no cbState");
  });
});