import type { SessionEntry } from "@earendil-works/pi-coding-agent"; import { describe, expect, it } from "vitest"; import { quotaProviderKey } from "../src/routing.ts"; const virtual = { provider: "grts", api: "pi-virtual" }; function response(provider: string, stopReason = "stop"): SessionEntry { return { type: "message", message: { role: "assistant", provider, api: "openai-completions", stopReason, }, } as unknown as SessionEntry; } const switched = { type: "model_change" } as unknown as SessionEntry; describe("quota provider routing", () => { it("uses a physical selection directly", () => { expect( quotaProviderKey({ provider: "Zai", api: "openai-completions" }, []), ).toBe("zai"); }); it("follows the provider of the latest successful routed response", () => { const branch = [switched, response("openrouter"), response("google")]; expect(quotaProviderKey(virtual, branch)).toBe("google"); expect( quotaProviderKey(virtual, [...branch, response("nvidia", "error")]), ).toBe("google"); }); it("does not attribute responses from before the virtual selection", () => { expect( quotaProviderKey(virtual, [response("anthropic"), switched]), ).toBeUndefined(); expect(quotaProviderKey(virtual, [])).toBeUndefined(); }); });