Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 78 additions & 2 deletions apps/mobile/src/state/queued-run-edit.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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");
Expand Down
Loading