Skip to content
Open
Show file tree
Hide file tree
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
40 changes: 35 additions & 5 deletions apps/mobile/src/features/home/useThreadListActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const state = vi.hoisted(() => ({
dropBusy: false,
failedAction: null as string | null,
archivedRefreshes: [] as string[],
onArchivedRefresh: undefined as (() => void) | undefined,
scopes: new Map<string, Set<string>>(),
shells: [] as EnvironmentThreadShell[],
requests: [] as {
Expand Down Expand Up @@ -55,6 +56,7 @@ vi.mock("../../components/ui/Dialog", () => ({
vi.mock("../archive/useArchivedThreadSnapshots", () => ({
refreshArchivedThreadsForEnvironment: (environmentId: string) => {
state.archivedRefreshes.push(environmentId);
state.onArchivedRefresh?.();
},
}));
vi.mock("../../state/session", () => ({
Expand All @@ -67,8 +69,9 @@ vi.mock("../../state/presentation", () => ({
vi.mock("../../state/server", () => ({
environmentServerConfigsAtom: "server-configs",
}));
vi.mock("../../state/atom-registry", () => ({
appAtomRegistry: {
vi.mock("../../state/atom-registry", async () => {
const registry = (await import("effect/reactivity")).AtomRegistry.make();
const stub = {
set: (_atom: string, value: boolean) => {
state.dropBusy = value;
},
Expand Down Expand Up @@ -97,8 +100,21 @@ vi.mock("../../state/atom-registry", () => ({
},
]),
),
},
}));
};
// Real atoms such as composerDraftsAtom go to a real registry; strings are stubbed state.
return {
appAtomRegistry: {
get: (atom: unknown) =>
typeof atom === "string"
? stub.get(atom)
: registry.get(atom as Parameters<typeof registry.get>[0]),
set: (atom: unknown, value: unknown) =>
typeof atom === "string"
? stub.set(atom, value as boolean)
: registry.set(atom as Parameters<typeof registry.set>[0], value),
},
};
});
vi.mock("../../state/use-atom-command", () => ({
useAtomCommand: (command: unknown) => command,
}));
Expand Down Expand Up @@ -157,6 +173,8 @@ vi.mock("../../state/threads", () => ({
),
}));

import { appAtomRegistry } from "../../state/atom-registry";
import { composerDraftsAtom } from "../../state/use-composer-drafts";
import { dismissThreadUndoNotice, readThreadUndoNotice } from "./thread-undo-notice";
import { useThreadListActions } from "./useThreadListActions";

Expand Down Expand Up @@ -189,6 +207,7 @@ beforeEach(() => {
state.connectedEnvironmentIds = [primaryEnvironmentId, otherEnvironmentId];
state.failedAction = null;
state.archivedRefreshes = [];
state.onArchivedRefresh = undefined;
state.pendingOrder = null;
state.dropBusy = false;
state.scopes = new Map([
Expand Down Expand Up @@ -230,7 +249,9 @@ describe("thread list operation permissions", () => {
expect(state.requests).toEqual([]);
});

it("rechecks permission after a retained delete confirmation", async () => {
it("rechecks permission after a retained delete confirmation, then clears the draft", async () => {
const draft = { text: "unsent", attachments: [] };
appAtomRegistry.set(composerDraftsAtom, { "primary:thread": draft, "primary:other": draft });
useThreadListActions().confirmDeleteThread(makeThread());
const confirm = state.dialogs[0]?.onConfirm;
expect(confirm).toBeTypeOf("function");
Expand All @@ -239,6 +260,15 @@ describe("thread list operation permissions", () => {
await confirm!();

expect(state.requests).toEqual([]);
expect(Object.keys(appAtomRegistry.get(composerDraftsAtom))).toContain("primary:thread");

state.scopes.get(primaryEnvironmentId)!.add(AuthOrchestrationOperateScope);
const deleted = new Promise<void>((resolve) => (state.onArchivedRefresh = resolve));
confirm!();
await deleted;

expect(state.requests).toEqual([expect.objectContaining({ action: "delete" })]);
expect(Object.keys(appAtomRegistry.get(composerDraftsAtom))).toEqual(["primary:other"]);
});
});

Expand Down
2 changes: 2 additions & 0 deletions apps/mobile/src/features/home/useThreadListActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { appAtomRegistry } from "../../state/atom-registry";
import { environmentServerConfigsAtom } from "../../state/server";
import { connectedEnvironmentIdsAtom } from "../../state/presentation";
import { readEnvironmentScope } from "../../state/session";
import { clearComposerDraft } from "../../state/use-composer-drafts";
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
import { useAtomCommand } from "../../state/use-atom-command";
Expand Down Expand Up @@ -160,6 +161,7 @@ function useThreadActionExecutor(
if (action === "archive" || action === "unarchive" || action === "delete") {
refreshArchivedThreadsForEnvironment(thread.environmentId);
}
if (action === "delete") clearComposerDraft(key);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
onCompleted?.(action, thread);
return true;
})();
Expand Down
Loading