|
| 1 | +import { describe, it, expect } from "@jest/globals"; |
| 2 | +import { partitionWorkspacesByAge, formatOldWorkspaceThreshold } from "./workspaceFiltering"; |
| 3 | +import type { FrontendWorkspaceMetadata } from "@/types/workspace"; |
| 4 | + |
| 5 | +describe("partitionWorkspacesByAge", () => { |
| 6 | + const now = Date.now(); |
| 7 | + const ONE_DAY_MS = 24 * 60 * 60 * 1000; |
| 8 | + |
| 9 | + const createWorkspace = (id: string): FrontendWorkspaceMetadata => ({ |
| 10 | + id, |
| 11 | + name: `workspace-${id}`, |
| 12 | + projectName: "test-project", |
| 13 | + projectPath: "/test/project", |
| 14 | + namedWorkspacePath: `/test/project/.worktrees/${id}`, |
| 15 | + }); |
| 16 | + |
| 17 | + it("should partition workspaces into recent and old based on 24-hour threshold", () => { |
| 18 | + const workspaces = [ |
| 19 | + createWorkspace("recent1"), |
| 20 | + createWorkspace("old1"), |
| 21 | + createWorkspace("recent2"), |
| 22 | + createWorkspace("old2"), |
| 23 | + ]; |
| 24 | + |
| 25 | + const workspaceRecency = { |
| 26 | + recent1: now - 1000, // 1 second ago |
| 27 | + old1: now - ONE_DAY_MS - 1000, // 24 hours and 1 second ago |
| 28 | + recent2: now - 12 * 60 * 60 * 1000, // 12 hours ago |
| 29 | + old2: now - 2 * ONE_DAY_MS, // 2 days ago |
| 30 | + }; |
| 31 | + |
| 32 | + const { recent, old } = partitionWorkspacesByAge(workspaces, workspaceRecency); |
| 33 | + |
| 34 | + expect(recent).toHaveLength(2); |
| 35 | + expect(recent.map((w) => w.id)).toEqual(expect.arrayContaining(["recent1", "recent2"])); |
| 36 | + |
| 37 | + expect(old).toHaveLength(2); |
| 38 | + expect(old.map((w) => w.id)).toEqual(expect.arrayContaining(["old1", "old2"])); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should treat workspaces with no recency timestamp as old", () => { |
| 42 | + const workspaces = [createWorkspace("no-activity"), createWorkspace("recent")]; |
| 43 | + |
| 44 | + const workspaceRecency = { |
| 45 | + recent: now - 1000, |
| 46 | + // no-activity has no timestamp |
| 47 | + }; |
| 48 | + |
| 49 | + const { recent, old } = partitionWorkspacesByAge(workspaces, workspaceRecency); |
| 50 | + |
| 51 | + expect(recent).toHaveLength(1); |
| 52 | + expect(recent[0].id).toBe("recent"); |
| 53 | + |
| 54 | + expect(old).toHaveLength(1); |
| 55 | + expect(old[0].id).toBe("no-activity"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should handle empty workspace list", () => { |
| 59 | + const { recent, old } = partitionWorkspacesByAge([], {}); |
| 60 | + |
| 61 | + expect(recent).toHaveLength(0); |
| 62 | + expect(old).toHaveLength(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should handle workspace at exactly 24 hours (should show as recent due to always-show-one rule)", () => { |
| 66 | + const workspaces = [createWorkspace("exactly-24h")]; |
| 67 | + |
| 68 | + const workspaceRecency = { |
| 69 | + "exactly-24h": now - ONE_DAY_MS, |
| 70 | + }; |
| 71 | + |
| 72 | + const { recent, old } = partitionWorkspacesByAge(workspaces, workspaceRecency); |
| 73 | + |
| 74 | + // Even though it's exactly 24 hours old, it should show as recent (always show at least one) |
| 75 | + expect(recent).toHaveLength(1); |
| 76 | + expect(recent[0].id).toBe("exactly-24h"); |
| 77 | + expect(old).toHaveLength(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should preserve workspace order within partitions", () => { |
| 81 | + const workspaces = [ |
| 82 | + createWorkspace("recent"), |
| 83 | + createWorkspace("old1"), |
| 84 | + createWorkspace("old2"), |
| 85 | + createWorkspace("old3"), |
| 86 | + ]; |
| 87 | + |
| 88 | + const workspaceRecency = { |
| 89 | + recent: now - 1000, |
| 90 | + old1: now - 2 * ONE_DAY_MS, |
| 91 | + old2: now - 3 * ONE_DAY_MS, |
| 92 | + old3: now - 4 * ONE_DAY_MS, |
| 93 | + }; |
| 94 | + |
| 95 | + const { old } = partitionWorkspacesByAge(workspaces, workspaceRecency); |
| 96 | + |
| 97 | + expect(old.map((w) => w.id)).toEqual(["old1", "old2", "old3"]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should always show at least one workspace when all are old", () => { |
| 101 | + const workspaces = [createWorkspace("old1"), createWorkspace("old2"), createWorkspace("old3")]; |
| 102 | + |
| 103 | + const workspaceRecency = { |
| 104 | + old1: now - 2 * ONE_DAY_MS, |
| 105 | + old2: now - 3 * ONE_DAY_MS, |
| 106 | + old3: now - 4 * ONE_DAY_MS, |
| 107 | + }; |
| 108 | + |
| 109 | + const { recent, old } = partitionWorkspacesByAge(workspaces, workspaceRecency); |
| 110 | + |
| 111 | + // Most recent should be moved to recent section |
| 112 | + expect(recent).toHaveLength(1); |
| 113 | + expect(recent[0].id).toBe("old1"); |
| 114 | + |
| 115 | + // Remaining should stay in old section |
| 116 | + expect(old).toHaveLength(2); |
| 117 | + expect(old.map((w) => w.id)).toEqual(["old2", "old3"]); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe("formatOldWorkspaceThreshold", () => { |
| 122 | + it("should format the threshold as a human-readable string", () => { |
| 123 | + const result = formatOldWorkspaceThreshold(); |
| 124 | + expect(result).toBe("1 day"); |
| 125 | + }); |
| 126 | +}); |
0 commit comments