Skip to content

Commit 9a566ef

Browse files
committed
fix(mobile): wait for question attachment uploads before submitting
1 parent e697568 commit 9a566ef

2 files changed

Lines changed: 149 additions & 5 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
});

‎apps/mobile/src/state/use-selected-thread-requests.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { composerDraftsAtom, clearComposerDraft } from "./use-composer-drafts";
1010
import {
1111
composerAttachmentUploadsAtom,
1212
composerAttachmentUploadBlockReason,
13+
composerAttachmentsStillUploading,
1314
} from "./composer-attachment-uploads";
1415
import { useAtomValue } from "@effect/atom-react";
1516
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
@@ -149,6 +150,12 @@ export function useSelectedThreadRequests() {
149150
question.id,
150151
);
151152
const attachments = attachmentDrafts[key]?.attachments ?? [];
153+
const uploadInput = {
154+
environmentId: selectedThreadShell.environmentId,
155+
attachments,
156+
serverConfig: questionServerConfigs.get(selectedThreadShell.environmentId) ?? null,
157+
states: uploadStates,
158+
};
152159
return [
153160
question.id,
154161
{
@@ -164,13 +171,10 @@ export function useSelectedThreadRequests() {
164171
questionServerConfigs.get(selectedThreadShell.environmentId)?.environment
165172
.capabilities.questionAttachments !== true) ||
166173
(preparationCounts[key] ?? 0) > 0 ||
174+
composerAttachmentsStillUploading(uploadInput) ||
167175
composerAttachmentUploadBlockReason({
168-
environmentId: selectedThreadShell.environmentId,
169-
attachments,
176+
...uploadInput,
170177
connected: true,
171-
serverConfig:
172-
questionServerConfigs.get(selectedThreadShell.environmentId) ?? null,
173-
states: uploadStates,
174178
}) !== null,
175179
},
176180
];

0 commit comments

Comments
 (0)