@blackbelt-technology/pi-dashboard-client-utils 0.6.1 → 0.8.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.
@@ -1,92 +0,0 @@
1
- import { describe, it, expect, beforeEach, vi } from "vitest";
2
- import React from "react";
3
- import { renderHook } from "@testing-library/react";
4
- import { MobileProvider, useMobile } from "../useMobile.js";
5
-
6
- /**
7
- * Mocks `window.matchMedia` so it answers any query string the way a browser
8
- * with the given viewport (vw, vh) would: parses `(max-width: Npx)` and
9
- * `(max-height: Npx)` clauses, treats commas as OR. This mirrors what
10
- * `useMediaQuery` consumes — the comma-OR semantics is the actual contract
11
- * we want to pin in `useMobile`.
12
- */
13
- function installMatchMediaForViewport(vw: number, vh: number): void {
14
- Object.defineProperty(window, "matchMedia", {
15
- writable: true,
16
- configurable: true,
17
- value: vi.fn().mockImplementation((query: string) => {
18
- const clauses = query.split(",").map((c) => c.trim());
19
- const matches = clauses.some((clause) => {
20
- const w = clause.match(/\(\s*max-width:\s*(\d+)px\s*\)/);
21
- if (w) return vw <= Number(w[1]);
22
- const h = clause.match(/\(\s*max-height:\s*(\d+)px\s*\)/);
23
- if (h) return vh <= Number(h[1]);
24
- return false;
25
- });
26
- return {
27
- matches,
28
- media: query,
29
- onchange: null,
30
- addListener: vi.fn(),
31
- removeListener: vi.fn(),
32
- addEventListener: vi.fn(),
33
- removeEventListener: vi.fn(),
34
- dispatchEvent: vi.fn(),
35
- };
36
- }),
37
- });
38
- }
39
-
40
- function wrapper({ children }: { children: React.ReactNode }) {
41
- return <MobileProvider>{children}</MobileProvider>;
42
- }
43
-
44
- describe("useMobile — width-or-height predicate", () => {
45
- beforeEach(() => {
46
- vi.restoreAllMocks();
47
- });
48
-
49
- it("portrait phone (375x812) is mobile (width arm)", () => {
50
- installMatchMediaForViewport(375, 812);
51
- const { result } = renderHook(() => useMobile(), { wrapper });
52
- expect(result.current).toBe(true);
53
- });
54
-
55
- it("landscape phone (844x390) is mobile (height arm) — the iPhone 14 case", () => {
56
- installMatchMediaForViewport(844, 390);
57
- const { result } = renderHook(() => useMobile(), { wrapper });
58
- expect(result.current).toBe(true);
59
- });
60
-
61
- it("landscape phone (915x412) is mobile (height arm) — the Pixel 8 case", () => {
62
- installMatchMediaForViewport(915, 412);
63
- const { result } = renderHook(() => useMobile(), { wrapper });
64
- expect(result.current).toBe(true);
65
- });
66
-
67
- it("tablet portrait (768x1024) is NOT mobile", () => {
68
- installMatchMediaForViewport(768, 1024);
69
- const { result } = renderHook(() => useMobile(), { wrapper });
70
- expect(result.current).toBe(false);
71
- });
72
-
73
- it("tablet landscape (1024x768) is NOT mobile", () => {
74
- installMatchMediaForViewport(1024, 768);
75
- const { result } = renderHook(() => useMobile(), { wrapper });
76
- expect(result.current).toBe(false);
77
- });
78
-
79
- it("desktop (1440x900) is NOT mobile", () => {
80
- installMatchMediaForViewport(1440, 900);
81
- const { result } = renderHook(() => useMobile(), { wrapper });
82
- expect(result.current).toBe(false);
83
- });
84
-
85
- it("desktop short window (1200x500) flips to mobile (documented side effect)", () => {
86
- // Pinned regression: shrinking a desktop window vertically below 600px
87
- // intentionally enters mobile mode. See design.md decision 1 alternatives.
88
- installMatchMediaForViewport(1200, 500);
89
- const { result } = renderHook(() => useMobile(), { wrapper });
90
- expect(result.current).toBe(true);
91
- });
92
- });
@@ -1,128 +0,0 @@
1
- import { act, renderHook } from "@testing-library/react";
2
- import { describe, expect, it } from "vitest";
3
- import { useZoomPan } from "../useZoomPan.js";
4
-
5
- describe("useZoomPan", () => {
6
- it("starts at scale 1 with zero translate", () => {
7
- const { result } = renderHook(() => useZoomPan());
8
- expect(result.current.state).toEqual({ scale: 1, translateX: 0, translateY: 0 });
9
- });
10
-
11
- it("zoomIn increases scale by step", () => {
12
- const { result } = renderHook(() => useZoomPan({ zoomStep: 1.2 }));
13
- act(() => result.current.zoomIn());
14
- expect(result.current.state.scale).toBeCloseTo(1.2);
15
- });
16
-
17
- it("zoomOut decreases scale by step", () => {
18
- const { result } = renderHook(() => useZoomPan({ zoomStep: 1.2 }));
19
- act(() => result.current.zoomOut());
20
- expect(result.current.state.scale).toBeCloseTo(1 / 1.2);
21
- });
22
-
23
- it("clamps scale at maxScale", () => {
24
- const { result } = renderHook(() => useZoomPan({ maxScale: 2, zoomStep: 2 }));
25
- act(() => result.current.zoomIn()); // 2
26
- act(() => result.current.zoomIn()); // clamped to 2
27
- expect(result.current.state.scale).toBe(2);
28
- });
29
-
30
- it("clamps scale at minScale", () => {
31
- const { result } = renderHook(() => useZoomPan({ minScale: 0.5, zoomStep: 2 }));
32
- act(() => result.current.zoomOut()); // 0.5
33
- act(() => result.current.zoomOut()); // clamped to 0.5
34
- expect(result.current.state.scale).toBe(0.5);
35
- });
36
-
37
- it("reset restores initial state", () => {
38
- const { result } = renderHook(() => useZoomPan());
39
- act(() => result.current.zoomIn());
40
- act(() => result.current.zoomIn());
41
- expect(result.current.state.scale).not.toBe(1);
42
- act(() => result.current.reset());
43
- expect(result.current.state).toEqual({ scale: 1, translateX: 0, translateY: 0 });
44
- });
45
-
46
- it("double-click resets state", () => {
47
- const { result } = renderHook(() => useZoomPan());
48
- act(() => result.current.zoomIn());
49
- act(() => result.current.handlers.onDoubleClick());
50
- expect(result.current.state).toEqual({ scale: 1, translateX: 0, translateY: 0 });
51
- });
52
-
53
- it("exposes all expected handler keys", () => {
54
- const { result } = renderHook(() => useZoomPan());
55
- const keys = Object.keys(result.current.handlers);
56
- expect(keys).toContain("onWheel");
57
- expect(keys).toContain("onPointerDown");
58
- expect(keys).toContain("onPointerMove");
59
- expect(keys).toContain("onPointerUp");
60
- expect(keys).toContain("onTouchMove");
61
- expect(keys).toContain("onTouchEnd");
62
- expect(keys).toContain("onDoubleClick");
63
- });
64
- });
65
-
66
- describe("useZoomPan click-vs-drag threshold", () => {
67
- function makeTarget() {
68
- let captures = 0;
69
- const target = {
70
- setPointerCapture: () => { captures += 1; },
71
- releasePointerCapture: () => {},
72
- };
73
- return { target, captureCount: () => captures };
74
- }
75
-
76
- // A press+release with no movement must remain a click: no capture (which would
77
- // steal the click from a child onClick), no translate.
78
- it("tap with no movement: no capture, no translate", () => {
79
- const { result } = renderHook(() => useZoomPan());
80
- const { target, captureCount } = makeTarget();
81
- act(() => {
82
- result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
83
- result.current.handlers.onPointerUp();
84
- });
85
- expect(captureCount()).toBe(0);
86
- expect(result.current.state).toEqual({ scale: 1, translateX: 0, translateY: 0 });
87
- });
88
-
89
- it("movement at or below threshold (3px): no capture, no translate", () => {
90
- const { result } = renderHook(() => useZoomPan());
91
- const { target, captureCount } = makeTarget();
92
- act(() => {
93
- result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
94
- result.current.handlers.onPointerMove({ clientX: 53, clientY: 50, pointerId: 1, currentTarget: target } as never);
95
- });
96
- expect(captureCount()).toBe(0);
97
- expect(result.current.state.translateX).toBe(0);
98
- expect(result.current.state.translateY).toBe(0);
99
- });
100
-
101
- it("movement beyond threshold: captures once and translates", () => {
102
- const { result } = renderHook(() => useZoomPan());
103
- const { target, captureCount } = makeTarget();
104
- act(() => {
105
- result.current.handlers.onPointerDown({ button: 0, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
106
- result.current.handlers.onPointerMove({ clientX: 60, clientY: 50, pointerId: 1, currentTarget: target } as never);
107
- });
108
- expect(captureCount()).toBe(1);
109
- expect(result.current.state.translateX).toBe(10);
110
- // A further move keeps dragging without re-capturing.
111
- act(() => {
112
- result.current.handlers.onPointerMove({ clientX: 65, clientY: 50, pointerId: 1, currentTarget: target } as never);
113
- });
114
- expect(captureCount()).toBe(1);
115
- expect(result.current.state.translateX).toBe(15);
116
- });
117
-
118
- it("non-primary button does not start a pending drag", () => {
119
- const { result } = renderHook(() => useZoomPan());
120
- const { target, captureCount } = makeTarget();
121
- act(() => {
122
- result.current.handlers.onPointerDown({ button: 2, clientX: 50, clientY: 50, pointerId: 1, currentTarget: target } as never);
123
- result.current.handlers.onPointerMove({ clientX: 80, clientY: 50, pointerId: 1, currentTarget: target } as never);
124
- });
125
- expect(captureCount()).toBe(0);
126
- expect(result.current.state.translateX).toBe(0);
127
- });
128
- });
@@ -1,10 +0,0 @@
1
- # DOX — packages/client-utils/src/extension-ui
2
-
3
- Files in this directory. One row per source file.
4
-
5
- | File | Purpose |
6
- |------|---------|
7
- | `AgentMetricSlot.tsx` | Phase-2 `agent-metric` slot. Exports `AgentMetricSlot({ session, agentId })` — renders `kind: "agent-metric"` decorators matching `payload.agentId`, inside `FlowAgentCard`. Ignores unknown agentId. Uses `decoratorsOfKind`. |
8
- | `BreadcrumbSlot.tsx` | Phase-2 `breadcrumb` slot. Exports `BreadcrumbSlot({ session })` — renders most-recently-cached `kind: "breadcrumb"` descriptor as step indicator at top of `FlowDashboard`. Last-write-wins on collision. Step status active/done/error/pending. Uses `decoratorsOfKind`. |
9
- | `decorator-utils.ts` | Exports `decoratorsOfKind(decorators, kind)` — type-narrowing helper filtering `DashboardSession.uiDecorators` record by `DecoratorKind`. Shared by all Phase-2 decorator slot components (`AgentMetricSlot`, `BreadcrumbSlot`, `GateSlot`). |
10
- | `GateSlot.tsx` | Phase-2 `gate` slot. Exports `GateSlot({ session, flowId })` + `aggregateGateState` pure helper. Most-restrictive-wins: any `available: false` for `flowId` blocks; reasons concatenated. Renders amber banner inside `FlowLaunchDialog`; `useGateState` disables Run button. |
@@ -1,9 +0,0 @@
1
- # DOX — packages/client-utils/src/minimal-chat
2
-
3
- Files in this directory. One row per source file.
4
-
5
- | File | Purpose |
6
- |------|---------|
7
- | `index.ts` | Barrel for `./minimal-chat` subpath export. Re-exports `MinimalChatView`, `statusVisualsFor`, `extractInputPreview` from `MinimalChatView.js`; re-exports types `MinimalChatEntry`, `MinimalChatMeta`, `MinimalChatMode`, `MinimalChatStatus`, `MinimalChatViewProps` from `types.js`. Import path: `@blackbelt-technology/pi-dashboard-client-utils/minimal-chat`. |
8
- | `MinimalChatView.tsx` | Shared subagent/agent timeline renderer. Exports `MinimalChatView` (props `MinimalChatViewProps`), `statusVisualsFor(status)` → `{ iconPath, colorClass }`, `extractInputPreview(toolName, input)` → preview string. Modes: `inline` (body `h-[60vh]`), `popout` (`h-full`), `row` (single-line, no body). Resolves `MarkdownContent`, `formatTokens`, `formatDuration`, `toolCallStep`, `thinkingBlock` via `useUiPrimitive`/`useUiPrimitiveOrNull`; falls back to inline renderers when primitive absent. Entry kinds: `tool`, `text`, `thinking`, `error`. Single source of truth replacing duplicated timeline UI in `SubagentDetailView` and `FlowAgentDetail`. Header renders `meta.cost` (pre-formatted string) between tokens and duration when present. See change: surface-flow-agent-cost. |
9
- | `types.ts` | Type contracts for `MinimalChatView`. Exports `MinimalChatMode` (`"inline" | "popout" | "row"`), `MinimalChatStatus` (`"pending" | "running" | "complete" | "error" | "blocked"`), `MinimalChatEntry` (discriminated union `tool | text | thinking | error`), `MinimalChatMeta` (`modelName?`, `tokens?`, `cost?` pre-formatted USD string, `durationMs?`; see change surface-flow-agent-cost), `MinimalChatViewProps`. Producer adapters map plugin-specific state into these structural types at shim boundary. |
@@ -1,235 +0,0 @@
1
- /**
2
- * MinimalChatView unit tests.
3
- *
4
- * Tests use `withUiPrimitiveProvider` to mock `MarkdownContent`,
5
- * `formatTokens`, `formatDuration`. Rendering without the provider must
6
- * throw a hook-resolution error (covered by the negative case below).
7
- *
8
- * See change: extract-minimal-chat-view.
9
- */
10
- import React from "react";
11
- import { afterEach, describe, expect, it } from "vitest";
12
- import { cleanup, fireEvent, render, screen } from "@testing-library/react";
13
- import { withUiPrimitiveProvider } from "@blackbelt-technology/dashboard-plugin-runtime/test-support";
14
- import { MinimalChatView } from "../MinimalChatView.js";
15
- import type { MinimalChatEntry, MinimalChatStatus } from "../types.js";
16
-
17
- const MockMarkdown: React.FC<{ content: string }> = ({ content }) => (
18
- <div data-testid="md">{content}</div>
19
- );
20
-
21
- const mockFormatTokens = (n: number) => {
22
- if (n >= 1000) return `${Math.round(n / 100) / 10}k`;
23
- return String(n);
24
- };
25
- const mockFormatDuration = (ms: number) => `${(ms / 1000).toFixed(1)}s`;
26
-
27
- function renderView(ui: React.ReactElement) {
28
- return render(
29
- withUiPrimitiveProvider(
30
- {
31
- "ui:markdown-content": MockMarkdown,
32
- "ui:format-tokens": mockFormatTokens,
33
- "ui:format-duration": mockFormatDuration,
34
- },
35
- ui,
36
- ),
37
- );
38
- }
39
-
40
- describe("MinimalChatView", () => {
41
- afterEach(() => cleanup());
42
-
43
- it("renders each of the four entry kinds in order", () => {
44
- const entries: MinimalChatEntry[] = [
45
- { kind: "tool", toolName: "Read", input: { file_path: "/foo.ts" } },
46
- { kind: "text", text: "Hello world" },
47
- { kind: "thinking", text: "I should look here" },
48
- { kind: "error", text: "Boom" },
49
- ];
50
- renderView(
51
- <MinimalChatView title="alpha" status="running" entries={entries} />,
52
- );
53
- expect(screen.getByText("Read")).toBeTruthy();
54
- expect(screen.getByText("/foo.ts")).toBeTruthy();
55
- expect(screen.getByText(/Hello world/)).toBeTruthy();
56
- expect(screen.getByText("Thinking")).toBeTruthy();
57
- expect(screen.getByText("Boom")).toBeTruthy();
58
- });
59
-
60
- it("status enum maps to expected icon + color (5 values)", () => {
61
- const cases: Array<{ status: MinimalChatStatus; color: string }> = [
62
- { status: "complete", color: "text-green-400" },
63
- { status: "error", color: "text-red-400" },
64
- { status: "running", color: "text-yellow-400" },
65
- { status: "blocked", color: "text-orange-400" },
66
- { status: "pending", color: "text-[var(--text-tertiary)]" },
67
- ];
68
- for (const { status, color } of cases) {
69
- cleanup();
70
- const { container } = renderView(
71
- <MinimalChatView title="t" status={status} entries={[]} />,
72
- );
73
- // Walk all spans, find one whose classList contains both the color
74
- // and `inline-flex`. Avoids CSS.escape (not available in jsdom env).
75
- const spans = Array.from(container.querySelectorAll<HTMLElement>("span"));
76
- const span = spans.find(
77
- (s) => s.classList.contains(color) && s.classList.contains("inline-flex"),
78
- );
79
- expect(span, `expected color span for status=${status}`).toBeTruthy();
80
- }
81
- });
82
-
83
- it("three modes apply expected root container classes", () => {
84
- {
85
- const { container } = renderView(
86
- <MinimalChatView title="a" status="running" entries={[]} mode="popout" />,
87
- );
88
- const root = container.firstElementChild as HTMLElement;
89
- expect(root.className).toContain("h-full");
90
- expect(root.className).toContain("overflow-hidden");
91
- }
92
- cleanup();
93
- {
94
- const { container } = renderView(
95
- <MinimalChatView title="a" status="running" entries={[]} mode="inline" />,
96
- );
97
- const root = container.firstElementChild as HTMLElement;
98
- // Inline mode now uses a STABLE `h-[60vh]` (not `max-h-[60vh]`) so the
99
- // container doesn't bounce as content streams in. The body's
100
- // `flex-1 min-h-0 overflow-y-auto` provides the scroll surface.
101
- // See change: fix-flows-plugin-polish (stable inline height).
102
- expect(root.className).toContain("h-[60vh]");
103
- expect(root.className).not.toContain("max-h-[60vh]");
104
- }
105
- cleanup();
106
- {
107
- const { container } = renderView(
108
- <MinimalChatView title="a" status="running" entries={[{ kind: "text", text: "body" }]} mode="row" activity="reading" />,
109
- );
110
- // Row mode: no body, only single line
111
- expect(screen.queryByTestId("md")).toBeNull();
112
- expect(screen.getByText("a")).toBeTruthy();
113
- expect(screen.getByText("reading")).toBeTruthy();
114
- // Root has no flex-col h-full
115
- const root = container.firstElementChild as HTMLElement;
116
- expect(root.className).not.toContain("h-full");
117
- expect(root.className).not.toContain("h-[60vh]");
118
- }
119
- });
120
-
121
- it("hides meta when prop is omitted", () => {
122
- renderView(<MinimalChatView title="hdr" status="complete" entries={[]} />);
123
- expect(screen.queryByText(/↑/)).toBeNull();
124
- expect(screen.queryByText(/↓/)).toBeNull();
125
- });
126
-
127
- it("renders ↑/↓ tokens and formatted duration when meta supplied", () => {
128
- renderView(
129
- <MinimalChatView
130
- title="hdr"
131
- status="complete"
132
- entries={[]}
133
- meta={{ tokens: { input: 1234, output: 567 }, durationMs: 4500 }}
134
- />,
135
- );
136
- expect(screen.getByText(/↑1\.2k/)).toBeTruthy();
137
- expect(screen.getByText(/↓567/)).toBeTruthy();
138
- expect(screen.getByText(/4\.5s/)).toBeTruthy();
139
- });
140
-
141
- it("renders subtitle path under the title in monospace", () => {
142
- renderView(
143
- <MinimalChatView
144
- title="explorer"
145
- subtitle="~/.pi/agent/agents/Explore.md"
146
- status="complete"
147
- entries={[]}
148
- />,
149
- );
150
- const sub = screen.getByText("~/.pi/agent/agents/Explore.md");
151
- expect(sub).toBeTruthy();
152
- expect(sub.className).toContain("font-mono");
153
- expect(sub.getAttribute("title")).toBe("~/.pi/agent/agents/Explore.md");
154
- });
155
-
156
- it("tool entry without output shows no expand toggle", () => {
157
- renderView(
158
- <MinimalChatView
159
- title="t"
160
- status="running"
161
- entries={[{ kind: "tool", toolName: "Read", input: { file_path: "foo.ts" } }]}
162
- />,
163
- );
164
- expect(screen.queryByText("▸")).toBeNull();
165
- expect(screen.queryByText("▾")).toBeNull();
166
- });
167
-
168
- it("tool entry with output toggles open on click", () => {
169
- renderView(
170
- <MinimalChatView
171
- title="t"
172
- status="running"
173
- entries={[{ kind: "tool", toolName: "Bash", input: { command: "ls" }, output: "abc-output" }]}
174
- />,
175
- );
176
- const toggle = screen.getByText("▸");
177
- expect(screen.queryByText("abc-output")).toBeNull();
178
- fireEvent.click(toggle);
179
- expect(screen.getByText("▾")).toBeTruthy();
180
- expect(screen.getByText("abc-output")).toBeTruthy();
181
- });
182
-
183
- it("tool entry with isError paints row border and name red", () => {
184
- const { container } = renderView(
185
- <MinimalChatView
186
- title="t"
187
- status="error"
188
- entries={[{ kind: "tool", toolName: "Bash", input: {}, isError: true }]}
189
- />,
190
- );
191
- // Border red on row container
192
- const row = container.querySelector("div.border-l-2");
193
- expect(row?.className).toContain("border-red-500/50");
194
- // Name red
195
- const name = screen.getByText("Bash");
196
- expect(name.className).toContain("text-red-400");
197
- });
198
-
199
- it("renders the supplied empty-state message when entries is empty", () => {
200
- renderView(
201
- <MinimalChatView
202
- title="t"
203
- status="pending"
204
- entries={[]}
205
- emptyMessage="Waiting to start..."
206
- />,
207
- );
208
- expect(screen.getByText(/Waiting to start/)).toBeTruthy();
209
- });
210
-
211
- it("renders footer node below entries", () => {
212
- renderView(
213
- <MinimalChatView
214
- title="t"
215
- status="complete"
216
- entries={[]}
217
- footer={<div data-testid="footer">FOOTER</div>}
218
- />,
219
- );
220
- expect(screen.getByTestId("footer")).toBeTruthy();
221
- });
222
-
223
- it("throws when rendered without the UiPrimitiveProvider (negative case)", () => {
224
- // Suppress console.error noise from the rendered error
225
- const orig = console.error;
226
- console.error = () => {};
227
- try {
228
- expect(() =>
229
- render(<MinimalChatView title="t" status="running" entries={[]} />),
230
- ).toThrow();
231
- } finally {
232
- console.error = orig;
233
- }
234
- });
235
- });