summaryrefslogtreecommitdiffhomepage
path: root/src/core/protocol/reducer.test.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-11 16:06:48 +0900
committerAdam Malczewski <[email protected]>2026-06-11 16:06:48 +0900
commite45cab2a2d9d7bf5e48ace7111fd84b1b9bf2df3 (patch)
treee9cd8665a3eea609ef1e027906be4abdfe67d876 /src/core/protocol/reducer.test.ts
parentb3f7ba523f644224364d155b575fa3f9f13c5eb9 (diff)
downloaddispatch-web-e45cab2a2d9d7bf5e48ace7111fd84b1b9bf2df3.tar.gz
dispatch-web-e45cab2a2d9d7bf5e48ace7111fd84b1b9bf2df3.zip
feat(cache-warming,surfaces,metrics,markdown): conversation-scoped surfaces, cache warming + retention, markdown
Consumes the backend cache-warming + cache-rate handoffs end-to-end and adds supporting infra: - protocol/transport: conversation-scoped surfaces (conversationId on subscribe/invoke/surface + staleness routing); store auto-subscribes the catalog with the focused conversation and re-scopes on switch. - surface-host: generic Number field renderer + custom rendererId dispatch (graceful skip on unknown). - cache-warming feature: enabled toggle, min+sec interval, AUTHORITATIVE countdown from the surface's cache-warming-timer nextWarmAt, manual Warm now (POST /chat/warm), lastWarmAt-keyed history, cache-retention stat, expectedCacheRate headline. - metrics: cross-turn expected-cache (retention) derivation + bubble badge; cache-rate fix needs no code change (inputTokens now total). - markdown feature: marked + marked-highlight + highlight.js + dompurify, rendered in ChatView. - fixes (gemini review): {#key activeConversationId} remount of CacheWarmingView to stop history/feedback leaking across tabs; guard NaN interval inputs from committing 0. - docs/contracts: regenerated transport/ui-contract mirrors; backend-handoff updated (CR-3 resolved). Verified: svelte-check 0 errors, biome clean, 494 tests pass, vite build OK.
Diffstat (limited to 'src/core/protocol/reducer.test.ts')
-rw-r--r--src/core/protocol/reducer.test.ts110
1 files changed, 102 insertions, 8 deletions
diff --git a/src/core/protocol/reducer.test.ts b/src/core/protocol/reducer.test.ts
index 57e12f2..c8e517a 100644
--- a/src/core/protocol/reducer.test.ts
+++ b/src/core/protocol/reducer.test.ts
@@ -1,5 +1,12 @@
import { describe, expect, it } from "vitest";
-import { applyServerMessage, initialState, invoke, subscribe, unsubscribe } from "./reducer";
+import {
+ applyServerMessage,
+ getSurfaceSpec,
+ initialState,
+ invoke,
+ subscribe,
+ unsubscribe,
+} from "./reducer";
const makeSpec = (id: string, title = id) => ({
id,
@@ -32,11 +39,10 @@ describe("applyServerMessage — catalog", () => {
describe("applyServerMessage — surface", () => {
it("sets the spec for a subscribed surface", () => {
let s = initialState();
- const result = subscribe(s, "s1");
- s = result.state;
+ s = subscribe(s, "s1").state;
const spec = makeSpec("s1", "Surface 1");
const next = applyServerMessage(s, { type: "surface", spec });
- expect(next.subscriptions.get("s1")).toEqual(spec);
+ expect(getSurfaceSpec(next, "s1")).toEqual(spec);
});
it("ignores a surface message for a non-subscribed surface", () => {
@@ -56,7 +62,7 @@ describe("applyServerMessage — update", () => {
type: "update",
update: { surfaceId: "s1", spec: makeSpec("s1", "V2") },
});
- expect(next.subscriptions.get("s1")?.title).toBe("V2");
+ expect(getSurfaceSpec(next, "s1")?.title).toBe("V2");
});
it("ignores an update for a non-subscribed surface", () => {
@@ -86,7 +92,7 @@ describe("applyServerMessage — error", () => {
});
describe("subscribe", () => {
- it("emits exactly one subscribe message", () => {
+ it("emits exactly one subscribe message (global, no conversationId)", () => {
const s = initialState();
const result = subscribe(s, "s1");
expect(result.outgoing).toEqual([{ type: "subscribe", surfaceId: "s1" }]);
@@ -96,10 +102,14 @@ describe("subscribe", () => {
it("adds the surface to subscriptions with null spec", () => {
const s = initialState();
const result = subscribe(s, "s1");
- expect(result.state.subscriptions.get("s1")).toBeNull();
+ expect(result.state.subscriptions.get("s1")).toEqual({
+ conversationId: undefined,
+ spec: null,
+ });
+ expect(getSurfaceSpec(result.state, "s1")).toBeNull();
});
- it("is idempotent — second subscribe is a no-op", () => {
+ it("is idempotent — second subscribe with the same scope is a no-op", () => {
let s = initialState();
s = subscribe(s, "s1").state;
const result = subscribe(s, "s1");
@@ -108,6 +118,67 @@ describe("subscribe", () => {
});
});
+describe("subscribe — conversation-scoped", () => {
+ it("includes conversationId in the subscribe message", () => {
+ const s = initialState();
+ const result = subscribe(s, "cache-warming", "conv-A");
+ expect(result.outgoing).toEqual([
+ { type: "subscribe", surfaceId: "cache-warming", conversationId: "conv-A" },
+ ]);
+ expect(result.state.subscriptions.get("cache-warming")?.conversationId).toBe("conv-A");
+ });
+
+ it("re-scopes on conversation switch: unsubscribe old pair then subscribe new", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ s = applyServerMessage(s, {
+ type: "surface",
+ spec: makeSpec("cw", "A-spec"),
+ conversationId: "conv-A",
+ });
+ const result = subscribe(s, "cw", "conv-B");
+ expect(result.outgoing).toEqual([
+ { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
+ { type: "subscribe", surfaceId: "cw", conversationId: "conv-B" },
+ ]);
+ // previous spec retained until the new one arrives (no flicker)
+ expect(getSurfaceSpec(result.state, "cw")?.title).toBe("A-spec");
+ expect(result.state.subscriptions.get("cw")?.conversationId).toBe("conv-B");
+ });
+
+ it("drops a stale update echoing the previous conversationId", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ s = subscribe(s, "cw", "conv-B").state; // re-scoped to B
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "cw", spec: makeSpec("cw", "STALE-A"), conversationId: "conv-A" },
+ });
+ expect(getSurfaceSpec(next, "cw")).toBeNull(); // stale ignored, no spec yet for B
+ });
+
+ it("accepts an update echoing the current conversationId", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-B").state;
+ const next = applyServerMessage(s, {
+ type: "update",
+ update: { surfaceId: "cw", spec: makeSpec("cw", "B-spec"), conversationId: "conv-B" },
+ });
+ expect(getSurfaceSpec(next, "cw")?.title).toBe("B-spec");
+ });
+
+ it("accepts a global (no-echo) surface message even when subscribed with a conversationId", () => {
+ // loaded-extensions is global: server ignores our conversationId and echoes none.
+ let s = initialState();
+ s = subscribe(s, "loaded-extensions", "conv-A").state;
+ const next = applyServerMessage(s, {
+ type: "surface",
+ spec: makeSpec("loaded-extensions", "Ext"),
+ });
+ expect(getSurfaceSpec(next, "loaded-extensions")?.title).toBe("Ext");
+ });
+});
+
describe("unsubscribe", () => {
it("emits unsubscribe and drops the spec", () => {
let s = initialState();
@@ -118,6 +189,15 @@ describe("unsubscribe", () => {
expect(result.state.subscriptions.has("s1")).toBe(false);
});
+ it("includes conversationId for a scoped subscription", () => {
+ let s = initialState();
+ s = subscribe(s, "cw", "conv-A").state;
+ const result = unsubscribe(s, "cw");
+ expect(result.outgoing).toEqual([
+ { type: "unsubscribe", surfaceId: "cw", conversationId: "conv-A" },
+ ]);
+ });
+
it("is a no-op if not subscribed", () => {
const s = initialState();
const result = unsubscribe(s, "nope");
@@ -143,6 +223,20 @@ describe("invoke", () => {
]);
});
+ it("includes conversationId when provided", () => {
+ const s = initialState();
+ const result = invoke(s, "cw", "cache-warming/set-interval", 120, "conv-A");
+ expect(result.outgoing).toEqual([
+ {
+ type: "invoke",
+ surfaceId: "cw",
+ actionId: "cache-warming/set-interval",
+ payload: 120,
+ conversationId: "conv-A",
+ },
+ ]);
+ });
+
it("does not mutate state", () => {
const s = initialState();
const result = invoke(s, "s1", "a1");