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

use-selected-thread-requests.test.tsx

142 lines4.6 KB

1import { renderToStaticMarkup } from "react-dom/server";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3​
4const fixture = vi.hoisted(() => ({
5 drafts: {} as Record<string, unknown>,
6 uploads: {} as Record<string, unknown>,
7 preparations: {} as Record<string, number>,
8 preparationAtom: Symbol("preparation"),
9}));
10vi.mock("react-native", () => ({ Alert: { alert: vi.fn() } }));
11vi.mock("@effect/atom-react", () => ({
12 useAtomValue: (atom: unknown) =>
13 atom === "drafts"
14 ? fixture.drafts
15 : atom === "uploads"
16 ? fixture.uploads
17 : atom === fixture.preparationAtom
18 ? fixture.preparations
19 : {},
20}));
21vi.mock("./use-composer-drafts", () => ({
22 composerDraftsAtom: "all-drafts",
23 questionAttachmentDraftsAtom: () => "drafts",
24 clearComposerDraft: vi.fn(),
25}));
26vi.mock("./composer-attachment-uploads", async () => ({
27 ...(await import("../lib/composerAttachmentUploadQueue")),
28 composerAttachmentUploadsAtom: "uploads",
29}));
30vi.mock("./question-attachments", async (importOriginal) => ({
31 ...(await importOriginal<typeof import("./question-attachments")>()),
32 questionAttachmentPreparationAtom: fixture.preparationAtom,
33}));
34vi.mock("./entities", () => ({
35 useServerConfigs: () =>
36 new Map([
37 [
38 "environment-1",
39 {
40 environment: {
41 capabilities: {
42 questionAttachments: true,
43 attachmentUploads: true,
44 fileAttachments: { maxUploadBytes: 20_000_000 },
45 },
46 },
47 },
48 ],
49 ]),
50}));
51vi.mock("./session", () => ({ readEnvironmentScope: () => true }));
52vi.mock("./threads", () => ({ threadEnvironment: {} }));
53vi.mock("./use-atom-command", () => ({ useAtomCommand: () => vi.fn() }));
54vi.mock("./use-thread-selection", () => ({
55 useThreadSelection: () => ({
56 selectedThread: { environmentId: "environment-1", id: "thread-1" },
57 }),
58}));
59vi.mock("./use-thread-detail", () => ({
60 useSelectedThreadPendingRequests: () => ({
61 approvals: [],
62 userInputs: [
63 {
64 requestId: "request-1",
65 createdAt: "2026-09-08T00:00:00Z",
66 responseCapability: "live",
67 dismissible: false,
68 questions: ["first", "second"].map((id) => ({
69 id,
70 header: id,
71 question: `Attach ${id} file`,
72 options: [],
73 allowCustomAnswer: true,
74 multiSelect: false,
75 })),
76 },
77 ],
78 }),
79}));
80​
81import { ApprovalRequestId, EnvironmentId, ThreadId } from "@t3tools/contracts";
82import { questionAttachmentDraftKey } from "./question-attachments";
83import { useSelectedThreadRequests } from "./use-selected-thread-requests";
84​
85const environmentId = EnvironmentId.make("environment-1");
86const key = (question: string) =>
87 questionAttachmentDraftKey(
88 environmentId,
89 ThreadId.make("thread-1"),
90 ApprovalRequestId.make("request-1"),
91 question,
92 );
93function submitButtonMarkup() {
94 function Probe() {
95 const { activePendingUserInputAnswers } = useSelectedThreadRequests();
96 return <button disabled={activePendingUserInputAnswers === null}>Submit answers</button>;
97 }
98 return renderToStaticMarkup(<Probe />);
100beforeEach(() => {
101 fixture.preparations = {};
102 fixture.drafts = Object.fromEntries(
103 ["first", "second"].map((id) => [
104 key(id),
105 {
106 attachments: [
107 {
108 id,
109 type: "file",
110 name: `${id}.txt`,
111 mimeType: "text/plain",
112 sizeBytes: 4,
113 fileUri: `file:///${id}.txt`,
114 },
115 ],
116 },
117 ]),
118 );
119 fixture.uploads = { "environment-1:first": { status: "ready" } };
120});
121describe("question attachment submission readiness", () => {
122 it.each([
123 undefined,
124 { status: "uploading", progress: 0.5 },
125 { status: "failed", reason: "Offline" },
126 ])("keeps Submit disabled until all question uploads finish: %j", (state) => {
127 if (state) fixture.uploads["environment-1:second"] = state;
128 expect(submitButtonMarkup()).toContain("disabled");
129 fixture.uploads["environment-1:second"] = { status: "ready" };
130 expect(submitButtonMarkup()).not.toContain("disabled");
131 });
132 it("ignores an upload in another environment", () => {
133 fixture.uploads["environment-1:second"] = { status: "ready" };
134 fixture.uploads["environment-2:second"] = { status: "uploading", progress: 0.5 };
135 expect(submitButtonMarkup()).not.toContain("disabled");
136 });
137 it("waits for attachment preparation even when uploads are ready", () => {
138 fixture.uploads["environment-1:second"] = { status: "ready" };
139 fixture.preparations[key("first")] = 1;
140 expect(submitButtonMarkup()).toContain("disabled");
141 });
142});