The rate tests held one date, 2026-10-14, so a promotion starting after it was never compared with PROMOTIONS. Dropping gemini-4-carbon's line (#953, from 2026-11-10) left every test green. The new test checks each promotion droid's registry lists, inline or named, a minute after it starts, a minute before it ends and a minute after, against what configModels gives then. GPT-6 Astra's 60% off ended 2026-09-05, before PROMOTIONS was kept, and is skipped by a fixed LISTED_SINCE (2026-10-01); with no cutoff the test was red on it. 7 promotions are checked. Run on mote with a sandbox HOME: bun test packages/factory, 194 pass. Broken by hand: deleting Carbon's PROMOTIONS line turns the new test red (the old ones stay green); Argon at 0.4 turns it and two more red; the count bar at 99 shows 7 checked. Not run: the root suite (test-only change in one package; CI runs it).
152 lines
7.2 KiB
JavaScript
152 lines
7.2 KiB
JavaScript
// A model's price reaches magpie as its rate: droid's own picker shows a model
|
|
// by the token multiplier its entry keeps (cost.tokenMultiplier), and magpie
|
|
// shows the same beside the model, so a cheap one is picked without opening
|
|
// droid. A promotion running comes off it, with the multiplier before the
|
|
// discount as rateWas — magpie strikes that through beside the price, as it
|
|
// does for WorkBuddy's credits and Qoder's price factors. The cases are droid
|
|
// 0.241.1's registry costs, 0.251.0's 1017 entry and 1.7.0's new models,
|
|
// saved in testdata.
|
|
import { expect, test } from "bun:test"
|
|
import { readFileSync } from "node:fs"
|
|
import { FactoryAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const DROID = ["droid-0.241.1-costs.txt", "droid-0.251.0-deepseek-1017.txt", "droid-1.7.0-models.txt", "droid-1.7.1-carbon.txt"]
|
|
.map((name) => readFileSync(new URL(`./testdata/${name}`, import.meta.url), "utf8")).join("\n")
|
|
|
|
// cost is a model's cost text, as its entry keeps it (Inkling's key unquoted).
|
|
function cost(id) {
|
|
const line = DROID.split("\n").find((l) => l.startsWith(`"${id}":`) || l.startsWith(`${id}:{id:"${id}",`))
|
|
if (!line) throw new Error(`${id} is not in the fixture`)
|
|
return line.slice(line.indexOf("cost:"))
|
|
}
|
|
|
|
// multiplier is the token multiplier an entry lists.
|
|
function multiplier(id) {
|
|
return Number(cost(id).match(/tokenMultiplier:([\d.]+)/)[1])
|
|
}
|
|
|
|
// promotions are the ones an entry names: droid keeps them apart from it, by a
|
|
// name, or inline in the entry itself.
|
|
function promotions(id) {
|
|
const text = cost(id)
|
|
const inline = text.match(/promotions:\[\{discount:([\d.]+),startsAt:new Date\("([^"]+)"\)(?:,expiresAt:new Date\("([^"]+)"\))?/)
|
|
if (inline) return [{ discount: +inline[1], from: inline[2], to: inline[3] ?? null }]
|
|
const named = text.match(/promotions:\[([A-Za-z_$][\w$]*(?:,[A-Za-z_$][\w$]*)*)\]/)
|
|
if (!named) return []
|
|
const defs = Object.fromEntries(
|
|
[...DROID.matchAll(/^([A-Za-z_$][\w$]*)=\{discount:([\d.]+),startsAt:new Date\("([^"]+)"\)(?:,expiresAt:new Date\("([^"]+)"\))?/gm)].map((m) => [
|
|
m[1],
|
|
{ discount: +m[2], from: m[3], to: m[4] ?? null },
|
|
]),
|
|
)
|
|
return named[1].split(",").map((n) => {
|
|
const d = defs[n.trim()]
|
|
if (!d) throw new Error(`promotion ${n} is not defined in the fixture`)
|
|
return d
|
|
})
|
|
}
|
|
|
|
// price is what magpie shows for a model at now: the multiplier less the first
|
|
// promotion running then, and the multiplier before it when one runs.
|
|
function price(id, now) {
|
|
const base = multiplier(id)
|
|
const p = promotions(id).find((p) => now >= Date.parse(p.from) && (!p.to || now < Date.parse(p.to)))
|
|
if (!p) return { rate: base, rateWas: 0 }
|
|
return { rate: Number((base * (1 - p.discount)).toFixed(4)), rateWas: base }
|
|
}
|
|
|
|
test("every model's rate is the token multiplier its droid registry entry lists", () => {
|
|
for (const m of _internal.MODELS) {
|
|
expect(m.rate).toBe(multiplier(m.id))
|
|
}
|
|
})
|
|
|
|
test("a promotion running comes off the rate, the multiplier before it as rateWas", () => {
|
|
const now = Date.parse("2026-10-14T21:30:00Z")
|
|
const models = _internal.configModels(now)
|
|
for (const m of _internal.MODELS) {
|
|
expect(models[m.id].rate).toBe(price(m.id, now).rate)
|
|
expect(models[m.id].rateWas).toBe(price(m.id, now).rateWas)
|
|
}
|
|
// the five droid 0.241.1 offers a price off on: Sol's 20%, Mistral Large 4's
|
|
// launch discount, and Gemini 4 Argon's, 3.8 Flash's and 3.7 Flash's 50%;
|
|
// and Sol's 20% on GPT-5.6 Sol Fast Mode in 1.7.0
|
|
const discounted = _internal.MODELS.filter((m) => models[m.id].rateWas > 0).map((m) => m.id)
|
|
expect(discounted).toEqual(["gpt-5.6-sol", "mistral-large-4", "gemini-4-argon", "gemini-3.8-flash", "gemini-3.7-flash", "gpt-5.6-sol-fast"])
|
|
expect(models["gpt-5.6-sol-fast"].rate).toBe(3.2)
|
|
expect(models["gpt-5.6-sol-fast"].rateWas).toBe(4)
|
|
expect(models["gemini-4-argon"].rate).toBe(0.8)
|
|
expect(models["gemini-4-argon"].rateWas).toBe(1.6)
|
|
})
|
|
|
|
// The test above holds one date, so a promotion that starts after it (Gemini 4
|
|
// Carbon's, 2026-11-10) was never checked (#954). Every promotion droid lists
|
|
// is checked inside its own window, and after it ends. One that ended before
|
|
// PROMOTIONS began (GPT-6 Astra's 60% off, to 2026-09-05) was never listed.
|
|
const LISTED_SINCE = Date.parse("2026-10-01T00:00:00Z")
|
|
test("every promotion droid lists comes off the rate in its own window, and not after", () => {
|
|
let seen = 0
|
|
for (const m of _internal.MODELS) {
|
|
for (const p of promotions(m.id)) {
|
|
if (p.to && Date.parse(p.to) < LISTED_SINCE) continue
|
|
const from = Date.parse(p.from)
|
|
const times = [from + 60_000]
|
|
if (p.to) times.push(Date.parse(p.to) - 60_000, Date.parse(p.to) + 60_000)
|
|
for (const now of times) {
|
|
const got = _internal.configModels(now)[m.id]
|
|
expect({ id: m.id, at: new Date(now).toISOString(), rate: got.rate, rateWas: got.rateWas }).toEqual({
|
|
id: m.id,
|
|
at: new Date(now).toISOString(),
|
|
...price(m.id, now),
|
|
})
|
|
}
|
|
seen++
|
|
}
|
|
}
|
|
expect(seen).toBeGreaterThanOrEqual(7)
|
|
})
|
|
|
|
test("a promotion that has ended, or hasn't started, is no discount at all", () => {
|
|
// Gemini 4 Argon's 50% off ends 2026-12-01T08:00Z; GLM-5.6 Sol's 20% starts
|
|
// 2026-08-22T00:00Z. Either way the multiplier is the price.
|
|
const after = _internal.configModels(Date.parse("2026-12-02T00:00:00Z"))
|
|
expect(after["gemini-4-argon"].rate).toBe(1.6)
|
|
expect(after["gemini-4-argon"].rateWas).toBe(0)
|
|
const before = _internal.configModels(Date.parse("2026-08-01T00:00:00Z"))
|
|
expect(before["gpt-5.6-sol"].rate).toBe(2)
|
|
expect(before["gpt-5.6-sol"].rateWas).toBe(0)
|
|
})
|
|
|
|
// magpie reads a model's rate from a provider hook's models, not from a config
|
|
// hook's (host.js, providers()): a price given only in config never reaches the
|
|
// model list. So the same list is offered both ways.
|
|
test("the provider hook carries every model with its rate, as magpie reads it", async () => {
|
|
const now = Date.parse("2026-10-14T21:30:00Z")
|
|
const models = _internal.providerModels({ id: "factory" }, now)
|
|
expect(Object.keys(models).sort()).toEqual(_internal.MODELS.map((m) => m.id).sort())
|
|
for (const m of _internal.MODELS) {
|
|
const got = models[m.id]
|
|
expect(got.rate).toBe(price(m.id, now).rate)
|
|
expect(got.rateWas).toBe(price(m.id, now).rateWas)
|
|
// the fields magpie reads a provider's model list for
|
|
expect(got.id).toBe(m.id)
|
|
expect(got.name).toBe(m.name)
|
|
expect(got.api).toEqual({ id: m.id, url: m.wire.api, npm: m.wire.npm })
|
|
expect(got.limit).toEqual({ context: m.context, output: m.output })
|
|
expect(got.capabilities.reasoning).toBe(m.efforts.length > 0)
|
|
expect(got.capabilities.input.image).toBe(m.images)
|
|
expect(Object.keys(got.variants)).toEqual([...m.efforts])
|
|
}
|
|
expect(models["gemini-4-argon"].rate).toBe(0.8)
|
|
expect(models["gemini-4-argon"].rateWas).toBe(1.6)
|
|
})
|
|
|
|
test("the plugin answers a provider hook, which a config hook alone doesn't", async () => {
|
|
const hooks = await FactoryAuthPlugin({ client: { auth: { set: async () => {} } } })
|
|
expect(hooks.provider?.id).toBe("factory")
|
|
expect(typeof hooks.provider.models).toBe("function")
|
|
const models = await hooks.provider.models({ id: "factory" }, {})
|
|
expect(models["gpt-6.1-sol"].rate).toBeGreaterThan(0)
|
|
expect(Object.keys(models)).toHaveLength(_internal.MODELS.length)
|
|
})
|