Files
plugins/packages/trae/refresh.test.mjs
T
yetone 6c048c7fc2 trae 0.1.0: Trae CN (trae.cn) accounts, free tier included, with Trae's browser sign-in (experimental; Discord pdzebkbd0701)
Sign-in is trae.cn's authorization page back to a 127.0.0.1 /authorize
callback (userJwt + userInfo, or a refresh token exchanged), with a device
made at sign-in and kept for the account. ExchangeToken renews the JWT
through auth.refresh (refreshLead 10 min, one renewal per refresh token).
Requests go to the IDE agent's llm_utils_chat (chat_v3, then
solo_work_lite on 4001/4023/1005), its SSE events turned into OpenAI
chunks; tools named natively and as <tool_call> blocks. Usage is the
entitlement packs' credits. Worked out from wangqi233/trae2api and
autumnsentiment/Trae2api-cn; not run against a real account.
2026-10-03 14:34:31 +08:00

60 lines
2.8 KiB
JavaScript

// Renewing the JWT with the refresh token, once at a time (Trae spends a
// refresh token once): magpie's auth.refresh, and the check before a
// request.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin } from "./index.mjs"
import { fakeTrae, json, signedIn, sse } from "./fake.mjs"
let f
afterEach(() => f?.close())
test("auth.refresh exchanges the refresh token, once for two asking", async () => {
f = fakeTrae()
let n = 0
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", async () => {
n++
await Bun.sleep(20)
return json({ Result: { Token: "jwt-2", RefreshToken: "r-2", TokenExpireAt: 2000000000 } })
})
const hooks = await TraeCNAuthPlugin({ client: {} })
expect(hooks.auth.refreshLead).toBe(10 * 60 * 1000)
const auth = signedIn({ expires: Date.now() + 60_000 })
const [x, y] = await Promise.all([hooks.auth.refresh(auth), hooks.auth.refresh(auth)])
expect(n).toBe(1)
expect(x).toEqual({ access: "jwt-2", refresh: "r-2", expires: 2000000000 * 1000 })
expect(y).toEqual(x)
// asked again with the sign-in from before, it gives the one it got
expect(await hooks.auth.refresh(auth)).toEqual(x)
expect(n).toBe(1)
})
test("a refresh token Trae turns away is signIn expired", async () => {
f = fakeTrae()
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", () => json({ ResponseMetadata: { Error: { Code: "10101", Message: "refresh token is not matched to the client" } } }, 400))
const hooks = await TraeCNAuthPlugin({ client: {} })
const e = await hooks.auth.refresh(signedIn()).catch((e) => e)
expect(e.signIn).toBe("expired")
expect(e.message).toContain("not matched")
})
test("a request with a token about to end renews it first and saves it", async () => {
f = fakeTrae()
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", () => json({ Result: { Token: "jwt-2", RefreshToken: "r-2", TokenExpireAt: Date.now() + 7200_000 } }))
let auth = null
f.route("POST /api/agent/v3/llm_utils_chat", (r) => {
auth = r.headers.get("authorization")
return sse([["output", { response: "hi" }], ["done", {}]])
})
const saved = []
const hooks = await TraeCNAuthPlugin({ client: { auth: { set: async (x) => saved.push(x) } } })
const opts = await hooks.auth.loader(async () => signedIn({ expires: Date.now() + 30_000 }))
const res = await opts.fetch(opts.baseURL + "/chat/completions", { method: "POST", body: JSON.stringify({ model: "glm-5", messages: [{ role: "user", content: "hi" }] }) })
expect(res.status).toBe(200)
expect(res.headers.get("X-Magpie-Sign-In")).toBe("renewed")
expect(auth).toBe("Cloud-IDE-JWT jwt-2")
expect(saved[0].body.access).toBe("jwt-2")
expect(saved[0].body.refresh).toBe("r-2")
expect(saved[0].body.deviceId).toBe("1234567890123456789")
})