@@ -56,4 +56,82 @@ describe("trimSessions", () => {
5656 "root-2" ,
5757 ] )
5858 } )
59+
60+ test ( "keeps descendants of a kept root transitively" , ( ) => {
61+ const now = 1_000_000
62+ const old = now - 20_000_000
63+ const list = [
64+ session ( { id : "root-1" , created : now - 1000 } ) ,
65+ session ( { id : "child" , parentID : "root-1" , created : old } ) ,
66+ session ( { id : "grandchild" , parentID : "child" , created : old } ) ,
67+ session ( { id : "great-grandchild" , parentID : "grandchild" , created : old } ) ,
68+ ]
69+
70+ const result = trimSessions ( list , { limit : 2 , permission : { } , now } )
71+ expect ( result . map ( ( x ) => x . id ) ) . toEqual ( [ "child" , "grandchild" , "great-grandchild" , "root-1" ] )
72+ } )
73+
74+ test ( "trims descendants of a trimmed root transitively" , ( ) => {
75+ const now = 1_000_000
76+ const old = now - 20_000_000
77+ const list = [
78+ session ( { id : "root-1" , created : now - 1000 } ) ,
79+ session ( { id : "root-2" , created : now - 2000 } ) ,
80+ session ( { id : "z-root" , created : now - 30_000_000 } ) ,
81+ session ( { id : "z-child" , parentID : "z-root" , created : old } ) ,
82+ session ( { id : "z-grandchild" , parentID : "z-child" , created : old } ) ,
83+ ]
84+
85+ const result = trimSessions ( list , { limit : 2 , permission : { } , now } )
86+ expect ( result . map ( ( x ) => x . id ) ) . toEqual ( [ "root-1" , "root-2" ] )
87+ } )
88+
89+ test ( "permission and recency still keep deep children of a trimmed root" , ( ) => {
90+ const now = 1_000_000
91+ const old = now - 20_000_000
92+ const list = [
93+ session ( { id : "root-1" , created : now - 1000 } ) ,
94+ session ( { id : "root-2" , created : now - 2000 } ) ,
95+ session ( { id : "z-root" , created : now - 30_000_000 } ) ,
96+ session ( { id : "z-child" , parentID : "z-root" , created : old } ) ,
97+ session ( { id : "z-grandchild-permission" , parentID : "z-child" , created : old } ) ,
98+ session ( { id : "z-grandchild-recent" , parentID : "z-child" , created : now - 500 } ) ,
99+ session ( { id : "z-grandchild-trimmed" , parentID : "z-child" , created : old } ) ,
100+ ]
101+
102+ const result = trimSessions ( list , {
103+ limit : 2 ,
104+ permission : {
105+ "z-grandchild-permission" : [ { id : "perm-1" } as PermissionRequest ] ,
106+ } ,
107+ now,
108+ } )
109+ expect ( result . map ( ( x ) => x . id ) ) . toEqual ( [ "root-1" , "root-2" , "z-grandchild-permission" , "z-grandchild-recent" ] )
110+ } )
111+
112+ test ( "drops old orphan children whose parent chain never reaches a kept root" , ( ) => {
113+ const now = 1_000_000
114+ const old = now - 20_000_000
115+ const list = [
116+ session ( { id : "root-1" , created : now - 1000 } ) ,
117+ session ( { id : "orphan" , parentID : "missing-parent" , created : old } ) ,
118+ session ( { id : "orphan-child" , parentID : "orphan" , created : old } ) ,
119+ ]
120+
121+ const result = trimSessions ( list , { limit : 2 , permission : { } , now } )
122+ expect ( result . map ( ( x ) => x . id ) ) . toEqual ( [ "root-1" ] )
123+ } )
124+
125+ test ( "does not hang on a parentID cycle" , ( ) => {
126+ const now = 1_000_000
127+ const old = now - 20_000_000
128+ const list = [
129+ session ( { id : "root-1" , created : now - 1000 } ) ,
130+ session ( { id : "cycle-a" , parentID : "cycle-b" , created : old } ) ,
131+ session ( { id : "cycle-b" , parentID : "cycle-a" , created : old } ) ,
132+ ]
133+
134+ const result = trimSessions ( list , { limit : 2 , permission : { } , now } )
135+ expect ( result . map ( ( x ) => x . id ) ) . toEqual ( [ "root-1" ] )
136+ } )
59137} )
0 commit comments