@ftrdotdev/pi-tui 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/CHANGELOG.md +225 -0
  2. package/README.md +5 -0
  3. package/package.json +18 -15
  4. package/src/activity-service.test.ts +97 -0
  5. package/src/activity.test.ts +1 -1
  6. package/src/activity.ts +38 -0
  7. package/src/chrome/budget.test.ts +345 -0
  8. package/src/chrome/budget.ts +220 -0
  9. package/src/chrome/declarations.ts +303 -0
  10. package/src/chrome/facts.test.ts +165 -0
  11. package/src/chrome/facts.ts +267 -0
  12. package/src/chrome/frame.ts +45 -0
  13. package/src/chrome/laws.test.ts +231 -0
  14. package/src/chrome/port.test.ts +164 -0
  15. package/src/chrome/port.ts +67 -0
  16. package/src/chrome/service-provider.test.ts +383 -0
  17. package/src/chrome/service-provider.ts +55 -0
  18. package/src/commands/git-execution.test.ts +5 -2
  19. package/src/commands/git-execution.ts +27 -15
  20. package/src/commands/git-status.test.ts +47 -15
  21. package/src/commands/git-status.ts +83 -31
  22. package/src/components/bordered-box.test.ts +12 -15
  23. package/src/components/bordered-box.ts +11 -7
  24. package/src/components/define-component.ts +1 -1
  25. package/src/components/index.ts +7 -0
  26. package/src/components/row-list.test.ts +310 -0
  27. package/src/components/row-list.ts +272 -0
  28. package/src/config.test.ts +118 -3
  29. package/src/config.ts +46 -40
  30. package/src/frame/box-frame.test.ts +53 -7
  31. package/src/harness-policy.test.ts +47 -0
  32. package/src/icons.test.ts +19 -0
  33. package/src/icons.ts +50 -31
  34. package/src/index.test.ts +135 -0
  35. package/src/index.ts +118 -119
  36. package/src/layers.test.ts +94 -0
  37. package/src/layers.ts +92 -0
  38. package/src/runtime-service.test.ts +218 -0
  39. package/src/runtime-service.ts +103 -0
  40. package/src/runtime.test.ts +165 -66
  41. package/src/runtime.ts +127 -93
  42. package/src/session-lifecycle-service.test.ts +95 -0
  43. package/src/session-lifecycle.test.ts +77 -0
  44. package/src/session-lifecycle.ts +21 -0
  45. package/src/state.test.ts +132 -3
  46. package/src/state.ts +43 -13
  47. package/src/telemetry-service.test.ts +250 -0
  48. package/src/telemetry.test.ts +322 -0
  49. package/src/telemetry.ts +130 -23
  50. package/src/ui/editor-tint.test.ts +58 -45
  51. package/src/ui/editor-tint.ts +1 -1
  52. package/src/ui/editor.ts +117 -53
  53. package/src/ui/footer.test.ts +349 -120
  54. package/src/ui/footer.ts +147 -124
  55. package/src/ui/header.test.ts +203 -223
  56. package/src/ui/header.ts +32 -96
  57. package/src/ui/preview.test.ts +73 -70
  58. package/src/ui/preview.ts +48 -33
  59. package/src/ui/render-helpers.test.ts +82 -13
  60. package/src/ui/render-helpers.ts +110 -122
  61. package/src/ui/settings-command.test.ts +70 -11
  62. package/src/ui/settings-command.ts +30 -45
  63. package/src/ui/theme-color.test.ts +4 -3
  64. package/src/usage-totals-service.test.ts +230 -0
  65. package/src/usage-totals-service.ts +59 -0
  66. package/src/utils.test.ts +46 -50
  67. package/src/utils.ts +41 -239
  68. package/src/vim/editor-host.test.ts +201 -75
  69. package/src/vim/editor-state.ts +2 -2
  70. package/src/vim/mode-indicator.test.ts +6 -5
  71. package/src/vim/mode-indicator.ts +13 -9
  72. package/src/vim/selection-render.ts +8 -4
  73. package/src/vim/text-model.test.ts +52 -14
  74. package/src/vim/text-model.ts +1 -1
  75. package/src/vim/vim-router.test.ts +3 -0
  76. package/src/vim/vim-router.ts +1 -1
