|
| 1 | +import { |
| 2 | + type ContributionStatusEntry, |
| 3 | + ProviderDriverKind, |
| 4 | + ProviderInstanceId, |
| 5 | + ProviderSessionId, |
| 6 | + ThreadId, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { describe, expect, it } from "vite-plus/test"; |
| 9 | + |
| 10 | +import { |
| 11 | + reservesContributionStatusBand, |
| 12 | + threadContributionStatusChips, |
| 13 | + threadHasContributionStatus, |
| 14 | +} from "./thread-contribution-status-presentation"; |
| 15 | + |
| 16 | +const entry = ( |
| 17 | + session: string, |
| 18 | + items: ContributionStatusEntry["items"], |
| 19 | + driver = "pi", |
| 20 | +): ContributionStatusEntry => ({ |
| 21 | + threadId: ThreadId.make("thread-1"), |
| 22 | + source: { |
| 23 | + kind: "provider-session", |
| 24 | + providerSessionId: ProviderSessionId.make(session), |
| 25 | + providerInstanceId: ProviderInstanceId.make(driver), |
| 26 | + driver: ProviderDriverKind.make(driver), |
| 27 | + }, |
| 28 | + items, |
| 29 | +}); |
| 30 | + |
| 31 | +describe("threadContributionStatusChips", () => { |
| 32 | + it("shows nothing for a thread without statuses", () => { |
| 33 | + expect(threadContributionStatusChips([])).toEqual([]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("keys chips by source and item key, in the server's order", () => { |
| 37 | + const chips = threadContributionStatusChips([ |
| 38 | + entry("session-a", [ |
| 39 | + { key: "mode", text: "● plan" }, |
| 40 | + { key: "tokens", text: "12k", tone: "warning", tooltip: "Context is filling up" }, |
| 41 | + ]), |
| 42 | + entry("session-b", [{ key: "mode", text: "● plan" }], "codex"), |
| 43 | + ]); |
| 44 | + |
| 45 | + expect(chips.map((chip) => [chip.text, chip.leadsSource, chip.tone, chip.tooltip])).toEqual([ |
| 46 | + ["● plan", true, "neutral", null], |
| 47 | + ["12k", false, "warning", "Context is filling up"], |
| 48 | + ["● plan", true, "neutral", null], |
| 49 | + ]); |
| 50 | + // The same item key under two sources must stay two distinct rows. |
| 51 | + expect(new Set(chips.map((chip) => chip.id)).size).toBe(3); |
| 52 | + }); |
| 53 | + |
| 54 | + it("re-keys a chip when another provider session takes the thread over", () => { |
| 55 | + const [before] = threadContributionStatusChips([entry("old", [{ key: "mode", text: "x" }])]); |
| 56 | + const [after] = threadContributionStatusChips([entry("new", [{ key: "mode", text: "x" }])]); |
| 57 | + |
| 58 | + expect(after?.id).not.toBe(before?.id); |
| 59 | + }); |
| 60 | + |
| 61 | + it("attributes the status to its producer without claiming freshness", () => { |
| 62 | + const [pi] = threadContributionStatusChips([entry("s", [{ key: "mode", text: "● startup" }])]); |
| 63 | + const [codex] = threadContributionStatusChips([ |
| 64 | + entry("s", [{ key: "mode", text: "busy" }], "codex"), |
| 65 | + ]); |
| 66 | + |
| 67 | + expect(pi?.accessibilityLabel).toBe("Pi status: ● startup"); |
| 68 | + expect(pi?.help).toBe("Set by a Pi extension. It can lag a session change."); |
| 69 | + expect(codex?.help).toBe("Set by Codex. It can lag a session change."); |
| 70 | + }); |
| 71 | + |
| 72 | + it("puts the full status text in the details body, where it is never truncated", () => { |
| 73 | + const text = "● build — indexing 1,284 files in packages/client-runtime before the next turn"; |
| 74 | + const [plain, withTooltip] = threadContributionStatusChips([ |
| 75 | + entry("s", [ |
| 76 | + { key: "mode", text }, |
| 77 | + { key: "tokens", text: "12k", tooltip: "Context is filling up" }, |
| 78 | + ]), |
| 79 | + ]); |
| 80 | + |
| 81 | + expect(plain?.details).toEqual({ |
| 82 | + title: "Pi status", |
| 83 | + message: `${text}\n\nSet by a Pi extension. It can lag a session change.`, |
| 84 | + }); |
| 85 | + expect(withTooltip?.details.message).toBe( |
| 86 | + "12k\n\nContext is filling up\n\nSet by a Pi extension. It can lag a session change.", |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("status strip band", () => { |
| 92 | + it("counts a thread as having statuses only when an entry has items", () => { |
| 93 | + expect(threadHasContributionStatus([])).toBe(false); |
| 94 | + expect(threadHasContributionStatus([entry("s", [])])).toBe(false); |
| 95 | + expect(threadHasContributionStatus([entry("s", [{ key: "mode", text: "x" }])])).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it("keeps a Pi thread's band through set, replace and clear", () => { |
| 99 | + const pi = ProviderDriverKind.make("pi"); |
| 100 | + const band = [false, true, true, false].map((hasStatus) => |
| 101 | + reservesContributionStatusBand({ supported: true, driver: pi, hasStatus }), |
| 102 | + ); |
| 103 | + |
| 104 | + expect(band).toEqual([true, true, true, true]); |
| 105 | + }); |
| 106 | + |
| 107 | + it("reserves nothing for other providers without statuses or on servers without the stream", () => { |
| 108 | + const codex = ProviderDriverKind.make("codex"); |
| 109 | + const pi = ProviderDriverKind.make("pi"); |
| 110 | + |
| 111 | + expect( |
| 112 | + reservesContributionStatusBand({ supported: true, driver: codex, hasStatus: false }), |
| 113 | + ).toBe(false); |
| 114 | + expect( |
| 115 | + reservesContributionStatusBand({ supported: true, driver: codex, hasStatus: true }), |
| 116 | + ).toBe(true); |
| 117 | + expect(reservesContributionStatusBand({ supported: false, driver: pi, hasStatus: false })).toBe( |
| 118 | + false, |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
0 commit comments