Skip to content

Commit 24d26bf

Browse files
only21milclaude
andcommitted
fix(web): an attachment-only send that fails behind a new draft now says it was not sent
The interrupted-send toast only showed when the failed message had text to copy, so an attachment-only send dropped its row with no notice. The toast now shows whenever the draft was not restored and no other toast fired, and offers Copy message only when there is text. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 11aeff9 commit 24d26bf

2 files changed

Lines changed: 67 additions & 17 deletions

File tree

‎apps/web/src/components/ChatView.browser.test.tsx‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ORCHESTRATION_V2_WS_METHODS,
55
OrchestrationDispatchCommandError,
66
ThreadId,
7+
WS_METHODS,
78
} from "@t3tools/contracts";
89
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
910
import type * as Context from "effect/Context";
@@ -28,6 +29,33 @@ import {
2829
} from "../../test/fakeEnvironment";
2930
import { render } from "../../test/render";
3031

32+
const PNG = Uint8Array.from(
33+
atob(
34+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=",
35+
),
36+
(char) => char.charCodeAt(0),
37+
);
38+
39+
/** Accepts every attachment upload without sending its bytes, until disposed. */
40+
function acceptAttachmentUploads(): Disposable {
41+
const mint = answerRpc(WS_METHODS.attachmentsCreateUploadUrl, () =>
42+
Effect.succeed({ attachmentId: "upload-1", relativeUrl: "/upload", expiresAt: 0 }),
43+
);
44+
const status = vi.spyOn(XMLHttpRequest.prototype, "status", "get").mockReturnValue(200);
45+
const send = vi.spyOn(XMLHttpRequest.prototype, "send").mockImplementation(function (
46+
this: XMLHttpRequest,
47+
) {
48+
queueMicrotask(() => this.dispatchEvent(new ProgressEvent("load")));
49+
});
50+
return {
51+
[Symbol.dispose]: () => {
52+
send.mockRestore();
53+
status.mockRestore();
54+
mint[Symbol.dispose]();
55+
},
56+
};
57+
}
58+
3159
// The socket is the only fake layer; see test/appScenarios.renders.tsx.
3260
vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (importOriginal) => {
3361
const [actual, { fakeDriver }, Layer] = await Promise.all([
@@ -80,14 +108,22 @@ it.each([
80108
new OrchestrationDispatchCommandError({ message: "Provider is unavailable." }),
81109
),
82110
banner: "Provider is unavailable.",
111+
failedText: "first message that will fail",
83112
},
84113
// A dropped connection interrupts the request and shows no thread error.
85-
{ failure: "interrupted", end: Effect.interrupt, banner: null },
114+
{
115+
failure: "interrupted",
116+
end: Effect.interrupt,
117+
banner: null,
118+
failedText: "first message that will fail",
119+
},
120+
// An image alone leaves no text to copy, but the failure still needs a toast.
121+
{ failure: "interrupted image", end: Effect.interrupt, banner: null, failedText: "" },
86122
])(
87123
"a failed send ($failure) drops its message from the timeline and keeps the draft typed meanwhile",
88-
async ({ failure, end, banner }) => {
124+
async ({ failure, end, banner, failedText }) => {
89125
// Drafts persist per thread, so each case gets its own.
90-
const thread = threadWithHistory(`thread-${failure}-send`, 1);
126+
const thread = threadWithHistory(`thread-${failure.replace(" ", "-")}-send`, 1);
91127
using _environment = installFakeEnvironment([thread]);
92128
__setClientSettingsForTests({
93129
...DEFAULT_CLIENT_SETTINGS,
@@ -109,30 +145,45 @@ it.each([
109145
router: false,
110146
});
111147
using write = vi.spyOn(navigator.clipboard, "writeText").mockResolvedValue(undefined);
112-
// Only timeline rows, so the composer and sidebar cannot match the text.
148+
using _uploads = failedText ? null : acceptAttachmentUploads();
149+
// Only timeline rows, not the composer or sidebar.
113150
const sentMessages = () =>
114151
Array.from(document.querySelectorAll('[data-message-role="user"]'), (row) => row.textContent);
115-
const failedText = "first message that will fail";
152+
const onlyHistory = [expect.stringContaining(`${thread.id}: question 0`)];
116153
const editor = page.getByTestId("composer-editor");
117154
await expect.element(editor).toBeVisible();
155+
await vi.waitFor(() => expect(sentMessages()).toEqual(onlyHistory));
118156

119157
await userEvent.click(editor);
120-
await userEvent.keyboard(`${failedText}{Enter}`);
121-
await vi.waitFor(() =>
122-
expect(sentMessages()).toContainEqual(expect.stringContaining(failedText)),
123-
);
158+
if (failedText) {
159+
await userEvent.keyboard(failedText);
160+
} else {
161+
const clipboardData = new DataTransfer();
162+
clipboardData.items.add(new File([PNG], "shot.png", { type: "image/png" }));
163+
editor
164+
.element()
165+
.dispatchEvent(
166+
new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }),
167+
);
168+
await expect.element(page.getByRole("img", { name: "shot.png" })).toBeVisible();
169+
}
170+
await userEvent.keyboard("{Enter}");
171+
await vi.waitFor(() => expect(sentMessages()).toHaveLength(2));
124172
await userEvent.keyboard("next draft");
125173
await expect.element(editor).toHaveTextContent("next draft");
126174
failDispatch();
127175

128-
await vi.waitFor(() =>
129-
expect(sentMessages()).not.toContainEqual(expect.stringContaining(failedText)),
130-
);
176+
await vi.waitFor(() => expect(sentMessages()).toEqual(onlyHistory));
131177
await expect.element(page.getByText("Message not sent")).toBeVisible();
132178
if (banner) await expect.element(page.getByText(banner)).toBeVisible();
133179
await expect.element(editor).toHaveTextContent("next draft");
134180
// By text, since each timeline row has an icon button labelled the same.
135-
await page.getByText("Copy message", { exact: true }).click();
181+
const copyAction = page.getByText("Copy message", { exact: true });
182+
if (!failedText) {
183+
expect(copyAction.query()).toBeNull();
184+
return;
185+
}
186+
await copyAction.click();
136187
expect(write).toHaveBeenCalledWith(failedText);
137188
},
138189
);

‎apps/web/src/components/chat/useComposerSend.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,15 +1542,14 @@ export function useComposerSend(input: ComposerSendInput) {
15421542
failureToastShown = true;
15431543
}
15441544
}
1545-
// An interrupted send shows no error, but its text still needs a way back.
1546-
if (copyFailedMessage && !failureToastShown) {
1545+
// Keeping the new draft drops the failed message, so say so even with no text to copy.
1546+
if (!restoresDraft && !failureToastShown) {
15471547
toastManager.add(
15481548
stackedThreadToast({
15491549
type: "error",
15501550
title: "Message not sent",
15511551
description: "Your new draft was kept.",
1552-
timeout: 0,
1553-
actionProps: copyFailedMessage,
1552+
...(copyFailedMessage ? { timeout: 0, actionProps: copyFailedMessage } : {}),
15541553
}),
15551554
);
15561555
}

0 commit comments

Comments
 (0)