Files
yetone b3a3e609ed middleware-reserve 0.1.0: routings that fill to a ceiling, or accounts before keys
A routing-strategy gateway plugin (magpie.gateway, exports strategies):
reserve/ceiling fills accounts in listed order up to `ceiling` percent of
their allowance (80 by default), then the next; past it everywhere, the
least used first. reserve/accounts-first puts subscriptions before API
keys, accounts least used first, keys by weight then recent tokens.
Resting seats go last in both. One file, no imports, for moejs.

Run:
- bun test packages/reserve: 12 tests (10 strategies.json cases through
  checkStrategies, exactly two strategies and no hooks, every answer a
  permutation for 2-8 seats). Breaking the resting rule by hand turned
  its case red.
- Sandbox magpie from yetone/magpie origin/main b03031738: `magpie plugin`
  lists "routings reserve/accounts-first, reserve/ceiling". A stub
  OpenAI-compatible provider on 127.0.0.1:3593 recorded the key of each
  request; the provider had key-a, key-b, key-c (weight 5), listed in that
  order. routing=reserve/accounts-first: 3 of 3 went to key-c. With key-c
  answering 429: key-c 429 once, then key-a, key-b, key-a (key-c resting,
  last; equal weights by fewer tokens). routing=reserve/ceiling: 4 of 4
  to key-a (keys' allowance unknown, so listed order).
Not tried: the over-ceiling tier with real subscription accounts; it is
covered by strategies.json only.
2026-10-16 02:57:43 +08:00

23 lines
890 B
JavaScript

import { expect, test } from "bun:test"
import * as mod from "./reserve.gateway.js"
import { checkStrategies, order } from "../../scripts/middleware.mjs"
checkStrategies(import.meta.dir, mod)
test("gives the two routings, and no hook it doesn't use", () => {
expect(Object.keys(mod.strategies).sort()).toEqual(["accounts-first", "ceiling"])
expect(mod.onRequest).toBeUndefined()
expect(mod.onUsage).toBeUndefined()
})
test("every answer is an order of the seats, whatever they hold", () => {
for (const name of Object.keys(mod.strategies)) {
for (let n = 2; n < 9; n++) {
const seats = Array.from({ length: n }, (_, i) => ({
account: i % 2 === 0, known: i % 3 !== 0, used: (i * 37) % 101, resting: i === 4, weight: (i % 3) + 1, tokens: i * 1000,
}))
expect(order(mod, name, "group/fast", seats, { ceiling: 50 }).failed).toBeUndefined()
}
}
})