diff --git a/app/main.py b/app/main.py index bddc1d3..c872dc7 100644 --- a/app/main.py +++ b/app/main.py @@ -1681,14 +1681,34 @@ function renderKeys(keys, keySpend) { const el = $('keys'); keys = keys || []; keySpend = keySpend || []; keyRowsData = { keys: keys, spend: keySpend }; - const rows = keys.map(k => { - const s = keySpend.find(x => !x.is_chat && x.name === k.name) || {}; - return { + // One row per key that HAS usage, not per key in the metadata list: the usage + // rows are the authoritative set (a live account had three keys with spend and + // an empty key list, so charting only the intersection showed one bar out of + // four). Metadata is attached when we have it; keys without usage still get a + // row, so a key you own is never invisible. + const metaFor = {}; + keys.forEach(k => { metaFor[k.name] = k; }); + const rows = []; + const seen = {}; + keySpend.forEach(sp => { + if (sp.is_chat) return; + seen[sp.name] = true; + const k = metaFor[sp.name] || {}; + rows.push({ + name: sp.name, chat: false, + meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') + + (k.fingerprint ? ' · ends …' + k.fingerprint : ''), + spend: Number(sp.spend) || 0, requests: Number(sp.requests) || 0 + }); + }); + keys.forEach(k => { + if (seen[k.name]) return; + rows.push({ name: k.name, chat: false, meta: (k.created_at ? 'created ' + k.created_at.slice(0, 10) : '') + (k.fingerprint ? ' · ends …' + k.fingerprint : ''), - spend: Number(s.spend) || 0, requests: Number(s.requests) || 0 - }; + spend: 0, requests: 0 + }); }); const chat = keySpend.find(x => x.is_chat); if (chat) { diff --git a/dashboard-keys-harness.js b/dashboard-keys-harness.js new file mode 100644 index 0000000..c18c476 --- /dev/null +++ b/dashboard-keys-harness.js @@ -0,0 +1,96 @@ +// The API keys as a chart: one bar row per key, the chat key included, and a +// Spend/Requests toggle that changes both the values and the order. +// Assertions are invariants (proportional lengths, sorting, presence), not the +// markup that happened to be rendered the day this was written. +const assert = (cond, msg) => { if (!cond) { console.error("FAIL: " + msg); process.exit(1); } }; + +const KEYS = [ + { name: "my-laptop", fingerprint: "abcd1234", created_at: "2026-11-01T10:00:00Z" }, + { name: "script", fingerprint: "efgh5678", created_at: "2026-11-02T10:00:00Z" }, +]; +const SPEND = [ + { name: "my-laptop", is_chat: false, spend: 12.5, requests: 100 }, + { name: "script", is_chat: false, spend: 2.5, requests: 400 }, + { name: "chat", is_chat: true, spend: 5.0, requests: 250 }, +]; + +const widthOf = (html, label) => { + // the bar immediately following this key's label + const i = html.indexOf(label); + assert(i !== -1, "the chart has a row labelled " + label); + const seg = html.slice(i); + const m = seg.match(/key-bar-fill[^>]*width:(\d+)%/); + assert(m, "the " + label + " row has a bar width"); + return parseInt(m[1], 10); +}; +const valueOf = (html, label) => { + const i = html.indexOf(label); + const seg = html.slice(i, html.indexOf("key-bar-row", i + 1) === -1 ? undefined : html.indexOf("key-bar-row", i + 1)); + const m = seg.match(/key-bar-val">([^<]*) chat ($5.00) > script ($2.50)"); + +console.log("4. the toggle re-renders by REQUESTS, values and order together"); +setKeyMetric("requests"); +html = document.getElementById("keys").innerHTML; +assert(document.getElementById("km-requests").className.indexOf("active") !== -1, "Requests is the active button"); +assert(document.getElementById("km-spend").className.indexOf("active") === -1, "Spend is no longer active"); +assert(widthOf(html, "script") === 100, "by requests, the biggest requester is full width (got " + widthOf(html, "script") + "%)"); +assert(html.indexOf("400 requests") !== -1, "requests are labelled as such, with a thousands separator"); +assert(html.indexOf("script") < html.indexOf("my-laptop"), "the order changed with the metric"); +setKeyMetric("spend"); +html = document.getElementById("keys").innerHTML; +assert(widthOf(html, "my-laptop") === 100, "and switching back restores the spend view"); + +console.log("5. revoke stays where it belongs"); +assert((html.match(/revokeKey\(/g) || []).length === 2, + "one Revoke per real key, none for the built-in chat key (got " + (html.match(/revokeKey\(/g) || []).length + ")"); + +console.log("5b. a key with usage but no metadata row is STILL charted"); +renderKeys([], SPEND); // the live case: usage rows, an empty key list +{ + const h = document.getElementById("keys").innerHTML; + assert((h.match(/key-bar-row/g) || []).length === 3, + "all three usage rows appear even with no key metadata (got " + (h.match(/key-bar-row/g) || []).length + ")"); + assert(h.indexOf("my-laptop") !== -1 && h.indexOf("script") !== -1, + "the named usage rows are charted, not dropped for want of a metadata entry"); + assert((h.match(/revokeKey\(/g) || []).length === 2, + "and each still gets its Revoke control"); +} +renderKeys([{ name: "unused-key", fingerprint: "zz", created_at: "2026-11-03T00:00:00Z" }], SPEND); +{ + const h = document.getElementById("keys").innerHTML; + assert((h.match(/key-bar-row/g) || []).length === 4, + "a key you own but have not used still gets a row (got " + (h.match(/key-bar-row/g) || []).length + ")"); +} + +console.log("6. the empty state means NO rows at all"); +renderKeys([], []); // no keys and no chat usage +assert(document.getElementById("keys").innerHTML.indexOf("No API keys yet") !== -1, + "with nothing to show, the card says so"); +renderKeys([], [{ name: "chat", is_chat: true, spend: 1, requests: 2 }]); +const chatOnly = document.getElementById("keys").innerHTML; +assert(chatOnly.indexOf("No API keys yet") === -1 && chatOnly.indexOf("Chat (built-in)") !== -1, + "with only chat usage, the chat row still shows — a chat key IS usage worth seeing"); + +console.log("DASHBOARD KEYS HARNESS OK: proportional bars, sorting, toggle, revoke placement");