Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { act, useRef } from "react";
3import { createRoot } from "react-dom/client";
4import { describe, expect, it, vi } from "vite-plus/test";
5
6import { importComposerThreadAttachment } from "./composerThreadImport";
7import { THREAD_CONTEXT_DROP_EVENT, useThreadContextDrop } from "./threadContextDrag";
8
9function deferredFile() {
10 let resolve!: (file: File) => void;
11 const promise = new Promise<File>((resolveFile) => {
12 resolve = resolveFile;
13 });
14 return { promise, resolve };
15}
16
17describe("importComposerThreadAttachment from a thread drop", () => {
18 it.each(["expand", "draft switch", "environment switch", "unmount"])(
19 "keeps import ownership correct after %s while loading",
20 async (change) => {
21 const target = new EventTarget();
22 const transcript = deferredFile();
23 const pendingImports = new Map<string, number>();
24 const attached: string[] = [];
25 const expandedAtDrop: boolean[] = [];
26 let importing!: Promise<void>;
27 function Composer({
28 environmentId,
29 targetKey,
30 expanded,
31 }: {
32 environmentId: EnvironmentId;
33 targetKey: string;
34 expanded: boolean;
35 }) {
36 const targetRef = useRef<EventTarget | null>(target);
37 useThreadContextDrop(targetRef, environmentId, targetKey, (_refs, isActive) => {
38 expandedAtDrop.push(expanded);
39 importing = importComposerThreadAttachment({
40 targetKey,
41 pendingImports,
42 countReservedAttachments: () => pendingImports.get(targetKey) ?? 0,
43 load: () => transcript.promise,
44 isActive,
45 attach: async (file) => {
46 attached.push(file.name);
47 return true;
48 },
49 onLimitReached: () => {
50 throw new Error("The slot should be available");
51 },
52 });
53 });
54 return null;
55 }
56
57 const props = {
58 environmentId: EnvironmentId.make("destination"),
59 targetKey: "draft",
60 expanded: false,
61 };
62 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
63 const root = createRoot(document.createElement("div"));
64 try {
65 act(() => root.render(<Composer {...props} />));
66 act(() => target.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: [] })));
67 expect(pendingImports.get("draft")).toBe(1);
68 if (change === "unmount") {
69 act(() => root.unmount());
70 } else {
71 act(() =>
72 root.render(
73 <Composer
74 {...props}
75 expanded
76 {...(change === "draft switch" ? { targetKey: "another-draft" } : {})}
77 {...(change === "environment switch"
78 ? { environmentId: EnvironmentId.make("another-environment") }
79 : {})}
80 />,
81 ),
82 );
83 if (change !== "expand") {
84 act(() => root.render(<Composer {...props} />));
85 }
86 }
87 transcript.resolve(new File(["transcript"], "thread.txt"));
88 await act(async () => importing);
89 expect(attached).toEqual(change === "expand" ? ["thread.txt"] : []);
90 expect(pendingImports.size).toBe(0);
91 if (change === "expand") {
92 act(() =>
93 target.dispatchEvent(new CustomEvent(THREAD_CONTEXT_DROP_EVENT, { detail: [] })),
94 );
95 await act(async () => importing);
96 expect(expandedAtDrop).toEqual([false, true]);
97 expect(attached).toEqual(["thread.txt", "thread.txt"]);
98 }
99 } finally {
100 if (change !== "unmount") act(() => root.unmount());
101 vi.unstubAllGlobals();
102 }
103 },
104 );
105});