Files
plugins/packages/orcaterm/usage.test.mjs
T
狂気のMad Scientist鳳凰院胸針 fdc5002319 orcaterm 0.1.0: Tencent Cloud OrcaTerm's assistant as a provider, with WeChat QR sign-in (#460)
New package @magpie-community/opencode-orcaterm-auth 0.1.0: Tencent Cloud
OrcaTerm's own AI assistant (its monthly question allowance) as provider
`orcaterm`, with WeChat QR sign-in (the same sign-in OrcaTerm's clients
use) or a pasted console cookie. Credentials go only to Tencent's hosts
(orcaterm.com, *.cloud.tencent.com) and WeChat's widget (open.weixin.qq.com,
res.wx.qq.com). The nine models OrcaTerm's picker offers are listed;
others resolve only as aliases. Per-account pacing at 10 chats/min,
maxConcurrency 1; a JSON or SSE refusal inside a 200 becomes 429/401/502.
registry.json gains its entry.

Author: hg1048596-pixel (#460, replaces #422)

The last review's three points are fixed at this head: the index.mjs
header, toUpstreamBody and plugin-entry comments describe the code; the
README's License links the package's own LICENSE; /cb accepts a bridge
error without a code and shows it, and a later scan still finishes.

Run at a75a2ae0fb554b3cb6ae879d5571b5dd7b2300a7 on mote (macOS, Bun 1.4.2),
HOME=$(mktemp -d) as CI:
- bun test packages/orcaterm: 88 pass, 0 fail
- bun test (whole repo): 3624 pass, 8 skip, 0 fail
- bun scripts/check.mjs: every package ok, orcaterm included
- npm pack: 5 files (index.mjs, wechat.mjs, README.md, LICENSE,
  package.json); the extracted index.mjs imports with bun and its plugin
  returns provider orcaterm; node scripts/dist.mjs packs
  opencode-orcaterm-auth@0.1.0
- The earlier review's fake-upstream script against the packed package:
  27 of 27 (JSON 429 with and without rate-limit words, SSE quota, rate
  and sign-in errors, envelopes in stream and JSON mode, per-account
  pacing, bearer headers, nine listed models, credentials only to
  lightai.cloud.tencent.com)
- The /cb script against the packed wechat.mjs: foreign, missing and
  rebinding origins 403, wrong state refused, right origin and state
  finish the sign-in
Broken by hand: requiring a string code again turns "callback preserves
bridge errors without a code" red (87 pass, 1 fail).
Not tried: a real OrcaTerm account, so neither the QR sign-in nor the real
upstream.
2026-10-30 00:44:34 +08:00

77 lines
2.8 KiB
JavaScript

// The quota magpie shows comes from DescribeUserBillingStatus's usageStats,
// where only hunyuan_ai is the AI-answer allowance. The other rows there
// (session sharing, secrets, space) are unrelated, and reporting one of
// them as the AI window would show a number that never moves.
import { afterEach, expect, test } from "bun:test"
import { OrcaTermAuthPlugin } from "./index.mjs"
// The suite fires far more than the account's ten-a-minute budget; lift the
// pacer so these tests measure the plugin, not the limiter.
process.env.ORCATERM_NO_PACE = "1"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const usage = async (reply) => {
globalThis.fetch = async () => reply()
const hooks = await OrcaTermAuthPlugin({ client: { app: { log: () => {} } } })
return hooks.auth.usage(async () => ({ type: "api", key: "uin=o1234567890; skey=deadbeef" }))
}
const billing = (usageStats) =>
new Response(JSON.stringify({ data: { Response: { roleType: "basic", usageStats } } }), {
status: 200,
headers: { "content-type": "application/json" },
})
test("the AI allowance is reported as used out of its limit", async () => {
const u = await usage(() =>
billing([
{ feature: "session_share", quotaLimit: -1, quotaUsed: 0 },
{ feature: "hunyuan_ai", quotaLimit: 1000, quotaUsed: 370 },
]),
)
expect(u.windows).toHaveLength(1)
expect(u.windows[0].name).toBe("AI Q&A")
expect(u.windows[0].amount).toBe(370)
expect(u.windows[0].limit).toBe(1000)
expect(u.windows[0].used).toBe(37)
expect(u.plan).toContain("basic")
})
// The unrelated rows must not be shown: session sharing's -1 limit would
// read as an exhausted account.
test("rows that are not the AI allowance are left out", async () => {
const u = await usage(() =>
billing([
{ feature: "session_share", quotaLimit: -1, quotaUsed: 0 },
{ feature: "secret", quotaLimit: 5, quotaUsed: 1 },
]),
)
expect(u.windows).toHaveLength(0)
expect(u.error).toBeTruthy()
})
// A refusal here is the sign-in's, and magpie should mark the account
// rather than keep asking it for quota.
test("a refused quota read marks the sign-in", async () => {
const u = await usage(
() =>
new Response(JSON.stringify({ data: { Response: { Error: { Code: "VERIFY_SKEY_UIN_FAILED" } } } }), {
status: 200,
headers: { "content-type": "application/json" },
}),
)
expect(u.signIn).toBe("expired")
expect(u.windows).toEqual([])
})
// Reading quota must never throw: a broken answer would take down the
// account card instead of just leaving the number out.
test("an unreadable answer becomes an error string, not a throw", async () => {
const u = await usage(() => new Response("not json", { status: 200 }))
expect(typeof u.error).toBe("string")
expect(u.windows).toEqual([])
})