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