Skip to content
103 changes: 103 additions & 0 deletions apps/web/src/components/ChatView.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ vi.mock("../../../../packages/client-runtime/src/connection/driver.ts", async (i
return { ...actual, layer: Layer.succeed(actual.ConnectionDriver, fakeDriver) };
});

// Null runs the real compression; a test sets it to hold an image in preparation.
const compression = vi.hoisted(() => ({ gate: null as Promise<void> | null }));
vi.mock(import("~/lib/imageCompression"), async (original) => {
const actual = await original();
return {
...actual,
prepareImageForAttachment: async (...args) => {
await compression.gate;
return actual.prepareImageForAttachment(...args);
},
};
});

vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({
...(await original()),
trackRpcRequestSent: () => {},
Expand Down Expand Up @@ -438,6 +451,96 @@ it("an interrupted image-only first send of a new thread says it may not have be
expect(page.getByText("Copy message", { exact: true }).query()).toBeNull();
});

it.each(["new", "existing"] as const)(
"Enter while an image compresses shows a cancellable banner, cancels on edit or Esc, and still sends after leaving a %s thread",
async (kind) => {
const thread = threadWithHistory(`upload-wait-${kind}`, 1);
const other = threadWithHistory(`upload-wait-other-${kind}`, 1);
using _environment = installFakeEnvironment(kind === "existing" ? [thread, other] : [other], {
acceptCommands: true,
});
__setClientSettingsForTests({
...DEFAULT_CLIENT_SETTINGS,
onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
});
const draftId = DraftId.make(`draft-upload-wait-${kind}`);
if (kind === "new") {
useComposerDraftStore
.getState()
.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, {
threadId: thread.id,
});
}
using launch = holdRpc(ORCHESTRATION_V2_WS_METHODS.launchThread, Effect.interrupt);
const router = getRouter(
createMemoryHistory({
initialEntries: [kind === "new" ? `/draft/${draftId}` : `/${ENVIRONMENT_ID}/${thread.id}`],
}),
);
await router.load();
await using _app = await render(<AppRoot router={router} />, {
// AppRoot brings its own router.
router: false,
});
const compressed = Promise.withResolvers<void>();
compression.gate = compressed.promise;
using _compression = { [Symbol.dispose]: () => (compression.gate = null) };
using _uploads = acceptAttachmentUploads();
const editor = page.getByTestId("composer-editor");
await expect.element(editor).toBeVisible();
await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible();

await userEvent.click(editor);
const clipboardData = new DataTransfer();
clipboardData.items.add(new File([PNG], "shot.png", { type: "image/png" }));
editor
.element()
.dispatchEvent(
new ClipboardEvent("paste", { clipboardData, bubbles: true, cancelable: true }),
);

// Enter arms the send and shows the banner plus a disabled spinner button.
await userEvent.keyboard("check image{Enter}");
await expect.element(page.getByText("Sending once image is ready")).toBeVisible();
const waitingButton = page.getByRole("button", { name: "Sending when ready", exact: true });
await expect.element(waitingButton).toBeDisabled();
await expect.element(waitingButton).toHaveAttribute("aria-busy", "true");

// Typing disarms the queued send and keeps the edit.
await userEvent.keyboard(" now");
await expect.element(page.getByText("Sending once image is ready")).not.toBeInTheDocument();
await expect.element(editor).toHaveTextContent("check image now");

// Enter arms again; Esc cancels cleanly.
await userEvent.keyboard("{Enter}");
await expect.element(page.getByText("Sending once image is ready")).toBeVisible();
await userEvent.keyboard("{Escape}");
await expect.element(page.getByText("Sending once image is ready")).not.toBeInTheDocument();

// Enter arms again; leaving the thread still sends once the image is ready.
await userEvent.keyboard("{Enter}");
await expect.element(page.getByText("Sending once image is ready")).toBeVisible();
await router.navigate({ to: `/${ENVIRONMENT_ID}/${other.id}` });
await expect.element(page.getByText(`Answer 0 for upload-wait-other-${kind}.`)).toBeVisible();

compressed.resolve();
await vi.waitFor(() => {
if (kind === "new") {
expect(launch.calls).toHaveLength(1);
} else {
expect(dispatchedCommands()).toContainEqual(
expect.objectContaining({
type: "message.dispatch",
threadId: thread.id,
attachments: [expect.objectContaining({ type: "image", id: "upload-1" })],
}),
);
}
});
launch.release();
},
);

