Files
plugins/packages/trae/checkin.test.mjs
T
yetone 503ad3eed3 trae 0.1.4: the account's fetch sends Trae CN's own pages on api.trae.cn as the account, for magpie's daily check-in (yetone/magpie#694, Hu9956: TRAE cn 也有每天签到送100积分) — the loader's fetch served chat completions only (400 for anything else), so magpie had no way to ask Trae as a signed-in account. A URL on api.trae.cn under /trae/api/ (ownPage) is now sent as it is, with the IDE's headers (Authorization: Cloud-IDE-JWT, x-device-id, x-uid, x-app-id …; the token renewed first when near its end, as a chat is) and the body given ({} when none), and Trae's answer comes back as it is, X-Magpie-Sign-In expired on a 401 or code 1001, renewed or kept otherwise; any other URL that isn't a chat is still refused. magpie asks POST /trae/api/v2/ug/checkin_credits/status, then /claim, both with {} — the endpoints autumnsentiment/Trae2api-cn (whose protocol this plugin follows) and qilimixingkong/trae-checkin both call; an unauthenticated POST to /status answers 200 {code:1001, enable:false, checked_in:false}, and a made-up sibling path 404s. Code 9095 (this device is in today) is left as Trae's answer: nothing sends another device id to get round it. checkin.test.mjs (status and claim sent as the account and answered as they are; 1001 marks the sign-in; a token near its end renewed first; another path refused with nothing sent); bun test (28 pass) and scripts/check.mjs trae pass. Not run with a real Trae CN account: none was signed in to try with. README gains a Daily check-in section.
2026-10-04 03:44:28 +08:00

66 lines
3.4 KiB
JavaScript

// The daily check-in (签到): magpie asks Trae CN's own pages on
// api.trae.cn (/trae/api/v2/ug/checkin_credits/status, then /claim)
// through the account's fetch, which sends them as the account.
import "./nonet.mjs"
import { afterEach, expect, test } from "bun:test"
import { TraeCNAuthPlugin } from "./index.mjs"
import { fakeTrae, json, signedIn } from "./fake.mjs"
let f
afterEach(() => f?.close())
async function fetcher(auth = signedIn(), client = {}) {
const hooks = await TraeCNAuthPlugin({ client })
return hooks.auth.loader(async () => auth)
}
test("a check-in page is sent as the account, its answer back as it is", async () => {
f = fakeTrae()
f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 0, enable: true, checked_in: false, credits: 100 }))
f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json({ code: 0, credits: 100 }))
const o = await fetcher()
const st = await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", headers: { authorization: "Bearer trae" }, body: "{}" })
expect(st.status).toBe(200)
expect(st.headers.get("x-magpie-sign-in")).toBe("kept")
expect(await st.json()).toEqual({ code: 0, enable: true, checked_in: false, credits: 100 })
const cl = await o.fetch(new Request(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST", body: "{}" }))
expect(await cl.json()).toEqual({ code: 0, credits: 100 })
expect(f.seen.map((r) => r.path)).toEqual(["/trae/api/v2/ug/checkin_credits/status", "/trae/api/v2/ug/checkin_credits/claim"])
for (const r of f.seen) {
expect(r.method).toBe("POST")
expect(r.json).toEqual({})
expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
expect(r.headers.get("x-device-id")).toBe("1234567890123456789")
expect(r.headers.get("x-uid")).toBe("u-1")
}
})
test("a token Trae turns away marks the sign-in", async () => {
f = fakeTrae()
f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 1001, enable: false, checked_in: false, message: "not able to authenticate you" }))
const o = await fetcher()
const st = await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", body: "{}" })
expect(st.headers.get("x-magpie-sign-in")).toBe("expired")
expect((await st.json()).code).toBe(1001)
})
test("a token near its end is renewed first", async () => {
f = fakeTrae()
f.route("POST /cloudide/api/v3/trae/oauth/ExchangeToken", () => json({ Result: { Token: "jwt-2", RefreshToken: "r-2", TokenExpireAt: Date.now() + 86400_000 } }))
f.route("POST /trae/api/v2/ug/checkin_credits/status", () => json({ code: 0, enable: true, checked_in: true, credits: 100 }))
const saved = []
const o = await fetcher(signedIn({ expires: Date.now() + 30_000 }), { auth: { set: async (x) => saved.push(x) } })
const st = await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", body: "{}" })
expect(st.headers.get("x-magpie-sign-in")).toBe("renewed")
expect(f.seen.at(-1).headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-2")
expect(saved[0].body.access).toBe("jwt-2")
})
test("other hosts' pages and other paths aren't sent as a page", async () => {
f = fakeTrae()
const o = await fetcher()
const r = await o.fetch(f.origin + "/cloudide/whatever", { method: "POST", body: "{}" })
expect(r.status).toBe(400) // not a chat: refused, nothing sent
expect(f.seen).toEqual([])
})