|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, cleanup, render, screen, within } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { afterEach, expect, it, vi } from "vitest"; |
| 5 | +import type { RelaySession } from "../../features/relay/session"; |
| 6 | +import type { ArchiveHost, ArchivePage } from "../../features/archive/types"; |
| 7 | +import { ChannelUsage } from "./ChannelUsage"; |
| 8 | + |
| 9 | +const key = (n: number) => n.toString(16).padStart(64, "0"); |
| 10 | +const frame = ( |
| 11 | + agent: number, |
| 12 | + session: string, |
| 13 | + turn: number, |
| 14 | + total?: number, |
| 15 | +): ArchivePage["records"][number] => ({ |
| 16 | + id: key(agent * 100 + turn + (session === "b" ? 50 : 0)), |
| 17 | + agent: key(agent), |
| 18 | + createdAt: turn, |
| 19 | + receivedAt: turn, |
| 20 | + plaintext: JSON.stringify({ |
| 21 | + harness: "goose", |
| 22 | + model: "model", |
| 23 | + timestamp: new Date((turn + 1) * 1000).toISOString(), |
| 24 | + channelId: "channel", |
| 25 | + sessionId: session, |
| 26 | + turnId: `turn-${turn}`, |
| 27 | + turnSeq: turn, |
| 28 | + turn: { inputTokens: 0, costUsd: 0 }, |
| 29 | + cumulative: { inputTokens: 10, totalTokens: total ?? null, costUsd: 0 }, |
| 30 | + }), |
| 31 | +}); |
| 32 | +const host = (records: ArchivePage["records"]): ArchiveHost => ({ |
| 33 | + location: "device", |
| 34 | + settings: vi.fn(async () => ({ |
| 35 | + observer: true, |
| 36 | + metrics: true, |
| 37 | + observerDays: 30, |
| 38 | + revision: 1, |
| 39 | + location: "device" as const, |
| 40 | + path: "isolated", |
| 41 | + bytes: 0, |
| 42 | + })), |
| 43 | + read: vi.fn(async () => ({ |
| 44 | + records, |
| 45 | + before: null, |
| 46 | + agents: [], |
| 47 | + skipped: 0, |
| 48 | + revision: 1, |
| 49 | + })), |
| 50 | + configure: vi.fn(), |
| 51 | + clear: vi.fn(), |
| 52 | +}); |
| 53 | +function fixture(records: ArchivePage["records"]) { |
| 54 | + const archive = host(records); |
| 55 | + let access = true; |
| 56 | + const listeners = new Set<() => void>(); |
| 57 | + const list = { |
| 58 | + status: "ready", |
| 59 | + channels: [{ id: "channel", name: "Channel", members: [key(9)] }], |
| 60 | + }; |
| 61 | + const empty = { status: "ready", channels: [] }; |
| 62 | + const session = { |
| 63 | + viewer: key(9), |
| 64 | + agentActivity: { archive }, |
| 65 | + channels: { |
| 66 | + list: () => (access ? list : empty), |
| 67 | + subscribeList: (listener: () => void) => { |
| 68 | + listeners.add(listener); |
| 69 | + return () => listeners.delete(listener); |
| 70 | + }, |
| 71 | + }, |
| 72 | + } as unknown as RelaySession; |
| 73 | + return { |
| 74 | + session, |
| 75 | + archive, |
| 76 | + revoke: () => |
| 77 | + act(() => { |
| 78 | + access = false; |
| 79 | + for (const listener of listeners) listener(); |
| 80 | + }), |
| 81 | + }; |
| 82 | +} |
| 83 | +afterEach(cleanup); |
| 84 | +it("opens the selected agent, requires explicit multi-session selection, and expands zero-valued turn details", async () => { |
| 85 | + const records = [ |
| 86 | + frame(1, "a", 1, 0), |
| 87 | + frame(1, "b", 2, 20), |
| 88 | + frame(2, "a", 1, 30), |
| 89 | + ]; |
| 90 | + const { session } = fixture(records); |
| 91 | + render(<ChannelUsage session={session} channelId="channel" />); |
| 92 | + const user = userEvent.setup(); |
| 93 | + const picker = screen.getByRole("combobox", { name: "Agent usage" }); |
| 94 | + expect(picker.textContent).toContain("Select agent"); |
| 95 | + await user.click(picker); |
| 96 | + await user.click(await screen.findByRole("option", { name: /2 sessions/ })); |
| 97 | + expect(picker.textContent).toContain("2 sessions"); |
| 98 | + const totals = screen.getByRole("region", { name: "All session totals" }); |
| 99 | + expect(within(totals).getByText("Totals across 2 sessions")).toBeTruthy(); |
| 100 | + expect(within(totals).getAllByText("20")).toHaveLength(2); |
| 101 | + // Both latest snapshots report input; token totals and costs use different coverage. |
| 102 | + expect(within(totals).getByText("$0.00")).toBeTruthy(); |
| 103 | + expect(screen.queryByText("Latest reported session counters")).toBeNull(); |
| 104 | + const sessions = screen.getByRole("combobox", { name: "Session" }); |
| 105 | + expect(sessions.textContent).toContain("Select session"); |
| 106 | + await user.click(sessions); |
| 107 | + await user.click(await screen.findByRole("option", { name: /Session 1 ·/ })); |
| 108 | + expect(screen.getByText("Latest reported session counters")).toBeTruthy(); |
| 109 | + const list = screen.getByRole("region", { name: /recorded turns/ }); |
| 110 | + await user.click(within(list).getByRole("button", { name: /Show turn/ })); |
| 111 | + expect(within(list).getByText("Input")).toBeTruthy(); |
| 112 | + expect(within(list).getAllByText("$0.00").length).toBeGreaterThan(0); |
| 113 | + await user.click(sessions); |
| 114 | + await user.click(await screen.findByRole("option", { name: /Session 2 ·/ })); |
| 115 | + expect( |
| 116 | + within(screen.getByRole("region", { name: /recorded turns/ })).queryByText( |
| 117 | + "Input", |
| 118 | + ), |
| 119 | + ).toBeNull(); |
| 120 | + const close = screen.getByRole("button", { name: "Close" }); |
| 121 | + close.focus(); |
| 122 | + await user.keyboard("{Enter}"); |
| 123 | + expect(document.activeElement).toBe(picker); |
| 124 | + expect( |
| 125 | + screen.queryByRole("region", { name: "Session usage details" }), |
| 126 | + ).toBeNull(); |
| 127 | +}); |
| 128 | +it("hides decoded usage when the authorized channel disappears", async () => { |
| 129 | + const { session, revoke } = fixture([frame(1, "a", 1, 10)]); |
| 130 | + render(<ChannelUsage session={session} channelId="channel" />); |
| 131 | + expect( |
| 132 | + await screen.findByRole("combobox", { name: "Agent usage" }), |
| 133 | + ).toBeTruthy(); |
| 134 | + revoke(); |
| 135 | + expect(screen.queryByRole("combobox", { name: "Agent usage" })).toBeNull(); |
| 136 | + expect(screen.queryByText("Channel session usage")).toBeNull(); |
| 137 | +}); |
| 138 | +it("keeps incomplete cross-session counters unknown and handles a hundred sessions in one dropdown", async () => { |
| 139 | + const records = Array.from({ length: 100 }, (_, index) => ({ |
| 140 | + ...frame(1, `session-${index}`, index + 1, index + 1), |
| 141 | + id: key(index + 1000), |
| 142 | + ...(index === 0 |
| 143 | + ? { |
| 144 | + plaintext: JSON.stringify({ |
| 145 | + harness: "goose", |
| 146 | + timestamp: new Date(2000).toISOString(), |
| 147 | + channelId: "channel", |
| 148 | + turnSeq: 1, |
| 149 | + turn: { inputTokens: 1 }, |
| 150 | + }), |
| 151 | + } |
| 152 | + : {}), |
| 153 | + })); |
| 154 | + // One unidentified session has no trustworthy cumulative snapshot. |
| 155 | + const { session } = fixture(records); |
| 156 | + render(<ChannelUsage session={session} channelId="channel" />); |
| 157 | + const user = userEvent.setup(); |
| 158 | + await user.click(screen.getByRole("combobox", { name: "Agent usage" })); |
| 159 | + await user.click(await screen.findByRole("option", { name: /100 sessions/ })); |
| 160 | + const totals = screen.getByRole("region", { name: "All session totals" }); |
| 161 | + expect(within(totals).getByText(/99 of 100 sessions/)).toBeTruthy(); |
| 162 | + expect(within(totals).getAllByText("—").length).toBe(6); |
| 163 | + const picker = screen.getByRole("combobox", { name: "Session" }); |
| 164 | + await user.click(picker); |
| 165 | + expect(await screen.findAllByRole("option")).toHaveLength(100); |
| 166 | + await user.click(screen.getByRole("option", { name: /Session 99 ·/ })); |
| 167 | + expect(screen.getByText("Latest reported session counters")).toBeTruthy(); |
| 168 | +}); |
0 commit comments