Skip to content
TypeScript

fix(mobile): discard unchanged queued edits after starting (#25232)

maria04f35381b650
diff --git a/apps/mobile/src/state/queued-run-edit.test.ts b/apps/mobile/src/state/queued-run-edit.test.ts
index 361a743ac71..6670505ad39 100644
--- a/apps/mobile/src/state/queued-run-edit.test.ts
+++ b/apps/mobile/src/state/queued-run-edit.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vite-plus/test";
+import { describe, expect, it, vi } from "vite-plus/test";
import { Atom } from "effect/reactivity";
import {
ChatAttachmentId,
@@ -20,7 +20,25 @@ import {
type QueuedRunEdit,
} from "./queued-run-edit";
import { threadComposerErrorsAtom } from "./thread-composer-error";
-import { getComposerDraftSnapshot, setComposerDraftText } from "./use-composer-drafts";
+import {
+ flushComposerDrafts,
+ getComposerDraftSnapshot,
+ setComposerDraftText,
+ waitForComposerDraftsLoaded,
+} from "./use-composer-drafts";
+
+vi.mock("expo-file-system", () => ({
+ Directory: class {
+ create() {}
+ },
+ File: class {
+ exists = false;
+ create() {}
+ write() {}
+ moveSync() {}
+ },
+ Paths: { document: { uri: "file:///documents" } },
+}));
const image = (id: string): ChatAttachment => ({
type: "image",
@@ -94,6 +112,64 @@ describe("resolveQueuedEditPayload", () => {
});
describe("beginQueuedRunEdit", () => {
+ it("silently drops an untouched edit when its run starts", () => {
+ const threadKey = "env-exit:thread-untouched";
+ const runId = RunId.make("run-1");
+ const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
+ { id: runId, status: "queued" },
+ ]);
+ beginQueuedRunEdit(threadKey, edit([]), runs);
+
+ appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
+
+ expect(getQueuedRunEdit(threadKey)).toBeNull();
+ expect(getComposerDraftSnapshot(threadKey).text).toBe("");
+ expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
+ expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBeUndefined();
+ });
+
+ it("recovers text after a server attachment was removed", async () => {
+ await waitForComposerDraftsLoaded();
+ const threadKey = "env-exit:thread-removed-recover";
+ const runId = RunId.make("run-1");
+ const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
+ { id: runId, status: "queued" },
+ ]);
+ beginQueuedRunEdit(threadKey, edit([image("removed")]), runs);
+ removeQueuedRunEditAttachment(threadKey, "removed");
+
+ appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
+ await flushComposerDrafts();
+
+ expect(getQueuedRunEdit(threadKey)).toBeNull();
+ expect(getComposerDraftSnapshot(threadKey).text).toBe("original");
+ expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
+ expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
+ "That message already started. Your edit is back in the composer.",
+ );
+ });
+
+ it("recovers changed text into an empty thread draft", async () => {
+ await waitForComposerDraftsLoaded();
+ const threadKey = "env-exit:thread-changed-recover";
+ const runId = RunId.make("run-1");
+ const runs = Atom.make<ReadonlyArray<{ readonly id: RunId; readonly status: string }> | null>([
+ { id: runId, status: "queued" },
+ ]);
+ beginQueuedRunEdit(threadKey, edit([]), runs);
+ setComposerDraftText(queuedEditDraftKey(threadKey, runId), "revised");
+
+ appAtomRegistry.set(runs, [{ id: runId, status: "running" }]);
+ await flushComposerDrafts();
+
+ expect(getQueuedRunEdit(threadKey)).toBeNull();
+ expect(getComposerDraftSnapshot(threadKey).text).toBe("revised");
+ expect(getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId)).text).toBe("");
+ expect(appAtomRegistry.get(threadComposerErrorsAtom)[threadKey]?.message).toBe(
+ "That message already started. Your edit is back in the composer.",
+ );
+ });
+
it("ends a changed edit when its run starts, without the thread's composer mounted", () => {
const threadKey = "env-exit:thread-exit";
const runId = RunId.make("run-1");