Files
plugins/packages/trae/checkin.test.mjs
T
yetone 709e4dc79b trae 0.1.7: the daily check-in is sent as Trae CN's IDE sends it (Discord, ARNO on magpie v0.1.813: trae-cn的签到功能会失败) — ownRequest sent api.trae.cn's check-in pages with the chat's headers (x-uid, x-app-id, X-Cloudide-Token, x-ide-token, x-machine-id, x-ide-version… as TRAE SOLO CN's ai-agent sends llm_utils_chat) and the body magpie gave ({}). Trae CN 3.3.104's own client (out/main.js, eb()/fb(), from the linux tarball the version API names) posts /trae/api/v2/ug/checkin_credits/status and /claim with {req_source: 1} (2 only for SOLO Lite/enterprise) and only Content-Type, Authorization: Cloud-IDE-JWT and the device's x-device-id, x-device-brand, x-device-type, x-os-version, x-app-version. Pages under /trae/api/v2/ug/ now go with those headers (ugHeaders) and {req_source:1} where none or {} was given (ugBody); Trae's other pages are sent as before. Trae's 9074 (当前参与用户太多) is a device-level risk control that both open-source check-in clients say trips on requests that don't look like the client's; whether that is what ARNO saw is not known (no error text, and no Trae CN account to try with). checkin.test.mjs gains "the check-in is sent as Trae CN's IDE sends it" (body and header set; fails on 0.1.6); bun test (30 pass) and bun scripts/check.mjs trae under a temp HOME pass; the plugin's fetch against the real api.trae.cn with a placeholder token answers 200 code 1001 marked expired, so the route is reached. Not tried with a real Trae CN account.
2026-10-04 13:12:51 +08:00

83 lines
4.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.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
expect(r.headers.get("x-device-id")).toBe("1234567890123456789")
}
})
// Trae CN's IDE (3.3.104, out/main.js eb()/fb()) posts the check-in with
// {req_source: 1} and only the JWT and the device's headers; the chat's
// own (x-uid, x-app-id, x-ide-token …) aren't among them
test("the check-in is sent as Trae CN's IDE sends it", 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 }))
const o = await fetcher()
await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/status", { method: "POST", body: "{}" })
await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST" })
expect(f.seen.length).toBe(2)
for (const r of f.seen) {
expect(r.json).toEqual({ req_source: 1 })
const names = [...r.headers.keys()].filter((k) => k === "authorization" || k.startsWith("x-")).sort()
expect(names).toEqual(["authorization", "x-app-version", "x-device-brand", "x-device-id", "x-device-type", "x-os-version"])
expect(r.headers.get("content-type")).toBe("application/json")
}
})
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([])
})