|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
2 | 2 | import { defaultAnimateLayoutChanges, type AnimateLayoutChanges } from "@dnd-kit/sortable"; |
| 3 | +import * as Cause from "effect/Cause"; |
| 4 | +import { AsyncResult } from "effect/unstable/reactivity"; |
3 | 5 | import { |
4 | 6 | animatePinnedLayoutChanges, |
5 | 7 | archiveSelectedThreadEntries, |
6 | 8 | buildBulkTitleRegenerationContextMenuItem, |
7 | 9 | buildBulkUnpinContextMenuItem, |
8 | 10 | buildMultiSelectThreadContextMenuItems, |
9 | 11 | createThreadJumpHintVisibilityController, |
| 12 | + deleteSelectedThreadEntries, |
10 | 13 | filterSidebarProjectScopeItems, |
11 | 14 | getSidebarThreadIdsToPrewarm, |
12 | 15 | resolveAdjacentThreadId, |
@@ -81,6 +84,102 @@ describe("animatePinnedLayoutChanges", () => { |
81 | 84 | }); |
82 | 85 | }); |
83 | 86 |
|
| 87 | +describe("deleteSelectedThreadEntries", () => { |
| 88 | + const entries = [{ threadKey: "one" }, { threadKey: "two" }, { threadKey: "three" }] as const; |
| 89 | + const success = AsyncResult.success(undefined); |
| 90 | + const failure = AsyncResult.failure(Cause.fail(new Error("Delete failed"))); |
| 91 | + const interrupted = AsyncResult.failure(Cause.interrupt()); |
| 92 | + |
| 93 | + it("waits for each delete and excludes only earlier successes from worktree checks", async () => { |
| 94 | + let resolveDelete!: (result: typeof success) => void; |
| 95 | + const pendingDelete = new Promise<typeof success>((resolve) => { |
| 96 | + resolveDelete = resolve; |
| 97 | + }); |
| 98 | + const worktreeChecks: { threadKey: string; deletedThreadKeys: string[] }[] = []; |
| 99 | + const deletion = deleteSelectedThreadEntries({ |
| 100 | + entries, |
| 101 | + delete: async ({ threadKey }, deletedThreadKeys) => { |
| 102 | + worktreeChecks.push({ threadKey, deletedThreadKeys: [...deletedThreadKeys] }); |
| 103 | + return threadKey === "one" ? pendingDelete : success; |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(worktreeChecks).toEqual([{ threadKey: "one", deletedThreadKeys: [] }]); |
| 108 | + resolveDelete(success); |
| 109 | + const outcome = await deletion; |
| 110 | + |
| 111 | + expect(worktreeChecks).toEqual([ |
| 112 | + { threadKey: "one", deletedThreadKeys: [] }, |
| 113 | + { threadKey: "two", deletedThreadKeys: ["one"] }, |
| 114 | + { threadKey: "three", deletedThreadKeys: ["one", "two"] }, |
| 115 | + ]); |
| 116 | + expect(outcome).toEqual({ |
| 117 | + deletedThreadKeys: new Set(["one", "two", "three"]), |
| 118 | + firstFailure: null, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("continues after ordinary failures and keeps the first failure", async () => { |
| 123 | + const laterFailure = AsyncResult.failure(Cause.fail(new Error("Later failure"))); |
| 124 | + const deletedKeysAtLastEntry: string[][] = []; |
| 125 | + const outcome = await deleteSelectedThreadEntries({ |
| 126 | + entries: [...entries, { threadKey: "four" }], |
| 127 | + delete: async ({ threadKey }, deletedThreadKeys) => { |
| 128 | + if (threadKey === "one") return failure; |
| 129 | + if (threadKey === "three") return laterFailure; |
| 130 | + if (threadKey === "four") deletedKeysAtLastEntry.push([...deletedThreadKeys]); |
| 131 | + return success; |
| 132 | + }, |
| 133 | + }); |
| 134 | + |
| 135 | + expect(deletedKeysAtLastEntry).toEqual([["two"]]); |
| 136 | + expect(outcome).toEqual({ |
| 137 | + deletedThreadKeys: new Set(["two", "four"]), |
| 138 | + firstFailure: failure, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it.each([ |
| 143 | + { firstResult: success, deletedThreadKeys: new Set(["one"]), firstFailure: null }, |
| 144 | + { firstResult: failure, deletedThreadKeys: new Set<string>(), firstFailure: failure }, |
| 145 | + ])("stops on interruption and preserves earlier results %#", async (testCase) => { |
| 146 | + const attemptedThreadKeys: string[] = []; |
| 147 | + const outcome = await deleteSelectedThreadEntries({ |
| 148 | + entries, |
| 149 | + delete: async ({ threadKey }) => { |
| 150 | + attemptedThreadKeys.push(threadKey); |
| 151 | + return threadKey === "one" ? testCase.firstResult : interrupted; |
| 152 | + }, |
| 153 | + }); |
| 154 | + |
| 155 | + expect(attemptedThreadKeys).toEqual(["one", "two"]); |
| 156 | + expect(outcome).toEqual({ |
| 157 | + deletedThreadKeys: testCase.deletedThreadKeys, |
| 158 | + firstFailure: testCase.firstFailure, |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it("does not count a skipped entry as deleted", async () => { |
| 163 | + const visibleEntries = new Set(entries.map(({ threadKey }) => threadKey)); |
| 164 | + const worktreeChecks: string[][] = []; |
| 165 | + const outcome = await deleteSelectedThreadEntries({ |
| 166 | + entries, |
| 167 | + delete: async ({ threadKey }, deletedThreadKeys) => { |
| 168 | + if (!visibleEntries.has(threadKey)) return null; |
| 169 | + worktreeChecks.push([...deletedThreadKeys]); |
| 170 | + visibleEntries.delete("two"); |
| 171 | + return success; |
| 172 | + }, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(worktreeChecks).toEqual([[], ["one"]]); |
| 176 | + expect(outcome).toEqual({ |
| 177 | + deletedThreadKeys: new Set(["one", "three"]), |
| 178 | + firstFailure: null, |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
84 | 183 | describe("archiveSelectedThreadEntries", () => { |
85 | 184 | const entries = [{ threadKey: "one" }, { threadKey: "two" }, { threadKey: "three" }] as const; |
86 | 185 | const success = { _tag: "Success" } as const; |
|
0 commit comments