@blackbelt-technology/pi-dashboard-extension 0.5.3 → 0.5.4
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/package.json +2 -2
- package/src/__tests__/bridge-default-model-gate.test.ts +47 -0
- package/src/__tests__/bridge-followup-chat-order.test.ts +215 -0
- package/src/__tests__/bridge-followup-multi-entry.test.ts +202 -0
- package/src/__tests__/bridge-queue-update-forward.test.ts +77 -0
- package/src/__tests__/bridge-retry-ordering.test.ts +148 -0
- package/src/__tests__/bridge-shadow-queue-drain.test.ts +221 -0
- package/src/__tests__/bridge-shadow-queue-gate.test.ts +299 -0
- package/src/__tests__/bridge-shutdown-reset.test.ts +238 -0
- package/src/__tests__/bridge-slash-command-routing.test.ts +127 -31
- package/src/__tests__/command-handler.test.ts +105 -3
- package/src/__tests__/fixtures/usage-limit-error-strings.ts +127 -0
- package/src/__tests__/source-detector.test.ts +15 -0
- package/src/__tests__/usage-limit-orderer.test.ts +12 -0
- package/src/bridge-default-model-gate.ts +32 -0
- package/src/bridge.ts +299 -20
- package/src/command-handler.ts +53 -7
- package/src/dashboard-default-adapter.ts +5 -0
- package/src/prompt-bus.ts +15 -0
- package/src/slash-dispatch.ts +30 -15
- package/src/source-detector.ts +13 -5
- package/src/usage-limit-orderer.ts +18 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blackbelt-technology/pi-dashboard-extension",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "Pi bridge extension for pi-dashboard",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
".pi/skills/pi-dashboard/"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@blackbelt-technology/pi-dashboard-shared": "^0.5.
|
|
27
|
+
"@blackbelt-technology/pi-dashboard-shared": "^0.5.4",
|
|
28
28
|
"ws": "^8.18.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { shouldApplyDefaultModel } from "../bridge-default-model-gate.js";
|
|
3
|
+
|
|
4
|
+
describe("shouldApplyDefaultModel", () => {
|
|
5
|
+
const base = { hasModelRegistry: true, hasDefaultModel: true };
|
|
6
|
+
|
|
7
|
+
it("applies for a brand-new session (reason=startup, entries=0)", () => {
|
|
8
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "startup", entryCount: 0 })).toBe(true);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("does NOT apply for resumed sessions (entries>0, reason=startup)", () => {
|
|
12
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "startup", entryCount: 5 })).toBe(false);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("does NOT apply for in-process new (reason=new)", () => {
|
|
16
|
+
// pi handles its own default for in-process /new — bridge stays out
|
|
17
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "new", entryCount: 0 })).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("does NOT apply for in-process resume (reason=resume)", () => {
|
|
21
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "resume", entryCount: 5 })).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("does NOT apply for in-process fork (reason=fork)", () => {
|
|
25
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "fork", entryCount: 5 })).toBe(false);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("does NOT apply for reload of in-flight session (reason=reload, entries>0)", () => {
|
|
29
|
+
expect(shouldApplyDefaultModel({ ...base, reason: "reload", entryCount: 5 })).toBe(false);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("does NOT apply when defaultModel is not configured", () => {
|
|
33
|
+
expect(
|
|
34
|
+
shouldApplyDefaultModel({ ...base, hasDefaultModel: false, reason: "startup", entryCount: 0 }),
|
|
35
|
+
).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("does NOT apply when model registry not yet available", () => {
|
|
39
|
+
expect(
|
|
40
|
+
shouldApplyDefaultModel({ ...base, hasModelRegistry: false, reason: "startup", entryCount: 0 }),
|
|
41
|
+
).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("does NOT apply when reason is undefined", () => {
|
|
45
|
+
expect(shouldApplyDefaultModel({ ...base, reason: undefined, entryCount: 0 })).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge wire-ordering invariant for drained queued user messages.
|
|
3
|
+
*
|
|
4
|
+
* Applies to BOTH drain boundaries:
|
|
5
|
+
* - Steer drain at `turn_end` (Enter while streaming)
|
|
6
|
+
* - Follow-up drain at `agent_end` (Alt+Enter while streaming)
|
|
7
|
+
*
|
|
8
|
+
* Pi emits four events synchronously back-to-back at the drain boundary:
|
|
9
|
+
*
|
|
10
|
+
* 1. message_end (assistant — the final response of the just-completed turn)
|
|
11
|
+
* 2. turn_end OR agent_end (the drain boundary)
|
|
12
|
+
* 3. message_start (user — the drained steer or follow-up text, e.g. "asd")
|
|
13
|
+
* 4. message_end (user — same text)
|
|
14
|
+
*
|
|
15
|
+
* Pre-fix bug: the bridge defers `message_end` sends via `setTimeout(0)`
|
|
16
|
+
* (for entryId capture per `fix-per-message-fork`) but sends `message_start`
|
|
17
|
+
* synchronously. The drained user `message_start` therefore lands on the
|
|
18
|
+
* wire BEFORE the preceding assistant `message_end`. The client's reducer
|
|
19
|
+
* appends the user message to `state.messages[]` at `message_start`, then
|
|
20
|
+
* the assistant message at the (later) `message_end` — so the chat shows
|
|
21
|
+
* the follow-up "asd" ABOVE the assistant's final response.
|
|
22
|
+
*
|
|
23
|
+
* Fix: defer user `message_start` sends via the same `setTimeout(0)`. All
|
|
24
|
+
* user-role messages are queued in the timer FIFO behind any pending
|
|
25
|
+
* `message_end` deferrals, preserving pi's emit order on the wire.
|
|
26
|
+
*
|
|
27
|
+
* Assistant `message_start` MUST stay sync — `message_update` events fire
|
|
28
|
+
* sync and depend on the reducer having seen `message_start` first (to
|
|
29
|
+
* reset `streamingTextFlushed`).
|
|
30
|
+
*
|
|
31
|
+
* See change: add-followup-edit-and-steer-cancel (chat-order scenario).
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { describe, it, expect } from "vitest";
|
|
35
|
+
|
|
36
|
+
interface WireEvent {
|
|
37
|
+
eventType: string;
|
|
38
|
+
role?: "user" | "assistant";
|
|
39
|
+
content?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Simulates the bridge's event-forwarding pipeline with the new
|
|
44
|
+
* deferral rule applied to USER message_start. Runs everything in
|
|
45
|
+
* a single synchronous tick to mirror pi's emit cadence, then drains
|
|
46
|
+
* the macrotask queue to capture the final wire order.
|
|
47
|
+
*/
|
|
48
|
+
class BridgeSim {
|
|
49
|
+
readonly wire: WireEvent[] = [];
|
|
50
|
+
|
|
51
|
+
onMessageStart(role: "user" | "assistant", content: string): void {
|
|
52
|
+
if (role === "user") {
|
|
53
|
+
// FIX: defer user message_start to match message_end's deferral.
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
this.wire.push({ eventType: "message_start", role, content });
|
|
56
|
+
}, 0);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// Assistant message_start sent sync (message_update depends on it).
|
|
60
|
+
this.wire.push({ eventType: "message_start", role, content });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
onMessageEnd(role: "user" | "assistant", content: string): void {
|
|
64
|
+
// Existing behaviour: ALL message_end sends are deferred via setTimeout(0)
|
|
65
|
+
// for entryId capture (fix-per-message-fork).
|
|
66
|
+
setTimeout(() => {
|
|
67
|
+
this.wire.push({ eventType: "message_end", role, content });
|
|
68
|
+
}, 0);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onAgentEnd(): void {
|
|
72
|
+
// Sent sync.
|
|
73
|
+
this.wire.push({ eventType: "agent_end" });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
onTurnEnd(): void {
|
|
77
|
+
// Sent sync (mirrors agent_end).
|
|
78
|
+
this.wire.push({ eventType: "turn_end" });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Flush pending setTimeout(0) callbacks. */
|
|
82
|
+
async flush(): Promise<void> {
|
|
83
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
84
|
+
// Two extra ticks in case any callback re-queues.
|
|
85
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 0));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
describe("Bridge drained-followup chat-order invariant", () => {
|
|
90
|
+
it("user message_start for drained follow-up lands AFTER preceding assistant message_end", async () => {
|
|
91
|
+
const sim = new BridgeSim();
|
|
92
|
+
|
|
93
|
+
// Pi's sync emit order at agent_end with a queued follow-up:
|
|
94
|
+
// 1. assistant message_end (the weather report)
|
|
95
|
+
// 2. agent_end
|
|
96
|
+
// 3. user message_start ("asd" — the drained follow-up)
|
|
97
|
+
// 4. user message_end ("asd")
|
|
98
|
+
sim.onMessageEnd("assistant", "weather report");
|
|
99
|
+
sim.onAgentEnd();
|
|
100
|
+
sim.onMessageStart("user", "asd");
|
|
101
|
+
sim.onMessageEnd("user", "asd");
|
|
102
|
+
|
|
103
|
+
await sim.flush();
|
|
104
|
+
|
|
105
|
+
const types = sim.wire.map((e) => `${e.eventType}:${e.role ?? "-"}`);
|
|
106
|
+
// Expected wire order after the fix:
|
|
107
|
+
// 1. agent_end (sync, fires first)
|
|
108
|
+
// 2. assistant message_end (deferred, FIFO #1)
|
|
109
|
+
// 3. user message_start (deferred, FIFO #2 — after the fix)
|
|
110
|
+
// 4. user message_end (deferred, FIFO #3)
|
|
111
|
+
expect(types).toEqual([
|
|
112
|
+
"agent_end:-",
|
|
113
|
+
"message_end:assistant",
|
|
114
|
+
"message_start:user",
|
|
115
|
+
"message_end:user",
|
|
116
|
+
]);
|
|
117
|
+
|
|
118
|
+
// Critical invariant: the drained user message_start MUST NOT come
|
|
119
|
+
// before the preceding assistant message_end.
|
|
120
|
+
const userStartIdx = sim.wire.findIndex(
|
|
121
|
+
(e) => e.eventType === "message_start" && e.role === "user"
|
|
122
|
+
);
|
|
123
|
+
const assistantEndIdx = sim.wire.findIndex(
|
|
124
|
+
(e) => e.eventType === "message_end" && e.role === "assistant"
|
|
125
|
+
);
|
|
126
|
+
expect(assistantEndIdx).toBeGreaterThanOrEqual(0);
|
|
127
|
+
expect(userStartIdx).toBeGreaterThan(assistantEndIdx);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("assistant message_start stays SYNC (message_update relies on reducer seeing it first)", () => {
|
|
131
|
+
const sim = new BridgeSim();
|
|
132
|
+
sim.onMessageStart("assistant", "hello");
|
|
133
|
+
// No flush — the event must already be on the wire.
|
|
134
|
+
expect(sim.wire).toEqual([
|
|
135
|
+
{ eventType: "message_start", role: "assistant", content: "hello" },
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("multiple drained follow-ups preserve their relative pi emit order", async () => {
|
|
140
|
+
const sim = new BridgeSim();
|
|
141
|
+
|
|
142
|
+
// Pi delivers two queued follow-ups in order ["a", "b"].
|
|
143
|
+
sim.onMessageEnd("assistant", "final");
|
|
144
|
+
sim.onAgentEnd();
|
|
145
|
+
sim.onMessageStart("user", "a");
|
|
146
|
+
sim.onMessageEnd("user", "a");
|
|
147
|
+
sim.onMessageStart("user", "b");
|
|
148
|
+
sim.onMessageEnd("user", "b");
|
|
149
|
+
|
|
150
|
+
await sim.flush();
|
|
151
|
+
|
|
152
|
+
const summary = sim.wire.map((e) =>
|
|
153
|
+
e.eventType === "agent_end"
|
|
154
|
+
? "agent_end"
|
|
155
|
+
: `${e.eventType}:${e.role}:${e.content}`
|
|
156
|
+
);
|
|
157
|
+
expect(summary).toEqual([
|
|
158
|
+
"agent_end",
|
|
159
|
+
"message_end:assistant:final",
|
|
160
|
+
"message_start:user:a",
|
|
161
|
+
"message_end:user:a",
|
|
162
|
+
"message_start:user:b",
|
|
163
|
+
"message_end:user:b",
|
|
164
|
+
]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("drained STEER at turn_end lands AFTER preceding assistant message_end (same bug, different drain boundary)", async () => {
|
|
168
|
+
const sim = new BridgeSim();
|
|
169
|
+
|
|
170
|
+
// Pi's sync emit order at turn_end with a queued steer:
|
|
171
|
+
// 1. assistant message_end (the weather report)
|
|
172
|
+
// 2. turn_end
|
|
173
|
+
// 3. user message_start ("asd" — the drained steer)
|
|
174
|
+
// 4. user message_end ("asd")
|
|
175
|
+
// (Identical to the follow-up case but the drain boundary is turn_end
|
|
176
|
+
// instead of agent_end. The fix is uniform: defer USER message_start.)
|
|
177
|
+
sim.onMessageEnd("assistant", "weather report");
|
|
178
|
+
sim.onTurnEnd();
|
|
179
|
+
sim.onMessageStart("user", "asd");
|
|
180
|
+
sim.onMessageEnd("user", "asd");
|
|
181
|
+
|
|
182
|
+
await sim.flush();
|
|
183
|
+
|
|
184
|
+
const types = sim.wire.map((e) => `${e.eventType}:${e.role ?? "-"}`);
|
|
185
|
+
expect(types).toEqual([
|
|
186
|
+
"turn_end:-",
|
|
187
|
+
"message_end:assistant",
|
|
188
|
+
"message_start:user",
|
|
189
|
+
"message_end:user",
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
const userStartIdx = sim.wire.findIndex(
|
|
193
|
+
(e) => e.eventType === "message_start" && e.role === "user"
|
|
194
|
+
);
|
|
195
|
+
const assistantEndIdx = sim.wire.findIndex(
|
|
196
|
+
(e) => e.eventType === "message_end" && e.role === "assistant"
|
|
197
|
+
);
|
|
198
|
+
expect(userStartIdx).toBeGreaterThan(assistantEndIdx);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("idle user send (no preceding deferred message_end) still arrives intact", async () => {
|
|
202
|
+
const sim = new BridgeSim();
|
|
203
|
+
|
|
204
|
+
// No pending deferrals in flight — a fresh user prompt.
|
|
205
|
+
sim.onMessageStart("user", "hi");
|
|
206
|
+
sim.onMessageEnd("user", "hi");
|
|
207
|
+
|
|
208
|
+
await sim.flush();
|
|
209
|
+
|
|
210
|
+
expect(sim.wire.map((e) => `${e.eventType}:${e.role}`)).toEqual([
|
|
211
|
+
"message_start:user",
|
|
212
|
+
"message_end:user",
|
|
213
|
+
]);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v2 bridge behavior tests for the multi-entry follow-up queue.
|
|
3
|
+
*
|
|
4
|
+
* Encodes the contract of the bridge's `rewriteFollowupQueue` helper and the
|
|
5
|
+
* three new browser-message handlers (promote / remove / edit_entry). Tests
|
|
6
|
+
* use a pure-helper reproduction (same shape as the production code) so the
|
|
7
|
+
* contract is verifiable without instantiating the full bridge.
|
|
8
|
+
*
|
|
9
|
+
* See change: add-followup-edit-and-steer-cancel (tasks 13.6, 13.7, 13.13).
|
|
10
|
+
*/
|
|
11
|
+
import { describe, it, expect, vi } from "vitest";
|
|
12
|
+
|
|
13
|
+
const FOLLOWUP_QUEUE_CAP = 20;
|
|
14
|
+
|
|
15
|
+
/** Pure reproduction of the bridge's follow-up shadow + rewrite logic. */
|
|
16
|
+
function makeShadow() {
|
|
17
|
+
const calls: Array<{ kind: "clear" } | { kind: "send"; text: string }> = [];
|
|
18
|
+
let bridgeFollowUp: string[] = [];
|
|
19
|
+
const fakePiClearFollowUpQueue = () => { calls.push({ kind: "clear" }); };
|
|
20
|
+
const fakePiSendUserMessage = (text: string, opts: { deliverAs: string }) => {
|
|
21
|
+
calls.push({ kind: "send", text });
|
|
22
|
+
expect(opts).toEqual({ deliverAs: "followUp" });
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function rewriteFollowupQueue(newEntries: string[]) {
|
|
26
|
+
const capped = newEntries.slice(0, FOLLOWUP_QUEUE_CAP);
|
|
27
|
+
fakePiClearFollowUpQueue();
|
|
28
|
+
for (const t of capped) fakePiSendUserMessage(t, { deliverAs: "followUp" });
|
|
29
|
+
bridgeFollowUp = [...capped];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function recordFollowupSent(text: string, isStreaming: boolean) {
|
|
33
|
+
if (!isStreaming) return;
|
|
34
|
+
if (bridgeFollowUp.length >= FOLLOWUP_QUEUE_CAP) return;
|
|
35
|
+
bridgeFollowUp.push(text);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Browser-message handlers (mirror bridge.ts shape)
|
|
39
|
+
function handlePromoteEntry(index: number) {
|
|
40
|
+
if (index < 0 || index >= bridgeFollowUp.length) return;
|
|
41
|
+
const head = bridgeFollowUp[index];
|
|
42
|
+
const rest = bridgeFollowUp.filter((_, i) => i !== index);
|
|
43
|
+
rewriteFollowupQueue([head, ...rest]);
|
|
44
|
+
}
|
|
45
|
+
function handleRemoveEntry(index: number) {
|
|
46
|
+
if (index < 0 || index >= bridgeFollowUp.length) return;
|
|
47
|
+
const surviving = bridgeFollowUp.filter((_, i) => i !== index);
|
|
48
|
+
rewriteFollowupQueue(surviving);
|
|
49
|
+
}
|
|
50
|
+
function handleEditEntry(index: number, text: string) {
|
|
51
|
+
if (index < 0 || index >= bridgeFollowUp.length) return;
|
|
52
|
+
const next = bridgeFollowUp.map((t, i) => (i === index ? text : t));
|
|
53
|
+
rewriteFollowupQueue(next);
|
|
54
|
+
}
|
|
55
|
+
function handleEditFollowupSlotV1Compat(text: string) {
|
|
56
|
+
rewriteFollowupQueue([text]);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
snapshot: () => [...bridgeFollowUp],
|
|
61
|
+
calls,
|
|
62
|
+
rewriteFollowupQueue,
|
|
63
|
+
recordFollowupSent,
|
|
64
|
+
handlePromoteEntry,
|
|
65
|
+
handleRemoveEntry,
|
|
66
|
+
handleEditEntry,
|
|
67
|
+
handleEditFollowupSlotV1Compat,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
describe("bridge follow-up multi-entry queue: rewrite helper", () => {
|
|
72
|
+
it("clears pi + sends each entry in new order", () => {
|
|
73
|
+
const s = makeShadow();
|
|
74
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
75
|
+
expect(s.calls).toEqual([
|
|
76
|
+
{ kind: "clear" },
|
|
77
|
+
{ kind: "send", text: "a" },
|
|
78
|
+
{ kind: "send", text: "b" },
|
|
79
|
+
{ kind: "send", text: "c" },
|
|
80
|
+
]);
|
|
81
|
+
expect(s.snapshot()).toEqual(["a", "b", "c"]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("clears + sends nothing for empty rewrite (queue drained)", () => {
|
|
85
|
+
const s = makeShadow();
|
|
86
|
+
s.rewriteFollowupQueue([]);
|
|
87
|
+
expect(s.calls).toEqual([{ kind: "clear" }]);
|
|
88
|
+
expect(s.snapshot()).toEqual([]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("caps at FOLLOWUP_QUEUE_CAP (20)", () => {
|
|
92
|
+
const s = makeShadow();
|
|
93
|
+
const big = Array.from({ length: 25 }, (_, i) => `entry-${i}`);
|
|
94
|
+
s.rewriteFollowupQueue(big);
|
|
95
|
+
const sends = s.calls.filter((c) => c.kind === "send");
|
|
96
|
+
expect(sends).toHaveLength(20);
|
|
97
|
+
expect(s.snapshot()).toHaveLength(20);
|
|
98
|
+
expect(s.snapshot()[0]).toBe("entry-0");
|
|
99
|
+
expect(s.snapshot()[19]).toBe("entry-19");
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("bridge follow-up multi-entry queue: recordFollowupSent (append)", () => {
|
|
104
|
+
it("appends when streaming", () => {
|
|
105
|
+
const s = makeShadow();
|
|
106
|
+
s.recordFollowupSent("a", true);
|
|
107
|
+
s.recordFollowupSent("b", true);
|
|
108
|
+
expect(s.snapshot()).toEqual(["a", "b"]);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("does NOT append when idle (race fix)", () => {
|
|
112
|
+
const s = makeShadow();
|
|
113
|
+
s.recordFollowupSent("a", false);
|
|
114
|
+
expect(s.snapshot()).toEqual([]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("drops silently at soft cap", () => {
|
|
118
|
+
const s = makeShadow();
|
|
119
|
+
for (let i = 0; i < FOLLOWUP_QUEUE_CAP; i++) {
|
|
120
|
+
s.recordFollowupSent(`e${i}`, true);
|
|
121
|
+
}
|
|
122
|
+
expect(s.snapshot()).toHaveLength(FOLLOWUP_QUEUE_CAP);
|
|
123
|
+
s.recordFollowupSent("over-cap", true);
|
|
124
|
+
expect(s.snapshot()).toHaveLength(FOLLOWUP_QUEUE_CAP);
|
|
125
|
+
expect(s.snapshot()).not.toContain("over-cap");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe("bridge follow-up multi-entry queue: promote handler", () => {
|
|
130
|
+
it("moves entry at index N to position 0", () => {
|
|
131
|
+
const s = makeShadow();
|
|
132
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
133
|
+
s.handlePromoteEntry(2); // promote "c" to head
|
|
134
|
+
expect(s.snapshot()).toEqual(["c", "a", "b"]);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("promoting index 0 is a no-op (already at head)", () => {
|
|
138
|
+
const s = makeShadow();
|
|
139
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
140
|
+
s.handlePromoteEntry(0);
|
|
141
|
+
expect(s.snapshot()).toEqual(["a", "b", "c"]);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("out-of-bounds index is ignored", () => {
|
|
145
|
+
const s = makeShadow();
|
|
146
|
+
s.rewriteFollowupQueue(["a"]);
|
|
147
|
+
const before = s.snapshot();
|
|
148
|
+
s.handlePromoteEntry(5);
|
|
149
|
+
s.handlePromoteEntry(-1);
|
|
150
|
+
expect(s.snapshot()).toEqual(before);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("bridge follow-up multi-entry queue: remove handler", () => {
|
|
155
|
+
it("removes entry at index N", () => {
|
|
156
|
+
const s = makeShadow();
|
|
157
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
158
|
+
s.handleRemoveEntry(1);
|
|
159
|
+
expect(s.snapshot()).toEqual(["a", "c"]);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("removes last entry to leave empty queue", () => {
|
|
163
|
+
const s = makeShadow();
|
|
164
|
+
s.rewriteFollowupQueue(["only"]);
|
|
165
|
+
s.handleRemoveEntry(0);
|
|
166
|
+
expect(s.snapshot()).toEqual([]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("out-of-bounds index is ignored", () => {
|
|
170
|
+
const s = makeShadow();
|
|
171
|
+
s.rewriteFollowupQueue(["a", "b"]);
|
|
172
|
+
const before = s.snapshot();
|
|
173
|
+
s.handleRemoveEntry(99);
|
|
174
|
+
expect(s.snapshot()).toEqual(before);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("bridge follow-up multi-entry queue: edit handler", () => {
|
|
179
|
+
it("replaces entry at index N with new text", () => {
|
|
180
|
+
const s = makeShadow();
|
|
181
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
182
|
+
s.handleEditEntry(1, "b-revised");
|
|
183
|
+
expect(s.snapshot()).toEqual(["a", "b-revised", "c"]);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("out-of-bounds index is ignored", () => {
|
|
187
|
+
const s = makeShadow();
|
|
188
|
+
s.rewriteFollowupQueue(["only"]);
|
|
189
|
+
const before = s.snapshot();
|
|
190
|
+
s.handleEditEntry(99, "nope");
|
|
191
|
+
expect(s.snapshot()).toEqual(before);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
describe("bridge follow-up multi-entry queue: v1 edit_followup_slot back-compat", () => {
|
|
196
|
+
it("replaces the ENTIRE queue with a single entry (v1 semantic)", () => {
|
|
197
|
+
const s = makeShadow();
|
|
198
|
+
s.rewriteFollowupQueue(["a", "b", "c"]);
|
|
199
|
+
s.handleEditFollowupSlotV1Compat("replacement");
|
|
200
|
+
expect(s.snapshot()).toEqual(["replacement"]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests that the bridge forwards pi's `queue_update` event as a typed
|
|
3
|
+
* QueueUpdateToServerMessage. Also covers idempotent listener registration
|
|
4
|
+
* via pi.on("queue_update", ...).
|
|
5
|
+
*
|
|
6
|
+
* See change: add-followup-edit-and-steer-cancel.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, expect, vi } from "vitest";
|
|
9
|
+
|
|
10
|
+
// We don't test the full bridge here (too much wiring) — we drive the
|
|
11
|
+
// listener-registration-and-forward shape directly with a fake pi.
|
|
12
|
+
|
|
13
|
+
describe("bridge queue_update forwarding (shape contract)", () => {
|
|
14
|
+
it("registered queue_update listener emits a typed QueueUpdateToServerMessage on event", () => {
|
|
15
|
+
// Simulate the listener registration the bridge performs.
|
|
16
|
+
const listeners: Record<string, (event: any) => void> = {};
|
|
17
|
+
const fakePi = {
|
|
18
|
+
on: vi.fn((eventType: string, handler: any) => { listeners[eventType] = handler; }),
|
|
19
|
+
};
|
|
20
|
+
const sent: any[] = [];
|
|
21
|
+
const fakeConnection = { send: (m: any) => sent.push(m) };
|
|
22
|
+
const sessionId = "S1";
|
|
23
|
+
|
|
24
|
+
// Equivalent of the bridge's pi.on("queue_update", ...) registration.
|
|
25
|
+
fakePi.on("queue_update", (event: any) => {
|
|
26
|
+
const steering = Array.isArray(event?.steering) ? Array.from(event.steering as readonly string[]) : [];
|
|
27
|
+
const followUp = Array.isArray(event?.followUp) ? Array.from(event.followUp as readonly string[]) : [];
|
|
28
|
+
fakeConnection.send({ type: "queue_update", sessionId, steering, followUp });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Fire pi's queue_update event.
|
|
32
|
+
listeners["queue_update"]({ type: "queue_update", steering: ["a", "b"], followUp: ["c"] });
|
|
33
|
+
|
|
34
|
+
expect(sent).toHaveLength(1);
|
|
35
|
+
expect(sent[0]).toEqual({
|
|
36
|
+
type: "queue_update",
|
|
37
|
+
sessionId: "S1",
|
|
38
|
+
steering: ["a", "b"],
|
|
39
|
+
followUp: ["c"],
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("forwards empty arrays when pi reports empty queues", () => {
|
|
44
|
+
const listeners: Record<string, (event: any) => void> = {};
|
|
45
|
+
const fakePi = { on: vi.fn((t: string, h: any) => { listeners[t] = h; }) };
|
|
46
|
+
const sent: any[] = [];
|
|
47
|
+
const sessionId = "S2";
|
|
48
|
+
|
|
49
|
+
fakePi.on("queue_update", (event: any) => {
|
|
50
|
+
const steering = Array.isArray(event?.steering) ? Array.from(event.steering as readonly string[]) : [];
|
|
51
|
+
const followUp = Array.isArray(event?.followUp) ? Array.from(event.followUp as readonly string[]) : [];
|
|
52
|
+
sent.push({ type: "queue_update", sessionId, steering, followUp });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
listeners["queue_update"]({ type: "queue_update", steering: [], followUp: [] });
|
|
56
|
+
|
|
57
|
+
expect(sent).toEqual([{ type: "queue_update", sessionId: "S2", steering: [], followUp: [] }]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("defends against malformed event payloads (missing arrays)", () => {
|
|
61
|
+
const listeners: Record<string, (event: any) => void> = {};
|
|
62
|
+
const fakePi = { on: vi.fn((t: string, h: any) => { listeners[t] = h; }) };
|
|
63
|
+
const sent: any[] = [];
|
|
64
|
+
const sessionId = "S3";
|
|
65
|
+
|
|
66
|
+
fakePi.on("queue_update", (event: any) => {
|
|
67
|
+
const steering = Array.isArray(event?.steering) ? Array.from(event.steering as readonly string[]) : [];
|
|
68
|
+
const followUp = Array.isArray(event?.followUp) ? Array.from(event.followUp as readonly string[]) : [];
|
|
69
|
+
sent.push({ type: "queue_update", sessionId, steering, followUp });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Pi returns object missing the expected fields.
|
|
73
|
+
listeners["queue_update"]({ type: "queue_update" });
|
|
74
|
+
|
|
75
|
+
expect(sent).toEqual([{ type: "queue_update", sessionId: "S3", steering: [], followUp: [] }]);
|
|
76
|
+
});
|
|
77
|
+
});
|