diff --git a/packages/trae/enterprise.test.mjs b/packages/trae/enterprise.test.mjs index 2bf5646..f4c5a03 100644 --- a/packages/trae/enterprise.test.mjs +++ b/packages/trae/enterprise.test.mjs @@ -16,11 +16,11 @@ const spent = (costs) => json({ code: 0, message: "success", user_usage_list: [{ // the consumer credits page as it answers an enterprise account: a real // allowance that real calls do not touch (measured), shown beside the pool const page = (over = {}) => json({ is_credits_billing: true, user_entitlement_pack_list: [ - { display_desc: "老用户福利", entitlement_base_info: { quota: { credits_limit: 4000 }, end_time: 1795012095 }, usage: { credits_amount: 4.396 } }, - { display_desc: "每月登录赠送", entitlement_base_info: { quota: { credits_limit: 500 }, end_time: 1796054399 }, usage: {} }, + { display_desc: "老用户福利", entitlement_base_info: { quota: { credits_limit: 4000 }, end_time: 1895012095 }, usage: { credits_amount: 4.396 } }, + { display_desc: "每月登录赠送", entitlement_base_info: { quota: { credits_limit: 500 }, end_time: 1896054399 }, usage: {} }, ...(over.extra ?? []), ] }) -const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1795012095 * 1000).toISOString() } +const creditsWindow = { name: "Credits", used: (4.396 / 4500) * 100, display: "4.4 / 4500", amount: 4.4, limit: 4500, unit: "credits", aside: true, resetsAt: new Date(1895012095 * 1000).toISOString() } // the console's own period, which the usage page reads its reset date from const RESET = 1796054400000 const core = (over = {}) => json({ code: 0, message: "", Data: { ChatUsed: 0, ChatTotal: 500, PayAsYouGoUsed: 0, PayAsYouGoTotal: 0, ResetTime: RESET, ChatStartTime: 1793462399000, ChatEndTime: 1796054399000, PayAsYouGoStartTime: 1793462399000, PayAsYouGoEndTime: 1796054399000, TotalSeats: 163, ...over }, count: 0 }) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index a285d99..c0080a3 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -3233,8 +3233,11 @@ const effortOf = (levels, effort) => { // ---- usage ------------------------------------------------------------------------ // credits adds up the account's entitlement packs as the IDE shows them: -// each pack with a credit limit (-1 unlimited) and what it used -function credits(v) { +// each pack with a credit limit (-1 unlimited) and what it used. until is +// the soonest end of a pack that still has credits left and has not ended: +// a spent daily pack or one already past its end changes nothing on the +// account, so it does not name the date the credits turn over (#718, #1157). +function credits(v, now = Date.now()) { let limit = 0 let used = 0 let unlimited = false @@ -3248,7 +3251,8 @@ function credits(v) { else if (l >= 0) limit += l if (u > 0) used += u const end = whenOf(bi.end_time ?? bi.expire_time ?? p.end_time) - if (end && (!soonest || end < soonest)) soonest = end + const left = l === -1 || (l > 0 && u < l) + if (end && end > now && left && (!soonest || end < soonest)) soonest = end } return { limit, used, unlimited, until: soonest } } diff --git a/packages/trae/package.json b/packages/trae/package.json index 35b5a27..f81affd 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.36", + "version": "0.2.37", "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in", "type": "module", "main": "./opencode.mjs", diff --git a/packages/trae/usage.test.mjs b/packages/trae/usage.test.mjs index 255a2ee..7ddb1bf 100644 --- a/packages/trae/usage.test.mjs +++ b/packages/trae/usage.test.mjs @@ -11,7 +11,7 @@ afterEach(() => f?.close()) test("credits are the packs' limits and what they used", async () => { f = fakeTrae() f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ is_credits_billing: false, user_entitlement_pack_list: [ - { entitlement_base_info: { quota: { credits_limit: 300 }, end_time: 1790000000 }, usage: { credits_amount: 75 } }, + { entitlement_base_info: { quota: { credits_limit: 300 }, end_time: 4102444800 }, usage: { credits_amount: 75 } }, { entitlement_base_info: { quota: {} }, usage: {} }, // a feature pack, no credits { entitlement_base_info: { quota: { credits_limit: 100 } }, usage: { credits_amount: 25 } }, ] })) @@ -23,7 +23,7 @@ test("credits are the packs' limits and what they used", async () => { expect(u.user).toBe("Ann") expect(u.signIn).toBe("kept") expect(u.balance).toBeUndefined() // the window carries the count (yetone/magpie#694) - expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(1790000000 * 1000).toISOString() }]) + expect(u.windows).toEqual([{ name: "Credits", used: 25, amount: 100, limit: 400, unit: "credits", resetsAt: new Date(4102444800 * 1000).toISOString() }]) }) test("unlimited packs say so", () => { @@ -364,3 +364,27 @@ test("a reply limit is Trae's own or none, and a request is held to Trae's", asy expect(await ask("glm-5.2", 8000)).toBe("8000") expect(await ask("deepseek-v4.1-flash", 384000)).toBe("384000") }) + +// the date the credits turn over is a pack's that still counts: a spent +// pack, or one already past its end, names nothing (#718, #1157) +test("the soonest end is a live pack's", () => { + const now = Date.parse("2026-11-14T12:00:00Z") + const sec = (iso) => Date.parse(iso) / 1000 + const pack = (limit, used, end) => ({ entitlement_base_info: { quota: { credits_limit: limit }, end_time: sec(end) }, usage: { credits_amount: used } }) + const c = _internal.credits({ user_entitlement_pack_list: [ + pack(300, 300, "2026-11-14T16:00:38Z"), // today's daily pack, spent + pack(500, 10, "2026-11-13T16:00:00Z"), // yesterday's, ended with credits left + pack(3000, 0, "2026-11-30T15:59:59Z"), // the monthly pack + ] }, now) + expect(c.until).toBe(Date.parse("2026-11-30T15:59:59Z")) + expect(c.limit).toBe(3800) + expect(c.used).toBe(310) + // the daily pack with credits left is the soonest again + const d = _internal.credits({ user_entitlement_pack_list: [pack(300, 100, "2026-11-14T16:00:38Z"), pack(3000, 0, "2026-11-30T15:59:59Z")] }, now) + expect(d.until).toBe(Date.parse("2026-11-14T16:00:38Z")) + // an unlimited pack counts as one with credits left + const u = _internal.credits({ user_entitlement_pack_list: [pack(-1, 5, "2026-11-20T00:00:00Z"), pack(3000, 0, "2026-11-30T15:59:59Z")] }, now) + expect(u.until).toBe(Date.parse("2026-11-20T00:00:00Z")) + // nothing live: no date at all + expect(_internal.credits({ user_entitlement_pack_list: [pack(300, 300, "2026-11-20T00:00:00Z")] }, now).until).toBe(0) +})