Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

promptStashStore.test.ts

208 lines6.9 KB

1import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
2​
3import { removeLocalStorageItem } from "./hooks/useLocalStorage";
4​
5import {
6 MAX_STASH_ENTRIES,
7 PROMPT_STASH_STORAGE_KEY,
8 MAX_STASH_ENTRY_ATTACHMENT_CHARS,
9 partitionStashAttachments,
10 usePromptStashStore,
11 writePromptStashStorageForTest,
12 type PromptStashEntry,
13} from "./promptStashStore";
14​
15function makeEntry(input: {
16 id: string;
17 prompt?: string;
18 attachmentChars?: number;
19}): PromptStashEntry {
20 return {
21 id: input.id,
22 createdAt: "2026-07-24T12:00:00.000Z",
23 prompt: input.prompt ?? `prompt ${input.id}`,
24 attachments:
25 input.attachmentChars !== undefined
26 ? [
27 {
28 id: `${input.id}-img`,
29 name: "shot.png",
30 mimeType: "image/png",
31 sizeBytes: input.attachmentChars,
32 dataUrl: "x".repeat(input.attachmentChars),
33 },
34 ]
35 : [],
36 droppedImageNames: [],
37 };
39​
40function resetPromptStashStore() {
41 usePromptStashStore.setState({ entries: [] });
42 writePromptStashStorageForTest("");
43 removeLocalStorageItem(PROMPT_STASH_STORAGE_KEY);
45​
46describe("partitionStashAttachments", () => {
47 it("keeps attachments within the budget and reports dropped names in order", () => {
48 const small = {
49 id: "a",
50 name: "small.png",
51 mimeType: "image/png",
52 sizeBytes: 10,
53 dataUrl: "x".repeat(10),
54 };
55 const huge = {
56 id: "b",
57 name: "huge.png",
58 mimeType: "image/png",
59 sizeBytes: MAX_STASH_ENTRY_ATTACHMENT_CHARS,
60 dataUrl: "x".repeat(MAX_STASH_ENTRY_ATTACHMENT_CHARS),
61 };
62 const alsoSmall = {
63 id: "c",
64 name: "also-small.png",
65 mimeType: "image/png",
66 sizeBytes: 10,
67 dataUrl: "x".repeat(10),
68 };
69 const { kept, droppedNames } = partitionStashAttachments([small, huge, alsoSmall]);
70 expect(kept.map((attachment) => attachment.id)).toEqual(["a", "c"]);
71 expect(droppedNames).toEqual(["huge.png"]);
72 });
73​
74 it("admits a single attachment that exactly fits the budget", () => {
75 const exact = {
76 id: "a",
77 name: "exact.png",
78 mimeType: "image/png",
79 sizeBytes: MAX_STASH_ENTRY_ATTACHMENT_CHARS,
80 dataUrl: "x".repeat(MAX_STASH_ENTRY_ATTACHMENT_CHARS),
81 };
82 const { kept, droppedNames } = partitionStashAttachments([exact]);
83 expect(kept).toHaveLength(1);
84 expect(droppedNames).toEqual([]);
85 });
86});
87​
88describe("promptStashStore", () => {
89 beforeEach(() => {
90 resetPromptStashStore();
91 });
92​
93 afterEach(() => {
94 resetPromptStashStore();
95 });
96​
97 it("prepends entries so the newest stash is first", () => {
98 const store = usePromptStashStore.getState();
99 store.stashEntry(makeEntry({ id: "first" }));
100 store.stashEntry(makeEntry({ id: "second" }));
101 const entries = usePromptStashStore.getState().entries;
102 expect(entries.map((entry) => entry.id)).toEqual(["second", "first"]);
103 });
104​
105 it("evicts the oldest entry past the cap and returns it", () => {
106 const store = usePromptStashStore.getState();
107 for (let index = 0; index < MAX_STASH_ENTRIES; index += 1) {
108 expect(store.stashEntry(makeEntry({ id: `entry-${index}` })).evicted).toBeNull();
109 }
110 const { evicted } = store.stashEntry(makeEntry({ id: "overflow" }));
111 expect(evicted?.id).toBe("entry-0");
112 const entries = usePromptStashStore.getState().entries;
113 expect(entries).toHaveLength(MAX_STASH_ENTRIES);
114 expect(entries[0]?.id).toBe("overflow");
115 });
116​
117 // This test environment has no `localStorage`, so the store runs on its
118 // in-memory fallback — the exact "kept for this session, gone on reload"
119 // case the composer must distinguish from an outright write failure.
120 it("distinguishes a memory-only write (written, not durable) from a failed one", () => {
121 const store = usePromptStashStore.getState();
122 const result = store.stashEntry(makeEntry({ id: "memory-only" }));
123 expect(result.written).toBe(true);
124 expect(result.durable).toBe(false);
125 expect(usePromptStashStore.getState().entries.map((entry) => entry.id)).toEqual([
126 "memory-only",
127 ]);
128 });
129​
130 it("takeEntry removes and returns the entry; second take returns null", () => {
131 const store = usePromptStashStore.getState();
132 store.stashEntry(makeEntry({ id: "keep" }));
133 store.stashEntry(makeEntry({ id: "take" }));
134 expect(store.takeEntry("take").entry?.id).toBe("take");
135 expect(store.takeEntry("take").entry).toBeNull();
136 const entries = usePromptStashStore.getState().entries;
137 expect(entries.map((entry) => entry.id)).toEqual(["keep"]);
138 });
139​
140 it("finalizeEntryImages attaches images and clears the pending count", () => {
141 const store = usePromptStashStore.getState();
142 store.stashEntry({ ...makeEntry({ id: "pending" }), pendingImageCount: 2 });
143​
144 const { attached } = store.finalizeEntryImages("pending", {
145 attachments: [
146 {
147 id: "img-1",
148 name: "a.webp",
149 mimeType: "image/webp",
150 sizeBytes: 10,
151 dataUrl: "data:image/webp;base64,AAAA",
152 },
153 ],
154 droppedImageNames: ["big.png"],
155 unreadableImageNames: [],
156 });
157​
158 expect(attached).toBe(true);
159 const entry = usePromptStashStore.getState().entries[0];
160 expect(entry?.attachments).toHaveLength(1);
161 expect(entry?.droppedImageNames).toEqual(["big.png"]);
162 expect(entry?.pendingImageCount).toBe(0);
163 });
164​
165 it("finalizeEntryImages reports false when the entry was already taken", () => {
166 const store = usePromptStashStore.getState();
167 store.stashEntry({ ...makeEntry({ id: "racing" }), pendingImageCount: 1 });
168 // Restored (or deleted) while its images were still encoding.
169 store.takeEntry("racing");
170​
171 const { attached } = store.finalizeEntryImages("racing", {
172 attachments: [],
173 droppedImageNames: [],
174 unreadableImageNames: [],
175 });
176​
177 expect(attached).toBe(false);
178 });
179​
180 it("settles a pending count left behind by a crashed or closed session", () => {
181 writePromptStashStorageForTest(
182 JSON.stringify({
183 version: 2,
184 state: {
185 entries: [{ ...makeEntry({ id: "orphan" }), pendingImageCount: 2 }],
186 },
187 }),
188 );
189​
190 // Hydration must settle the stale count, or the entry would stay stuck
191 // showing "saving…" with images that no longer exist anywhere.
192 const entry = usePromptStashStore.getState().entries[0];
193 expect(entry?.pendingImageCount).toBe(0);
194 expect(entry?.unreadableImageNames).toHaveLength(2);
195 });
196​
197 it("ignores an unreadable v1 payload seeded under the current key", () => {
198 // The v1 shape (per-provider queues) does not decode as v2; hydration
199 // must fall back to an empty stash rather than throw.
200 writePromptStashStorageForTest(
201 JSON.stringify({
202 version: 1,
203 state: { queuesByScopeKey: { "provider:claudeAgent": [] } },
204 }),
205 );
206 expect(usePromptStashStore.getState().entries).toEqual([]);
207 });
208});