feat(dashboard): lazy routes, Needs-you strip, URL state, exact numbers

- Workspace, SettingsHub, SessionDetail and the Workflow sub-tab are lazy
  (main bundle 2,166 kB -> ~920 kB), wrapped in RouteErrorBoundary: a stale
  chunk after a self-update reloads once (vite:preloadError too), any other
  error shows a Reload panel instead of a blank page.
- Live refresh: lib/throttle.ts + hooks/useLiveRefresh.ts replace trailing
  debounces that froze under a steady event stream; refresh skips hidden
  tabs and refetches after a WebSocket reconnect. Monitor loads only while
  shown, with one batched GET /api/agents?session_ids= instead of N+1; a
  failing endpoint is an inline error, not a blank Dashboard.
- NeedsYouStrip on Monitor and above Analytics. Tabs, filters, page, sort
  and search live in the URL (Dashboard, Sessions, Activity, SessionDetail).
- Sessions: tool / workspace / date-range filters, CSV/JSON export
  (formula-neutralised), focusable row links, Open in workspace (the dead
  /run links are gone), stale responses dropped, error + Retry.
- Fixes: "NaNs" durations, 30/7-day charts skipping idle days, weekday
  labels west of UTC (now averages), cache hit rate denominator, billable
  tokens vs cache reads, exact tool-call counts, "To" date = end of day,
  invalid ?from= no longer throws, recent list no longer shrinks to 15,
  stable message keys, transcript switch races.
- Light-theme-unreadable bg-black code panels and dark-only DAG fills use
  tokens; status configs and chart colours use theme tokens; Health tab
  and other literals translated.
- Server (additive): GET /api/agents?session_ids=, GET /api/sessions
  awaiting=1&from=&to=, stats tool_call_count/distinct_tool_count, event
  rows carry session/agent labels.
This commit is contained in:
2026-10-05 13:22:04 +07:00
parent 8fb703c6db
commit 03f1673fce
63 changed files with 3879 additions and 1662 deletions
@@ -0,0 +1,25 @@
/**
* @file exportData.test.ts
* @description Unit tests for the CSV export helper: quoting and formula neutralizing.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
import { describe, it, expect } from "vitest";
import { toCsv } from "../exportData";
describe("toCsv", () => {
it("writes a header row and quotes cells that need it", () => {
const csv = toCsv(
[
{ id: "a", name: 'say "hi", ok', n: 3 },
{ id: "b", name: null, n: 0 },
],
["id", "name", "n"]
);
expect(csv).toBe('id,name,n\r\na,"say ""hi"", ok",3\r\nb,,0\r\n');
});
it("neutralizes spreadsheet formulas", () => {
expect(toCsv([{ v: "=HYPERLINK(1)" }], ["v"])).toBe("v\r\n'=HYPERLINK(1)\r\n");
});
});
+21
View File
@@ -17,6 +17,7 @@ import {
formatTime,
getCurrentLocale,
formatModelName,
isoOrUndefined,
} from "../format";
describe("formatMs", () => {
@@ -256,3 +257,23 @@ describe("formatModelName", () => {
expect(formatModelName("o1-preview")).toBe("O1 Preview");
});
});
describe("NaN guards", () => {
it("formatDuration accepts timestamps that already end in Z", () => {
expect(formatDuration("2026-03-05T10:00:00Z", "2026-03-05T10:00:05Z")).toBe("5s");
});
it("formatMs renders '-' for NaN", () => {
expect(formatMs(NaN)).toBe("-");
});
it("timeAgo renders '-' for an unparseable timestamp", () => {
expect(timeAgo("not a date")).toBe("-");
});
});
describe("isoOrUndefined", () => {
it("returns undefined for an empty or unparseable value instead of throwing", () => {
expect(isoOrUndefined("")).toBeUndefined();
expect(isoOrUndefined("x")).toBeUndefined();
expect(isoOrUndefined("2026-01-02T03:04:05Z")).toBe("2026-01-02T03:04:05.000Z");
});
});
+41
View File
@@ -0,0 +1,41 @@
/**
* @file throttle.test.ts
* @description Unit tests for the leading+trailing throttle used by live-refresh pages.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
import { describe, it, expect, vi, afterEach } from "vitest";
import { throttle } from "../throttle";
afterEach(() => vi.useRealTimers());
describe("throttle", () => {
it("runs at once, then at most once per window with a trailing call", () => {
vi.useFakeTimers();
const fn = vi.fn();
const t = throttle(fn, 1000);
t();
expect(fn).toHaveBeenCalledTimes(1);
// A steady stream never freezes: it fires every window, not after it stops.
for (let i = 0; i < 25; i++) {
vi.advanceTimersByTime(100);
t();
}
expect(fn.mock.calls.length).toBeGreaterThanOrEqual(3);
vi.advanceTimersByTime(1000);
const n = fn.mock.calls.length;
vi.advanceTimersByTime(5000);
expect(fn).toHaveBeenCalledTimes(n);
});
it("cancel drops a pending trailing call", () => {
vi.useFakeTimers();
const fn = vi.fn();
const t = throttle(fn, 1000);
t();
t();
t.cancel();
vi.advanceTimersByTime(2000);
expect(fn).toHaveBeenCalledTimes(1);
});
});