@blackbelt-technology/pi-dashboard-extension 0.3.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.
- package/.pi/skills/pi-dashboard/SKILL.md +134 -0
- package/.pi/skills/pi-dashboard/references/api-reference.md +470 -0
- package/.pi/skills/pi-dashboard/references/recipes.md +252 -0
- package/.pi/skills/pi-dashboard/scripts/dashboard-api.sh +75 -0
- package/package.json +38 -0
- package/src/__tests__/ask-user-tool.test.ts +432 -0
- package/src/__tests__/command-handler.test.ts +712 -0
- package/src/__tests__/connection.test.ts +344 -0
- package/src/__tests__/credentials-updated.test.ts +26 -0
- package/src/__tests__/dashboard-default-adapter.test.ts +77 -0
- package/src/__tests__/dev-build.test.ts +79 -0
- package/src/__tests__/enrich-model-metadata.test.ts +201 -0
- package/src/__tests__/event-forwarder.test.ts +89 -0
- package/src/__tests__/fork-entryid-timing.test.ts +100 -0
- package/src/__tests__/git-info.test.ts +124 -0
- package/src/__tests__/git-link-builder.test.ts +102 -0
- package/src/__tests__/openspec-activity-detector.test.ts +232 -0
- package/src/__tests__/openspec-poller.test.ts +124 -0
- package/src/__tests__/process-metrics.test.ts +47 -0
- package/src/__tests__/process-scanner-kill.test.ts +61 -0
- package/src/__tests__/process-scanner.test.ts +202 -0
- package/src/__tests__/prompt-bus-wiring.test.ts +791 -0
- package/src/__tests__/prompt-bus.test.ts +469 -0
- package/src/__tests__/prompt-expander.test.ts +54 -0
- package/src/__tests__/provider-register-reload.test.ts +394 -0
- package/src/__tests__/server-auto-start.test.ts +258 -0
- package/src/__tests__/server-launcher.test.ts +61 -0
- package/src/__tests__/server-probe.test.ts +25 -0
- package/src/__tests__/session-switch.test.ts +139 -0
- package/src/__tests__/session-sync.test.ts +55 -0
- package/src/__tests__/source-detector.test.ts +73 -0
- package/src/__tests__/stats-extractor.test.ts +92 -0
- package/src/__tests__/tui-prompt-adapter.test.ts +207 -0
- package/src/__tests__/watchdog.test.ts +161 -0
- package/src/ask-user-tool.ts +352 -0
- package/src/bridge-context.ts +64 -0
- package/src/bridge.ts +1182 -0
- package/src/command-handler.ts +533 -0
- package/src/connection.ts +204 -0
- package/src/dashboard-default-adapter.ts +37 -0
- package/src/dev-build.ts +39 -0
- package/src/event-forwarder.ts +40 -0
- package/src/flow-event-wiring.ts +85 -0
- package/src/git-info.ts +55 -0
- package/src/git-link-builder.ts +112 -0
- package/src/model-tracker.ts +56 -0
- package/src/pi-env.d.ts +37 -0
- package/src/process-metrics.ts +70 -0
- package/src/process-scanner.ts +430 -0
- package/src/prompt-bus.ts +240 -0
- package/src/prompt-expander.ts +108 -0
- package/src/provider-register.ts +564 -0
- package/src/server-auto-start.ts +113 -0
- package/src/server-launcher.ts +126 -0
- package/src/server-probe.ts +33 -0
- package/src/session-sync.ts +155 -0
- package/src/source-detector.ts +26 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for enrichModelMetadata — the pure helper that resolves a discovered
|
|
3
|
+
* custom-provider model id against a catalog probe (typically backed by pi's
|
|
4
|
+
* `modelRegistry.find(provider, id)`) and falls back to api-appropriate
|
|
5
|
+
* defaults when the probe has no match.
|
|
6
|
+
*
|
|
7
|
+
* The helper takes an optional `probe` parameter so unit tests can supply a
|
|
8
|
+
* fake catalog without needing `@mariozechner/pi-ai` installed — in
|
|
9
|
+
* production, registerEntry() injects `modelRegistry.find` as the probe.
|
|
10
|
+
*
|
|
11
|
+
* Spec: openspec/changes/enrich-custom-provider-model-metadata/specs/provider-auth-bridge/spec.md
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect } from "vitest";
|
|
15
|
+
import { enrichModelMetadata, type CatalogProbe } from "../provider-register.js";
|
|
16
|
+
|
|
17
|
+
// Minimal fake catalog mirroring a subset of pi-ai's real MODELS export.
|
|
18
|
+
// Keys are `${provider}|${id}` so our probe is a single Map lookup.
|
|
19
|
+
const FAKE_CATALOG = new Map<string, any>([
|
|
20
|
+
[
|
|
21
|
+
"anthropic|claude-opus-4-7",
|
|
22
|
+
{
|
|
23
|
+
contextWindow: 1_000_000,
|
|
24
|
+
maxTokens: 128_000,
|
|
25
|
+
reasoning: true,
|
|
26
|
+
cost: { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 },
|
|
27
|
+
input: ["text", "image"],
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
"anthropic|claude-sonnet-4-6",
|
|
32
|
+
{
|
|
33
|
+
contextWindow: 1_000_000,
|
|
34
|
+
maxTokens: 64_000,
|
|
35
|
+
reasoning: true,
|
|
36
|
+
cost: { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 },
|
|
37
|
+
input: ["text", "image"],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
"anthropic|claude-haiku-4-5",
|
|
42
|
+
{
|
|
43
|
+
contextWindow: 200_000,
|
|
44
|
+
maxTokens: 64_000,
|
|
45
|
+
reasoning: true,
|
|
46
|
+
cost: { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 },
|
|
47
|
+
input: ["text", "image"],
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
"openai|gpt-5",
|
|
52
|
+
{
|
|
53
|
+
contextWindow: 400_000,
|
|
54
|
+
maxTokens: 128_000,
|
|
55
|
+
reasoning: true,
|
|
56
|
+
cost: { input: 1.25, output: 10, cacheRead: 0.125, cacheWrite: 0 },
|
|
57
|
+
input: ["text", "image"],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
"opencode|claude-opus-4-7",
|
|
62
|
+
{
|
|
63
|
+
// Deliberately different values so tests can prove the candidate-
|
|
64
|
+
// provider probe order is deterministic (anthropic checked first).
|
|
65
|
+
contextWindow: 999,
|
|
66
|
+
maxTokens: 1,
|
|
67
|
+
reasoning: true,
|
|
68
|
+
cost: { input: 9, output: 9, cacheRead: 9, cacheWrite: 9 },
|
|
69
|
+
input: ["text"],
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const fakeProbe: CatalogProbe = (provider, id) =>
|
|
75
|
+
FAKE_CATALOG.get(`${provider}|${id}`) ?? null;
|
|
76
|
+
|
|
77
|
+
describe("enrichModelMetadata — catalog matches via probe", () => {
|
|
78
|
+
it("resolves `cc/claude-opus-4-7` + anthropic-messages to Opus 4.7 catalog entry (1M ctx)", () => {
|
|
79
|
+
const result = enrichModelMetadata("cc/claude-opus-4-7", "anthropic-messages", fakeProbe);
|
|
80
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
81
|
+
expect(result.maxTokens).toBe(128_000);
|
|
82
|
+
expect(result.reasoning).toBe(true);
|
|
83
|
+
expect(result.cost).toEqual({
|
|
84
|
+
input: 5,
|
|
85
|
+
output: 25,
|
|
86
|
+
cacheRead: 0.5,
|
|
87
|
+
cacheWrite: 6.25,
|
|
88
|
+
});
|
|
89
|
+
expect(result.input).toEqual(["text", "image"]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("resolves bare `claude-sonnet-4-6` + anthropic-messages to Sonnet 4.6 (1M ctx)", () => {
|
|
93
|
+
const result = enrichModelMetadata("claude-sonnet-4-6", "anthropic-messages", fakeProbe);
|
|
94
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
95
|
+
expect(result.reasoning).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("resolves `anthropic/claude-opus-4-7` prefix to Opus 4.7 (1M ctx)", () => {
|
|
99
|
+
const result = enrichModelMetadata("anthropic/claude-opus-4-7", "anthropic-messages", fakeProbe);
|
|
100
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
101
|
+
expect(result.maxTokens).toBe(128_000);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("resolves `claude-haiku-4-5` + anthropic-messages to Haiku 4.5 (200k ctx — verifies we don't over-claim)", () => {
|
|
105
|
+
const result = enrichModelMetadata("claude-haiku-4-5", "anthropic-messages", fakeProbe);
|
|
106
|
+
expect(result.contextWindow).toBe(200_000);
|
|
107
|
+
expect(result.reasoning).toBe(true);
|
|
108
|
+
expect(result.cost.input).toBe(1);
|
|
109
|
+
expect(result.cost.output).toBe(5);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("resolves `openrouter/openai/gpt-5` prefix to gpt-5 via openai-completions candidates", () => {
|
|
113
|
+
const result = enrichModelMetadata("openrouter/openai/gpt-5", "openai-completions", fakeProbe);
|
|
114
|
+
expect(result.contextWindow).toBe(400_000);
|
|
115
|
+
expect(result.maxTokens).toBe(128_000);
|
|
116
|
+
expect(result.reasoning).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("enrichModelMetadata — fallback defaults (no probe or no match)", () => {
|
|
121
|
+
it("no probe supplied → falls back to api-appropriate default", () => {
|
|
122
|
+
const result = enrichModelMetadata("cc/claude-opus-4-7", "anthropic-messages");
|
|
123
|
+
expect(result.contextWindow).toBe(200_000);
|
|
124
|
+
expect(result.maxTokens).toBe(64_000);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("unknown id + anthropic-messages returns 200k / 64k / no reasoning / zero cost / text+image", () => {
|
|
128
|
+
const result = enrichModelMetadata("some-unknown-anthropic-model", "anthropic-messages", fakeProbe);
|
|
129
|
+
expect(result.contextWindow).toBe(200_000);
|
|
130
|
+
expect(result.maxTokens).toBe(64_000);
|
|
131
|
+
expect(result.reasoning).toBe(false);
|
|
132
|
+
expect(result.cost).toEqual({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
|
133
|
+
expect(result.input).toEqual(["text", "image"]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("unknown id + openai-completions returns 128k / 16k / zero cost / text+image", () => {
|
|
137
|
+
const result = enrichModelMetadata("some-unknown-openai-model", "openai-completions", fakeProbe);
|
|
138
|
+
expect(result.contextWindow).toBe(128_000);
|
|
139
|
+
expect(result.maxTokens).toBe(16_384);
|
|
140
|
+
expect(result.reasoning).toBe(false);
|
|
141
|
+
expect(result.cost).toEqual({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
|
142
|
+
expect(result.input).toEqual(["text", "image"]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("unknown id + google-generative-ai returns 1M / 65k / zero cost / text+image", () => {
|
|
146
|
+
const result = enrichModelMetadata("some-unknown-gemini-model", "google-generative-ai", fakeProbe);
|
|
147
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
148
|
+
expect(result.maxTokens).toBe(65_536);
|
|
149
|
+
expect(result.reasoning).toBe(false);
|
|
150
|
+
expect(result.cost).toEqual({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
|
151
|
+
expect(result.input).toEqual(["text", "image"]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("prefixed unknown + openai-completions falls back (both prefixed and bare miss)", () => {
|
|
155
|
+
const result = enrichModelMetadata("minimax/custom-private-model", "openai-completions", fakeProbe);
|
|
156
|
+
expect(result.contextWindow).toBe(128_000);
|
|
157
|
+
expect(result.maxTokens).toBe(16_384);
|
|
158
|
+
expect(result.cost.input).toBe(0);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("no api argument + no probe defaults to openai-completions fallback", () => {
|
|
162
|
+
const result = enrichModelMetadata("some-unknown-model");
|
|
163
|
+
expect(result.contextWindow).toBe(128_000);
|
|
164
|
+
expect(result.maxTokens).toBe(16_384);
|
|
165
|
+
expect(result.input).toEqual(["text", "image"]);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("unknown api string + probe still probes openai-completions candidates", () => {
|
|
169
|
+
const result = enrichModelMetadata("some-unknown-model", "some-weird-api", fakeProbe);
|
|
170
|
+
expect(result.contextWindow).toBe(128_000);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("probe that throws is tolerated (falls through to fallback)", () => {
|
|
174
|
+
const throwingProbe: CatalogProbe = () => {
|
|
175
|
+
throw new Error("registry not ready");
|
|
176
|
+
};
|
|
177
|
+
const result = enrichModelMetadata("claude-opus-4-7", "anthropic-messages", throwingProbe);
|
|
178
|
+
expect(result.contextWindow).toBe(200_000); // anthropic-messages fallback
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("enrichModelMetadata — probe order determinism", () => {
|
|
183
|
+
it("prefers `anthropic` over `opencode` for anthropic-messages (first in candidate list wins)", () => {
|
|
184
|
+
// The fake catalog has `claude-opus-4-7` under BOTH `anthropic` (1M) and
|
|
185
|
+
// `opencode` (999). With api=anthropic-messages, candidates = ["anthropic", "opencode"]
|
|
186
|
+
// so the anthropic entry must win.
|
|
187
|
+
const result = enrichModelMetadata("claude-opus-4-7", "anthropic-messages", fakeProbe);
|
|
188
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
189
|
+
expect(result.maxTokens).toBe(128_000);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("multi-segment prefix `openrouter/anthropic/claude-opus-4-7` resolves via bare-id lookup", () => {
|
|
193
|
+
// After stripping the last segment, `claude-opus-4-7` matches `anthropic` (1M).
|
|
194
|
+
const result = enrichModelMetadata(
|
|
195
|
+
"openrouter/anthropic/claude-opus-4-7",
|
|
196
|
+
"anthropic-messages",
|
|
197
|
+
fakeProbe,
|
|
198
|
+
);
|
|
199
|
+
expect(result.contextWindow).toBe(1_000_000);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { mapEventToProtocol } from "../event-forwarder.js";
|
|
3
|
+
|
|
4
|
+
describe("mapEventToProtocol", () => {
|
|
5
|
+
const sessionId = "test-session-1";
|
|
6
|
+
|
|
7
|
+
it("should map a message_update event", () => {
|
|
8
|
+
const piEvent = {
|
|
9
|
+
type: "message_update",
|
|
10
|
+
message: {
|
|
11
|
+
role: "assistant",
|
|
12
|
+
content: [{ type: "text", text: "Hello world" }],
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
17
|
+
expect(result.type).toBe("event_forward");
|
|
18
|
+
expect(result.sessionId).toBe(sessionId);
|
|
19
|
+
expect(result.event.eventType).toBe("message_update");
|
|
20
|
+
expect(result.event.timestamp).toBeGreaterThan(0);
|
|
21
|
+
expect(result.event.data).toEqual(piEvent);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should map a tool_execution_start event", () => {
|
|
25
|
+
const piEvent = {
|
|
26
|
+
type: "tool_execution_start",
|
|
27
|
+
toolCallId: "tc1",
|
|
28
|
+
toolName: "bash",
|
|
29
|
+
args: { command: "ls -la" },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
33
|
+
expect(result.event.eventType).toBe("tool_execution_start");
|
|
34
|
+
expect(result.event.data.toolName).toBe("bash");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should map an agent_start event", () => {
|
|
38
|
+
const piEvent = { type: "agent_start" };
|
|
39
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
40
|
+
expect(result.event.eventType).toBe("agent_start");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should map an agent_end event", () => {
|
|
44
|
+
const piEvent = { type: "agent_end", messages: [] };
|
|
45
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
46
|
+
expect(result.event.eventType).toBe("agent_end");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should map a turn_end event", () => {
|
|
50
|
+
const piEvent = {
|
|
51
|
+
type: "turn_end",
|
|
52
|
+
turnIndex: 2,
|
|
53
|
+
message: { role: "assistant", content: [] },
|
|
54
|
+
toolResults: [],
|
|
55
|
+
};
|
|
56
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
57
|
+
expect(result.event.eventType).toBe("turn_end");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should map a session_compact event", () => {
|
|
61
|
+
const piEvent = {
|
|
62
|
+
type: "session_compact",
|
|
63
|
+
compactionEntry: { summary: "compacted" },
|
|
64
|
+
fromExtension: false,
|
|
65
|
+
};
|
|
66
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
67
|
+
expect(result.event.eventType).toBe("session_compact");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should handle unknown event types gracefully", () => {
|
|
71
|
+
const piEvent = { type: "some_future_event", data: 123 };
|
|
72
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
73
|
+
expect(result.event.eventType).toBe("some_future_event");
|
|
74
|
+
expect(result.event.data).toEqual(piEvent);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should strip non-serializable fields", () => {
|
|
78
|
+
const piEvent = {
|
|
79
|
+
type: "test_event",
|
|
80
|
+
signal: new AbortController().signal, // not serializable
|
|
81
|
+
text: "hello",
|
|
82
|
+
};
|
|
83
|
+
const result = mapEventToProtocol(sessionId, piEvent);
|
|
84
|
+
// The signal should be stripped during serialization
|
|
85
|
+
const serialized = JSON.stringify(result);
|
|
86
|
+
expect(serialized).not.toContain("signal");
|
|
87
|
+
expect(JSON.parse(serialized).event.data.text).toBe("hello");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for fork entryId timing fix.
|
|
3
|
+
*
|
|
4
|
+
* Verifies that the bridge's message_end entryId enrichment captures the
|
|
5
|
+
* correct leaf ID (after pi core persists the entry), not the stale one
|
|
6
|
+
* (before appendMessage runs).
|
|
7
|
+
*
|
|
8
|
+
* The bug: pi core emits message_end via _emit() BEFORE calling
|
|
9
|
+
* sessionManager.appendMessage(), so getLeafId() returns the previous leaf.
|
|
10
|
+
* The fix: the bridge defers getLeafId() for message_end using queueMicrotask,
|
|
11
|
+
* allowing appendMessage to run first.
|
|
12
|
+
*/
|
|
13
|
+
import { describe, it, expect, vi } from "vitest";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Simulates the pi core + bridge interaction for entryId enrichment.
|
|
17
|
+
*
|
|
18
|
+
* Pi core's _processAgentEvent does:
|
|
19
|
+
* 1. _emit(event) — bridge handler called (async, not awaited)
|
|
20
|
+
* 2. appendMessage(msg) — updates leafId synchronously
|
|
21
|
+
*
|
|
22
|
+
* The bridge handler (async) should yield via queueMicrotask before reading
|
|
23
|
+
* getLeafId(), so that appendMessage has already run.
|
|
24
|
+
*/
|
|
25
|
+
describe("message_end entryId timing", () => {
|
|
26
|
+
it("deferred getLeafId() captures the post-persist entry ID", async () => {
|
|
27
|
+
// Simulate sessionManager with mutable leafId
|
|
28
|
+
let leafId = "user-entry-100"; // stale leaf before appendMessage
|
|
29
|
+
const sessionManager = {
|
|
30
|
+
getLeafId: () => leafId,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
let capturedEntryId: string | undefined;
|
|
34
|
+
|
|
35
|
+
// Simulate the bridge handler (with the fix: defers via queueMicrotask)
|
|
36
|
+
const bridgeHandler = async () => {
|
|
37
|
+
// This is what the fixed bridge does for message_end:
|
|
38
|
+
await new Promise<void>(resolve => queueMicrotask(resolve));
|
|
39
|
+
capturedEntryId = sessionManager.getLeafId();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Simulate pi core's _processAgentEvent:
|
|
43
|
+
// 1. _emit calls handler (async, NOT awaited)
|
|
44
|
+
const handlerPromise = bridgeHandler();
|
|
45
|
+
// 2. appendMessage runs synchronously, updating leafId
|
|
46
|
+
leafId = "assistant-entry-101";
|
|
47
|
+
|
|
48
|
+
// Wait for the deferred handler to complete
|
|
49
|
+
await handlerPromise;
|
|
50
|
+
|
|
51
|
+
expect(capturedEntryId).toBe("assistant-entry-101");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("immediate getLeafId() would capture the stale entry ID (demonstrates the bug)", async () => {
|
|
55
|
+
let leafId = "user-entry-100";
|
|
56
|
+
const sessionManager = {
|
|
57
|
+
getLeafId: () => leafId,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
let capturedEntryId: string | undefined;
|
|
61
|
+
|
|
62
|
+
// Simulate the OLD (buggy) bridge handler: reads getLeafId() immediately
|
|
63
|
+
const buggyBridgeHandler = async () => {
|
|
64
|
+
// No deferral — reads leafId before appendMessage runs
|
|
65
|
+
capturedEntryId = sessionManager.getLeafId();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Simulate pi core's _processAgentEvent:
|
|
69
|
+
const handlerPromise = buggyBridgeHandler();
|
|
70
|
+
leafId = "assistant-entry-101"; // too late — handler already read it
|
|
71
|
+
|
|
72
|
+
await handlerPromise;
|
|
73
|
+
|
|
74
|
+
// Bug: captures the stale leaf, not the assistant's entry
|
|
75
|
+
expect(capturedEntryId).toBe("user-entry-100");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("message_start should still capture entryId immediately (no deferral)", async () => {
|
|
79
|
+
let leafId = "previous-assistant-entry-99";
|
|
80
|
+
const sessionManager = {
|
|
81
|
+
getLeafId: () => leafId,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
let capturedEntryId: string | undefined;
|
|
85
|
+
|
|
86
|
+
// Simulate bridge handler for message_start (immediate, no deferral)
|
|
87
|
+
const messageStartHandler = async () => {
|
|
88
|
+
capturedEntryId = sessionManager.getLeafId();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const handlerPromise = messageStartHandler();
|
|
92
|
+
// User entry gets written after message_start
|
|
93
|
+
leafId = "user-entry-100";
|
|
94
|
+
|
|
95
|
+
await handlerPromise;
|
|
96
|
+
|
|
97
|
+
// message_start should capture the leaf BEFORE the user entry is written
|
|
98
|
+
expect(capturedEntryId).toBe("previous-assistant-entry-99");
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for git-info.ts.
|
|
3
|
+
*
|
|
4
|
+
* The file now delegates to `@blackbelt-technology/pi-dashboard-shared/platform/git.js`
|
|
5
|
+
* (the Recipe-based tool module). We mock that module so the tests focus
|
|
6
|
+
* on the git-info orchestration logic (branch detection, detached HEAD
|
|
7
|
+
* fallback, PR detection) without spawning git.
|
|
8
|
+
*
|
|
9
|
+
* See change: platform-command-executor.
|
|
10
|
+
*/
|
|
11
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
12
|
+
|
|
13
|
+
const { currentBranchOr, headShaOr, remoteUrlOr, prNumberOr } = vi.hoisted(() => ({
|
|
14
|
+
currentBranchOr: vi.fn(),
|
|
15
|
+
headShaOr: vi.fn(),
|
|
16
|
+
remoteUrlOr: vi.fn(),
|
|
17
|
+
prNumberOr: vi.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("@blackbelt-technology/pi-dashboard-shared/platform/git.js", () => ({
|
|
21
|
+
currentBranchOr,
|
|
22
|
+
headShaOr,
|
|
23
|
+
remoteUrlOr,
|
|
24
|
+
prNumberOr,
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
import { gatherGitInfo, detectBranch, detectRemoteUrl, detectPrNumber } from "../git-info.js";
|
|
28
|
+
|
|
29
|
+
describe("git-info", () => {
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
currentBranchOr.mockReset();
|
|
32
|
+
headShaOr.mockReset();
|
|
33
|
+
remoteUrlOr.mockReset();
|
|
34
|
+
prNumberOr.mockReset();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe("detectBranch", () => {
|
|
38
|
+
it("returns branch name", () => {
|
|
39
|
+
currentBranchOr.mockReturnValue("main");
|
|
40
|
+
expect(detectBranch("/test")).toBe("main");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("returns undefined when not a git repo", () => {
|
|
44
|
+
currentBranchOr.mockReturnValue(undefined);
|
|
45
|
+
expect(detectBranch("/test")).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns short SHA for detached HEAD", () => {
|
|
49
|
+
currentBranchOr.mockReturnValue("HEAD");
|
|
50
|
+
headShaOr.mockReturnValue("abc1234");
|
|
51
|
+
expect(detectBranch("/test")).toBe("abc1234");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("returns 'HEAD' as fallback if short SHA fails", () => {
|
|
55
|
+
currentBranchOr.mockReturnValue("HEAD");
|
|
56
|
+
headShaOr.mockReturnValue(undefined);
|
|
57
|
+
expect(detectBranch("/test")).toBe("HEAD");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("detectRemoteUrl", () => {
|
|
62
|
+
it("returns origin remote URL", () => {
|
|
63
|
+
remoteUrlOr.mockReturnValue("git@github.com:org/repo.git");
|
|
64
|
+
expect(detectRemoteUrl("/test")).toBe("git@github.com:org/repo.git");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("returns undefined when no remote is configured", () => {
|
|
68
|
+
remoteUrlOr.mockReturnValue(undefined);
|
|
69
|
+
expect(detectRemoteUrl("/test")).toBeUndefined();
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("detectPrNumber", () => {
|
|
74
|
+
it("returns PR number when gh finds one", () => {
|
|
75
|
+
prNumberOr.mockReturnValue(42);
|
|
76
|
+
expect(detectPrNumber("/test")).toBe(42);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("returns undefined when gh is missing or no PR exists", () => {
|
|
80
|
+
prNumberOr.mockReturnValue(undefined);
|
|
81
|
+
expect(detectPrNumber("/test")).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("gatherGitInfo", () => {
|
|
86
|
+
it("returns undefined when not a git repo", () => {
|
|
87
|
+
currentBranchOr.mockReturnValue(undefined);
|
|
88
|
+
expect(gatherGitInfo("/test")).toBeUndefined();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("returns GitInfo for a repo with branch + remote + PR", () => {
|
|
92
|
+
currentBranchOr.mockReturnValue("feature/x");
|
|
93
|
+
remoteUrlOr.mockReturnValue("git@github.com:org/repo.git");
|
|
94
|
+
prNumberOr.mockReturnValue(123);
|
|
95
|
+
|
|
96
|
+
const info = gatherGitInfo("/test");
|
|
97
|
+
expect(info?.gitBranch).toBe("feature/x");
|
|
98
|
+
expect(info?.gitPrNumber).toBe(123);
|
|
99
|
+
// Branch URLs URL-encode slashes (feature/x → feature%2Fx) in some builders
|
|
100
|
+
expect(info?.gitBranchUrl).toMatch(/feature(\/|%2F)x/);
|
|
101
|
+
expect(info?.gitPrUrl).toContain("123");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("returns GitInfo without links when there's no remote", () => {
|
|
105
|
+
currentBranchOr.mockReturnValue("main");
|
|
106
|
+
remoteUrlOr.mockReturnValue(undefined);
|
|
107
|
+
prNumberOr.mockReturnValue(undefined);
|
|
108
|
+
|
|
109
|
+
const info = gatherGitInfo("/test");
|
|
110
|
+
expect(info?.gitBranch).toBe("main");
|
|
111
|
+
expect(info?.gitBranchUrl).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("handles detached HEAD with short SHA", () => {
|
|
115
|
+
currentBranchOr.mockReturnValue("HEAD");
|
|
116
|
+
headShaOr.mockReturnValue("abc1234");
|
|
117
|
+
remoteUrlOr.mockReturnValue(undefined);
|
|
118
|
+
prNumberOr.mockReturnValue(undefined);
|
|
119
|
+
|
|
120
|
+
const info = gatherGitInfo("/test");
|
|
121
|
+
expect(info?.gitBranch).toBe("abc1234");
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { parseRemoteUrl, detectPlatform, buildGitLinks } from "../git-link-builder.js";
|
|
3
|
+
|
|
4
|
+
describe("parseRemoteUrl", () => {
|
|
5
|
+
it("parses SSH URL", () => {
|
|
6
|
+
expect(parseRemoteUrl("git@github.com:user/repo.git")).toEqual({
|
|
7
|
+
host: "github.com", user: "user", repo: "repo",
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("parses SSH URL without .git suffix", () => {
|
|
12
|
+
expect(parseRemoteUrl("git@github.com:user/repo")).toEqual({
|
|
13
|
+
host: "github.com", user: "user", repo: "repo",
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("parses HTTPS URL", () => {
|
|
18
|
+
expect(parseRemoteUrl("https://github.com/user/repo.git")).toEqual({
|
|
19
|
+
host: "github.com", user: "user", repo: "repo",
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("parses HTTPS URL without .git suffix", () => {
|
|
24
|
+
expect(parseRemoteUrl("https://gitlab.com/user/repo")).toEqual({
|
|
25
|
+
host: "gitlab.com", user: "user", repo: "repo",
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("parses nested group paths", () => {
|
|
30
|
+
expect(parseRemoteUrl("git@gitlab.com:group/subgroup/repo.git")).toEqual({
|
|
31
|
+
host: "gitlab.com", user: "group/subgroup", repo: "repo",
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("returns undefined for invalid URL", () => {
|
|
36
|
+
expect(parseRemoteUrl("not-a-url")).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("detectPlatform", () => {
|
|
41
|
+
it("detects GitHub", () => expect(detectPlatform("github.com")).toBe("github"));
|
|
42
|
+
it("detects GitLab", () => expect(detectPlatform("gitlab.com")).toBe("gitlab"));
|
|
43
|
+
it("detects Bitbucket", () => expect(detectPlatform("bitbucket.org")).toBe("bitbucket"));
|
|
44
|
+
it("detects Gitea", () => expect(detectPlatform("gitea.com")).toBe("gitea"));
|
|
45
|
+
it("detects Codeberg", () => expect(detectPlatform("codeberg.org")).toBe("codeberg"));
|
|
46
|
+
it("detects SourceHut", () => expect(detectPlatform("sr.ht")).toBe("sourcehut"));
|
|
47
|
+
it("returns undefined for unknown host", () => expect(detectPlatform("self-hosted.example.com")).toBeUndefined());
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("buildGitLinks", () => {
|
|
51
|
+
it("builds GitHub branch and PR links", () => {
|
|
52
|
+
const links = buildGitLinks("git@github.com:user/repo.git", "main", 42);
|
|
53
|
+
expect(links.branchUrl).toBe("https://github.com/user/repo/tree/main");
|
|
54
|
+
expect(links.prUrl).toBe("https://github.com/user/repo/pull/42");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("builds GitLab branch and MR links", () => {
|
|
58
|
+
const links = buildGitLinks("https://gitlab.com/user/repo.git", "main", 10);
|
|
59
|
+
expect(links.branchUrl).toBe("https://gitlab.com/user/repo/-/tree/main");
|
|
60
|
+
expect(links.prUrl).toBe("https://gitlab.com/user/repo/-/merge_requests/10");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("builds Bitbucket links", () => {
|
|
64
|
+
const links = buildGitLinks("git@bitbucket.org:user/repo.git", "develop", 5);
|
|
65
|
+
expect(links.branchUrl).toBe("https://bitbucket.org/user/repo/src/develop");
|
|
66
|
+
expect(links.prUrl).toBe("https://bitbucket.org/user/repo/pull-requests/5");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("builds Codeberg links", () => {
|
|
70
|
+
const links = buildGitLinks("https://codeberg.org/user/repo.git", "main", 3);
|
|
71
|
+
expect(links.branchUrl).toBe("https://codeberg.org/user/repo/src/branch/main");
|
|
72
|
+
expect(links.prUrl).toBe("https://codeberg.org/user/repo/pulls/3");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("builds SourceHut links", () => {
|
|
76
|
+
const links = buildGitLinks("git@sr.ht:user/repo.git", "main", 1);
|
|
77
|
+
expect(links.branchUrl).toBe("https://sr.ht/user/repo/tree/main");
|
|
78
|
+
expect(links.prUrl).toBe("https://sr.ht/user/repo/patches/1");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("URL-encodes branch names with slashes", () => {
|
|
82
|
+
const links = buildGitLinks("git@github.com:user/repo.git", "feat/my-feature");
|
|
83
|
+
expect(links.branchUrl).toBe("https://github.com/user/repo/tree/feat%2Fmy-feature");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("returns empty for unknown host", () => {
|
|
87
|
+
const links = buildGitLinks("git@self-hosted.example.com:user/repo.git", "main");
|
|
88
|
+
expect(links.branchUrl).toBeUndefined();
|
|
89
|
+
expect(links.prUrl).toBeUndefined();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("does not generate branch URL for detached HEAD", () => {
|
|
93
|
+
const links = buildGitLinks("git@github.com:user/repo.git", "HEAD");
|
|
94
|
+
expect(links.branchUrl).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("builds branch URL without PR", () => {
|
|
98
|
+
const links = buildGitLinks("git@github.com:user/repo.git", "main");
|
|
99
|
+
expect(links.branchUrl).toBe("https://github.com/user/repo/tree/main");
|
|
100
|
+
expect(links.prUrl).toBeUndefined();
|
|
101
|
+
});
|
|
102
|
+
});
|