Files
plugins/packages/sensenova/testdata.test.mjs
T
我是一条没有名字的区 6a0d9e5b7a sensenova 0.2.0: browser sign-in, plan-filtered models and credit-pool isolation (#730)
sensenova 0.2.0 (yunwang2442-creator): a browser sign-in that captures the console's
OAuth redirect in a one-off Chromium and reuses or creates a `magpie-<host>-<account>-*`
Token Plan API key; the password sign-in kept as a headless fallback; models filtered
to the account's plan; the Flash-Lite pool kept apart from the general pool. Credentials
are sealed with DPAPI on Windows and stored as plaintext elsewhere, as 0.1.3 stored them.
When DPAPI fails, the value is stored as plaintext and the reason is logged.

Reviewed at d57f4a8b (every line of the diff vs main, every new or changed test).

Run at d57f4a8b:
- mote (macOS, Bun 1.4.2, sandbox HOME): `bun test packages/sensenova`: 57 pass, 0 fail;
  `bun test` (whole repo): 5863 pass, 0 fail; `bun scripts/check.mjs`: ok.
- xiaomi-gpu (Windows 11, Bun 1.4.2) via win-run: `bun test packages/sensenova`: 57 pass, 0 fail.
  DPAPI is refused on that box, so this run took the real plaintext fallback.
- A 0.1.3-shaped record ({type:oauth, access, refresh, expires, accountId, uid, apiKey},
  all plaintext, no tokenHost) against a local fake with separate password and browser
  token paths: the refresh went to the password token endpoint only; then the loader
  served sk-legacy, usage read the pools and models listed the plan's model.
- Refresh failures: 400 invalid_grant gives dead + signIn expired, so the host stops.
  500, 503 HTML and an unreachable server give dead:false, so the host retries after
  RENEW_RETRY (30s). A dpapi: value that can't be unsealed gives signIn expired.

Broken by hand (each change in its own copy; each one turned the named test red):
- seal throws off Windows: 5 red, the 0.1.3 upgrade test among them
- seal throws when DPAPI refuses on Windows: 2 red
- seal throws when DPAPI can't load: 1 red
- pruning scoped to every magpie- key: "never touches another saved account's key"
- no activeSuffix protection: "the key in use is never pruned"
- the pre-prune at the 20-key limit put back: "a full account ... is a key_limit error"
- reuse from any magpie- key: "reuse picks this account's newest working key"
- refresh never dead: "a refresh token the platform has revoked says the sign-in is gone"
- create_key off when unset (the old optionOn): "create_key defaults on outside magpie"
- dedicated pool's grant left out: "several granted pools add up"

Not covered by a test: sending a 0.1.3 record (no tokenHost) to the browser token
endpoint keeps every test green, because the fake serves both token endpoints at
/oauth2/token. The code sends it to the password endpoint, as the run above shows.
HOST_TAG (os.hostname) has no test; checked by reading.

Not tried: a real sign-in (no SenseNova account here, no credentials entered). The
author reports real browser, password, refresh and 0.1.3-upgrade runs on Windows 10
and WSL2 Ubuntu (Node 24) on the PR.
2026-11-05 02:45:52 +08:00

106 lines
5.2 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.
// The parsers against the platform's literal bytes. testdata/ holds one
// real /v1/models reply and one real pool-usage reply from a free Token Plan
// account (only the account's pool ids are replaced), so what the fake serves
// is what the console actually sent — including its numbers as strings.
import "./nonet.mjs"
import { readFileSync } from "node:fs"
import { afterEach, expect, test } from "bun:test"
import { SenseNovaAuthPlugin, _internal } from "./index.mjs"
import { fakeSenseNova, json } from "./fake.mjs"
let f
afterEach(() => f?.close())
const load = (name) => JSON.parse(readFileSync(new URL("./testdata/" + name, import.meta.url), "utf8"))
const POOLS = load("pool-usage.json")
const MODELS = load("models.json")
const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000, accountId: "acct", apiKey: "sk-test" }
test("the real pools become one window per pool per span, with the models each covers", () => {
const u = _internal.usageOf(POOLS.pools)
// two pools × (5h, 7d): the console's own numbers, which arrive as strings
expect(u.windows.map((w) => w.name)).toEqual([
"通用积分池 · 5 hours",
"通用积分池 · 7 days",
"Flash-Lite积分池 · 5 hours",
"Flash-Lite积分池 · 7 days",
])
// the default pool leads, so its meters show whatever groups by model
expect(u.windows[0].models).toContain("glm-5.2")
expect(u.windows[0].models.length).toBe(18)
expect(u.windows[2].models).toEqual(["sensenova-6.7-flash-lite", "sensenova-6.8-flash-lite"])
// the counts are the console's, the share is computed from them
expect(u.windows[0].display).toBe("120000 / 120000")
expect(u.windows[0].used).toBe(100)
expect(u.windows[1].display).toBe("438285.55 / 1200000")
// reset_at is epoch seconds, turned into a time the GUI can count down to
expect(new Date(u.windows[0].resetsAt).getTime()).toBe(1793483140 * 1000)
// a grant of 0 is no balance to show
expect(u.balance).toBeUndefined()
})
test("the real catalogue is filtered to what this account's plan can chat with", async () => {
f = fakeSenseNova()
f.route("GET /models", () => json(MODELS))
f.route("GET /lite/console/v1/tokenplan/pool-usage", () => json(POOLS))
const p = await SenseNovaAuthPlugin({ client: { auth: { set: async () => {} } } })
const models = await p.provider.models({ id: "sensenova-token-plan", models: {} }, { auth })
// the catalogue serves seven, all of them marked "tokenplan"; the plan's
// pools cover six of those, and one of the six only draws images
expect(MODELS.data.length).toBe(7)
expect(Object.keys(models).sort()).toEqual([
"deepseek-flash",
"deepseek-v4-flash",
"glm-5.2",
"kimi-k3",
"sensenova-6.8-flash-lite",
])
// the one the plan doesn't cover is left out: the vendor answers 403 to it
expect(models["deepseek-v4.1-flash"]).toBeUndefined()
// the one that draws is left out: a chat endpoint answers 404 to it
expect(models["sensenova-u1.5-lite"]).toBeUndefined()
})
test("the real catalogue's own fields are what the models declare", async () => {
f = fakeSenseNova()
f.route("GET /models", () => json(MODELS))
f.route("GET /lite/console/v1/tokenplan/pool-usage", () => json(POOLS))
const p = await SenseNovaAuthPlugin({ client: { auth: { set: async () => {} } } })
const models = await p.provider.models({ id: "sensenova-token-plan", models: {} }, { auth })
const byId = Object.fromEntries(MODELS.data.map((m) => [m.id, m]))
// deepseek output limits are the measured max_tokens bounds, not the
// catalogue's conservative max_output_length (2026-10-29, a real account:
// [1, 393216] / [1, 384000]); the catalogue's own numbers would cut an
// agent's output to a sixth
const MEASURED = { "deepseek-flash": 393216, "deepseek-v4-flash": 384000 };
for (const id of Object.keys(models)) {
expect(models[id].limit).toEqual({
context: byId[id].context_length,
output: MEASURED[id] ?? byId[id].max_output_length,
})
const takesImage = byId[id].input_modalities.includes("image")
expect(models[id].capabilities.input.image).toBe(takesImage)
expect(models[id].modalities.input).toEqual(takesImage ? ["text", "image"] : ["text"])
}
// the model that only draws is left out of a chat provider's listing (the
// browser host would list it for magpie's Drawers; this plugin instance has
// no magpie input, so the chat convention holds)
expect(models["sensenova-u1.5-lite"]).toBeUndefined()
})
test("the bootstrap list is a subset of what the plan and catalogue agree on", async () => {
// config() declares models before any account is read: those ids must be
// ones a free Token Plan account can actually call, or a cold host would
// offer a model the vendor refuses
f = fakeSenseNova()
f.route("GET /models", () => json(MODELS))
f.route("GET /lite/console/v1/tokenplan/pool-usage", () => json(POOLS))
const p = await SenseNovaAuthPlugin({ client: { auth: { set: async () => {} } } })
const cfg = {}
await p.config(cfg)
const declared = Object.keys(cfg.provider["sensenova-token-plan"].models)
const listed = Object.keys(await p.provider.models({ id: "sensenova-token-plan", models: {} }, { auth }))
expect(declared.sort()).toEqual(listed.sort())
})