@@ -4,76 +4,113 @@ import { scopeThreadRef } from "@t3tools/client-runtime/environment";
44import { type EnvironmentId , ThreadId } from "@t3tools/contracts" ;
55
66import {
7+ isSameSidebarThreadRef ,
78 useSidebarPendingFileDropStore ,
89 type SidebarPendingFileDrop ,
910} from "./sidebarPendingFileDropStore" ;
1011
11- function makeFiles ( count : number ) : File [ ] {
12- return Array . from ( { length : count } , ( _ , index ) => new File ( [ "x" ] , `f ${ index } .png` ) ) ;
12+ function makeFiles ( ... names : string [ ] ) : File [ ] {
13+ return names . map ( ( name ) => new File ( [ "x" ] , name ) ) ;
1314}
1415
15- function makeEntry ( environmentId : string , threadId : string , files : File [ ] ) : SidebarPendingFileDrop {
16+ function makeEntry (
17+ environmentId : string ,
18+ threadId : string ,
19+ files : File [ ] ,
20+ ) : Omit < SidebarPendingFileDrop , "id" > {
1621 return {
1722 threadRef : scopeThreadRef ( environmentId as EnvironmentId , ThreadId . make ( threadId ) ) ,
1823 files,
1924 } ;
2025}
2126
27+ function refOf ( environmentId : string , threadId : string ) {
28+ return makeEntry ( environmentId , threadId , [ ] ) . threadRef ;
29+ }
30+
2231beforeEach ( ( ) => {
23- useSidebarPendingFileDropStore . setState ( { pending : null } ) ;
32+ useSidebarPendingFileDropStore . setState ( { pending : [ ] } ) ;
2433} ) ;
2534
2635describe ( "sidebarPendingFileDropStore" , ( ) => {
2736 it ( "starts empty" , ( ) => {
28- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toBeNull ( ) ;
37+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( [ ] ) ;
2938 } ) ;
3039
3140 it ( "stashes and consumes a drop for the matching thread" , ( ) => {
32- const files = makeFiles ( 2 ) ;
33- const entry = makeEntry ( "env-1" , "thread-1" , files ) ;
34- useSidebarPendingFileDropStore . getState ( ) . setPendingFileDrop ( entry ) ;
41+ const files = makeFiles ( "a.png" , "b.png" ) ;
42+ const store = useSidebarPendingFileDropStore . getState ( ) ;
43+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , files ) ) ;
3544
3645 expect (
37- useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( entry . threadRef ) ,
46+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "env-1" , "thread-1" ) ) ,
3847 ) . toEqual ( files ) ;
39- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toBeNull ( ) ;
48+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( [ ] ) ;
4049 } ) ;
4150
42- it ( "refuses to consume for a different thread without clearing the stash" , ( ) => {
43- const entry = makeEntry ( "env-1" , "thread-1" , makeFiles ( 1 ) ) ;
44- useSidebarPendingFileDropStore . getState ( ) . setPendingFileDrop ( entry ) ;
51+ it ( "accumulates repeat drops onto the same thread instead of replacing" , ( ) => {
52+ const store = useSidebarPendingFileDropStore . getState ( ) ;
53+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "a.png" ) ) ) ;
54+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "b.png" ) ) ) ;
4555
46- const otherThread = makeEntry ( "env-1" , "thread-2" , [ ] ) . threadRef ;
4756 expect (
48- useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( otherThread ) ,
49- ) . toBeNull ( ) ;
50- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( entry ) ;
57+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "env-1" , "thread-1" ) ) ,
58+ ) . toEqual ( makeFiles ( "a.png" , "b.png" ) ) ;
59+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( [ ] ) ;
60+ } ) ;
5161
52- useSidebarPendingFileDropStore . getState ( ) . clearPendingFileDrop ( ) ;
53- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toBeNull ( ) ;
62+ it ( "keeps drops for other threads when consuming one thread" , ( ) => {
63+ const store = useSidebarPendingFileDropStore . getState ( ) ;
64+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "a.png" ) ) ) ;
65+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-2" , makeFiles ( "b.png" ) ) ) ;
66+
67+ expect (
68+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "env-1" , "thread-2" ) ) ,
69+ ) . toEqual ( makeFiles ( "b.png" ) ) ;
70+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toHaveLength ( 1 ) ;
5471 } ) ;
5572
56- it ( "replaces an earlier pending drop with a newer one" , ( ) => {
57- const first = makeEntry ( "env-1" , "thread-1" , makeFiles ( 1 ) ) ;
58- const second = makeEntry ( "env-2" , "thread-2" , makeFiles ( 3 ) ) ;
59- useSidebarPendingFileDropStore . getState ( ) . setPendingFileDrop ( first ) ;
60- useSidebarPendingFileDropStore . getState ( ) . setPendingFileDrop ( second ) ;
73+ it ( "clears only the drop matching a stale navigation's id" , ( ) => {
74+ const store = useSidebarPendingFileDropStore . getState ( ) ;
75+ const firstId = store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "a.png" ) ) ) ;
76+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "b.png" ) ) ) ;
6177
62- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( second ) ;
78+ useSidebarPendingFileDropStore . getState ( ) . clearPendingFileDrop ( firstId ) ;
6379 expect (
64- useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( first . threadRef ) ,
65- ) . toBeNull ( ) ;
80+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "env-1" , "thread-1" ) ) ,
81+ ) . toEqual ( makeFiles ( "b.png" ) ) ;
82+ } ) ;
83+
84+ it ( "clears every drop for a missing thread" , ( ) => {
85+ const store = useSidebarPendingFileDropStore . getState ( ) ;
86+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "a.png" ) ) ) ;
87+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-1" , makeFiles ( "b.png" ) ) ) ;
88+ store . queuePendingFileDrop ( makeEntry ( "env-1" , "thread-2" , makeFiles ( "c.png" ) ) ) ;
89+
90+ useSidebarPendingFileDropStore
91+ . getState ( )
92+ . clearPendingFileDropsForThread ( refOf ( "env-1" , "thread-1" ) ) ;
6693 expect (
67- useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( second . threadRef ) ,
68- ) . not . toBeNull ( ) ;
94+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "env-1" , "thread-2" ) ) ,
95+ ) . toEqual ( makeFiles ( "c.png" ) ) ;
96+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( [ ] ) ;
6997 } ) ;
7098
7199 it ( "does not confuse refs whose joined keys collide on colons" , ( ) => {
72- const entry = makeEntry ( "a" , "b:c" , makeFiles ( 1 ) ) ;
73- useSidebarPendingFileDropStore . getState ( ) . setPendingFileDrop ( entry ) ;
100+ const store = useSidebarPendingFileDropStore . getState ( ) ;
101+ store . queuePendingFileDrop ( makeEntry ( "a" , "b:c" , makeFiles ( "a.png" ) ) ) ;
102+
103+ expect (
104+ useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( refOf ( "a:b" , "c" ) ) ,
105+ ) . toBeNull ( ) ;
106+ expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toHaveLength ( 1 ) ;
107+ } ) ;
108+ } ) ;
74109
75- const colliding = makeEntry ( "a:b" , "c" , [ ] ) . threadRef ;
76- expect ( useSidebarPendingFileDropStore . getState ( ) . consumePendingFileDrop ( colliding ) ) . toBeNull ( ) ;
77- expect ( useSidebarPendingFileDropStore . getState ( ) . pending ) . toEqual ( entry ) ;
110+ describe ( "isSameSidebarThreadRef" , ( ) => {
111+ it ( "compares fields, not joined keys" , ( ) => {
112+ expect ( isSameSidebarThreadRef ( refOf ( "a" , "b:c" ) , refOf ( "a" , "b:c" ) ) ) . toBe ( true ) ;
113+ expect ( isSameSidebarThreadRef ( refOf ( "a" , "b:c" ) , refOf ( "a:b" , "c" ) ) ) . toBe ( false ) ;
114+ expect ( isSameSidebarThreadRef ( refOf ( "a" , "b" ) , refOf ( "a" , "c" ) ) ) . toBe ( false ) ;
78115 } ) ;
79116} ) ;
0 commit comments