// 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");