Ported from NorethSea's fork (abed2e224) and re-derived on main.
What it does:
- A known reset ends a dry run (a stretch where a window read 0% left), even
when the next poll sees the refill later. A stale dry reading taken after
its own reset no longer extends the run. Calls from the reset on are not
treated as "maybe paid from credits".
- A refill observed later than reset minus span
(QuotaPoint.StartsCycleAfter, the boundaries history sampling keeps)
starts what is reckoned. Calls before a fixed-deadline refill stay in the
cycle before it. A first reading is not a reset.
- Picking a past cycle in Usage > Allowances > Trends > Cycle asks
GET /api/usage/quotas/history/holds once per selection
(quotaCycleHoldsOf). The route asks no vendor. HistoricalWindowHolds
reckons each window at its own last reading in the cycle, using the
ledger and readings from before that reading. "Loading..." shows while it
is pending. An answer for a cycle no longer picked is dropped. The card's
Refresh, or an asked read, reckons again. The estimate's tooltip names the
window's start and the reading's time.
Changed from the fork:
- After a run closed at its reset, the fork reset lastLeft to zero. A
window that ran dry again before its next reset then opened a run from
time zero, which hid every window's estimate, the weekly one included.
The next run now starts no earlier than that reset.
TestWindowHoldsDryAgainAfterReset covers it (red with the fork's line).
- The fork narrowed the live card's Span to the observed refill. Span also
feeds provider.WithWindowStarts, so the window's Pace would have moved.
withSpansOf puts the card's spans back after reckoning. A span assertion
in TestWindowHoldsUsesObservedRefillStart covers it (red without it).
- The fork's historical snapshot dropped the card's Balance. That slipped
past the gate a live card has (holdsBounds), so a card that tells a
balance got past-cycle estimates. Balance is now kept. Covered in
TestHistoricalWindowHoldsUsesEachWindowsOwnReading (red without it). The
fork's fixture had a Codex card with a Balance, which no Codex card has.
- Main has since added HoldsWhy (#2045). The fork's own "Whole: not enough
reliable data" line and its .quota-holds-unknown CSS are left out. A past
cycle uses main's holdsUnknownLine and its reasons, as a live card does.
A window with neither an estimate nor a reason shows nothing, where the
fork showed its unknown line.
- withRecordedWindowStarts is ported to main's holdsBounds(start, end, why).
- quota-refresh-order.test.cjs's VM harness now stubs quotaCycleHolds,
which loadQuotas and quotaRefresh clear.
Left out: nothing else from the fork. Past prices aren't kept, so a past
cycle's cost uses today's API list prices (documented). Credits can't be
attributed per call, so none is attempted (documented).
What was run:
- gofmt -l: clean. go vet -tags nogui ./... for GOOS darwin, linux and windows.
- go test -tags nogui ./internal/usage ./internal/gui ./internal/provider:
ok.
- Each of the three fixes above was reverted by hand, and its test went red.
- GUI, Chromium and WebKit:
- quota-curve: 30/30. Also run at 1100 and 420 wide: 60/60 (all five
languages).
- Also: quota-*, i18n-keys, gui-de/ja/zh-tw, usage-card-refresh,
usage-refresh*, usage-stale-answer, usage-remote-cards,
usage-accounts*, usage-quota-head, usage-pack and usage-alert.
- Reds: quota-count-wrap and quota-name-translated are red in WebKit,
the same as on origin/main 707fbb249. Filed as #2228.
- The GUI break-check (main's !sub.history gate put back) turns the new
quota-curve test red.
- Full suite: SUITE_LINE
Co-authored-by: NorethSea <963979204@qq.com>
152 lines
7.3 KiB
JavaScript
152 lines
7.3 KiB
JavaScript
// Drive the shipped callbacks with held API replies. A background read and
|
|
// two different card refreshes may finish in any order (#1630).
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const { test } = require("node:test");
|
|
|
|
const source = fs.readFileSync(process.env.MAGPIE_APP_JS || path.resolve(__dirname, "../assets/app.js"), "utf8").replace(/\r\n/g, "\n");
|
|
function section(start, end) {
|
|
const a = source.indexOf(start), b = source.indexOf(end, a);
|
|
assert(a >= 0 && b > a, `missing ${start}`);
|
|
return source.slice(a, b);
|
|
}
|
|
const plain = (x) => JSON.parse(JSON.stringify(x));
|
|
const card = (user, used) => ({ provider: "claude", user, windows: [{ name: "7 days", used }] });
|
|
|
|
function page() {
|
|
const context = vm.createContext({ Date, performance, setTimeout });
|
|
vm.runInContext(`
|
|
let quotas = [], quotasAt = 0, quotaHist = [], quotasAskedAt = 0, view = "usage";
|
|
const requests = [], errors = [];
|
|
const usageAcctKey = q => q.provider + "\\n" + (q.user || "").toLowerCase();
|
|
const quotaCycleHolds = new Map(); // past cycles' estimates (#2009), cleared by a refresh
|
|
const t = x => x;
|
|
const el = () => ({ classList: { add() {} }, setAttribute() {} });
|
|
function renderQuotas() {}
|
|
function loadUpstream() {}
|
|
function status(x) { errors.push(x); }
|
|
function api(path) {
|
|
if (path.includes("history")) return Promise.resolve([]);
|
|
return new Promise((resolve, reject) => requests.push({ path, resolve, reject }));
|
|
}
|
|
${section("let quotasLoading =", "// A period of picked days is")}
|
|
${section("const quotaRefreshing =", "// familyQuota:")}
|
|
globalThis.p = {
|
|
requests, errors,
|
|
seed: x => { quotas = x; },
|
|
cards: () => quotas,
|
|
load: () => loadQuotas(),
|
|
refresh: q => quotaRefresh(q).onclick({ stopPropagation() {} }),
|
|
answer: (i, x) => requests[i].resolve(x),
|
|
fail: i => requests[i].reject(new Error("offline")),
|
|
};
|
|
`, context);
|
|
return context.p;
|
|
}
|
|
const tick = () => new Promise(setImmediate);
|
|
|
|
for (const when of ["before", "during"]) {
|
|
test(`a background read started ${when} a card refresh cannot undo its result`, async () => {
|
|
const p = page(), old = card("a@x.com", 0), fresh = card("a@x.com", 32);
|
|
p.seed([old]);
|
|
let loading, refreshing;
|
|
if (when === "before") {
|
|
loading = p.load(); await tick(); refreshing = p.refresh(old);
|
|
} else {
|
|
refreshing = p.refresh(old); loading = p.load(); await tick();
|
|
}
|
|
const post = p.requests.findIndex(r => r.path.includes("/refresh?"));
|
|
const get = p.requests.findIndex(r => r.path === "usage/quotas");
|
|
assert(post >= 0 && get >= 0);
|
|
p.answer(post, [fresh]); await refreshing;
|
|
assert.equal(p.cards()[0].windows[0].used, 32);
|
|
p.answer(get, [old]); await loading;
|
|
assert.equal(p.cards()[0].windows[0].used, 32, "an older whole-page reply overwrote the refreshed card");
|
|
// A later whole-page reading remains authoritative, including removals.
|
|
const later = p.load(); await tick();
|
|
p.answer(p.requests.length - 1, []); await later;
|
|
assert.deepEqual(plain(p.cards()), []);
|
|
});
|
|
}
|
|
|
|
test("refreshing two cards keeps both results when their full-list replies cross", async () => {
|
|
const p = page(), a = card("a@x.com", 0), b = card("b@x.com", 0);
|
|
p.seed([a, b]);
|
|
const first = p.refresh(a), second = p.refresh(b);
|
|
p.answer(1, [a, card("b@x.com", 61)]); await second;
|
|
p.answer(0, [card("a@x.com", 32), b]); await first;
|
|
assert.deepEqual(plain(p.cards()).map(q => q.windows[0].used), [32, 61], "one card's reply undid another card's completed refresh");
|
|
});
|
|
|
|
test("a failed card refresh leaves background readings usable", async () => {
|
|
const p = page(), old = card("a@x.com", 0);
|
|
p.seed([old]);
|
|
const refresh = p.refresh(old), loading = p.load(); await tick();
|
|
p.fail(0); await refresh;
|
|
p.answer(1, [card("a@x.com", 32)]); await loading;
|
|
assert.equal(p.cards()[0].windows[0].used, 32);
|
|
assert.deepEqual(plain(p.errors), ["offline"]);
|
|
});
|
|
|
|
test("a card that comes back under another key still shows the reply", async () => {
|
|
// A card read before its account was known has no user; its refresh
|
|
// answers under the account's name.
|
|
const p = page(), unnamed = card("", 0);
|
|
p.seed([unnamed]);
|
|
const refresh = p.refresh(unnamed);
|
|
p.answer(0, [card("a@x.com", 32)]); await refresh;
|
|
assert.deepEqual(plain(p.cards()).map(q => [q.user, q.windows[0].used]), [["a@x.com", 32]], "the refreshed card's reply was dropped");
|
|
});
|
|
|
|
test("an account first named by refresh survives an older whole-page reply", async () => {
|
|
const p = page(), unnamed = card("", 0);
|
|
p.seed([unnamed]);
|
|
const loading = p.load(); await tick();
|
|
const refresh = p.refresh(unnamed);
|
|
p.answer(1, [card("a@x.com", 32)]); await refresh;
|
|
p.answer(0, [unnamed]); await loading;
|
|
assert.deepEqual(plain(p.cards()).map(q => [q.user, q.windows[0].used]), [["a@x.com", 32]], "an old reply restored the unnamed card over its refresh");
|
|
});
|
|
|
|
test("an account first named by refresh keeps another card's completed refresh", async () => {
|
|
const p = page(), unnamed = card("", 0), b = card("b@x.com", 0);
|
|
p.seed([unnamed,b]);
|
|
const first = p.refresh(unnamed), second = p.refresh(b);
|
|
p.answer(1,[unnamed,card("b@x.com",61)]); await second;
|
|
p.answer(0,[card("a@x.com",32),b]); await first;
|
|
assert.deepEqual(plain(p.cards()).map(q => [q.user,q.windows[0].used]), [["a@x.com",32],["b@x.com",61]], "the renamed card's reply undid another card's completed refresh");
|
|
});
|
|
|
|
test("a renamed card cannot retire another provider's protection against a pending GET", async () => {
|
|
const p = page(), unnamed = card("", 0), b = { ...card("b@x.com", 0), provider: "codex" };
|
|
const freshB = { ...card("b@x.com", 61), provider: "codex" };
|
|
p.seed([unnamed, b]);
|
|
const loading = p.load(); await tick();
|
|
const second = p.refresh(b);
|
|
p.answer(1, [unnamed, freshB]); await second;
|
|
const first = p.refresh(unnamed);
|
|
p.answer(2, [card("a@x.com", 32), b]); await first;
|
|
const readings = () => Object.fromEntries(plain(p.cards()).map(q => [q.user, q.windows[0].used]));
|
|
assert.deepEqual(readings(), { "a@x.com": 32, "b@x.com": 61 }, "the provider refresh replaced another provider's newer card");
|
|
p.answer(0, [unnamed, b]); await loading;
|
|
assert.deepEqual(readings(), { "a@x.com": 32, "b@x.com": 61 }, "the card reply retired protection needed by the older GET");
|
|
});
|
|
|
|
test("a remote summary becomes its child cards without undoing a local refresh", async () => {
|
|
const p = page(), summary = { ...card("", 0), provider: "home", from: "Home" };
|
|
const child = { ...card("a@x.com", 32), provider: "home/claude", from: "Home" };
|
|
const local = { ...card("b@x.com", 0), provider: "codex" };
|
|
const freshLocal = { ...local, windows: [{ name: "7 days", used: 61 }] };
|
|
p.seed([summary, local]);
|
|
const loading = p.load(); await tick();
|
|
const remote = p.refresh(summary), own = p.refresh(local);
|
|
p.answer(2, [summary, freshLocal]); await own;
|
|
p.answer(1, [child, local]); await remote;
|
|
const readings = () => Object.fromEntries(plain(p.cards()).map(q => [q.provider, q.windows[0].used]));
|
|
assert.deepEqual(readings(), { "home/claude": 32, codex: 61 });
|
|
p.answer(0, [summary, local]); await loading;
|
|
assert.deepEqual(readings(), { "home/claude": 32, codex: 61 }, "the late GET restored the remote summary or an old local reading");
|
|
});
|