The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | vi.mock("expo-quick-actions", () => ({})); |
| 4 | vi.mock("react-native", () => ({ Platform: { OS: "android" } })); |
| 5 | vi.mock("../../connection/catalog", () => ({})); |
| 6 | vi.mock("../../persistence/imperative", () => ({})); |
| 7 | vi.mock("../../state/atom-registry", () => ({})); |
| 8 | vi.mock("../../state/threads", () => ({ environmentThreadShells: {} })); |
| 9 | vi.mock("./appShortcuts", async () => { |
| 10 | const { withRecentThreadShortcut } = |
| 11 | await vi.importActual<typeof import("./appShortcuts")>("./appShortcuts"); |
| 12 | return { withRecentThreadShortcut }; |
| 13 | }); |
| 14 | |
| 15 | import type { RecentThreadShortcut } from "../../persistence/imperative"; |
| 16 | import { createRecentThreadShortcuts } from "./appShortcutsRuntime"; |
| 17 | |
| 18 | const thread = (threadId: string, title = threadId): RecentThreadShortcut => ({ |
| 19 | environmentId: "env", |
| 20 | threadId, |
| 21 | title, |
| 22 | }); |
| 23 | |
| 24 | function setup(load: () => Promise<ReadonlyArray<RecentThreadShortcut>>) { |
| 25 | const save = vi.fn<(list: ReadonlyArray<RecentThreadShortcut>) => Promise<void>>(() => |
| 26 | Promise.resolve(), |
| 27 | ); |
| 28 | const publish = vi.fn<(list: ReadonlyArray<RecentThreadShortcut>) => Promise<void>>(() => |
| 29 | Promise.resolve(), |
| 30 | ); |
| 31 | return { save, publish, recents: createRecentThreadShortcuts({ load, save, publish }) }; |
| 32 | } |
| 33 | |
| 34 | describe("createRecentThreadShortcuts", () => { |
| 35 | it("waits for the persisted list before recording the open thread", async () => { |
| 36 | const { save, publish, recents } = setup(() => Promise.resolve([thread("old")])); |
| 37 | recents.recordActiveThread(thread("new")); |
| 38 | expect(publish).not.toHaveBeenCalled(); |
| 39 | await recents.loaded; |
| 40 | await recents.saved(); |
| 41 | expect(publish).toHaveBeenLastCalledWith([thread("new"), thread("old")]); |
| 42 | expect(save).toHaveBeenLastCalledWith([thread("new"), thread("old")]); |
| 43 | }); |
| 44 | |
| 45 | it("does not persist the empty fallback after a failed load", async () => { |
| 46 | using _warn = vi.spyOn(console, "warn").mockImplementation(() => undefined); |
| 47 | const { save, publish, recents } = setup(() => Promise.reject(new Error("read failed"))); |
| 48 | await recents.loaded; |
| 49 | await recents.saved(); |
| 50 | expect(publish).toHaveBeenLastCalledWith([]); |
| 51 | expect(save).not.toHaveBeenCalled(); |
| 52 | |
| 53 | recents.recordActiveThread(thread("a")); |
| 54 | await recents.saved(); |
| 55 | expect(save).toHaveBeenLastCalledWith([thread("a")]); |
| 56 | }); |
| 57 | |
| 58 | it("drops threads of environments that are no longer saved", async () => { |
| 59 | const gone = { environmentId: "gone", threadId: "b", title: "b" }; |
| 60 | const kept = { environmentId: "kept", threadId: "c", title: "c" }; |
| 61 | const { save, publish, recents } = setup(() => Promise.resolve([thread("a"), gone, kept])); |
| 62 | recents.retainEnvironments(new Set(["env", "kept"])); |
| 63 | await recents.loaded; |
| 64 | expect(publish).toHaveBeenLastCalledWith([thread("a"), kept]); |
| 65 | |
| 66 | recents.retainEnvironments(new Set(["kept"])); |
| 67 | await recents.saved(); |
| 68 | expect(publish).toHaveBeenLastCalledWith([kept]); |
| 69 | expect(save).toHaveBeenLastCalledWith([kept]); |
| 70 | |
| 71 | recents.recordActiveThread(thread("a")); |
| 72 | expect(publish).toHaveBeenLastCalledWith([kept]); |
| 73 | }); |
| 74 | |
| 75 | it("records title changes of the open thread and ignores repeats", async () => { |
| 76 | const { publish, recents } = setup(() => Promise.resolve([])); |
| 77 | await recents.loaded; |
| 78 | recents.recordActiveThread(thread("a", "")); |
| 79 | recents.recordActiveThread(thread("a", "Named")); |
| 80 | recents.recordActiveThread(thread("a", "Named")); |
| 81 | recents.recordActiveThread(null); |
| 82 | expect(publish.mock.calls.map(([list]) => list)).toEqual([ |
| 83 | [], |
| 84 | [thread("a", "")], |
| 85 | [thread("a", "Named")], |
| 86 | ]); |
| 87 | }); |
| 88 | }); |