Skip to content

Commit ddcc0d5

Browse files
committed
test(mobile): cover queued edit recovery when its run leaves the queue
1 parent 64350fe commit ddcc0d5

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",
@@ -110,13 +113,36 @@ describe("resolveQueuedEditPayload", () => {
110113
});
111114
});
112115

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

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

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { appAtomRegistry } from "./atom-registry";
2828
import { queuedEditDraftKey } from "./queued-edit-draft-key";
2929
import {
3030
clearComposerDraft,
31+
getComposerDraftSnapshot,
32+
mergeComposerDraftContent,
3133
setComposerDraftContext,
3234
setComposerDraftText,
3335
} from "./use-composer-drafts";
@@ -94,14 +96,6 @@ export function removeQueuedRunEditAttachment(threadKey: string, attachmentId: s
9496
});
9597
}
9698

97-
/** Whether the edit draft holds anything the queued message does not already have. */
98-
export function queuedRunEditIsDirty(
99-
edit: QueuedRunEdit,
100-
draft: { readonly text: string; readonly attachments: ReadonlyArray<unknown> },
101-
): boolean {
102-
return draft.text !== edit.originalText || draft.attachments.length > 0;
103-
}
104-
10599
export function endQueuedRunEdit(
106100
threadKey: string,
107101
options?: { readonly deferAttachmentCleanup?: boolean },
@@ -112,6 +106,41 @@ export function endQueuedRunEdit(
112106
setQueuedRunEdit(threadKey, null);
113107
}
114108

109+
/**
110+
* Leaves edit mode once the edited run is no longer queued (it started, was
111+
* steered, or was removed elsewhere). A changed edit moves into the thread's
112+
* own draft when that draft is empty and is otherwise discarded; an untouched
113+
* edit is just the message that was sent, so it is dropped as "clean".
114+
* Returns null when the composer is not editing `runId`.
115+
*/
116+
export function recoverQueuedRunEdit(
117+
threadKey: string,
118+
runId: RunId,
119+
): "kept" | "discarded" | "clean" | null {
120+
const edit = getQueuedRunEdit(threadKey);
121+
if (edit === null || edit.runId !== runId) return null;
122+
const editDraft = getComposerDraftSnapshot(queuedEditDraftKey(threadKey, runId));
123+
const dirty = editDraft.text !== edit.originalText || editDraft.attachments.length > 0;
124+
if (!dirty) {
125+
endQueuedRunEdit(threadKey);
126+
return "clean";
127+
}
128+
const threadDraft = getComposerDraftSnapshot(threadKey);
129+
const keepable =
130+
editDraft.text.trim().length > 0 &&
131+
threadDraft.text.trim().length === 0 &&
132+
threadDraft.attachments.length === 0;
133+
if (keepable) {
134+
void mergeComposerDraftContent(threadKey, {
135+
text: editDraft.text,
136+
attachments: editDraft.attachments,
137+
...(editDraft.context ? { context: editDraft.context } : {}),
138+
});
139+
}
140+
endQueuedRunEdit(threadKey, { deferAttachmentCleanup: keepable });
141+
return keepable ? "kept" : "discarded";
142+
}
143+
115144
/**
116145
* The replacement payload for `queued-run.edit`. Attachments replace the
117146
* message's list wholesale, so kept server attachments are resent alongside

‎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,
@@ -452,34 +452,15 @@ export function useThreadComposerState() {
452452
(run) => run.id === editedRunId && run.status === "queued",
453453
);
454454
if (stillQueued) return;
455-
const edit = getQueuedRunEdit(selectedThreadKey);
456-
const editDraftKey = queuedEditDraftKey(selectedThreadKey, editedRunId);
457-
const editDraft = getComposerDraftSnapshot(editDraftKey);
458-
// An untouched edit is just the message that was sent (or steered), so
459-
// there is nothing to recover and nothing to tell the user.
460-
if (edit === null || !queuedRunEditIsDirty(edit, editDraft)) {
461-
endQueuedRunEdit(selectedThreadKey);
462-
return;
463-
}
464-
const threadDraft = getComposerDraftSnapshot(selectedThreadKey);
465-
const keepable =
466-
editDraft.text.trim().length > 0 &&
467-
threadDraft.text.trim().length === 0 &&
468-
threadDraft.attachments.length === 0;
469-
if (keepable) {
470-
void mergeComposerDraftContent(selectedThreadKey, {
471-
text: editDraft.text,
472-
attachments: editDraft.attachments,
473-
...(editDraft.context ? { context: editDraft.context } : {}),
474-
});
455+
const recovery = recoverQueuedRunEdit(selectedThreadKey, editedRunId);
456+
if (recovery === "kept" || recovery === "discarded") {
457+
setThreadComposerError(
458+
selectedThreadKey,
459+
recovery === "kept"
460+
? "That message already started. Your edit is back in the composer."
461+
: "That message already started, so the edit was discarded.",
462+
);
475463
}
476-
endQueuedRunEdit(selectedThreadKey, { deferAttachmentCleanup: keepable });
477-
setThreadComposerError(
478-
selectedThreadKey,
479-
keepable
480-
? "That message already started. Your edit is back in the composer."
481-
: "That message already started, so the edit was discarded.",
482-
);
483464
}, [editedRunId, selectedThreadKey, selectedThreadRuns]);
484465

485466
const activeThreadBusy = threadRuntimeIsActive(selectedThreadRuntime);

0 commit comments

Comments
 (0)