Spaces:
Runtime error
Runtime error
File size: 4,785 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 | import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
stepEventsToRunModel,
appendInFlightStep,
clearInFlightOnComplete,
} from "../../../src/app/(dashboard)/dashboard/compression/studio/compressionFlowModel.ts";
import { applyStackedCompression } from "../../../open-sse/services/compression/strategySelector.ts";
import { registerCompressionEngine } from "../../../open-sse/services/compression/index.ts";
import type { CompressionEngine } from "../../../open-sse/services/compression/engines/types.ts";
import type { CompressionStepPayload } from "../../../src/lib/events/types.ts";
function step(over: Partial<CompressionStepPayload>): CompressionStepPayload {
return {
requestId: "r1",
comboId: null,
mode: "stacked",
stepIndex: 0,
totalSteps: 2,
engine: "e",
state: "done",
originalTokens: 1000,
compressedTokens: 800,
savingsPercent: 20,
timestamp: 1,
...over,
};
}
describe("stepEventsToRunModel", () => {
it("builds a run model from accumulated step events (run-level totals span firstβlast)", () => {
const model = stepEventsToRunModel([
step({ engine: "a", stepIndex: 0, originalTokens: 1000, compressedTokens: 900, savingsPercent: 10, timestamp: 1 }),
step({ engine: "b", stepIndex: 1, originalTokens: 900, compressedTokens: 700, savingsPercent: 22, timestamp: 2 }),
]);
assert.equal(model.requestId, "r1");
assert.equal(model.mode, "stacked");
assert.equal(model.originalTokens, 1000); // first step's input
assert.equal(model.compressedTokens, 700); // last step's output
assert.equal(model.savingsPercent, 30); // (1000-700)/1000
assert.equal(model.steps.length, 2);
assert.equal(model.steps[1].engine, "b");
assert.equal(model.timestamp, 2);
});
});
describe("in-flight step reducer", () => {
it("appends steps for the same requestId and starts fresh on a new requestId", () => {
let s = appendInFlightStep(null, step({ requestId: "r1", engine: "a" }));
assert.equal(s.requestId, "r1");
assert.equal(s.steps.length, 1);
s = appendInFlightStep(s, step({ requestId: "r1", engine: "b" }));
assert.equal(s.steps.length, 2);
// A new requestId replaces the in-flight run (latest wins).
s = appendInFlightStep(s, step({ requestId: "r2", engine: "x" }));
assert.equal(s.requestId, "r2");
assert.equal(s.steps.length, 1);
});
it("clears the in-flight run only when the completing requestId matches", () => {
const s = appendInFlightStep(null, step({ requestId: "r1" }));
assert.equal(clearInFlightOnComplete(s, "other"), s);
assert.equal(clearInFlightOnComplete(s, "r1"), null);
});
});
// ββ Integration: applyStackedCompression emits a step per engine βββββββββββββββ
function fakeEngine(id: string, compressed: boolean, orig: number, comp: number): CompressionEngine {
return {
id,
name: id,
description: "",
icon: "",
targets: ["messages"],
stackable: true,
stackPriority: 1,
metadata: {
id,
name: id,
description: "",
inputScope: "messages",
targetLatencyMs: 1,
supportsPreview: false,
stable: false,
},
apply(body) {
return {
body,
compressed,
stats: {
originalTokens: orig,
compressedTokens: comp,
savingsPercent: orig > 0 ? Math.round(((orig - comp) / orig) * 100) : 0,
techniquesUsed: [],
mode: "stacked",
timestamp: 0,
durationMs: 2,
},
};
},
compress(body) {
return this.apply(body);
},
getConfigSchema() {
return [];
},
validateConfig() {
return { valid: true, errors: [] };
},
};
}
describe("applyStackedCompression β onEngineStep emission", () => {
it("fires onEngineStep once per engine with index/total/state", () => {
registerCompressionEngine(fakeEngine("step-e1", true, 1000, 900));
registerCompressionEngine(fakeEngine("step-e2", false, 900, 900));
const captured: Array<{ stepIndex: number; totalSteps: number; engine: string; state: string }> = [];
applyStackedCompression(
{ messages: [{ role: "user", content: "hello" }] },
[{ engine: "step-e1" }, { engine: "step-e2" }],
{ onEngineStep: (s) => captured.push(s) }
);
assert.equal(captured.length, 2, "one step per engine");
assert.equal(captured[0].engine, "step-e1");
assert.equal(captured[0].stepIndex, 0);
assert.equal(captured[0].totalSteps, 2);
assert.equal(captured[0].state, "done");
assert.equal(captured[1].engine, "step-e2");
assert.equal(captured[1].stepIndex, 1);
assert.equal(captured[1].state, "skipped");
});
});
|