|
1 | 1 | import { describe, it, expect } from "@jest/globals"; |
2 | | -import { partitionWorkspacesByAge, formatOldWorkspaceThreshold } from "./workspaceFiltering"; |
| 2 | +import { |
| 3 | + partitionWorkspacesByAge, |
| 4 | + formatOldWorkspaceThreshold, |
| 5 | + buildSortedWorkspacesByProject, |
| 6 | +} from "./workspaceFiltering"; |
3 | 7 | import type { FrontendWorkspaceMetadata } from "@/common/types/workspace"; |
| 8 | +import type { ProjectConfig } from "@/common/types/project"; |
4 | 9 | import { DEFAULT_RUNTIME_CONFIG } from "@/common/constants/workspace"; |
5 | 10 |
|
6 | 11 | describe("partitionWorkspacesByAge", () => { |
@@ -126,3 +131,145 @@ describe("formatOldWorkspaceThreshold", () => { |
126 | 131 | expect(result).toBe("1 day"); |
127 | 132 | }); |
128 | 133 | }); |
| 134 | + |
| 135 | +describe("buildSortedWorkspacesByProject", () => { |
| 136 | + const createWorkspace = ( |
| 137 | + id: string, |
| 138 | + projectPath: string, |
| 139 | + status?: "creating" |
| 140 | + ): FrontendWorkspaceMetadata => ({ |
| 141 | + id, |
| 142 | + name: `workspace-${id}`, |
| 143 | + projectName: projectPath.split("/").pop() ?? "unknown", |
| 144 | + projectPath, |
| 145 | + namedWorkspacePath: `${projectPath}/workspace-${id}`, |
| 146 | + runtimeConfig: DEFAULT_RUNTIME_CONFIG, |
| 147 | + status, |
| 148 | + }); |
| 149 | + |
| 150 | + it("should include workspaces from persisted config", () => { |
| 151 | + const projects = new Map<string, ProjectConfig>([ |
| 152 | + ["/project/a", { workspaces: [{ path: "/a/ws1", id: "ws1" }] }], |
| 153 | + ]); |
| 154 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 155 | + ["ws1", createWorkspace("ws1", "/project/a")], |
| 156 | + ]); |
| 157 | + |
| 158 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 159 | + |
| 160 | + expect(result.get("/project/a")).toHaveLength(1); |
| 161 | + expect(result.get("/project/a")?.[0].id).toBe("ws1"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("should include pending workspaces not yet in config", () => { |
| 165 | + const projects = new Map<string, ProjectConfig>([ |
| 166 | + ["/project/a", { workspaces: [{ path: "/a/ws1", id: "ws1" }] }], |
| 167 | + ]); |
| 168 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 169 | + ["ws1", createWorkspace("ws1", "/project/a")], |
| 170 | + ["pending1", createWorkspace("pending1", "/project/a", "creating")], |
| 171 | + ]); |
| 172 | + |
| 173 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 174 | + |
| 175 | + expect(result.get("/project/a")).toHaveLength(2); |
| 176 | + expect(result.get("/project/a")?.map((w) => w.id)).toContain("ws1"); |
| 177 | + expect(result.get("/project/a")?.map((w) => w.id)).toContain("pending1"); |
| 178 | + }); |
| 179 | + |
| 180 | + it("should handle multiple concurrent pending workspaces", () => { |
| 181 | + const projects = new Map<string, ProjectConfig>([["/project/a", { workspaces: [] }]]); |
| 182 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 183 | + ["pending1", createWorkspace("pending1", "/project/a", "creating")], |
| 184 | + ["pending2", createWorkspace("pending2", "/project/a", "creating")], |
| 185 | + ["pending3", createWorkspace("pending3", "/project/a", "creating")], |
| 186 | + ]); |
| 187 | + |
| 188 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 189 | + |
| 190 | + expect(result.get("/project/a")).toHaveLength(3); |
| 191 | + }); |
| 192 | + |
| 193 | + it("should add pending workspaces for projects not yet in config", () => { |
| 194 | + const projects = new Map<string, ProjectConfig>(); |
| 195 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 196 | + ["pending1", createWorkspace("pending1", "/new/project", "creating")], |
| 197 | + ]); |
| 198 | + |
| 199 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 200 | + |
| 201 | + expect(result.get("/new/project")).toHaveLength(1); |
| 202 | + expect(result.get("/new/project")?.[0].id).toBe("pending1"); |
| 203 | + }); |
| 204 | + |
| 205 | + it("should sort workspaces by recency (most recent first)", () => { |
| 206 | + const now = Date.now(); |
| 207 | + const projects = new Map<string, ProjectConfig>([ |
| 208 | + [ |
| 209 | + "/project/a", |
| 210 | + { |
| 211 | + workspaces: [ |
| 212 | + { path: "/a/ws1", id: "ws1" }, |
| 213 | + { path: "/a/ws2", id: "ws2" }, |
| 214 | + { path: "/a/ws3", id: "ws3" }, |
| 215 | + ], |
| 216 | + }, |
| 217 | + ], |
| 218 | + ]); |
| 219 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 220 | + ["ws1", createWorkspace("ws1", "/project/a")], |
| 221 | + ["ws2", createWorkspace("ws2", "/project/a")], |
| 222 | + ["ws3", createWorkspace("ws3", "/project/a")], |
| 223 | + ]); |
| 224 | + const recency = { |
| 225 | + ws1: now - 3000, // oldest |
| 226 | + ws2: now - 1000, // newest |
| 227 | + ws3: now - 2000, // middle |
| 228 | + }; |
| 229 | + |
| 230 | + const result = buildSortedWorkspacesByProject(projects, metadata, recency); |
| 231 | + |
| 232 | + expect(result.get("/project/a")?.map((w) => w.id)).toEqual(["ws2", "ws3", "ws1"]); |
| 233 | + }); |
| 234 | + |
| 235 | + it("should not duplicate workspaces that exist in both config and have creating status", () => { |
| 236 | + // Edge case: workspace was saved to config but still has status: "creating" |
| 237 | + // (this shouldn't happen in practice but tests defensive coding) |
| 238 | + const projects = new Map<string, ProjectConfig>([ |
| 239 | + ["/project/a", { workspaces: [{ path: "/a/ws1", id: "ws1" }] }], |
| 240 | + ]); |
| 241 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 242 | + ["ws1", createWorkspace("ws1", "/project/a", "creating")], |
| 243 | + ]); |
| 244 | + |
| 245 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 246 | + |
| 247 | + expect(result.get("/project/a")).toHaveLength(1); |
| 248 | + expect(result.get("/project/a")?.[0].id).toBe("ws1"); |
| 249 | + }); |
| 250 | + |
| 251 | + it("should skip workspaces with no id in config", () => { |
| 252 | + const projects = new Map<string, ProjectConfig>([ |
| 253 | + ["/project/a", { workspaces: [{ path: "/a/legacy" }, { path: "/a/ws1", id: "ws1" }] }], |
| 254 | + ]); |
| 255 | + const metadata = new Map<string, FrontendWorkspaceMetadata>([ |
| 256 | + ["ws1", createWorkspace("ws1", "/project/a")], |
| 257 | + ]); |
| 258 | + |
| 259 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 260 | + |
| 261 | + expect(result.get("/project/a")).toHaveLength(1); |
| 262 | + expect(result.get("/project/a")?.[0].id).toBe("ws1"); |
| 263 | + }); |
| 264 | + |
| 265 | + it("should skip config workspaces with no matching metadata", () => { |
| 266 | + const projects = new Map<string, ProjectConfig>([ |
| 267 | + ["/project/a", { workspaces: [{ path: "/a/ws1", id: "ws1" }] }], |
| 268 | + ]); |
| 269 | + const metadata = new Map<string, FrontendWorkspaceMetadata>(); // empty |
| 270 | + |
| 271 | + const result = buildSortedWorkspacesByProject(projects, metadata, {}); |
| 272 | + |
| 273 | + expect(result.get("/project/a")).toHaveLength(0); |
| 274 | + }); |
| 275 | +}); |
0 commit comments