Skip to content

Commit 29768d8

Browse files
committed
test(mobile): cover queued edit recovery when its run leaves the queue
1 parent 168da88 commit 29768d8

3 files changed

Lines changed: 79 additions & 43 deletions

File tree

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import {
1111
import { composerDraftEnvironmentId } from "../lib/composerAttachmentUploadQueue";
1212
import { isQueuedEditDraftKey, queuedEditDraftKey } from "./queued-edit-draft-key";
1313
import {
14-
queuedRunEditIsDirty,
14+
beginQueuedRunEdit,
15+
getQueuedRunEdit,
16+
recoverQueuedRunEdit,
1517
resolveQueuedEditPayload,
1618
type QueuedRunEdit,
1719
} from "./queued-run-edit";
20+
import { getComposerDraftSnapshot, setComposerDraftText } from "./use-composer-drafts";
1821

1922
const image = (id: string): ChatAttachment => ({
2023
type: "image",
@@ -109,13 +112,36 @@ describe("resolveQueuedEditPayload", () => {
109112
});
110113
});
111114

112-
describe("queuedRunEditIsDirty", () => {
113-
it("treats an untouched edit as clean so a steered message is not restored", () => {
114-
expect(queuedRunEditIsDirty(edit([]), { text: "original", attachments: [] })).toBe(false);
115+
describe("recoverQueuedRunEdit", () => {
116+
const threadKey = "env:thread-recover";
117+
const runId = RunId.make("run-1");
118+
const editKey = queuedEditDraftKey(threadKey, runId);
119+
120+
it("drops an untouched edit without restoring the sent message", () => {
121+
beginQueuedRunEdit(threadKey, edit([]));
122+
123+
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("clean");
124+
expect(getQueuedRunEdit(threadKey)).toBeNull();
125+
expect(getComposerDraftSnapshot(threadKey).text).toBe("");
126+
expect(getComposerDraftSnapshot(editKey).text).toBe("");
127+
});
128+
129+
it("discards a changed edit when the thread draft already has content", () => {
130+
setComposerDraftText(threadKey, "unsent draft");
131+
beginQueuedRunEdit(threadKey, edit([]));
132+
setComposerDraftText(editKey, "revised");
133+
134+
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("discarded");
135+
expect(getQueuedRunEdit(threadKey)).toBeNull();
136+
expect(getComposerDraftSnapshot(threadKey).text).toBe("unsent draft");
137+
setComposerDraftText(threadKey, "");
115138
});
116139

117-
it("treats changed text or a new attachment as an edit worth keeping", () => {
118-
expect(queuedRunEditIsDirty(edit([]), { text: "revised", attachments: [] })).toBe(true);
119-
expect(queuedRunEditIsDirty(edit([]), { text: "original", attachments: [{}] })).toBe(true);
140+
it("ignores a run the composer is no longer editing", () => {
141+
beginQueuedRunEdit(threadKey, edit([]));
142+
143+
expect(recoverQueuedRunEdit(threadKey, RunId.make("run-2"))).toBeNull();
144+
expect(getQueuedRunEdit(threadKey)?.runId).toBe(runId);
145+
expect(recoverQueuedRunEdit(threadKey, runId)).toBe("clean");
120146
});
121147
});

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { appAtomRegistry } from "./atom-registry";
1919
import { queuedEditDraftKey } from "./queued-edit-draft-key";
2020
import {
2121
clearComposerDraft,
22+
getComposerDraftSnapshot,
23+
mergeComposerDraftContent,
2224
setComposerDraftContext,
2325
setComposerDraftText,
2426
} from "./use-composer-drafts";
@@ -83,14 +85,6 @@ export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: s
8385
});
8486
}
8587

86-
/** Whether the edit draft holds anything the queued message does not already have. */
87-
export function queuedRunEditIsDirty(
88-
edit: QueuedRunEdit,
89-
draft: { readonly text: string; readonly attachments: ReadonlyArray<unknown> },
90-
): boolean {
91-
return draft.text !== edit.originalText || draft.attachments.length > 0;
92-
}
93-
9488
export function endQueuedRunEdit(
9589
threadKey: string,
9690
options?: { readonly deferAttachmentCleanup?: boolean },
@@ -101,6 +95,41 @@ export function endQueuedRunEdit(
10195
setQueuedRunEdit(threadKey, null);
10296
}
10397

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+
*/
105+
export function recoverQueuedRunEdit(
106+
threadKey: string,
107+
runId: RunId,
108+
): "kept" | "discarded" | "clean" | null {
109+
const edit = getQueuedRunEdit(threadKey);
110+
if (edit === null || edit.runId !== runId) return null;
111+
const editDraft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId));
112+
const dirty = editDraft.text !== edit.originalText || editDraft.attachments.length > 0;
113+
if (!dirty) {
114+
endQueuedRunEdit(threadKey);
115+
return "clean";
116+
}
117+
const threadDraft = getComposerDraftSnapshot(threadKey);
118+
const keepable =
119+
editDraft.text.trim().length > 0 &&
120+
threadDraft.text.trim().length === 0 &&
121+
threadDraft.attachments.length === 0;
122+
if (keepable) {
123+
void mergeComposerDraftContent(threadKey, {
124+
text: editDraft.text,
125+
attachments: editDraft.attachments,
126+
...(editDraft.context ? { context: editDraft.context } : {}),
127+
});
128+
}
129+
endQueuedRunEdit(threadKey, { deferAttachmentCleanup: keepable });
130+
return keepable ? "kept" : "discarded";
131+
}
132+
104133
/**
105134
* Attachments replace the message's list wholesale, so kept server attachments
106135
* are resent alongside the newly uploaded ones, in composer order.

‎apps/mobile/src/state/use-thread-composer-state.ts‎

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ import { environmentThreadDetails } from "./threads";
9696
import {
9797
endQueuedRunEdit,
9898
getQueuedRunEdit,
99-
queuedRunEditIsDirty,
99+
recoverQueuedRunEdit,
100100
queuedEditDraftKey,
101101
removeQueuedRunEditAttachment,
102102
resolveQueuedEditPayload,
@@ -435,34 +435,15 @@ export function useThreadComposerState() {
435435
(run) => run.id === editedRunId && run.status === "queued",
436436
);
437437
if (stillQueued) return;
438-
const edit = getQueuedRunEdit(selectedThreadKey);
439-
const editDraftKey = queuedEditDraftKey(selectedThreadKey, editedRunId);
440-
const editDraft = getComposerDraftSnapshot(editDraftKey);
441-
// An untouched edit is just the message that was sent (or steered), so
442-
// there is nothing to recover and nothing to tell the user.
443-
if (edit === null || !queuedRunEditIsDirty(edit, editDraft)) {
444-
endQueuedRunEdit(selectedThreadKey);
445-
return;
446-
}
447-
const threadDraft = getComposerDraftSnapshot(selectedThreadKey);
448-
const keepable =
449-
editDraft.text.trim().length > 0 &&
450-
threadDraft.text.trim().length === 0 &&
451-
threadDraft.attachments.length === 0;
452-
if (keepable) {
453-
void mergeComposerDraftContent(selectedThreadKey, {
454-
text: editDraft.text,
455-
attachments: editDraft.attachments,
456-
...(editDraft.context ? { context: editDraft.context } : {}),
457-
});
438+
const recovery = recoverQueuedRunEdit(selectedThreadKey, editedRunId);
439+
if (recovery === "kept" || recovery === "discarded") {
440+
setThreadComposerError(
441+
selectedThreadKey,
442+
recovery === "kept"
443+
? "That message already started. Your edit is back in the composer."
444+
: "That message already started, so the edit was discarded.",
445+
);
458446
}
459-
endQueuedRunEdit(selectedThreadKey, { deferAttachmentCleanup: keepable });
460-
setThreadComposerError(
461-
selectedThreadKey,
462-
keepable
463-
? "That message already started. Your edit is back in the composer."
464-
: "That message already started, so the edit was discarded.",
465-
);
466447
}, [editedRunId, selectedThreadKey, selectedThreadRuns]);
467448

468449
const activeThreadBusy = threadRuntimeIsActive(selectedThreadRuntime);

0 commit comments

Comments
 (0)