@@ -0,0 +1,267 @@
1
+ // The chrome fact registry: every fact the chrome may show, with its
2
+ // user-visible quantity, its single owning surface, its fact-scoped config key
3
+ // and its default eligibility. This is the one identity domain every other
4
+ // chrome component references instead of ad-hoc strings.
5
+
6
+ export type SurfaceId = "footer" | "header" | "notify" | "border";
7
+
8
+ export type FactId =
9
+ | "cwd"
10
+ | "gitBranch"
11
+ | "gitStatus"
12
+ | "gitCommit"
13
+ | "runtime"
14
+ | "workTimer"
15
+ | "workSplit"
16
+ | "contextOccupancy"
17
+ | "tokenCounts"
18
+ | "cost"
19
+ | "modelIdentity"
20
+ | "thinking"
21
+ | "extensionStatuses"
22
+ | "workState"
23
+ | "vimMode"
24
+ | "scrollHint"
25
+ | "version"
26
+ | "turnTps"
27
+ | "turnTtft"
28
+ | "turnStalls"
29
+ | "turnDuration"
30
+ | "turnTokens"
31
+ | "turnCostRate";
32
+
33
+ /** Every fact-scoped config key as a literal path: a typo is a type error, not a default. */
34
+ export type FactKey =
35
+ | "footerSegments.cwd"
36
+ | "footerSegments.gitBranch"
37
+ | "footerSegments.gitStatus"
38
+ | "footerSegments.gitCommit"
39
+ | "footerSegments.runtime"
40
+ | "footerSegments.context"
41
+ | "footerSegments.tokens"
42
+ | "footerSegments.cost"
43
+ | "footerSegments.extensionStatuses"
44
+ | "telemetry.tps"
45
+ | "telemetry.ttft"
46
+ | "telemetry.stalls"
47
+ | "telemetry.duration"
48
+ | "telemetry.tokens"
49
+ | "telemetry.cost";
50
+
51
+ export interface FactRecord {
52
+ readonly id: FactId;
53
+ /** The user-visible quantity; two facts sharing a quantity collide in one frame. */
54
+ readonly quantity: string;
55
+ readonly owner: SurfaceId;
56
+ /** Fact-scoped config key, verbatim from today's tui.json. Never renamed. */
57
+ readonly key?: FactKey;
58
+ readonly label?: string;
59
+ readonly defaultEnabled: boolean;
60
+ }
61
+
62
+ /** Exhaustive by construction: a missing or extra fact is a type error. */
63
+ export const FACTS = {
64
+ cwd: {
65
+ id: "cwd",
66
+ quantity: "cwd",
67
+ owner: "footer",
68
+ key: "footerSegments.cwd",
69
+ label: "CWD",
70
+ defaultEnabled: true,
71
+ },
72
+ gitBranch: {
73
+ id: "gitBranch",
74
+ quantity: "gitBranch",
75
+ owner: "footer",
76
+ key: "footerSegments.gitBranch",
77
+ label: "Git branch",
78
+ defaultEnabled: true,
79
+ },
80
+ gitStatus: {
81
+ id: "gitStatus",
82
+ quantity: "gitStatus",
83
+ owner: "footer",
84
+ key: "footerSegments.gitStatus",
85
+ label: "Git status",
86
+ defaultEnabled: true,
87
+ },
88
+ gitCommit: {
89
+ id: "gitCommit",
90
+ quantity: "gitCommit",
91
+ owner: "footer",
92
+ key: "footerSegments.gitCommit",
93
+ label: "Git commit (detached)",
94
+ defaultEnabled: false,
95
+ },
96
+ runtime: {
97
+ id: "runtime",
98
+ quantity: "runtime",
99
+ owner: "footer",
100
+ key: "footerSegments.runtime",
101
+ label: "Runtime",
102
+ defaultEnabled: false,
103
+ },
104
+ workTimer: {
105
+ id: "workTimer",
106
+ quantity: "duration",
107
+ owner: "footer",
108
+ defaultEnabled: true,
109
+ },
110
+ workSplit: {
111
+ id: "workSplit",
112
+ quantity: "workSplit",
113
+ owner: "footer",
114
+ defaultEnabled: true,
115
+ },
116
+ contextOccupancy: {
117
+ id: "contextOccupancy",
118
+ quantity: "tokens",
119
+ owner: "footer",
120
+ key: "footerSegments.context",
121
+ label: "Context usage",
122
+ defaultEnabled: true,
123
+ },
124
+ tokenCounts: {
125
+ id: "tokenCounts",
126
+ quantity: "tokens",
127
+ owner: "footer",
128
+ key: "footerSegments.tokens",
129
+ label: "Tokens",
130
+ defaultEnabled: true,
131
+ },
132
+ cost: {
133
+ id: "cost",
134
+ quantity: "cost",
135
+ owner: "footer",
136
+ key: "footerSegments.cost",
137
+ label: "Cost",
138
+ defaultEnabled: true,
139
+ },
140
+ modelIdentity: {
141
+ id: "modelIdentity",
142
+ quantity: "model",
143
+ owner: "footer",
144
+ defaultEnabled: true,
145
+ },
146
+ thinking: {
147
+ id: "thinking",
148
+ quantity: "thinking",
149
+ owner: "footer",
150
+ defaultEnabled: true,
151
+ },
152
+ extensionStatuses: {
153
+ id: "extensionStatuses",
154
+ quantity: "extensions",
155
+ owner: "footer",
156
+ key: "footerSegments.extensionStatuses",
157
+ label: "Extension status line",
158
+ defaultEnabled: true,
159
+ },
160
+ workState: {
161
+ id: "workState",
162
+ quantity: "workState",
163
+ owner: "border",
164
+ defaultEnabled: true,
165
+ },
166
+ vimMode: {
167
+ id: "vimMode",
168
+ quantity: "vimMode",
169
+ owner: "border",
170
+ defaultEnabled: true,
171
+ },
172
+ scrollHint: {
173
+ id: "scrollHint",
174
+ quantity: "scrollHint",
175
+ owner: "border",
176
+ defaultEnabled: true,
177
+ },
178
+ version: {
179
+ id: "version",
180
+ quantity: "version",
181
+ owner: "header",
182
+ defaultEnabled: true,
183
+ },
184
+ turnTps: {
185
+ id: "turnTps",
186
+ quantity: "tps",
187
+ owner: "notify",
188
+ key: "telemetry.tps",
189
+ label: "TPS",
190
+ defaultEnabled: true,
191
+ },
192
+ turnTtft: {
193
+ id: "turnTtft",
194
+ quantity: "ttft",
195
+ owner: "notify",
196
+ key: "telemetry.ttft",
197
+ label: "TTFT",
198
+ defaultEnabled: true,
199
+ },
200
+ turnDuration: {
201
+ id: "turnDuration",
202
+ quantity: "duration",
203
+ owner: "notify",
204
+ key: "telemetry.duration",
205
+ label: "Total duration",
206
+ defaultEnabled: false,
207
+ },
208
+ turnTokens: {
209
+ id: "turnTokens",
210
+ quantity: "tokens",
211
+ owner: "notify",
212
+ key: "telemetry.tokens",
213
+ label: "Token counts",
214
+ defaultEnabled: false,
215
+ },
216
+ turnStalls: {
217
+ id: "turnStalls",
218
+ quantity: "stalls",
219
+ owner: "notify",
220
+ key: "telemetry.stalls",
221
+ label: "Stall details",
222
+ defaultEnabled: true,
223
+ },
224
+ turnCostRate: {
225
+ id: "turnCostRate",
226
+ quantity: "cost",
227
+ owner: "notify",
228
+ key: "telemetry.cost",
229
+ label: "Cost rate",
230
+ defaultEnabled: false,
231
+ },
232
+ } as const satisfies Record<FactId, FactRecord>;
233
+
234
+ /** The same registry widened for id lookups (the const form has no optional keys). */
235
+ const registry: Record<FactId, FactRecord> = FACTS;
236
+
237
+ /** The only place a duplicate user-visible quantity is legal. */
238
+ export const QUANTITY_EXCEPTIONS = [
239
+ { quantity: "tokens", facts: ["contextOccupancy", "tokenCounts", "turnTokens"] },
240
+ { quantity: "cost", facts: ["cost", "turnCostRate"] },
241
+ { quantity: "duration", facts: ["workTimer", "turnDuration"] },
242
+ ] as const satisfies readonly { quantity: string; facts: readonly FactId[] }[];
243
+
244
+ /** The facts a surface owns, in registry order. */
245
+ export function factsFor(surface: SurfaceId): readonly FactRecord[] {
246
+ return Object.values(FACTS).filter((fact) => fact.owner === surface);
247
+ }
248
+
249
+ export function factQuantity(id: FactId): string {
250
+ return registry[id].quantity;
251
+ }
252
+
253
+ export function factKey(id: FactId): FactKey | undefined {
254
+ return registry[id].key;
255
+ }
256
+
257
+ export function factLabel(id: FactId): string | undefined {
258
+ return registry[id].label;
259
+ }
260
+
261
+ /** The flag part of a literal `section.flag` key, derived at the type level. */
262
+ export type FlagOf<K extends FactKey> = K extends `${string}.${infer F}` ? F : never;
263
+
264
+ /** Cut a fact key's section prefix. The one place that does so. */
265
+ export function flagOf<K extends FactKey>(key: K): FlagOf<K> {
266
+ return key.slice(key.indexOf(".") + 1) as FlagOf<K>;
267
+ }
@@ -0,0 +1,45 @@
1
+ // The frame planner: one call yields the fact decisions for the fitted
2
+ // surfaces, so the quiet laws are asserted over the same data the renderers
3
+ // consume instead of over pixels. The header is exempt: `buildBoxFrame` owns
4
+ // its one fact's fit rule, and the frozen header table is its guard.
5
+ import type { Decision } from "./budget.ts";
6
+ import type { SurfaceId } from "./facts.ts";
7
+ import type { ChromePort } from "./port.ts";
8
+ import type { TurnTelemetry } from "../telemetry.ts";
9
+ import { planNotify } from "../telemetry.ts";
10
+ import { resolveGlyphs } from "../icons.ts";
11
+ import { planFooter } from "../ui/footer.ts";
12
+ import { planBorder, type BorderState } from "../ui/editor.ts";
13
+
14
+ export interface FramePlan {
15
+ readonly surface: SurfaceId;
16
+ readonly decisions: readonly Decision[];
17
+ }
18
+
19
+ /**
20
+ * Plan the frame's fact decisions at `width`.
21
+ *
22
+ * `measurements` is the turn-scoped telemetry the caller owns (undefined
23
+ * between turns); `border` is the editor-owned border state. Neither is read
24
+ * from module state, so the planner stays pure over its inputs.
25
+ */
26
+ export function planFrame(
27
+ port: ChromePort,
28
+ width: number,
29
+ measurements: TurnTelemetry | undefined,
30
+ border: BorderState,
31
+ ): readonly FramePlan[] {
32
+ const config = port.facts.config();
33
+ const glyphs = resolveGlyphs(config.icons.mode);
34
+ return [
35
+ { surface: "footer", decisions: planFooter(port, width) },
36
+ {
37
+ surface: "notify",
38
+ decisions:
39
+ measurements === undefined || !config.telemetry.enabled
40
+ ? []
41
+ : planNotify(measurements, config.telemetry, width, glyphs, port.facts.theme()),
42
+ },
43
+ { surface: "border", decisions: planBorder(border, width) },
44
+ ];
45
+ }
@@ -0,0 +1,231 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import { themeDouble } from "@ftrdotdev/pi-test-kit";
3
+ import { footerCtxDouble } from "../../test-support/doubles.ts";
4
+ import {
5
+ glyphCollisions,
6
+ glyphLetterViolations,
7
+ glyphWidthViolations,
8
+ } from "../../test-support/glyph-laws.ts";
9
+ import { ActivityTracker } from "../activity.ts";
10
+ import { GitStatus } from "../commands/git-status.ts";
11
+ import { DEFAULT_CONFIG } from "../config.ts";
12
+ import { DECLARATIONS, SURFACES } from "./declarations.ts";
13
+ import { FACTS, type SurfaceId } from "./facts.ts";
14
+ import { planFrame } from "./frame.ts";
15
+ import { makePlainChromeProvider } from "./port.ts";
16
+ import type { TurnTelemetry } from "../telemetry.ts";
17
+ import type { BorderState } from "../ui/editor.ts";
18
+
19
+ const plainTheme = themeDouble();
20
+
21
+ const ENTRIES = [
22
+ {
23
+ type: "message",
24
+ id: "a1",
25
+ parentId: null,
26
+ timestamp: "t1",
27
+ message: {
28
+ role: "assistant",
29
+ content: [],
30
+ usage: {
31
+ input: 1200,
32
+ output: 400,
33
+ cacheRead: 0,
34
+ cacheWrite: 0,
35
+ totalTokens: 1600,
36
+ cost: { input: 0.02, output: 0.01, cacheRead: 0, cacheWrite: 0, total: 0.084 },
37
+ },
38
+ },
39
+ },
40
+ ];
41
+
42
+ /** A port under `DEFAULT_CONFIG`: every default-enabled fact is resolvable. */
43
+ const port = makePlainChromeProvider(
44
+ footerCtxDouble({
45
+ model: {
46
+ provider: "anthropic",
47
+ id: "claude-sonnet-4-0",
48
+ contextWindow: 200000,
49
+ reasoning: true,
50
+ },
51
+ cwd: "/workspace/pi-mono",
52
+ getContextUsage: () => ({ contextWindow: 200000, tokens: 100000, percent: 50 }),
53
+ sessionManager: {
54
+ getCwd: () => "/workspace/pi-mono",
55
+ getEntries: () => ENTRIES,
56
+ },
57
+ ui: { theme: plainTheme },
58
+ }),
59
+ {
60
+ state: () => ({
61
+ git: GitStatus.make({
62
+ branch: "feature/refactor",
63
+ ahead: 2,
64
+ behind: 1,
65
+ modified: 3,
66
+ untracked: 1,
67
+ staged: 2,
68
+ stashed: 1,
69
+ conflicted: 0,
70
+ renamed: 0,
71
+ deleted: 0,
72
+ commit: null,
73
+ }),
74
+ runtime: [],
75
+ sessionStartEpoch: 0,
76
+ lastDoneIn: undefined,
77
+ tracker: new ActivityTracker(),
78
+ }),
79
+ config: () => ({ ...DEFAULT_CONFIG }),
80
+ model: () => ({ provider: "Anthropic", model: "claude-sonnet-4-0", effort: "medium" }),
81
+ extensionStatuses: () => new Map(),
82
+ },
83
+ );
84
+
85
+ /** Every turn-scoped quantity non-zero (the default three are the ones shown). */
86
+ const TURN_FIXTURE: TurnTelemetry = {
87
+ tps: 42.3,
88
+ ttftMs: 400,
89
+ totalMs: 1500,
90
+ inputTokens: 1200,
91
+ outputTokens: 400,
92
+ stallMs: 3100,
93
+ stallCount: 2,
94
+ rateUsdPerMTokens: 3.5,
95
+ generationMs: 1200,
96
+ totalTokens: 1600,
97
+ costUsd: 0.0056,
98
+ measurementMs: 1200,
99
+ };
100
+
101
+ const BORDER_FIXTURE: BorderState = {
102
+ statusText: "Working…",
103
+ vimGlyph: "◇",
104
+ scrollHint: "↓ 3 more",
105
+ };
106
+
107
+ const WIDE_WIDTHS = [80, 100, 120];
108
+
109
+ /** Kept decisions per surface and line, in plan order. */
110
+ const keptBySurface = (
111
+ width: number,
112
+ ): Map<SurfaceId, Map<number, { id: string; width: number; action: string }[]>> => {
113
+ const result = new Map<SurfaceId, Map<number, { id: string; width: number; action: string }[]>>();
114
+ for (const plan of planFrame(port, width, TURN_FIXTURE, BORDER_FIXTURE)) {
115
+ const byLine = new Map<number, { id: string; width: number; action: string }[]>();
116
+ for (const decision of plan.decisions) {
117
+ if (decision.action === "drop") continue;
118
+ const kept = byLine.get(decision.line) ?? [];
119
+ kept.push({ id: decision.id, width: decision.width, action: decision.action });
120
+ byLine.set(decision.line, kept);
121
+ }
122
+ result.set(plan.surface, byLine);
123
+ }
124
+ return result;
125
+ };
126
+
127
+ /** Assert every kept decision reaches its declaration's legible floor. */
128
+ const assertDeclaredFloors = (
129
+ bySurface: ReadonlyMap<SurfaceId, ReadonlyMap<number, readonly { id: string; width: number }[]>>,
130
+ width: number,
131
+ ): void => {
132
+ for (const [surface, byLine] of bySurface) {
133
+ for (const kept of byLine.values()) {
134
+ for (const decision of kept) {
135
+ const declaration = DECLARATIONS[surface].find((entry) => entry.id === decision.id);
136
+ assert.ok(declaration, `${surface}/${decision.id}: no declaration`);
137
+ assert.ok(
138
+ decision.width >= declaration.minWidth,
139
+ `width ${width}: ${surface}/${decision.id} kept below its floor`,
140
+ );
141
+ }
142
+ }
143
+ }
144
+ };
145
+
146
+ it("no quantity renders on two surfaces in one frame", () => {
147
+ for (const width of WIDE_WIDTHS) {
148
+ // Arrange: the frame's kept decisions grouped by user-visible quantity.
149
+ const owners = new Map<string, Set<SurfaceId>>();
150
+ for (const plan of planFrame(port, width, TURN_FIXTURE, BORDER_FIXTURE)) {
151
+ for (const decision of plan.decisions) {
152
+ if (decision.action === "drop") continue;
153
+ const quantity = FACTS[decision.fact].quantity;
154
+ const surfaces = owners.get(quantity) ?? new Set<SurfaceId>();
155
+ surfaces.add(plan.surface);
156
+ owners.set(quantity, surfaces);
157
+ }
158
+ }
159
+
160
+ // Assert: every quantity has exactly one owning surface; the header is
161
+ // exempt and contributes nothing.
162
+ for (const [quantity, surfaces] of owners) {
163
+ assert.deepStrictEqual(
164
+ [...surfaces],
165
+ [surfaces.values().next().value],
166
+ `width ${width}: ${quantity} rendered on ${[...surfaces].join(", ")}`,
167
+ );
168
+ assert.ok(!surfaces.has("header"), `width ${width}: the header contributed a fact`);
169
+ }
170
+ }
171
+ });
172
+
173
+ it("each surface honours its declared ceiling and floor at wide widths", () => {
174
+ for (const width of WIDE_WIDTHS) {
175
+ const bySurface = keptBySurface(width);
176
+ for (const [surface, byLine] of bySurface) {
177
+ const total = [...byLine.values()].reduce((sum, kept) => sum + kept.length, 0);
178
+ assert.ok(
179
+ total <= SURFACES[surface].maxFields,
180
+ `width ${width}: ${surface} kept ${total} over ${SURFACES[surface].maxFields}`,
181
+ );
182
+ }
183
+ assertDeclaredFloors(bySurface, width);
184
+ }
185
+ });
186
+
187
+ it("each surface keeps its highest-priority field per line at 30 columns", () => {
188
+ // Act: plan the full frame at 30, below the narrow threshold.
189
+ const bySurface = keptBySurface(30);
190
+
191
+ // Assert: one field per declared line, the highest priority that reaches
192
+ // its floor.
193
+ assert.deepStrictEqual(
194
+ bySurface
195
+ .get("footer")
196
+ ?.get(1)
197
+ ?.map((decision) => decision.id),
198
+ ["contextOccupancy"],
199
+ );
200
+ assert.deepStrictEqual(
201
+ bySurface
202
+ .get("footer")
203
+ ?.get(2)
204
+ ?.map((decision) => decision.id),
205
+ ["modelIdentity"],
206
+ );
207
+ assert.deepStrictEqual(
208
+ bySurface
209
+ .get("notify")
210
+ ?.get(1)
211
+ ?.map((decision) => decision.id),
212
+ ["turnTps"],
213
+ );
214
+ assert.deepStrictEqual(
215
+ bySurface
216
+ .get("border")
217
+ ?.get(0)
218
+ ?.map((decision) => decision.id),
219
+ ["editorStatus"],
220
+ );
221
+ for (const byLine of bySurface.values()) {
222
+ for (const kept of byLine.values()) assert.ok(kept.length <= 1);
223
+ }
224
+ assertDeclaredFloors(bySurface, 30);
225
+ });
226
+
227
+ it("the glyph laws still hold after the final table", () => {
228
+ assert.deepStrictEqual(glyphCollisions(), []);
229
+ assert.deepStrictEqual(glyphWidthViolations(), []);
230
+ assert.deepStrictEqual(glyphLetterViolations(), []);
231
+ });
@@ -0,0 +1,164 @@
1
+ // Each test is its own entry point: it builds a fresh layer for one scenario and
2
+ // runs its effects inside it, so providing there is the pattern the rule allows.
3
+ /* oxlint-disable effecttsgo/strict-effect-provide */
4
+ import { assert, beforeEach, it, vi } from "@effect/vitest";
5
+ import { themeDouble } from "@ftrdotdev/pi-test-kit";
6
+ import { readFileSync } from "node:fs";
7
+ import { Effect } from "effect";
8
+ import { footerCtxDouble, stubChromePort } from "../../test-support/doubles.ts";
9
+ import { ActivityTracker } from "../activity.ts";
10
+ import { GitStatus } from "../commands/git-status.ts";
11
+ import { DEFAULT_CONFIG, type TuiConfig } from "../config.ts";
12
+ import { invalidateUsageCache, type UsageTotals } from "../state.ts";
13
+ import { FooterRenderService } from "../ui/footer.ts";
14
+ import type { ChromeFacts, ChromePort } from "./port.ts";
15
+ import { makePlainChromeProvider } from "./port.ts";
16
+
17
+ const plainTheme = themeDouble();
18
+
19
+ const ZERO_TOTALS: UsageTotals = {
20
+ input: 0,
21
+ output: 0,
22
+ cacheRead: 0,
23
+ cacheWrite: 0,
24
+ cost: 0,
25
+ latestCacheHitRate: undefined,
26
+ };
27
+
28
+ const onlyCwdConfig: TuiConfig = {
29
+ ...DEFAULT_CONFIG,
30
+ footerSegments: {
31
+ cwd: true,
32
+ gitBranch: false,
33
+ gitStatus: false,
34
+ gitCommit: false,
35
+ runtime: false,
36
+ context: false,
37
+ tokens: false,
38
+ cost: false,
39
+ extensionStatuses: false,
40
+ },
41
+ };
42
+
43
+ /** A `ChromePort` for the service tests, with the cwd-only config default. */
44
+ const stubPort = (overrides: Partial<ChromeFacts> = {}): ChromePort =>
45
+ stubChromePort({ config: () => onlyCwdConfig, ...overrides });
46
+
47
+ // The usage cache is a single module slot, so every test starts empty.
48
+ beforeEach(() => invalidateUsageCache());
49
+
50
+ it.effect("the footer renders from the port, not from module state", () => {
51
+ // Arrange: a port whose cwd and usage getters are spies.
52
+ const cwd = vi.fn(() => "/port/only");
53
+ const usage = vi.fn(() => ZERO_TOTALS);
54
+ const port = stubPort({ cwd, usage });
55
+
56
+ return Effect.gen(function* () {
57
+ const svc = yield* FooterRenderService;
58
+
59
+ // Act: render through the service.
60
+ const lines = yield* svc.render(80);
61
+
62
+ // Assert: the line came from the port's getters, each read once.
63
+ assert.ok(lines[0]!.includes("/port/only"));
64
+ assert.strictEqual(cwd.mock.calls.length, 1);
65
+ assert.strictEqual(usage.mock.calls.length, 1);
66
+ }).pipe(Effect.provide(FooterRenderService.make(port)));
67
+ });
68
+
69
+ it.effect("the port is live: a changed getter changes the next render", () => {
70
+ // Arrange: a mutable cwd behind one getter.
71
+ let cwd = "/first/cwd";
72
+ const port = stubPort({ cwd: () => cwd });
73
+
74
+ return Effect.gen(function* () {
75
+ const svc = yield* FooterRenderService;
76
+
77
+ // Act: render, mutate, render again.
78
+ const first = yield* svc.render(80);
79
+ cwd = "/second/cwd";
80
+ const second = yield* svc.render(80);
81
+
82
+ // Assert: nothing was captured at build time.
83
+ assert.ok(first[0]!.includes("/first/cwd"));
84
+ assert.ok(second[0]!.includes("/second/cwd"));
85
+ }).pipe(Effect.provide(FooterRenderService.make(port)));
86
+ });
87
+
88
+ it("the plain provider resolves every fact from the pi context", () => {
89
+ // Arrange: a footer context double with known values.
90
+ const entries = [
91
+ {
92
+ type: "message",
93
+ id: "a1",
94
+ parentId: null,
95
+ timestamp: "t1",
96
+ message: {
97
+ role: "assistant",
98
+ content: [],
99
+ usage: {
100
+ input: 1200,
101
+ output: 400,
102
+ cacheRead: 0,
103
+ cacheWrite: 0,
104
+ totalTokens: 1600,
105
+ cost: { input: 0.02, output: 0.01, cacheRead: 0, cacheWrite: 0, total: 0.03 },
106
+ },
107
+ },
108
+ },
109
+ ];
110
+ const sessionManager = {
111
+ getCwd: () => "/provider/cwd",
112
+ getEntries: () => entries,
113
+ };
114
+ const ctx = footerCtxDouble({
115
+ model: { provider: "anthropic", id: "claude", contextWindow: 200000 },
116
+ cwd: "/provider/cwd",
117
+ getContextUsage: () => ({ contextWindow: 200000, tokens: 100000, percent: 50 }),
118
+ sessionManager,
119
+ ui: { theme: plainTheme },
120
+ });
121
+ const config: TuiConfig = { ...DEFAULT_CONFIG };
122
+ const tracker = new ActivityTracker();
123
+ const statuses = new Map([["lint", "running"]]);
124
+ const port = makePlainChromeProvider(ctx, {
125
+ state: () => ({
126
+ git: GitStatus.empty(),
127
+ runtime: [],
128
+ sessionStartEpoch: 0,
129
+ lastDoneIn: 42,
130
+ tracker,
131
+ }),
132
+ config: () => config,
133
+ model: () => ({ provider: "Anthropic", model: "claude", effort: "medium" }),
134
+ extensionStatuses: () => statuses,
135
+ });
136
+
137
+ // Act/Assert: every fact comes from the double, through its getter.
138
+ assert.strictEqual(port.facts.cwd(), "/provider/cwd");
139
+ assert.strictEqual(port.facts.model().model, "claude");
140
+ assert.strictEqual(port.facts.usage().input, 1200);
141
+ assert.strictEqual(port.facts.contextUsage()?.percent, 50);
142
+ assert.strictEqual(port.facts.config(), config);
143
+ assert.strictEqual(port.facts.theme(), plainTheme);
144
+ assert.strictEqual(port.facts.activity(), tracker);
145
+ assert.strictEqual(port.facts.lastDoneIn(), 42);
146
+ assert.strictEqual(port.facts.extensionStatuses(), statuses);
147
+
148
+ // Liveness: a mutated double is reflected on the next read, not snapshotted.
149
+ sessionManager.getCwd = () => "/mutated/cwd";
150
+ assert.strictEqual(port.facts.cwd(), "/mutated/cwd");
151
+ });
152
+
153
+ it("the mount builds exactly one provider and hands it everywhere", () => {
154
+ // Arrange: the mount wiring's own source text (no test imports index.ts).
155
+ const source = readFileSync(new URL("../index.ts", import.meta.url), "utf8");
156
+
157
+ // Assert: one builder construction (D3/D11), and the plain selection gone.
158
+ assert.strictEqual(source.match(/makeChromePortBuilder\(/g)?.length ?? 0, 1);
159
+ assert.strictEqual(source.match(/makePlainChromeProvider\(/g)?.length ?? 0, 0);
160
+ assert.strictEqual(source.match(/buildChromePort\(/g)?.length ?? 0, 0);
161
+ assert.ok(source.includes("installHeader(ctx, port)"));
162
+ assert.ok(source.includes("installFooter(ctx, port,"));
163
+ assert.ok(source.includes("resolveGlyphs(port.facts.config().icons.mode)"));
164
+ });