|
| 1 | +import { describe, it, expect } from "@jest/globals"; |
| 2 | +import { |
| 3 | + sortProjectsByOrder, |
| 4 | + reorderProjects, |
| 5 | + normalizeOrder, |
| 6 | + equalOrders, |
| 7 | +} from "./projectOrdering"; |
| 8 | +import type { ProjectConfig } from "@/config"; |
| 9 | + |
| 10 | +describe("projectOrdering", () => { |
| 11 | + const createProjects = (paths: string[]): Map<string, ProjectConfig> => { |
| 12 | + const map = new Map<string, ProjectConfig>(); |
| 13 | + for (const p of paths) { |
| 14 | + map.set(p, { workspaces: [] }); |
| 15 | + } |
| 16 | + return map; |
| 17 | + }; |
| 18 | + |
| 19 | + describe("sortProjectsByOrder", () => { |
| 20 | + it("returns natural order when order array is empty", () => { |
| 21 | + const projects = createProjects(["/a", "/c", "/b"]); |
| 22 | + const result = sortProjectsByOrder(projects, []); |
| 23 | + expect(result.map(([p]) => p)).toEqual(["/a", "/c", "/b"]); |
| 24 | + }); |
| 25 | + |
| 26 | + it("sorts projects according to order array", () => { |
| 27 | + const projects = createProjects(["/a", "/b", "/c"]); |
| 28 | + const result = sortProjectsByOrder(projects, ["/c", "/a", "/b"]); |
| 29 | + expect(result.map(([p]) => p)).toEqual(["/c", "/a", "/b"]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("puts unknown projects at the end in natural order", () => { |
| 33 | + const projects = createProjects(["/a", "/b", "/c", "/d"]); |
| 34 | + const result = sortProjectsByOrder(projects, ["/c", "/a"]); |
| 35 | + // /c and /a are ordered, /b and /d are unknown and should appear in natural order |
| 36 | + expect(result.map(([p]) => p)).toEqual(["/c", "/a", "/b", "/d"]); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("reorderProjects", () => { |
| 41 | + it("moves dragged project to target position", () => { |
| 42 | + const projects = createProjects(["/a", "/b", "/c", "/d"]); |
| 43 | + const currentOrder = ["/a", "/b", "/c", "/d"]; |
| 44 | + // Drag /d onto /b (move /d to position 1) |
| 45 | + const result = reorderProjects(currentOrder, projects, "/d", "/b"); |
| 46 | + expect(result).toEqual(["/a", "/d", "/b", "/c"]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns current order if dragged or target not found", () => { |
| 50 | + const projects = createProjects(["/a", "/b", "/c"]); |
| 51 | + const currentOrder = ["/a", "/b", "/c"]; |
| 52 | + const result = reorderProjects(currentOrder, projects, "/x", "/b"); |
| 53 | + expect(result).toEqual(["/a", "/b", "/c"]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns current order if dragged === target", () => { |
| 57 | + const projects = createProjects(["/a", "/b", "/c"]); |
| 58 | + const currentOrder = ["/a", "/b", "/c"]; |
| 59 | + const result = reorderProjects(currentOrder, projects, "/b", "/b"); |
| 60 | + expect(result).toEqual(["/a", "/b", "/c"]); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("normalizeOrder", () => { |
| 65 | + it("removes paths that no longer exist", () => { |
| 66 | + const projects = createProjects(["/a", "/b"]); |
| 67 | + const order = ["/a", "/b", "/c", "/d"]; |
| 68 | + const result = normalizeOrder(order, projects); |
| 69 | + expect(result).toEqual(["/a", "/b"]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("appends new projects to the end", () => { |
| 73 | + const projects = createProjects(["/a", "/b", "/c", "/d"]); |
| 74 | + const order = ["/b", "/a"]; |
| 75 | + const result = normalizeOrder(order, projects); |
| 76 | + expect(result).toEqual(["/b", "/a", "/c", "/d"]); |
| 77 | + }); |
| 78 | + |
| 79 | + it("preserves order of existing projects", () => { |
| 80 | + const projects = createProjects(["/a", "/b", "/c"]); |
| 81 | + const order = ["/c", "/a", "/b"]; |
| 82 | + const result = normalizeOrder(order, projects); |
| 83 | + expect(result).toEqual(["/c", "/a", "/b"]); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("equalOrders", () => { |
| 88 | + it("returns true for identical arrays", () => { |
| 89 | + const a = ["/a", "/b", "/c"]; |
| 90 | + const b = ["/a", "/b", "/c"]; |
| 91 | + expect(equalOrders(a, b)).toBe(true); |
| 92 | + }); |
| 93 | + |
| 94 | + it("returns false for arrays with different lengths", () => { |
| 95 | + const a = ["/a", "/b"]; |
| 96 | + const b = ["/a", "/b", "/c"]; |
| 97 | + expect(equalOrders(a, b)).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns false for arrays with different order", () => { |
| 101 | + const a = ["/a", "/b", "/c"]; |
| 102 | + const b = ["/a", "/c", "/b"]; |
| 103 | + expect(equalOrders(a, b)).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it("returns true for same reference", () => { |
| 107 | + const a = ["/a", "/b", "/c"]; |
| 108 | + expect(equalOrders(a, a)).toBe(true); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("Bug fix: empty projects Map on initial load", () => { |
| 113 | + it("returns empty array when projects Map is empty", () => { |
| 114 | + // This documents the bug scenario: |
| 115 | + // 1. localStorage has projectOrder = ["/a", "/b", "/c"] |
| 116 | + // 2. Projects haven't loaded yet, so projects = new Map() |
| 117 | + // 3. If normalization runs, it would clear the order |
| 118 | + const emptyProjects = createProjects([]); |
| 119 | + const order = ["/a", "/b", "/c"]; |
| 120 | + const result = normalizeOrder(order, emptyProjects); |
| 121 | + |
| 122 | + // normalizeOrder returns [] when projects is empty |
| 123 | + expect(result).toEqual([]); |
| 124 | + |
| 125 | + // Fix: ProjectSidebar.tsx skips normalization when projects.size === 0 |
| 126 | + // This prevents clearing the order during initial component mount |
| 127 | + }); |
| 128 | + |
| 129 | + it("normalizes correctly after projects load", () => { |
| 130 | + // After projects load, normalization should work normally: |
| 131 | + // 1. projectOrder is still ["/a", "/b", "/c"] from localStorage |
| 132 | + // 2. Projects are now loaded with an additional project ["/d"] |
| 133 | + // 3. Normalization should append the new project |
| 134 | + const projectOrder = ["/a", "/b", "/c"]; |
| 135 | + const loadedProjects = createProjects(["/a", "/b", "/c", "/d"]); |
| 136 | + |
| 137 | + const result = normalizeOrder(projectOrder, loadedProjects); |
| 138 | + |
| 139 | + expect(result).toEqual(["/a", "/b", "/c", "/d"]); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments