File size: 3,002 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
// tests/unit/combo/combo-context.test.ts
// Unit tests for the first extracted combo phase (god-file decomposition fase 1):
// createComboContext (combo/context.ts) + phaseComboSetup (combo/comboSetup.ts).
// The pinning-ON path (getLastSessionModel) is covered end-to-end by the existing combo
// characterization suite (combo-sessionless-pin-3825, combo-config, etc.); here we exercise
// the pure path (context_cache_protection OFF, no settings -> no DB) and the body carrier.
import { test } from "node:test";
import assert from "node:assert/strict";
import { createComboContext } from "../../../open-sse/services/combo/context.ts";
import { phaseComboSetup } from "../../../open-sse/services/combo/comboSetup.ts";

const log = { info() {}, warn() {}, error() {}, debug() {} };

test("createComboContext carries inputs and the body BY REFERENCE", () => {
  const body = { model: "auto", messages: [], stream: true };
  const combo = { name: "c1", models: ["a", "b"] };
  const ctx = createComboContext({ body, combo, log });
  assert.equal(ctx.body, body, "body must be the same reference (not copied) for byte-identical pinning");
  assert.equal(ctx.combo, combo);
  assert.equal(ctx.settings, null);
  assert.equal(ctx.relayOptions, null);
  assert.equal(ctx.log, log);
});

test("phaseComboSetup resolves strategy/config/stream with pinning OFF (pure, no DB)", () => {
  const body = { model: "auto", messages: [], stream: true };
  const combo = { name: "c1", models: ["a"], strategy: "priority" };
  const ctx = createComboContext({ body, combo, log });

  const setup = phaseComboSetup(ctx);

  assert.equal(setup.strategy, "priority");
  assert.equal(setup.pinnedModel, null, "no pin when context_cache_protection is off");
  assert.equal(setup.effectiveSessionId, null);
  assert.equal(setup.clientRequestedStream, true, "body.stream === true");
  assert.equal(typeof setup.comboTargetTimeoutMs, "number");
  assert.equal(typeof setup.reasoningTokenBufferEnabled, "boolean");
  assert.ok(setup.config && typeof setup.config === "object", "config cascade resolved");
  assert.ok(
    setup.resilienceSettings && typeof setup.resilienceSettings === "object",
    "resilience settings resolved"
  );
});

test("phaseComboSetup: clientRequestedStream is false when body.stream is not true", () => {
  const ctx = createComboContext({
    body: { model: "auto", messages: [] },
    combo: { name: "c", models: ["a"] },
    log,
  });
  const setup = phaseComboSetup(ctx);
  assert.equal(setup.clientRequestedStream, false);
});

test("phaseComboSetup normalizes an unknown strategy to a valid routing strategy", () => {
  const ctx = createComboContext({
    body: { model: "auto", messages: [] },
    combo: { name: "c", models: ["a"], strategy: "not-a-real-strategy" },
    log,
  });
  const setup = phaseComboSetup(ctx);
  // normalizeRoutingStrategy falls back to the default for unknown values.
  assert.equal(typeof setup.strategy, "string");
  assert.ok(setup.strategy.length > 0);
});