Skip to content
Source
Sign inSign up

sidebarPendingFileDropStore.test.ts

84 lines2.9 KB

1import { beforeEach, describe, expect, it } from "vite-plus/test";
2​
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
5​
6import {
7 useSidebarPendingFileDropStore,
8 type SidebarPendingFileDrop,
9} from "./sidebarPendingFileDropStore";
10​
11function makeFiles(...names: string[]): File[] {
12 return names.map((name) => new File(["x"], name));
14​
15function makeEntry(
16 environmentId: string,
17 threadId: string,
18 files: File[],
19): Omit<SidebarPendingFileDrop, "id"> {
20 return {
21 threadRef: scopeThreadRef(environmentId as EnvironmentId, ThreadId.make(threadId)),
22 files,
23 };
25​
26function fileNames(files: File[]): string[] {
27 return files.map((file) => file.name);
29​
30function refOf(environmentId: string, threadId: string) {
31 return makeEntry(environmentId, threadId, []).threadRef;
33​
34beforeEach(() => {
35 useSidebarPendingFileDropStore.setState({ pending: [] });
36});
37​
38describe("sidebarPendingFileDropStore", () => {
39 it("accumulates repeat drops onto the same thread instead of replacing", () => {
40 const store = useSidebarPendingFileDropStore.getState();
41 store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
42 store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
43​
44 expect(
45 fileNames(
46 useSidebarPendingFileDropStore
47 .getState()
48 .consumePendingFileDrop(refOf("env-1", "thread-1")) ?? [],
49 ),
50 ).toEqual(["a.png", "b.png"]);
51 expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
52 });
53​
54 it("keeps a newer drop deliverable after the first navigation fails", () => {
55 const store = useSidebarPendingFileDropStore.getState();
56 const firstId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("a.png")));
57 const secondId = store.queuePendingFileDrop(makeEntry("env-1", "thread-1", makeFiles("b.png")));
58​
59 useSidebarPendingFileDropStore.getState().clearPendingFileDrop(firstId);
60 const remaining = useSidebarPendingFileDropStore.getState().pending;
61 expect(remaining).toHaveLength(1);
62 expect(remaining[0]?.id).toBe(secondId);
63 expect(fileNames(remaining[0]?.files ?? [])).toEqual(["b.png"]);
64​
65 expect(
66 fileNames(
67 useSidebarPendingFileDropStore
68 .getState()
69 .consumePendingFileDrop(refOf("env-1", "thread-1")) ?? [],
70 ),
71 ).toEqual(["b.png"]);
72 expect(useSidebarPendingFileDropStore.getState().pending).toEqual([]);
73 });
74​
75 it("does not confuse refs whose joined keys collide on colons", () => {
76 const store = useSidebarPendingFileDropStore.getState();
77 store.queuePendingFileDrop(makeEntry("a", "b:c", makeFiles("a.png")));
78​
79 expect(
80 useSidebarPendingFileDropStore.getState().consumePendingFileDrop(refOf("a:b", "c")),
81 ).toBeNull();
82 expect(useSidebarPendingFileDropStore.getState().pending).toHaveLength(1);
83 });
84});