Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import type { DraftId } from "./composerDraftStore";
4import { createPendingHandoverStore } from "./pendingHandoverStore";
5
6describe("createPendingHandoverStore", () => {
7 it("notifies a remounted subscriber when an existing generation completes", () => {
8 const store = createPendingHandoverStore();
9 let oldNotifications = 0;
10 const unsubscribe = store.subscribe(() => {
11 oldNotifications += 1;
12 });
13 store.setGenerating("source", true);
14 unsubscribe();
15 let observedGenerating = store.isGenerating("source");
16 let observedHandover = store.get("source");
17 store.subscribe(() => {
18 observedGenerating = store.isGenerating("source");
19 observedHandover = store.get("source");
20 });
21 const previousVersion = store.getVersion();
22 store.save("source", { handover: "Saved continuation" });
23 store.setGenerating("source", false);
24 expect(observedGenerating).toBe(false);
25 expect(observedHandover).toEqual({ handover: "Saved continuation" });
26 expect(store.getVersion()).toBeGreaterThan(previousVersion);
27 expect(oldNotifications).toBe(1);
28 store.delete("source");
29 expect(observedHandover).toBeUndefined();
30 });
31 it("keeps a generated handover recoverable by source thread", () => {
32 const store = createPendingHandoverStore();
33 store.save("environment:source", { handover: "handover" });
34
35 expect(store.get("environment:source")).toEqual({ handover: "handover" });
36 expect(store.get("environment:other")).toBeUndefined();
37 });
38
39 it("evicts the oldest abandoned handover", () => {
40 const store = createPendingHandoverStore(2);
41 store.save("thread:one", { handover: "one" });
42 store.save("thread:two", { handover: "two" });
43 store.save("thread:three", { handover: "three" });
44
45 expect(store.get("thread:one")).toBeUndefined();
46 expect(store.get("thread:two")).toEqual({ handover: "two" });
47 expect(store.get("thread:three")).toEqual({ handover: "three" });
48 expect(store.size()).toBe(2);
49 });
50
51 it("refreshes an existing entry before eviction", () => {
52 const store = createPendingHandoverStore(2);
53 store.save("thread:one", { handover: "one" });
54 store.save("thread:two", { handover: "two" });
55 store.save("thread:one", {
56 handover: "one updated",
57 draftId: "draft-one" as DraftId,
58 });
59 store.save("thread:three", { handover: "three" });
60
61 expect(store.get("thread:one")).toEqual({
62 handover: "one updated",
63 draftId: "draft-one",
64 });
65 expect(store.get("thread:two")).toBeUndefined();
66 });
67});