SPB Git

Repository Public

budget.test.ts 2.6 KB 46 lines typescript

1import { describe, expect, it } from "vitest";2import { decide } from "../src/lib/budget";3import { estimateImage, estimateSpeech, estimateVideo, ticksToUsd } from "../src/providers/pricing";45const policy = { kill_switch: false, per_shot_usd: 2.5, per_film_usd: 8, per_day_usd: 25, per_month_usd: 150 };6const none = { shot: 0, film: 0, day: 0, month: 0 };7const scope = { shot: true, film: true };89describe("budget gate", () => {10  it("allows a call inside every ceiling", () => { expect(decide(policy, none, 1, scope)).toBeNull(); });11  it("refuses everything when the kill switch is on, even a free call", () => {12    expect(decide({ ...policy, kill_switch: true }, none, 0, scope)?.reason).toBe("kill_switch");13  });14  it("refuses an unknown price instead of guessing", () => { expect(decide(policy, none, null, scope)?.reason).toBe("unknown_price"); });15  it("checks the narrowest ceiling first: shot, film, day, month", () => {16    expect(decide(policy, { ...none, shot: 2 }, 0.6, scope)?.reason).toBe("per_shot");17    expect(decide(policy, { ...none, film: 7.5 }, 0.6, scope)?.reason).toBe("per_film");18    expect(decide(policy, { ...none, day: 24.9 }, 0.6, scope)?.reason).toBe("per_day");19    expect(decide(policy, { ...none, month: 149.9 }, 0.6, scope)?.reason).toBe("per_month");20  });21  it("counts money already committed to running jobs", () => {22    expect(decide(policy, { shot: 0, film: 7, day: 7, month: 7 }, 1, scope)).toBeNull();23    expect(decide(policy, { shot: 0, film: 7.01, day: 7, month: 7 }, 1, scope)?.reason).toBe("per_film");24  });25  it("ignores the shot ceiling for calls that are not tied to a shot", () => {26    expect(decide(policy, { ...none, shot: 99 }, 0.1, { shot: false, film: true })).toBeNull();27  });28});2930describe("pricing", () => {31  it("converts provider ticks to dollars (1 USD = 1e10 ticks)", () => {32    expect(ticksToUsd(1_300_000_000)).toBe(0.13);33    expect(ticksToUsd(400_000_000)).toBe(0.04);34  });35  it("prefers the observed rate, then the table, and says which one it used", () => {36    expect(estimateVideo("grok-imagine-video-1.5-lite", "720p", 4)).toEqual({ usd: 0.13, basis: "measured" });37    expect(estimateVideo("grok-imagine-video-1.5-lite", "720p", 4, 0.05)).toEqual({ usd: 0.2, basis: "observed" });38    expect(estimateVideo("some-future-model", "720p", 4)).toEqual({ usd: null, basis: "unknown" });39  });40  it("prices images and speech from the list", () => {41    expect(estimateImage("grok-imagine-image-2.0", "low", "1k").usd).toBe(0.04);42    expect(estimateImage("unknown").usd).toBeNull();43    expect(estimateSpeech(1000).usd).toBe(0.015);44  });45});46
Files main
Menu

Theme