codearts 0.3.11: OfficeAce routes by its total credit, not each bucket (#986)
An OfficeAce account is now routed by one 总积分 window: the total of every unexpired plan bucket (`skus[].quotas`) and activity bonus (`bonus_skus`). Each bucket stays visible as an `aside` detail, so a spent daily check-in bonus (每日签到有礼 0 / 100) no longer pauses an account with thousands of credits left elsewhere (#981). What changed (packages/codearts, 0.3.10 -> 0.3.11) - fetchOfficeAceUsage sums unexpired buckets into 总积分 (amount, limit, two-decimal rounding; sub-half-cent totals keep their raw value). Every per-bucket window is `aside`. - 总积分's `runsOutAt` is the soonest expiry among unexpired buckets that still hold credit, and `runsOut` is what those buckets hold on that Beijing day (soonestLapse, shared with #982), never more than the total's remainder. Trial and spent buckets never choose the expiry. - Overspent buckets are capped at their own total; an empty or unparseable `expired_time` is no expiry instead of Invalid Date. - When every bucket has expired, a used-up 总积分 (0 / total, used 100) stays so the account is paused, not routed without a limit. - README (en, zh) says the same. Rebased ontodea9e4e3(0.3.10, #998): merge-base is origin/main, and the diff touches only the OfficeAce usage hunks, the tests, the README and the version. The international station signing, the Tokens window, the state.vscdb temp copy and the statement finalize are unchanged. What was run at the reviewed head 4c454b99 (mica, macOS, Bun 1.4.2, HOME=$(mktemp -d)) - bun test packages/codearts: 201 tests; green except a pre-existing flake on main, "an IDE sign-in is renewed by reading the IDE's file again" (red 3 of 6 on origin/main4b0aef53, 2 of 6 here; cause found and injected, filed as #1007). - bun scripts/check.mjs and check.mjs codearts: exit 0. - Root bun test under suite.lock: 8321 pass, 20 skip, 0 fail. Broken by hand, each restored before the next, each red: - a spent bucket choosing runsOutAt (the open item from the last review): "a spent OfficeAce daily bonus does not pause credits left in other buckets"; - runsOut as one bucket instead of the Beijing day's sum; runsOutAt from the latest expiry instead of the soonest; - details not aside; no 总积分 window; an expired bonus counted; no used-up total when all expired; - 0.3.10: a credential signed for every station's hosts; reading state.vscdb in place instead of a copy; no Tokens window. Not tried - No real Huawei Cloud / OfficeAce account; every response is a fake fetch. - Windows: the diff doesn't touch the state.vscdb path, so the codearts tests weren't run there for this change. Refs #981, #982 Co-authored-by: zhou.han <18181754+yetihan@users.noreply.github.com>
This commit is contained in:
+78
-18
@@ -3941,17 +3941,18 @@ function officeAceLiveModels(rows, baseUrl, rates) {
|
||||
* One usage window.
|
||||
*
|
||||
* An expired bucket is folded away (`aside`) and carries no amount/limit: its
|
||||
* own total would become the denominator and read as "100% left". It takes no
|
||||
* part in routing either, so the fold is all it needs to say.
|
||||
* own total would become the denominator and read as "100% left". Active
|
||||
* package details keep their numbers but are also `aside`; only the aggregate
|
||||
* window may pause the account.
|
||||
*/
|
||||
function officeAceWindow({ name, used, display, unit, resetsAt = "", expired = false, amount = 0, limit = 0 }) {
|
||||
function officeAceWindow({ name, used, display, unit, resetsAt = "", expired = false, aside = false, amount = 0, limit = 0 }) {
|
||||
return {
|
||||
name,
|
||||
used,
|
||||
display,
|
||||
unit,
|
||||
...(resetsAt ? { resetsAt } : {}),
|
||||
...(expired ? { aside: true } : { amount, limit, span: 0 }),
|
||||
...(expired ? { aside: true } : { amount, limit, span: 0, ...(aside ? { aside: true } : {}) }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3960,8 +3961,9 @@ function officeAceWindow({ name, used, display, unit, resetsAt = "", expired = f
|
||||
*
|
||||
* Two kinds of bucket: `skus[].quotas[]` (`sku_attr_code === 'officeace_points'`)
|
||||
* and `bonus_skus[]`. A `.trial` sku carries no expiry (the trial never
|
||||
* expires — show it as it is, never guess a date); an expired bucket is marked
|
||||
* `aside` so the card folds it away instead of letting it hold the top slot.
|
||||
* expires — show it as it is, never guess a date). Only the aggregate of
|
||||
* unexpired buckets participates in routing; package details are `aside`, and
|
||||
* expired buckets are folded away without routing amounts.
|
||||
*/
|
||||
async function fetchOfficeAceUsage(cred, fetcher = fetch) {
|
||||
const r = await officeAceSigned(cred, `${OA_SUBSCRIPTION_BASE}${OA_SUBSCRIPTION_PATH}`, "GET", undefined, fetcher)
|
||||
@@ -3977,9 +3979,18 @@ async function fetchOfficeAceUsage(cred, fetcher = fetch) {
|
||||
}
|
||||
|
||||
const round = (n) => Math.round(n * 100) / 100
|
||||
const expiryOf = (value, ignored = false) => {
|
||||
if (ignored || typeof value !== "string" || value.length === 0) return undefined
|
||||
const at = Date.parse(value)
|
||||
return Number.isFinite(at) ? at : undefined
|
||||
}
|
||||
const windows = []
|
||||
const nowMs = Date.now()
|
||||
let plan = ""
|
||||
let aggregateAmount = 0
|
||||
let aggregateLimit = 0
|
||||
let expiredLimit = 0
|
||||
const aggregateBatches = []
|
||||
|
||||
for (const sku of Array.isArray(j?.skus) ? j.skus : []) {
|
||||
for (const q of Array.isArray(sku?.quotas) ? sku.quotas : []) {
|
||||
@@ -3988,19 +3999,28 @@ async function fetchOfficeAceUsage(cred, fetcher = fetch) {
|
||||
const used = Number(q.current_value)
|
||||
if (!Number.isFinite(total) || total <= 0) continue
|
||||
const isTrial = String(sku.sku_code ?? "").endsWith(".trial")
|
||||
const expiresAt = typeof q.expired_time === "string" && q.expired_time.length > 0 ? q.expired_time : null
|
||||
const remaining = Math.max(0, total - used)
|
||||
const expired = expiresAt !== null && !isTrial && new Date(expiresAt).getTime() <= nowMs
|
||||
const expiresAt = expiryOf(q.expired_time, isTrial)
|
||||
const amount = Math.min(total, Math.max(0, Number.isFinite(used) ? used : 0))
|
||||
const remaining = total - amount
|
||||
const expired = expiresAt !== undefined && expiresAt <= nowMs
|
||||
if (!plan && typeof sku.sku_name === "string") plan = sku.sku_name
|
||||
if (expired) {
|
||||
expiredLimit += total
|
||||
} else {
|
||||
aggregateAmount += amount
|
||||
aggregateLimit += total
|
||||
if (remaining > 0 && expiresAt !== undefined) aggregateBatches.push({ left: remaining, expiresAt })
|
||||
}
|
||||
windows.push(officeAceWindow({
|
||||
name: `${sku.sku_name ?? "套餐"} · 剩 ${round(remaining)}`,
|
||||
used: Math.min(100, Math.max(0, Math.round((used / total) * 100))),
|
||||
used: Math.min(100, Math.max(0, Math.round((amount / total) * 100))),
|
||||
// a `.trial` sku carries no expiry: nothing to invent
|
||||
resetsAt: isTrial || expiresAt === null ? "" : new Date(expiresAt).toISOString(),
|
||||
resetsAt: expiresAt === undefined ? "" : new Date(expiresAt).toISOString(),
|
||||
display: `${round(remaining)} / ${round(total)}`,
|
||||
unit: "积分",
|
||||
expired,
|
||||
amount: used,
|
||||
aside: true,
|
||||
amount,
|
||||
limit: total,
|
||||
}))
|
||||
}
|
||||
@@ -4010,21 +4030,61 @@ async function fetchOfficeAceUsage(cred, fetcher = fetch) {
|
||||
const points = Number(bonus?.points)
|
||||
if (!Number.isFinite(points) || points <= 0) continue
|
||||
const used = Number(bonus.current_value)
|
||||
const remaining = Math.max(0, points - (Number.isFinite(used) ? used : 0))
|
||||
const expiresAt = typeof bonus.expired_time === "string" ? bonus.expired_time : null
|
||||
const expired = expiresAt !== null && new Date(expiresAt).getTime() <= nowMs
|
||||
const amount = Math.min(points, Math.max(0, Number.isFinite(used) ? used : 0))
|
||||
const remaining = points - amount
|
||||
const expiresAt = expiryOf(bonus.expired_time)
|
||||
const expired = expiresAt !== undefined && expiresAt <= nowMs
|
||||
if (expired) {
|
||||
expiredLimit += points
|
||||
} else {
|
||||
aggregateAmount += amount
|
||||
aggregateLimit += points
|
||||
if (remaining > 0 && expiresAt !== undefined) aggregateBatches.push({ left: remaining, expiresAt })
|
||||
}
|
||||
windows.push(officeAceWindow({
|
||||
name: `${bonus.activity_name ?? "活动奖励"} · 剩 ${round(remaining)}`,
|
||||
used: Math.min(100, Math.round(((points - remaining) / points) * 100)),
|
||||
resetsAt: expiresAt === null ? "" : new Date(expiresAt).toISOString(),
|
||||
used: Math.min(100, Math.round((amount / points) * 100)),
|
||||
resetsAt: expiresAt === undefined ? "" : new Date(expiresAt).toISOString(),
|
||||
display: `${round(remaining)} / ${round(points)}`,
|
||||
unit: "积分",
|
||||
expired,
|
||||
amount: points - remaining,
|
||||
aside: true,
|
||||
amount,
|
||||
limit: points,
|
||||
}))
|
||||
}
|
||||
|
||||
// An account whose every bucket expired still needs one used-up routing
|
||||
// window. Without it every detail is aside and magpie would keep routing.
|
||||
const active = aggregateLimit > 0
|
||||
const rawLimit = active ? aggregateLimit : expiredLimit
|
||||
if (rawLimit > 0) {
|
||||
const rawAmount = active ? Math.min(rawLimit, aggregateAmount) : rawLimit
|
||||
const rawRemaining = Math.max(0, rawLimit - rawAmount)
|
||||
const roundedLimit = round(rawLimit)
|
||||
// Keep sub-half-cent values instead of turning the routing denominator
|
||||
// into zero. Normal credit values remain rounded to two decimals.
|
||||
const keepRaw = roundedLimit === 0
|
||||
const limit = keepRaw ? rawLimit : roundedLimit
|
||||
const amount = keepRaw ? rawAmount : Math.min(limit, round(rawAmount))
|
||||
const remaining = keepRaw ? rawRemaining : round(Math.max(0, limit - amount))
|
||||
const total = officeAceWindow({
|
||||
name: `总积分 · 剩 ${remaining}`,
|
||||
used: Math.min(100, Math.max(0, Math.round((rawAmount / rawLimit) * 100))),
|
||||
display: `${remaining} / ${limit}`,
|
||||
unit: "积分",
|
||||
amount,
|
||||
limit,
|
||||
})
|
||||
const lapse = active ? soonestLapse(aggregateBatches, nowMs) : undefined
|
||||
if (lapse && rawRemaining > 0) {
|
||||
const runsOut = Math.min(lapse.credit, rawRemaining)
|
||||
total.runsOutAt = new Date(lapse.at).toISOString()
|
||||
total.runsOut = round(runsOut) || runsOut
|
||||
}
|
||||
windows.unshift(total)
|
||||
}
|
||||
|
||||
return { plan, windows }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user