Skip to content

Commit ea5e01b

Browse files
fix(mobile): verify queued edit recovery without restoring sent text
1 parent b2d863b commit ea5e01b

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

‎apps/mobile/src/state/queued-run-edit.test.ts‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from "vite-plus/test";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22
import {
33
ChatAttachmentId,
44
ComposerContextId,
@@ -12,12 +12,32 @@ import { composerDraftEnvironmentId } from "../lib/composerAttachmentUploadQueue
1212
import { isQueuedEditDraftKey, queuedEditDraftKey } from "./queued-edit-draft-key";
1313
import {
1414
beginQueuedRunEdit,
15+
endQueuedRunEdit,
1516
getQueuedRunEdit,
1617
recoverQueuedRunEdit,
1718
resolveQueuedEditPayload,
1819
type QueuedRunEdit,
1920
} from "./queued-run-edit";
20-
import { getComposerDraftSnapshot, setComposerDraftText } from "./use-composer-drafts";
21+
import {
22+
clearComposerDraft,
23+
flushComposerDrafts,
24+
getComposerDraftSnapshot,
25+
setComposerDraftText,
26+
waitForComposerDraftsLoaded,
27+
} from "./use-composer-drafts";
28+
29+
vi.mock("expo-file-system", () => ({
30+
Directory: class {
31+
create() {}
32+
},
33+
File: class {
34+
exists = false;
35+
create() {}
36+
write() {}
37+
moveSync() {}
38+
},
39+
Paths: { document: { uri: "file:///documents" } },
40+
}));
2141

2242
const image = (id: string): ChatAttachment => ({
2343
type: "image",
@@ -117,31 +137,52 @@ describe("recoverQueuedRunEdit", () => {
117137
const runId = RunId.make("run-1");
118138
const editKey = queuedEditDraftKey(threadKey, runId);
119139

120-
it("drops an untouched edit without restoring the sent message", () => {
140+
beforeEach(async () => {
141+
await waitForComposerDraftsLoaded();
142+
});
143+
144+
afterEach(async () => {
145+
endQueuedRunEdit(threadKey);
146+
clearComposerDraft(threadKey);
147+
await flushComposerDrafts();
148+
});
149+
150+
it("drops an untouched edit without restoring the sent message", async () => {
121151
beginQueuedRunEdit(threadKey, edit([]));
122152

123-
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("clean");
124-
expect(getQueuedRunEdit(threadKey)).toBeNull();
153+
const recovery = recoverQueuedRunEdit(threadKey, runId);
154+
await flushComposerDrafts();
155+
125156
expect(getComposerDraftSnapshot(threadKey).text).toBe("");
126157
expect(getComposerDraftSnapshot(editKey).text).toBe("");
158+
expect(getQueuedRunEdit(threadKey)).toBeNull();
159+
expect(recovery).toBe("clean");
127160
});
128161

129-
it("discards a changed edit when the thread draft already has content", () => {
130-
setComposerDraftText(threadKey, "unsent draft");
162+
it("recovers a changed edit into an empty thread draft", async () => {
131163
beginQueuedRunEdit(threadKey, edit([]));
132164
setComposerDraftText(editKey, "revised");
133165

134-
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("discarded");
166+
const recovery = recoverQueuedRunEdit(threadKey, runId);
167+
await flushComposerDrafts();
168+
169+
expect(getComposerDraftSnapshot(threadKey).text).toBe("revised");
170+
expect(getComposerDraftSnapshot(editKey).text).toBe("");
135171
expect(getQueuedRunEdit(threadKey)).toBeNull();
136-
expect(getComposerDraftSnapshot(threadKey).text).toBe("unsent draft");
137-
setComposerDraftText(threadKey, "");
172+
expect(recovery).toBe("kept");
138173
});
139174

140-
it("ignores a run the composer is no longer editing", () => {
175+
it("preserves an occupied thread draft when discarding a changed edit", async () => {
176+
setComposerDraftText(threadKey, "unsent draft");
141177
beginQueuedRunEdit(threadKey, edit([]));
178+
setComposerDraftText(editKey, "revised");
142179

143-
expect(recoverQueuedRunEdit(threadKey, RunId.make("run-2"))).toBeNull();
144-
expect(getQueuedRunEdit(threadKey)?.runId).toBe(runId);
145-
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("clean");
180+
const recovery = recoverQueuedRunEdit(threadKey, runId);
181+
await flushComposerDrafts();
182+
183+
expect(getComposerDraftSnapshot(threadKey).text).toBe("unsent draft");
184+
expect(getComposerDraftSnapshot(editKey).text).toBe("");
185+
expect(getQueuedRunEdit(threadKey)).toBeNull();
186+
expect(recovery).toBe("discarded");
146187
});
147188
});

‎apps/mobile/src/state/queued-run-edit.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ export function endQueuedRunEdit(
9595
setQueuedRunEdit(threadKey, null);
9696
}
9797

98-
/**
99-
* Leaves edit mode once the edited run is no longer queued (it started, was
100-
* steered, or was removed elsewhere). A changed edit moves into the thread's
101-
* own draft when that draft is empty and is otherwise discarded; an untouched
102-
* edit is just the message that was sent, so it is dropped as "clean".
103-
* Returns null when the composer is not editing `runId`.
104-
*/
10598
export function recoverQueuedRunEdit(
10699
threadKey: string,
107100
runId: RunId,
@@ -111,6 +104,7 @@ export function recoverQueuedRunEdit(
111104
const editDraft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId));
112105
const dirty = editDraft.text !== edit.originalText || editDraft.attachments.length > 0;
113106
if (!dirty) {
107+
// Untouched text was already sent; restoring it would duplicate the message.
114108
endQueuedRunEdit(threadKey);
115109
return "clean";
116110
}

0 commit comments

Comments
 (0)