Adds runtime models.dev enrichment for discovered provider models, keeps shipped baseline metadata over stored catalog overlays, and turns the /usage panel into a paged per-day table (app.usage.pageUp/pageDown). Also updates docs, README, and changelogs.
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import { setKeybindings } from "@earendil-works/pi-tui";
|
|
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import { KeybindingsManager } from "../src/core/keybindings.ts";
|
|
import type { UsageReport, UsageReportEvent } from "../src/core/usage-report.ts";
|
|
import { UsagePanelComponent, type UsagePanelState } from "../src/modes/interactive/components/usage-panel.ts";
|
|
import { initTheme } from "../src/modes/interactive/theme/theme.ts";
|
|
import { stripAnsi } from "../src/utils/ansi.ts";
|
|
|
|
const UP = "\x1b[A";
|
|
const DOWN = "\x1b[B";
|
|
|
|
function reportWith(events: UsageReportEvent[]): UsageReport {
|
|
const totals = { input: 1000, output: 200, cacheRead: 0, cacheWrite: 0, cost: 0.5 };
|
|
return {
|
|
totals,
|
|
mainTotals: totals,
|
|
sessionCount: 1,
|
|
rows: [{ id: "main:anthropic/x", key: "anthropic/x", category: "main", totals }],
|
|
events,
|
|
};
|
|
}
|
|
|
|
function createPanel(report: UsageReport) {
|
|
const state: UsagePanelState = { scope: "session", loading: false, report, dayOffset: 0 };
|
|
const offsets: number[] = [];
|
|
const panel = new UsagePanelComponent(state, {
|
|
onClose: () => {},
|
|
onToggleScope: () => {},
|
|
onPageChange: (dayOffset: number) => {
|
|
state.dayOffset = dayOffset;
|
|
offsets.push(dayOffset);
|
|
},
|
|
});
|
|
return {
|
|
state,
|
|
offsets,
|
|
render: (): string[] => panel.render(80).map((line) => stripAnsi(line)),
|
|
input: (data: string): void => panel.handleInput(data),
|
|
};
|
|
}
|
|
|
|
/** One event per day, ascending from 2026-01-01. */
|
|
function dailyEvents(count: number): UsageReportEvent[] {
|
|
return Array.from({ length: count }, (_, index) => ({
|
|
t: new Date(2026, 0, 1 + index, 12).getTime(),
|
|
tokens: (index + 1) * 100,
|
|
cost: 0.01,
|
|
rowId: "main:anthropic/x",
|
|
}));
|
|
}
|
|
|
|
describe("usage panel", () => {
|
|
beforeAll(() => {
|
|
initTheme("dark");
|
|
});
|
|
|
|
beforeEach(() => {
|
|
setKeybindings(new KeybindingsManager());
|
|
});
|
|
|
|
it("renders a per-day table with tokens, cost, and a volume bar", () => {
|
|
const { render } = createPanel(
|
|
reportWith([{ t: new Date(2026, 0, 5, 12).getTime(), tokens: 1000, cost: 0.5, rowId: "main:anthropic/x" }]),
|
|
);
|
|
const lines = render();
|
|
const text = lines.join("\n");
|
|
expect(text).toContain("Daily usage (tokens per day)");
|
|
expect(text).toContain("date");
|
|
expect(text).toContain("tokens");
|
|
expect(text).toContain("cost");
|
|
expect(text).toContain("2026-01-05");
|
|
expect(text).toContain("1,000");
|
|
expect(text).toContain("$0.500");
|
|
expect(text).toContain("showing 1-1 of 1 day");
|
|
});
|
|
|
|
it("reports when no timestamped usage exists", () => {
|
|
const { render } = createPanel(reportWith([]));
|
|
expect(render().join("\n")).toContain("No timestamped usage recorded");
|
|
});
|
|
|
|
it("shows the newest days first, one page at a time", () => {
|
|
const panel = createPanel(reportWith(dailyEvents(25)));
|
|
|
|
const firstPage = panel.render().join("\n");
|
|
expect(firstPage).toContain("2026-01-25");
|
|
expect(firstPage).not.toContain("2026-01-15");
|
|
expect(firstPage).toContain("showing 1-10 of 25 days");
|
|
expect(firstPage).toContain("earlier days");
|
|
|
|
panel.input(DOWN);
|
|
expect(panel.offsets).toEqual([10]);
|
|
expect(panel.render().join("\n")).toContain("showing 11-20 of 25 days");
|
|
|
|
panel.input(DOWN);
|
|
expect(panel.offsets).toEqual([10, 20]);
|
|
expect(panel.render().join("\n")).toContain("showing 21-25 of 25 days");
|
|
|
|
// Past the last page the offset stops moving.
|
|
panel.input(DOWN);
|
|
expect(panel.offsets).toEqual([10, 20]);
|
|
|
|
panel.input(UP);
|
|
panel.input(UP);
|
|
panel.input(UP);
|
|
expect(panel.offsets).toEqual([10, 20, 10, 0]);
|
|
expect(panel.render().join("\n")).toContain("showing 1-10 of 25 days");
|
|
});
|
|
});
|