import { describe, expect, it, beforeEach, vi } from "vitest"; import { liveFlexElapsedSeconds, flexLiveTiers, liveFlexQueueState, formatLiveWait, streamNeuralwatt, resetSessionState, } from "../index"; describe("liveFlexElapsedSeconds", () => { const t0 = 1_000_000; it("stays hidden inside the 2s grace window", () => { expect(liveFlexElapsedSeconds(t0, t0)).toBeUndefined(); expect(liveFlexElapsedSeconds(t0, t0 + 1999)).toBeUndefined(); }); it("reports whole seconds from the 2s mark onward", () => { expect(liveFlexElapsedSeconds(t0, t0 + 2000)).toBe(2); expect(liveFlexElapsedSeconds(t0, t0 + 2999)).toBe(2); expect(liveFlexElapsedSeconds(t0, t0 + 65_000)).toBe(65); }); }); describe("flexLiveTiers", () => { it("keeps the previous turn's discount in the full tier", () => { expect(flexLiveTiers(125, 35)).toEqual([ "flex โˆ’35% ยท queued 02:05", "flex queued 02:05", "", ]); }); it("collapses to wait-only tiers without a previous discount", () => { expect(flexLiveTiers(12)).toEqual(["flex queued 00:12", ""]); }); it("is strictly shorter at each disclosure level", () => { const tiers = flexLiveTiers(60, 7); for (let i = 1; i < tiers.length; i++) { expect(tiers[i].length).toBeLessThan(tiers[i - 1].length); } }); }); describe("formatLiveWait", () => { it("renders a constant-width m:ss clock", () => { expect(formatLiveWait(2)).toBe("00:02"); expect(formatLiveWait(65)).toBe("01:05"); expect(formatLiveWait(605)).toBe("10:05"); // Every in-range rendering is exactly 5 chars: the ticking badge never // grows, so the right side of the footer never reflows. for (const s of [0, 2, 59, 60, 61, 599, 5999]) { expect(formatLiveWait(s)).toHaveLength(5); } }); it("saturates past 100 minutes instead of growing", () => { expect(formatLiveWait(6000)).toBe("99:59+"); expect(formatLiveWait(36000)).toBe("99:59+"); }); }); describe("streamNeuralwatt live flex queue state", () => { const flexModel = { id: "glm-5.2-flex", provider: "neuralwatt", reasoning: true, input: ["text"], compat: { supportsDeveloperRole: false, supportsReasoningEffort: true }, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 131072, maxTokens: 32768, }; const context = { messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }], }; beforeEach(() => { resetSessionState(); expect(liveFlexQueueState()).toEqual({ streams: 0, startedAt: null }); }); it("marks a flex stream in flight and clears once it settles", async () => { const stream = streamNeuralwatt(flexModel as any, context as any, { apiKey: "sk-test" } as any); expect(liveFlexQueueState().streams).toBe(1); expect(liveFlexQueueState().startedAt).toEqual(expect.any(Number)); // mock's inner stream auto-ends on a microtask โ†’ passthrough settles โ†’ stop callback await vi.waitFor(() => expect(liveFlexQueueState()).toEqual({ streams: 0, startedAt: null })); }); it("does not mark standard-tier models", async () => { const standard = { ...flexModel, id: "glm-5.2" }; const stream = streamNeuralwatt(standard as any, context as any, { apiKey: "sk-test" } as any); await vi.waitFor(() => expect(liveFlexQueueState()).toEqual({ streams: 0, startedAt: null })); }); it("reference-counts concurrent flex streams", async () => { const a = streamNeuralwatt(flexModel as any, context as any, { apiKey: "sk-test" } as any); const b = streamNeuralwatt(flexModel as any, context as any, { apiKey: "sk-test" } as any); expect(liveFlexQueueState().streams).toBe(2); await vi.waitFor(() => expect(liveFlexQueueState()).toEqual({ streams: 0, startedAt: null })); }); });