Files
tutti/packages/agent/gui/agent-gui/agentGuiNode/controller/useAgentGUIConversationBatchDeletion.spec.tsx
agent-radar 2ed3415fb7
Publish Claude Code Binaries / Stage and publish binaries (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-arm64 (push) Canceled after 0s
Publish Tutti App Runtime / Build darwin-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build linux-amd64 (push) Canceled after 0s
Publish Tutti App Runtime / Build windows-amd64 (push) Canceled after 0s
Sync Tutti Agent Skills / sync (push) Canceled after 0s
Windows Agent Adapters / Test Windows agent adapters (push) Canceled after 0s
Windows Daemon Adapters / Test Windows daemon adapters (push) Canceled after 0s
Publish Tutti App Runtime / Publish runtime catalog (push) Canceled after 0s
源码快照 @ 2026-09-01 (source-only, 剔除非源码素材)
2026-09-01 20:17:27 +08:00

258 lines
8.3 KiB
TypeScript

import { act, renderHook, waitFor } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { useState } from "react";
import type { AgentGUIRuntime } from "../../../agentActivityRuntime";
import { useAgentGUIConversationBatchDeletion } from "./useAgentGUIConversationBatchDeletion";
function createInput(agentActivityRuntime: AgentGUIRuntime) {
return {
activeConversationIdRef: { current: null as string | null },
agentActivityRuntime,
agentHostApi: { toast: { error: vi.fn() } } as never,
conversationsRef: {
current: [
{
cwd: "/workspace",
id: "loaded-session",
provider: "codex" as const,
sortTimeUnixMs: 1,
status: "completed" as const,
title: "Loaded",
titleFallback: null,
updatedAtUnixMs: 1,
userId: "user-1"
}
]
},
dataRef: { current: { provider: "codex" } } as never,
deleteAgentSessionView: vi.fn(),
isDeletingProjectConversations: false,
markSelectedConversationDetailPending: vi.fn(() => null),
persistActiveConversation: vi.fn(),
removeConversations: vi.fn(),
sessionViewRef: (agentSessionId: string | null | undefined) => ({
agentSessionId,
origin: "local",
workspaceId: "workspace-1"
}),
setActiveConversationId: vi.fn(),
setDetailError: vi.fn(),
setDraftByScopeKey: vi.fn(),
setIntent: vi.fn(),
setIsDeletingProjectConversations: vi.fn(),
setIsLoadingMessages: vi.fn(),
setListError: vi.fn(),
submittedDraftSnapshotsRef: { current: {} },
workspaceId: "workspace-1"
};
}
describe("useAgentGUIConversationBatchDeletion", () => {
it("requests one unpinned candidate snapshot and sends one exact batch delete", async () => {
const listSessionSectionDeletionCandidates = vi.fn(async () => ({
excludePinned: true,
sectionKey: "conversations",
sessionIds: ["loaded-session", "unloaded-session"],
workspaceId: "workspace-1"
}));
const deleteSessionsBatch = vi.fn(async () => ({
cleanupFailedSessionIds: [],
removedMessages: 1,
removedSessionIds: ["loaded-session", "unloaded-session"],
removedSessions: 2
}));
const deleteSession = vi.fn();
const runtime = {
deleteSession,
deleteSessionsBatch,
listSessionSectionDeletionCandidates
} as unknown as AgentGUIRuntime;
const input = createInput(runtime);
const { result } = renderHook(() =>
useAgentGUIConversationBatchDeletion(input)
);
let sessionIds: string[] = [];
await act(async () => {
sessionIds = await result.current.confirmDeleteProjectConversations(
"conversations",
"codex-target"
);
});
expect(listSessionSectionDeletionCandidates).toHaveBeenCalledTimes(1);
expect(listSessionSectionDeletionCandidates).toHaveBeenCalledWith({
agentTargetId: "codex-target",
excludePinned: true,
sectionKey: "conversations",
workspaceId: "workspace-1"
});
await act(async () => {
expect(await result.current.confirmDeleteConversations(sessionIds)).toBe(
true
);
});
await waitFor(() => expect(deleteSessionsBatch).toHaveBeenCalledTimes(1));
expect(deleteSessionsBatch).toHaveBeenCalledWith({
sessionIds: ["loaded-session", "unloaded-session"],
workspaceId: "workspace-1"
});
expect(deleteSession).not.toHaveBeenCalled();
await waitFor(() =>
expect(input.deleteAgentSessionView).toHaveBeenCalledTimes(2)
);
expect(input.removeConversations).toHaveBeenCalledWith([
"loaded-session",
"unloaded-session"
]);
});
it("refreshes activity and does not delete when the candidate snapshot is empty", async () => {
const load = vi.fn(async () => ({
presences: [],
sessionMessagesById: {},
sessions: [],
workspaceId: "workspace-1"
}));
const runtime = {
deleteSessionsBatch: vi.fn(),
listSessionSectionDeletionCandidates: vi.fn(async () => ({
excludePinned: true,
sectionKey: "conversations",
sessionIds: [],
workspaceId: "workspace-1"
})),
load
} as unknown as AgentGUIRuntime;
const { result } = renderHook(() =>
useAgentGUIConversationBatchDeletion(createInput(runtime))
);
let sessionIds: string[] = [];
await act(async () => {
sessionIds =
await result.current.confirmDeleteProjectConversations("conversations");
});
expect(sessionIds).toEqual([]);
await waitFor(() => expect(load).toHaveBeenCalledWith("workspace-1"));
expect(runtime.deleteSessionsBatch).not.toHaveBeenCalled();
});
it("commits a surviving conversation only after deleting a batch containing the active session", async () => {
let committedActiveConversationId: string | null = "loaded-session";
let activeConversationIdObservedByDelete: string | null = null;
const deleteSessionsBatch = vi.fn(async () => {
activeConversationIdObservedByDelete = committedActiveConversationId;
return {
cleanupFailedSessionIds: [],
removedMessages: 0,
removedSessionIds: ["loaded-session"],
removedSessions: 1
};
});
const runtime = {
deleteSession: vi.fn(),
deleteSessionsBatch
} as unknown as AgentGUIRuntime;
const input = createInput(runtime);
input.activeConversationIdRef.current = "loaded-session";
input.conversationsRef.current = [
...input.conversationsRef.current,
{
cwd: "/workspace",
id: "surviving-session",
provider: "codex" as const,
sortTimeUnixMs: 2,
status: "completed" as const,
title: "Surviving",
titleFallback: null,
updatedAtUnixMs: 2,
userId: "user-1"
}
];
const { result } = renderHook(() => {
const [activeConversationId, setActiveConversationId] = useState<
string | null
>("loaded-session");
committedActiveConversationId = activeConversationId;
return {
activeConversationId,
...useAgentGUIConversationBatchDeletion({
...input,
setActiveConversationId
})
};
});
await act(async () => {
await result.current.confirmDeleteConversations(["loaded-session"]);
});
expect(activeConversationIdObservedByDelete).toBe("loaded-session");
await waitFor(() =>
expect(input.deleteAgentSessionView).toHaveBeenCalledTimes(1)
);
expect(result.current.activeConversationId).toBe("surviving-session");
expect(input.persistActiveConversation).toHaveBeenCalledWith(
"surviving-session"
);
});
it("fails a protected batch closed without partially changing rail state", async () => {
const deleteSessionsBatch = vi.fn(async () => {
throw {
code: "workspace_issue_resource_exists",
reason: "tutti_execution_active",
details: {
protectedIssues: [
{
executionId: "execution-1",
issueId: "issue-1",
sourceSessionId: "loaded-session",
status: "running"
}
]
}
};
});
const input = createInput({
deleteSession: vi.fn(),
deleteSessionsBatch
} as unknown as AgentGUIRuntime);
input.activeConversationIdRef.current = "loaded-session";
const { result } = renderHook(() => {
const [activeConversationId, setActiveConversationId] = useState<
string | null
>("loaded-session");
return {
activeConversationId,
...useAgentGUIConversationBatchDeletion({
...input,
setActiveConversationId
})
};
});
await act(async () => {
expect(
await result.current.confirmDeleteConversations([
"loaded-session",
"unloaded-session"
])
).toBe(false);
});
await waitFor(() =>
expect(input.setIsDeletingProjectConversations).toHaveBeenLastCalledWith(
false
)
);
expect(result.current.activeConversationId).toBe("loaded-session");
expect(input.activeConversationIdRef.current).toBe("loaded-session");
expect(input.persistActiveConversation).not.toHaveBeenCalled();
expect(input.removeConversations).not.toHaveBeenCalled();
expect(input.deleteAgentSessionView).not.toHaveBeenCalled();
});
});