Files
qiuhao-wang001andqiuhao-wang001 907847bdf5 trae 0.2.34: an enterprise account's usage is its console's seat pool (#1040)
An enterprise Trae CN sign-in names console.enterprise.trae.cn. For such an account, the usage card now reads the console's seat pool:
- the limit is seat_pool_currency_quota, where 9999999999 means unlimited;
- the spend is this seat's total_cost_currency from get_user_model_usage;
- the reset date is get_personal_core_data's ResetTime.

The consumer credits row stays beside it, marked aside, so magpie does not route on it. If the spend can't be read, the card shows "? / ¥800.00" with no share; it never reads as ¥0. The Trae Global console path is unverified: a refusal there is an error card and keeps the sign-in.

Run at b96d0e58 (mote, temp HOME, fake server only):
- bun test in packages/trae: 262 pass, 0 fail.
- bun scripts/check.mjs: exit 0.
- git merge-tree against origin/main 3632d53b: clean.
- 15 probe cases, among them a spend read failing with 500, 401, an error code, a list under data, and the seat's own row missing.

Broken by hand, one at a time: 10 changes. All but the error label turned a test red: the unknown spend, Global's sign-in being kept, CN's console 401, own-seat summing, a null pool, and whenOf reset times.

Not tried: a real Trae account, the real console, and the GUI card.

Co-authored-by: qiuhao-wang001 <qiuhao-wang001@users.noreply.github.com>
2026-11-12 14:32:29 +08:00

61 lines
2.6 KiB
JavaScript

// A fake of Trae for the tests: one local server standing in for the
// realm's authorization page host and its other hosts, which the plugin's
// HOSTS are pointed at. route(method, path) answers a request; seen lists
// what came. The CN realm is the default, so a test written before the two
// realms shared a package is unchanged; pass "trae-global" for the
// international one, whose hosts and usage page differ.
import { _internal } from "./index.mjs"
const HOST_KEYS = ["web", "auth", "api", "console", "us", "pay", "usPay"]
// no test reads this machine's own Trae CN client: one that wants a
// client points ownClient.paths at a file of its own
_internal.ownClient.paths = () => []
export function fakeTrae(siteId = "trae-cn") {
const routes = new Map()
const seen = []
const server = Bun.serve({
port: 0,
hostname: "127.0.0.1",
async fetch(req) {
const url = new URL(req.url)
const text = await req.text()
const r = { method: req.method, path: url.pathname, query: url.searchParams, headers: req.headers, text }
if (text) try { r.json = JSON.parse(text) } catch {}
seen.push(r)
const h = routes.get(req.method + " " + url.pathname)
if (!h) return new Response("no route " + req.method + " " + url.pathname, { status: 404 })
return h(r)
},
})
const origin = `http://127.0.0.1:${server.port}`
const hosts = _internal.SITES[siteId].hosts
const was = { ...hosts }
Object.assign(hosts, { web: origin, auth: origin, api: origin, console: origin, us: origin, pay: origin, usPay: origin })
return {
origin,
site: _internal.SITES[siteId],
seen,
route: (key, h) => routes.set(key, h),
close() {
server.stop(true)
for (const k of HOST_KEYS) if (was[k] === undefined) delete hosts[k]
Object.assign(hosts, was)
},
}
}
export const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "content-type": "application/json" } })
// sse is an answer in Trae's events: [event, data] pairs
export const sse = (events, status = 200) =>
new Response(events.map(([e, d]) => `event: ${e}\ndata: ${typeof d === "string" ? d : JSON.stringify(d)}\n\n`).join(""), { status, headers: { "content-type": "text/event-stream" } })
export const jwtOf = (claims) => ["e30", Buffer.from(JSON.stringify(claims)).toString("base64url"), "sig"].join(".")
export const signedIn = (over = {}) => ({
type: "oauth", access: "jwt-1", refresh: "r-1", expires: Date.now() + 3600_000, accountId: "Ann", uid: "u-1",
clientId: "ono9krqynydwx5", deviceId: "1234567890123456789", machineId: "ab".repeat(16), ...over,
})