11import { ThreadId } from "@t3tools/contracts" ;
22import { beforeEach , describe , expect , it } from "vite-plus/test" ;
33
4- import { useThreadSelectionStore } from "./threadSelectionStore" ;
4+ import {
5+ getThreadKeysToDeselectAfterDelete ,
6+ useThreadSelectionStore ,
7+ } from "./threadSelectionStore" ;
58
69const THREAD_A = ThreadId . make ( "thread-a" ) ;
710const THREAD_B = ThreadId . make ( "thread-b" ) ;
@@ -16,6 +19,59 @@ describe("threadSelectionStore", () => {
1619 useThreadSelectionStore . getState ( ) . clearSelection ( ) ;
1720 } ) ;
1821
22+ describe ( "bulk deletion cleanup" , ( ) => {
23+ it ( "clears missing selection rows and completed deletions while retaining a failed thread" , ( ) => {
24+ const store = useThreadSelectionStore . getState ( ) ;
25+ store . toggleThread ( THREAD_A ) ;
26+ store . toggleThread ( THREAD_B ) ;
27+ store . toggleThread ( THREAD_C ) ;
28+ const selected = [ ...useThreadSelectionStore . getState ( ) . selectedThreadKeys ] ;
29+ // A deleted successfully but its shell has not refreshed; B failed;
30+ // C was deleted elsewhere and never entered this client's delete loop.
31+ const existingThreads = new Set ( [ THREAD_A , THREAD_B ] ) ;
32+ store . removeFromSelection (
33+ getThreadKeysToDeselectAfterDelete ( selected , new Set ( [ THREAD_A ] ) , ( key ) =>
34+ existingThreads . has ( ThreadId . make ( key ) ) ,
35+ ) ,
36+ ) ;
37+
38+ expect ( [ ...useThreadSelectionStore . getState ( ) . selectedThreadKeys ] ) . toEqual ( [ THREAD_B ] ) ;
39+ expect ( useThreadSelectionStore . getState ( ) . anchorThreadKey ) . toBeNull ( ) ;
40+ } ) ;
41+
42+ it ( "exits selection mode when the last selected thread disappeared elsewhere" , ( ) => {
43+ const store = useThreadSelectionStore . getState ( ) ;
44+ store . toggleThread ( THREAD_A ) ;
45+ store . removeFromSelection (
46+ getThreadKeysToDeselectAfterDelete ( [ THREAD_A ] , new Set ( ) , ( ) => false ) ,
47+ ) ;
48+
49+ expect ( useThreadSelectionStore . getState ( ) . hasSelection ( ) ) . toBe ( false ) ;
50+ expect ( useThreadSelectionStore . getState ( ) . anchorThreadKey ) . toBeNull ( ) ;
51+ } ) ;
52+
53+ it ( "keeps unprocessed and hidden live threads and selections added while deletion was pending" , ( ) => {
54+ const store = useThreadSelectionStore . getState ( ) ;
55+ store . toggleThread ( THREAD_A ) ;
56+ store . toggleThread ( THREAD_B ) ;
57+ store . toggleThread ( THREAD_C ) ;
58+ const selected = [ ...useThreadSelectionStore . getState ( ) . selectedThreadKeys ] ;
59+ store . toggleThread ( THREAD_D ) ;
60+ // Only A completed before interruption. B is unprocessed; C still
61+ // has a shell even though its row is outside the rendered page.
62+ store . removeFromSelection (
63+ getThreadKeysToDeselectAfterDelete ( selected , new Set ( [ THREAD_A ] ) , ( ) => true ) ,
64+ ) ;
65+
66+ expect ( [ ...useThreadSelectionStore . getState ( ) . selectedThreadKeys ] ) . toEqual ( [
67+ THREAD_B ,
68+ THREAD_C ,
69+ THREAD_D ,
70+ ] ) ;
71+ expect ( useThreadSelectionStore . getState ( ) . anchorThreadKey ) . toBe ( THREAD_D ) ;
72+ } ) ;
73+ } ) ;
74+
1975 describe ( "toggleThread" , ( ) => {
2076 it ( "adds a thread to empty selection" , ( ) => {
2177 useThreadSelectionStore . getState ( ) . toggleThread ( THREAD_A ) ;
0 commit comments