it("opens the thread actions menu for an older settled thread the shell leaves out", async () => {
const thread = { ...threadWithHistory("older-settled", 1), olderSettled: true as const };
using _environment = installFakeEnvironment([thread]);
Expand Down
24 changes: 23 additions & 1 deletion apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ import { visibleThreadPullRequests } from "@t3tools/shared/threadPullRequests";
import type { UsageLimitSourceSnapshots } from "@t3tools/contracts";
import { feedbackBannerItem } from "./chat/ComposerFeedback";
import { freshSessionBannerItem } from "./chat/ComposerFreshSession";
import { heldComposerSendBannerItem } from "./chat/ComposerHeldSendBanner";
import { armFreshSession, disarmFreshSession, useFreshSessionArmed } from "./chat/freshSessionArms";
import { cancelHeldComposerSend, useComposerSendHeld } from "./chat/preparingComposerImages";
import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel";
import { useWorktreeSetup } from "./chat/useWorktreeSetup";
import { useChatShortcuts } from "./chat/useChatShortcuts";
Expand Down Expand Up @@ -253,6 +255,7 @@ import {
type ComposerFileAttachment,
type ComposerImageAttachment,
type DraftThreadEnvMode,
composerTargetKey,
useComposerDraftStore,
DraftId,
} from "../composerDraftStore";
Expand Down Expand Up @@ -2658,7 +2661,16 @@ export default function ChatView(props: ChatViewProps) {
// While a question or approval owns the composer, its text answers that, not the agent.
const freshSessionCommandAvailable =
freshSessionAvailable && activePendingApproval === null && pendingUserInputs.length === 0;
const toggleKeepFullHistory = () => setKeepFullHistory(routeThreadKey, !keepFullHistory);
const composerDraftTargetKey = composerTargetKey(composerDraftTarget);
const composerSendHeld = useComposerSendHeld(composerDraftTargetKey);
const cancelHeldSendStable = useStableCallback(() => {
cancelHeldComposerSend(composerDraftTargetKey);
focusComposerStable();
});
const toggleKeepFullHistory = () => {
cancelHeldComposerSend(composerDraftTargetKey);
setKeepFullHistory(routeThreadKey, !keepFullHistory);
};
const toggleKeepFullHistoryStable = useStableCallback(toggleKeepFullHistory);
const armFreshSessionStable = useStableCallback(() => armFreshSession(routeThreadKey));
const handleRestoreThreadBranch = () => {
Expand Down Expand Up @@ -2769,6 +2781,14 @@ export default function ChatView(props: ChatViewProps) {
freshSessionPending && activePendingApproval === null
? [freshSessionBannerItem(() => disarmFreshSession(routeThreadKey), messages)]
: [];
const heldSendItems = composerSendHeld
? [
heldComposerSendBannerItem(
{ draftTarget: composerDraftTarget, onCancel: cancelHeldSendStable },
messages,
),
]
: [];
if (!localCheckoutBranchMismatch || !showBranchMismatchBanner || !activeBranchMismatchKey) {
return [
...feedbackBannerItems,
Expand All @@ -2778,6 +2798,7 @@ export default function ChatView(props: ChatViewProps) {
...usageLimitsItems,
...projectCloneItems,
...freshSessionItems,
...heldSendItems,
...systemComposerBannerItems,
];
}
Expand All @@ -2789,6 +2810,7 @@ export default function ChatView(props: ChatViewProps) {
...usageLimitsItems,
...projectCloneItems,
...freshSessionItems,
...heldSendItems,
...systemComposerBannerItems,
branchMismatchBannerItem(
{
Expand Down
Loading
Loading