Files
plugins/packages/workbuddy/price.test.mjs
T
lokic7123-star c89cee5b2b workbuddy: a model's credits given as "x0.34 credits" keep their rate (#216, lokic7123-star)
WorkBuddy AI's /v3/config gives some models' credits with a unit:
"x0.34 credits", "x0.59 credits", "x3.31 credits" (fast-model,
balanced-model, primary-model). creditsOf passed "0.34 credits" to Number,
got NaN, and so treated a known rate as unknown. It now strips a trailing
whitespace-separated "credit"/"credits" before the same strict conversion.
Any other trailing text ("x0.34 bananas"), "credits" alone, "x-1 credits",
"xNaN credits", "xInfinity credits" and "" stay unknown, never free.

Reviewed at head 2dda6eb5d42e271e948e4fb5aef6d3e5db7627b5, merged with
origin/main 5a83674 (clean):
- Real catalog: read www.workbuddy.ai/v3/config with the owner's WorkBuddy
  AI sign-in (config read only, no inference). Its cli models carry exactly
  "x0.34 credits", "x0.59 credits", "x3.31 credits", default-model "" and
  the rest "x0.77"-style. main's creditsOf parses the three as unknown;
  this one as 0.34 / 0.59 / 3.31, default-model unknown. CN WorkBuddy's
  config has no unit strings and parses the same on both.
