Files
狂気のMad Scientist鳳凰院胸針 848497c40b orcaterm 0.1.1: QR sign-in null fields, OAuth quota, stable account identity (#510)
orcaterm 0.1.1 (hg1048596-pixel):
- WeChat QR sign-in accepts the official bridge's null fields.
- QR accounts read their AI quota through orcaterm.com's product API, with the token sent as the ot_session cookie only to https://orcaterm.com/cgi/api and redirects refused.
- A QR re-sign-in reuses the same account, keyed by the stable OrcaTerm user id.
- The README (en and zh) lists every host and what each one receives.

Run at the reviewed head a9ab6853b (Mac mini, Bun 1.4.2): `bun test` in packages/orcaterm passed 101 of 101 with every non-loopback fetch and socket refused (none was attempted), and `bun scripts/check.mjs orcaterm` passed.

Broken by hand at fa028746c, the last code change, each going red: the /cb null check, accountId on the QR result, and redirect: "error".
2026-10-30 19:39:18 +08:00

131 lines
5.3 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([])
})
const oauthToken = `${Buffer.from('{"alg":"HS256","typ":"JWT"}').toString("base64url")}.${Buffer.from('{"userId":"test-user","providerUsername":"QR tester"}').toString("base64url")}.FAKE_SIGNATURE_ONLY`
test("QR accounts read quota using ot_session only on orcaterm.com", async () => {
const seen = []
globalThis.fetch = async (url, init) => {
seen.push({ url: String(url), init })
const action = JSON.parse(init.body).action
if (action === "GetUserInfo") return new Response(JSON.stringify({ data: { Response: { nickName: "QR tester" } } }))
return billing([{ feature: "hunyuan_ai", quotaLimit: 1000, quotaUsed: 0, quotaEndDate: "2026-11-21T16:00:00.000Z" }])
}
const hooks = await OrcaTermAuthPlugin()
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: oauthToken }))
expect(u.windows).toHaveLength(1)
expect(u.windows[0].amount).toBe(0)
expect(u.windows[0].limit).toBe(1000)
expect(u.windows[0].resetsAt).toBe("2026-11-21T16:00:00.000Z")
expect(u.user).toBe("QR tester")
expect(seen).toHaveLength(2)
for (const { url, init } of seen) {
expect(new URL(url).origin).toBe("https://orcaterm.com")
expect(new URL(url).pathname).toBe("/cgi/api")
expect(url.includes(oauthToken)).toBe(false)
const h = new Headers(init.headers)
expect(h.get("cookie")).toBe(`ot_session=${oauthToken}`)
expect(h.get("authorization")).toBeNull()
expect(h.get("x-csrfcode")).toBeNull()
expect(init.redirect).toBe("error")
}
})
test("an explicitly refused OAuth quota read marks that account expired", async () => {
const seen = []
globalThis.fetch = async url => {
seen.push(new URL(url).origin)
return new Response(JSON.stringify({ data: { Response: { Error: { Code: "VERIFY_USER_FAILED" } } } }))
}
const hooks = await OrcaTermAuthPlugin()
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: oauthToken }))
expect(u.signIn).toBe("expired")
expect(u.windows).toEqual([])
expect(seen).toEqual(["https://orcaterm.com"])
})
test("OAuth identity lookup failure preserves the token's display name", async () => {
globalThis.fetch = async (url, init) => {
if (JSON.parse(init.body).action === "GetUserInfo") throw new Error("identity unavailable")
return billing([{ feature: "hunyuan_ai", quotaLimit: 1000, quotaUsed: 10 }])
}
const hooks = await OrcaTermAuthPlugin()
const u = await hooks.auth.usage(async () => ({ type: "oauth", access: oauthToken }))
expect(u.user).toBe("QR tester")
expect(u.windows[0].amount).toBe(10)
})