Files
plugins/packages/minimax/usage.test.mjs
T
yetone 7c859cc532 minimax 0.1.0: MiniMax Code (mcode) accounts' credits and M Plan, China (minimax-code) and international (minimax-code-global), with MiniMax Code's own device sign-in (Magic_zero on Discord)
A new package, @magpie-community/opencode-minimax-auth, written from MiniMax Code 0.6.2's bundle.

Sign-in: MiniMax Code's OAuth device flow, the same one mcode uses (account.minimax.cn / account.minimax.io, client mcode-public, scope agent.default, audience agent-backend, PKCE S256). It also handles MiniMax's own variant, where there is no device_code, the user_code is polled for, and expired_in may be an at-ms. The account is named from MiniMax's signed /v1/api/user/info. mcode's ~/.minimax sign-in is never read or written. Each account is a sign-in of its own, so magpie can hold several.

Refresh: two minutes before the token ends, or once on a 401. One refresh runs per account (keyed by the refresh token it spends), and its result answers for the old token for a minute, so a late reader doesn't spend a rotated token twice. Only invalid_grant with HTTP 400 signs the account out (signIn: expired); any other failure keeps it and uses the token in hand while it lasts.

Chats: Anthropic messages at <agent>/mavis/api/v1/llm/v1/messages, carrying:
- Authorization: Bearer <token>
- x-api-key sk-xxx (mcode's placeholder)
- User-Agent MiniMaxAgent
- X-Mavis-Agent-Id main
- X-Mavis-Timezone-Offset
- X-Mavis-Session-Id, from chat.headers' sessionID (otherwise a fixed id per run)

A 402, 403 or 429 whose body says credits or quota ran out goes on as a 429 "usage limit reached".

Models: the live config at GET /mavis/api/v1/models?region=&buildEnv=prod (asked again without auth on a 401/403), with mcode's built-in four as the fallback: MiniMax-M3.1-Flash-Preview, MiniMax-M3, MiniMax-M2.7 and MiniMax-M2.7-highspeed. Variants follow what mcode sends: adaptive thinking at an effort (output_config.effort), and none to turn thinking off.

Usage: mcode's signed account API: user info, the type-0 workspace from get_user_extra_info, then get_membership_info. The card shows credits left (op_credit_summary / opcredit_balance), Free or "M Plan <tier>" with its end, and, for an M Plan, coding_plan/remains with X-Group-Id. That is read as magpie's readMiniMaxPlan reads it. There is no check-in.

Tests: fakes only. nonet.mjs is imported first, so only 127.0.0.1 can be reached. They cover:
- sign-in, the variant, refused, and other errors
- refresh, a three-way race plus a late stale reader, per-account refreshes, invalid_grant, other failures, and 401 then refresh then retry
- request headers and chat.headers
- credits running out going on as a 429
- the live list, the retry without auth, and the fallback (the given object back)
- signing, against fixed vectors and the bundle's formula
- usage parsing (readMiniMaxPlan's own fixture), Free, M Plan, and a refused account

bun test packages/minimax (27 pass) and bun scripts/check.mjs pass.

End to end through magpie: origin/main b0486663, built with -tags nogui in a worktree since removed, under env -i with a sandbox HOME, a fake upstream on 127.0.0.1:47811, and a copy of the package with its hosts pointed there:
- plugin add
- plugin login minimax-code: the device flow and the account named; a second login of the same account merged into one
- the live list: 2 models
- provider test minimax-code: the chat went out with the headers above
- quota: M Plan Plus, 321.5 credits, 5 hours and 7 days windows
- an expired token refreshed and saved

No request was made to MiniMax.
2026-10-02 22:38:41 +08:00

101 lines
6.2 KiB
JavaScript

// The usage card: the credits left, the M Plan's tier and end, and its
// windows read as magpie's own MiniMax plan reader (readMiniMaxPlan) reads
// them. Nothing is claimed: no check-in is made.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { MiniMaxCodeAuthPlugin, _internal } from "./index.mjs"
import { fakeMiniMax, json } from "./fake.mjs"
let f
afterEach(() => f?.close())
const h = 3600 * 1000
// readMiniMaxPlan's own test (magpie internal/provider/planquota_test.go)
test("plan windows are read as magpie's MiniMax plan reader reads them", () => {
const ws = _internal.planWindows({
model_remains: [
{ model_name: "general", start_time: 1790800000000, end_time: 1790800000000 + 5 * h, current_interval_remaining_percent: 72, current_interval_status: 1, current_interval_total_count: 0,
weekly_start_time: 1790500000000, weekly_end_time: 1790500000000 + 168 * h, current_weekly_remaining_percent: 90, current_weekly_status: 1, current_weekly_total_count: 0 },
{ model_name: "video", start_time: 1790800000000, end_time: 1790800000000 + 24 * h, current_interval_remaining_percent: 40, current_interval_status: 2, current_interval_total_count: 3,
current_weekly_status: 3, current_weekly_total_count: 0 },
{ model_name: "music", current_interval_remaining_percent: 100, current_interval_status: 3, current_interval_total_count: 0,
current_weekly_remaining_percent: 100, current_weekly_status: 3, current_weekly_total_count: 0 },
],
base_resp: { status_code: 0, status_msg: "success" },
})
expect(ws.length).toBe(3)
expect(ws[0]).toEqual({ name: "5 hours", used: 28, span: 5 * 3600, resetsAt: new Date(1790800000000 + 5 * h).toISOString() })
expect(ws[1]).toMatchObject({ name: "7 days", used: 10, span: 7 * 86400 })
expect(ws[1].aside).toBeUndefined()
expect(ws[2]).toMatchObject({ name: "Video · 24 hours", used: 100, aside: true })
expect(() => _internal.planWindows({ base_resp: { status_code: 1004, status_msg: "login fail" } })).toThrow("login fail")
expect(_internal.planWindows({ model_remains: [], base_resp: { status_code: 0 } })).toEqual([])
})
test("a membership is read from the answer or its data", () => {
expect(_internal.membershipOf({ data: { has_token_plan: true, op_group_id: "g1", token_plan_tier: "Plus", token_plan_expires_at: 1800000000000, op_credit_summary: { total_remaining_amount: "1234.50" } } }))
.toEqual({ hasTokenPlan: true, opGroupId: "g1", tier: "Plus", expiresAt: 1800000000000, balance: "1234.50" })
expect(_internal.membershipOf({ opcredit_balance: 300 })).toEqual({ balance: "300" })
expect(_internal.usageOf({ balance: "1234.50" })).toEqual({ plan: "Free", balance: "1234.5 credits", windows: [] })
expect(_internal.usageOf({ hasTokenPlan: true, tier: "Plus", expiresAt: 1800000000000, balance: "0" }))
.toEqual({ plan: "M Plan Plus", until: new Date(1800000000000).toISOString(), balance: "0 credits", windows: [] })
})
const auth = { type: "oauth", access: "tok", refresh: "r", expires: Date.now() + 3600_000, accountId: "a@example.com" }
function account(f, { plan = false } = {}) {
f.route("GET /v1/api/user/info", () => json({ statusInfo: { code: 0 }, data: { userInfo: { realUserID: "u-42", userEmail: "a@example.com" } } }))
f.route("POST /matrix/api/v1/user/get_user_extra_info", () => json({ statusInfo: { code: 0 }, data: { workspaces: [
{ workspace_id: 9, workspace_type: 1 },
{ workspace_id: 7, workspace_type: 0, op_group_id: "grp-7", opcredit_balance: "50" },
] } }))
f.route("POST /matrix/api/v1/commerce/get_membership_info", () => json({ statusInfo: { code: 0 }, data: plan
? { has_token_plan: true, token_plan_tier: "Plus", token_plan_expires_at: 1800000000000, op_credit_summary: { total_remaining_amount: "88.5" } }
: { has_token_plan: false, op_credit_summary: { total_remaining_amount: "1200" } } }))
f.route("GET /v1/api/openplatform/coding_plan/remains", () => json({ model_remains: [
{ model_name: "general", start_time: 1790800000000, end_time: 1790800000000 + 5 * h, current_interval_remaining_percent: 60, current_interval_status: 1,
weekly_start_time: 1790500000000, weekly_end_time: 1790500000000 + 168 * h, current_weekly_remaining_percent: 75, current_weekly_status: 1 },
], base_resp: { status_code: 0 } }))
}
test("a free account shows its credits", async () => {
f = fakeMiniMax()
account(f)
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => auth)
expect(u).toEqual({ plan: "Free", balance: "1200 credits", windows: [], user: "a@example.com", signIn: "kept" })
// its own workspace's membership was asked for, signed, with its user id
const m = f.seen.find((r) => r.path === "/matrix/api/v1/commerce/get_membership_info")
expect(m.json).toEqual({ workspace_id: 7 })
expect(m.query.get("user_id")).toBe("u-42")
expect(m.headers.get("x-signature")).toMatch(/^[0-9a-f]{32}$/)
// no plan, no windows asked for; and nothing claimed
expect(f.seen.some((r) => r.path.includes("coding_plan"))).toBe(false)
expect(f.seen.some((r) => r.path.includes("signin"))).toBe(false)
})
test("an M Plan shows its tier, its end and its windows", async () => {
f = fakeMiniMax()
account(f, { plan: true })
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => auth)
expect(u.plan).toBe("M Plan Plus")
expect(u.until).toBe(new Date(1800000000000).toISOString())
expect(u.balance).toBe("88.5 credits")
expect(u.windows.map((w) => [w.name, w.used])).toEqual([["5 hours", 40], ["7 days", 25]])
const q = f.seen.find((r) => r.path === "/v1/api/openplatform/coding_plan/remains")
expect(q.headers.get("x-group-id")).toBe("grp-7")
expect(q.headers.get("authorization")).toBe("Bearer tok")
})
test("the account refused says so, the sign-in kept", async () => {
f = fakeMiniMax()
f.route("GET /v1/api/user/info", () => json({ statusInfo: { code: 1000048, message: "login expired" } }))
const hooks = await MiniMaxCodeAuthPlugin({ client: {} })
const u = await hooks.auth.usage(async () => auth)
expect(u.error).toContain("refused")
expect(u.signIn).toBe("kept")
expect(await hooks.auth.usage(async () => undefined)).toEqual({ error: "not signed in" })
})