Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions apps/desktop-tauri/src/surfaces/TrayPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,56 @@ describe("TrayPanel provider grid", () => {
expect(container.querySelector("#card-claude")).not.toBeNull();
});

it("updates Overview spend after provider publications without reopening the panel", async () => {
const summary = (amount: number) => ({
contract: {},
reportingPeriod: "rolling:7",
reportingDay: "2026-10-26",
dashboardTimezone: "UTC",
rows: [{
providerId: "codex", displayName: "Codex", periodCost: amount,
periodTokens: 100, currency: "USD", source: "local", includedInOverview: true,
}],
});
tauriMocks.getUsageSpendSummary.mockResolvedValue(summary(70.31));
renderTrayPanel([provider("codex", "Codex", 35)]);
expect(await screen.findByText("$70.31")).toBeInTheDocument();
const initialReads = tauriMocks.getUsageSpendSummary.mock.calls.length;
tauriMocks.getUsageSpendSummary.mockResolvedValue(summary(71.08));
act(() => {
emitEvent("provider-updated", provider("codex", "Codex", 36));
emitEvent("provider-updated", provider("codex", "Codex", 37));
});
expect(await screen.findByText("$71.08")).toBeInTheDocument();
expect(screen.queryByText("$70.31")).not.toBeInTheDocument();
expect(tauriMocks.getUsageSpendSummary).toHaveBeenCalledTimes(initialReads + 1);
});

it("does not let an older Overview scan overwrite newer spend", async () => {
const summary = (amount: number) => ({
contract: {}, reportingPeriod: "rolling:7", reportingDay: "2026-10-26",
dashboardTimezone: "UTC", rows: [{
providerId: "codex", displayName: "Codex", periodCost: amount,
periodTokens: 100, currency: "USD", source: "local", includedInOverview: true,
}],
});
tauriMocks.getUsageSpendSummary.mockResolvedValue(summary(70.31));
renderTrayPanel([provider("codex", "Codex", 35)]);
expect(await screen.findByText("$70.31")).toBeInTheDocument();
const initialReads = tauriMocks.getUsageSpendSummary.mock.calls.length;
let resolveOld!: (value: ReturnType<typeof summary>) => void;
tauriMocks.getUsageSpendSummary.mockImplementationOnce(
() => new Promise((resolve) => { resolveOld = resolve; }),
).mockResolvedValue(summary(71.08));
act(() => emitEvent("provider-updated", provider("codex", "Codex", 36)));
await waitFor(() => expect(tauriMocks.getUsageSpendSummary).toHaveBeenCalledTimes(initialReads + 1));
act(() => emitEvent("provider-updated", provider("codex", "Codex", 37)));
expect(await screen.findByText("$71.08")).toBeInTheDocument();
await act(async () => resolveOld(summary(70.50)));
expect(screen.getByText("$71.08")).toBeInTheDocument();
expect(screen.queryByText("$70.50")).not.toBeInTheDocument();
});

it("dismisses the tray panel on unmodified Escape", async () => {
const { container } = renderTrayPanel([provider("claude", "Claude", 35)]);

Expand Down
32 changes: 27 additions & 5 deletions apps/desktop-tauri/src/surfaces/TrayPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Fragment, useEffect, useState, type CSSProperties } from "react";
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import type { BootstrapState, ProviderUsageSnapshot, UsageSpendSummary } from "../types/bridge";
import type { LocaleKey } from "../i18n/keys";
import { costPeriodShortLabel } from "../lib/costPeriod";
Expand Down Expand Up @@ -251,11 +252,32 @@ function OverviewSpendSummary({
setSummary(null);
return () => { cancelled = true; };
}
// No explicit period: the backend resolves the saved History window.
void getUsageSpendSummary()
.then((value) => { if (!cancelled) setSummary(value); })
.catch(() => { if (!cancelled) setSummary(null); });
return () => { cancelled = true; };
let request = 0;
let timer: ReturnType<typeof setTimeout> | null = null;
let unlisten: UnlistenFn | null = null;
const load = () => {
const current = ++request;
// No explicit period: the backend resolves the saved History window.
void getUsageSpendSummary()
.then((value) => { if (!cancelled && current === request) setSummary(value); })
.catch(() => { if (!cancelled && current === request) setSummary(null); });
};
// Subscribe before the first read so a publication that lands while the
// listener is registering still triggers a fresh read.
void listen("provider-updated", () => {
if (timer) clearTimeout(timer);
timer = setTimeout(load, 200);
}).then((fn) => {
if (cancelled) fn();
else unlisten = fn;
}).catch(() => {}).finally(() => {
if (!cancelled) load();
});
return () => {
cancelled = true;
if (timer) clearTimeout(timer);
unlisten?.();
};
}, [providerIds.join("|"), period, costTrackingEnabled]);

// Overview consumes the same backend spend catalog as Usage & Spend. Do not
Expand Down