11import { describe , it , expect } from "@jest/globals" ;
2- import { partitionWorkspacesByAge , formatOldWorkspaceThreshold } from "./workspaceFiltering" ;
2+ import {
3+ partitionWorkspacesByAge ,
4+ formatDaysThreshold ,
5+ AGE_THRESHOLDS_DAYS ,
6+ } from "./workspaceFiltering" ;
37import type { FrontendWorkspaceMetadata } from "@/common/types/workspace" ;
48import { DEFAULT_RUNTIME_CONFIG } from "@/common/constants/workspace" ;
59
@@ -12,10 +16,13 @@ describe("partitionWorkspacesByAge", () => {
1216 name : `workspace-${ id } ` ,
1317 projectName : "test-project" ,
1418 projectPath : "/test/project" ,
15- namedWorkspacePath : `/test/project/workspace-${ id } ` , // Path is arbitrary for this test
19+ namedWorkspacePath : `/test/project/workspace-${ id } ` ,
1620 runtimeConfig : DEFAULT_RUNTIME_CONFIG ,
1721 } ) ;
1822
23+ // Helper to get all "old" workspaces (all buckets combined)
24+ const getAllOld = ( buckets : FrontendWorkspaceMetadata [ ] [ ] ) => buckets . flat ( ) ;
25+
1926 it ( "should partition workspaces into recent and old based on 24-hour threshold" , ( ) => {
2027 const workspaces = [
2128 createWorkspace ( "recent1" ) ,
@@ -31,7 +38,8 @@ describe("partitionWorkspacesByAge", () => {
3138 old2 : now - 2 * ONE_DAY_MS , // 2 days ago
3239 } ;
3340
34- const { recent, old } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
41+ const { recent, buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
42+ const old = getAllOld ( buckets ) ;
3543
3644 expect ( recent ) . toHaveLength ( 2 ) ;
3745 expect ( recent . map ( ( w ) => w . id ) ) . toEqual ( expect . arrayContaining ( [ "recent1" , "recent2" ] ) ) ;
@@ -48,7 +56,8 @@ describe("partitionWorkspacesByAge", () => {
4856 // no-activity has no timestamp
4957 } ;
5058
51- const { recent, old } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
59+ const { recent, buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
60+ const old = getAllOld ( buckets ) ;
5261
5362 expect ( recent ) . toHaveLength ( 1 ) ;
5463 expect ( recent [ 0 ] . id ) . toBe ( "recent" ) ;
@@ -58,10 +67,11 @@ describe("partitionWorkspacesByAge", () => {
5867 } ) ;
5968
6069 it ( "should handle empty workspace list" , ( ) => {
61- const { recent, old } = partitionWorkspacesByAge ( [ ] , { } ) ;
70+ const { recent, buckets } = partitionWorkspacesByAge ( [ ] , { } ) ;
6271
6372 expect ( recent ) . toHaveLength ( 0 ) ;
64- expect ( old ) . toHaveLength ( 0 ) ;
73+ expect ( buckets ) . toHaveLength ( AGE_THRESHOLDS_DAYS . length ) ;
74+ expect ( buckets . every ( ( b ) => b . length === 0 ) ) . toBe ( true ) ;
6575 } ) ;
6676
6777 it ( "should handle workspace at exactly 24 hours (should show as recent due to always-show-one rule)" , ( ) => {
@@ -71,7 +81,8 @@ describe("partitionWorkspacesByAge", () => {
7181 "exactly-24h" : now - ONE_DAY_MS ,
7282 } ;
7383
74- const { recent, old } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
84+ const { recent, buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
85+ const old = getAllOld ( buckets ) ;
7586
7687 // Even though it's exactly 24 hours old, it should show as recent (always show at least one)
7788 expect ( recent ) . toHaveLength ( 1 ) ;
@@ -94,7 +105,8 @@ describe("partitionWorkspacesByAge", () => {
94105 old3 : now - 4 * ONE_DAY_MS ,
95106 } ;
96107
97- const { old } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
108+ const { buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
109+ const old = getAllOld ( buckets ) ;
98110
99111 expect ( old . map ( ( w ) => w . id ) ) . toEqual ( [ "old1" , "old2" , "old3" ] ) ;
100112 } ) ;
@@ -108,7 +120,8 @@ describe("partitionWorkspacesByAge", () => {
108120 old3 : now - 4 * ONE_DAY_MS ,
109121 } ;
110122
111- const { recent, old } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
123+ const { recent, buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
124+ const old = getAllOld ( buckets ) ;
112125
113126 // Most recent should be moved to recent section
114127 expect ( recent ) . toHaveLength ( 1 ) ;
@@ -118,11 +131,45 @@ describe("partitionWorkspacesByAge", () => {
118131 expect ( old ) . toHaveLength ( 2 ) ;
119132 expect ( old . map ( ( w ) => w . id ) ) . toEqual ( [ "old2" , "old3" ] ) ;
120133 } ) ;
134+
135+ it ( "should partition into correct age buckets" , ( ) => {
136+ const workspaces = [
137+ createWorkspace ( "recent" ) , // < 1 day
138+ createWorkspace ( "bucket0" ) , // 1-7 days
139+ createWorkspace ( "bucket1" ) , // 7-30 days
140+ createWorkspace ( "bucket2" ) , // > 30 days
141+ ] ;
142+
143+ const workspaceRecency = {
144+ recent : now - 12 * 60 * 60 * 1000 , // 12 hours
145+ bucket0 : now - 3 * ONE_DAY_MS , // 3 days (1-7 day bucket)
146+ bucket1 : now - 15 * ONE_DAY_MS , // 15 days (7-30 day bucket)
147+ bucket2 : now - 60 * ONE_DAY_MS , // 60 days (>30 day bucket)
148+ } ;
149+
150+ const { recent, buckets } = partitionWorkspacesByAge ( workspaces , workspaceRecency ) ;
151+
152+ expect ( recent ) . toHaveLength ( 1 ) ;
153+ expect ( recent [ 0 ] . id ) . toBe ( "recent" ) ;
154+
155+ expect ( buckets [ 0 ] ) . toHaveLength ( 1 ) ;
156+ expect ( buckets [ 0 ] [ 0 ] . id ) . toBe ( "bucket0" ) ;
157+
158+ expect ( buckets [ 1 ] ) . toHaveLength ( 1 ) ;
159+ expect ( buckets [ 1 ] [ 0 ] . id ) . toBe ( "bucket1" ) ;
160+
161+ expect ( buckets [ 2 ] ) . toHaveLength ( 1 ) ;
162+ expect ( buckets [ 2 ] [ 0 ] . id ) . toBe ( "bucket2" ) ;
163+ } ) ;
121164} ) ;
122165
123- describe ( "formatOldWorkspaceThreshold" , ( ) => {
124- it ( "should format the threshold as a human-readable string" , ( ) => {
125- const result = formatOldWorkspaceThreshold ( ) ;
126- expect ( result ) . toBe ( "1 day" ) ;
166+ describe ( "formatDaysThreshold" , ( ) => {
167+ it ( "should format singular day correctly" , ( ) => {
168+ expect ( formatDaysThreshold ( 1 ) ) . toBe ( "1 day" ) ;
169+ } ) ;
170+
171+ it ( "should format plural days correctly" , ( ) => {
172+ expect ( formatDaysThreshold ( 7 ) ) . toBe ( "7 days" ) ;
173+ expect ( formatDaysThreshold ( 30 ) ) . toBe ( "30 days" ) ;
127174 } ) ;
128175} ) ;
0 commit comments