- packages/workbuddy: HOME=<tmp> bun test, 99 pass 0 fail.
- Full repo: HOME=<tmp> bun test, 1802 pass 2 fail; bun scripts/check.mjs ok.
  The 2 reds are packages/cursor, untouched here, under load average ~400:
  latency.test "events never show in time" (#210) and stepusage.test
  "finds the Run's event ... once" (null at line 88: three polls at 10ms
  don't fit its 200ms window under load). Both pass in reruns of the
  cursor package.
- Broke it by hand: without the strip, both new tests red; with the strip
  widened to any trailing word, the unknown-values test red.
2026-10-19 06:31:09 +08:00

72 lines
5.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { test, expect } from "bun:test"
import { WorkBuddyAIAuthPlugin, _internal } from "./index.mjs"
// modelPromotions as copilot.tencent.com's /v3/config gave them on
// 2026-10-14 (icons left out): Hy4 preview free 23:00–8:00 only, Hy3 free
// for whole days to 10-31, Step-5-Preview free 10-12..10-25
const promotions = [
{ badge: { label: "限时免费", display: "activeOnly" }, discount: { discountedCredits: "0x", displayMode: "replace", factor: 0 }, enabled: true, id: "hy3-free-trial-202608", kind: "discount", modelIds: ["hy3", "hy3-b", "hy3-c"], priority: 200, schedule: { timezone: "Asia/Shanghai", validFrom: "2026-07-06T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } },
{ badge: { label: "夜间免费" }, enabled: true, id: "hy4-night-discount-badge-202609", kind: "discount", modelIds: ["hy4-preview", "hy4-preview-ctrl", "hy4-preview-exp"], priority: 50, schedule: { daily: [{ end: "23:00", start: "8:00" }], timezone: "Asia/Shanghai", validFrom: "2026-09-11T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } },
{ badge: { label: "夜间免费" }, discount: { discountedCredits: "0.00x", displayMode: "strikethrough", factor: 0 }, enabled: true, id: "hy4-night-discount-daytime-badge-202609", kind: "discount", modelIds: ["hy4-preview", "hy4-preview-ctrl", "hy4-preview-exp"], priority: 50, schedule: { daily: [{ end: "23:59", start: "23:00" }, { end: "8:00", start: "0:00" }], timezone: "Asia/Shanghai", validFrom: "2026-09-11T00:00:00+08:00", validUntil: "2026-11-01T00:00:00+08:00" } },
{ badge: { label: "夜间折扣" }, discount: { discountedCredits: "0.50x", displayMode: "strikethrough", factor: 0.5 }, enabled: true, id: "glm-52-night-discount-202607", kind: "discount", modelIds: ["glm-5.2"], priority: 100, schedule: { daily: [{ end: "7:50", start: "23:00" }], timezone: "Asia/Shanghai" } },
{ badge: { label: "独家限免" }, discount: { discountedCredits: "0.00x", displayMode: "strikethrough", factor: 0 }, enabled: true, id: "step-5-discount-daytime-badge-202610", kind: "discount", modelIds: ["step-5-preview"], priority: 50, schedule: { timezone: "Asia/Shanghai", validFrom: "2026-10-12T00:00:00+08:00", validUntil: "2026-10-26T00:00:00+08:00" } },
{ badge: { label: "夜间折扣" }, enabled: true, id: "ds-discount-daytime-badge-202608", kind: "discount", modelIds: ["deepseek-v4.1-flash"], priority: 50, schedule: { daily: [{ end: "23:59", start: "0:00" }], timezone: "Asia/Shanghai" } },
]
const day = Date.parse("2026-10-14T17:00:00+08:00")
const night = Date.parse("2026-10-14T23:30:00+08:00")
test("a model free only at night is never said free, day or night (#121)", () => {
for (const at of [day, night]) {
expect(_internal.priceOf({ id: "hy4-preview", credits: "x0.29" }, promotions, at)).toEqual({ free: false, rate: 0.29 })
// a list asked for at night whose credits already read 0
expect(_internal.priceOf({ id: "hy4-preview", credits: "x0.00" }, promotions, at).free).toBe(false)
}
expect(_internal.priceOf({ id: "glm-5.2", credits: "x0.79" }, promotions, night)).toEqual({ free: false, rate: 0.79 })
expect(_internal.priceOf({ id: "deepseek-v4.1-flash", credits: "x0.11" }, promotions, day)).toEqual({ free: false, rate: 0.11 })
})
test("a discount for whole days is the price now, with the price before it", () => {
expect(_internal.priceOf({ id: "hy3", credits: "x0.00" }, promotions, day)).toEqual({ free: true, rate: 0 })
expect(_internal.priceOf({ id: "step-5-preview", credits: "x0.40" }, promotions, day)).toEqual({ free: true, rate: 0, rateWas: 0.4 })
// after it ends, the credits again
expect(_internal.priceOf({ id: "step-5-preview", credits: "x0.40" }, promotions, Date.parse("2026-10-27T00:00:00+08:00"))).toEqual({ free: false, rate: 0.4 })
})
test("without promotions a model is priced by its credits, as before", () => {
expect(_internal.priceOf({ id: "hy3", credits: "x0.00" })).toEqual({ free: true, rate: 0 })
expect(_internal.priceOf({ id: "kimi-k3-1", credits: "x1.62" }, undefined)).toEqual({ free: false, rate: 1.62 })
expect(_internal.priceOf({ id: "auto" }, promotions)).toEqual({})
})
// International /v3/config returned these aliases with a credits suffix on
// 2026-10-17. Number("0.34 credits") loses an otherwise known multiplier.
test("international credit units preserve alias rates and unknown prices", () => {
for (const [credits, rate] of [["x0.34 credits", 0.34], ["x0.59 credits", 0.59], ["x0.99 credits", 0.99], ["x0.00 credits", 0]]) {
expect(_internal.priceOf({ id: "fast-model", credits })).toEqual({ free: rate === 0, rate })
}
for (const credits of ["", "credits", "x0.34 bananas", "xNaN credits", "xInfinity credits", "x-1 credits"]) {
expect(_internal.priceOf({ id: "default-model", credits })).toEqual({})
}
})
test("browser model discovery carries unit-suffixed rates to the host", async () => {
const real = globalThis.fetch
globalThis.fetch = async (input) => {
expect(String(input)).toBe("https://www.workbuddy.ai/v3/config")
return Response.json({ code: 0, data: {
agents: [{ name: "cli", models: ["default-model", "fast-model", "balanced-model", "primary-model"] }],
models: [{ id: "default-model", credits: "" }, { id: "fast-model", credits: "x0.34 credits" }, { id: "balanced-model", credits: "x0.59 credits" }, { id: "primary-model", credits: "x0.99 credits" }],
} })
}
try {
const h = await WorkBuddyAIAuthPlugin({ client: { auth: { set: async () => { throw Error("fixture does not write auth") } } } })
const config = {}
await h.config(config)
const models = await h.provider.models(config.provider["workbuddy-ai"], { auth: { type: "oauth", access: "offline-access", expires: Date.now() + 86400000 } })
expect(models["fast-model"].rate).toBe(0.34)
expect(models["balanced-model"].rate).toBe(0.59)
expect(models["primary-model"].rate).toBe(0.99)
expect(models["default-model"].free).toBe(false)
} finally { globalThis.fetch = real }
})