Skip to content

Commit 1adeaec

Browse files
🤖 fix: deterministic project ordering when no custom order set (#1213)
## Problem The Comprehensive demo story had flaky snapshots because projects in the sidebar did not render in a consistent order when `projectOrder` was empty (initial state in Storybook). **Root cause:** `sortProjectsByOrder()` returned projects in Map insertion order when `order` was empty. Map insertion order depends on how data is constructed, making it non-deterministic across test runs. ## Solution When no custom order is set, sort projects lexically instead of using insertion order. This guarantees a stable, deterministic render order. ## Impact - **Storybook:** Projects render in consistent alphabetical order when no custom ordering is saved - **Production:** New users see projects sorted alphabetically until they drag-reorder - **Existing users:** No change—their saved `projectOrder` is non-empty, so existing sort logic applies --- _Generated with `mux` • Model: `anthropic:claude-opus-4-5` • Thinking: `high`_
1 parent 682a04c commit 1adeaec

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

src/common/utils/projectOrdering.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe("projectOrdering", () => {
1717
};
1818

1919
describe("sortProjectsByOrder", () => {
20-
it("returns natural order when order array is empty", () => {
20+
it("returns lexical order when order array is empty", () => {
2121
const projects = createProjects(["/a", "/c", "/b"]);
2222
const result = sortProjectsByOrder(projects, []);
23-
expect(result.map(([p]) => p)).toEqual(["/a", "/c", "/b"]);
23+
expect(result.map(([p]) => p)).toEqual(["/a", "/b", "/c"]);
2424
});
2525

2626
it("sorts projects according to order array", () => {

src/common/utils/projectOrdering.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export function sortProjectsByOrder(
1515
): Array<[string, ProjectConfig]> {
1616
const entries = Array.from(projects.entries());
1717

18-
if (order.length === 0) return entries; // Natural order
18+
if (order.length === 0) {
19+
// Sort lexically for stable, deterministic order
20+
return entries.sort(([a], [b]) => a.localeCompare(b));
21+
}
1922

2023
const pos = new Map(order.map((p, i) => [p, i]));
2124

0 commit comments

Comments
 (0)