Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 insertSidebarRowSpacer,
5 MOBILE_DEFAULT_STORED_LAYOUT,
6 moveSidebarRowToken,
7 resolveSidebarRowLayout,
8 resolveSidebarRowLines,
9 setSidebarRowLineKept,
10 setSidebarRowTokenForm,
11 SIDEBAR_ROW_ITEMS,
12 storedSidebarRowLayout,
13 withoutSidebarRowItem,
14 type SidebarRowLayout,
15} from "./threadRowLayout.ts";
16
17const ids = (tokens: ReadonlyArray<{ id: string }> | null) => tokens?.map((token) => token.id);
18const LINES = ["top", "middle", "bottom", "hidden"] as const;
19const VISIBLE_LINES = ["top", "middle", "bottom"] as const;
20
21describe("thread row lines", () => {
22 const layout = resolveSidebarRowLayout({
23 top: ["projectIcon", "projectName", "spacer"],
24 middle: ["title"],
25 bottom: ["branch", "spacer", "agents"],
26 });
27 const resolve = (hasBranch: boolean, keepLines: ReadonlyArray<"middle" | "bottom"> = []) =>
28 resolveSidebarRowLines({
29 lines: layout,
30 titleLine: "middle",
31 keepLines,
32 hasContent: (token) => token.id === "title" || (token.id === "branch" && hasBranch),
33 });
34
35 it("folds a line with only agents and spacers, and moves its agents to the title's line", () => {
36 const lines = resolve(false);
37 expect(lines.bottom).toBeNull();
38 expect(ids(lines.middle)).toEqual(["title", "agents"]);
39 expect(ids(resolve(true).bottom)?.slice(0, 3)).toEqual(["branch", "spacer", "agents"]);
40 });
41
42 it("keeps a kept line with items, a spacer included, but never one with nothing on it", () => {
43 expect(resolve(false, ["bottom"]).bottom).not.toBeNull();
44 const empty = resolveSidebarRowLines({
45 lines: { ...layout, bottom: [] },
46 titleLine: "middle",
47 keepLines: ["bottom"],
48 hasContent: () => true,
49 });
50 expect(empty.bottom).toBeNull();
51 const spacerOnly = {
52 ...layout,
53 bottom: [{ id: "spacer" as const, form: null, key: "spacer-9" }],
54 };
55 const hasContent = () => false;
56 expect(
57 resolveSidebarRowLines({
58 lines: spacerOnly,
59 titleLine: "middle",
60 keepLines: ["bottom"],
61 hasContent,
62 }).bottom,
63 ).not.toBeNull();
64 expect(
65 resolveSidebarRowLines({ lines: spacerOnly, titleLine: "middle", keepLines: [], hasContent })
66 .bottom,
67 ).toBeNull();
68 });
69});
70
71describe("stored thread row layouts", () => {
72 it.each([
73 {
74 name: "unknown ids, junk, and bad forms",
75 stored: { top: ["", ":", "bogus", "branch:bogus"] },
76 },
77 { name: "duplicates", stored: { top: ["title", "time"], bottom: ["title", "time", "time"] } },
78 { name: "a hidden title", stored: { hidden: ["title", "spacer"], keep: ["top", "hidden"] } },
79 ])("tolerates $name and fills in what it never mentions", ({ stored }) => {
80 const layout = resolveSidebarRowLayout(stored);
81 const placed = LINES.flatMap((line) => layout[line]).filter((token) => token.id !== "spacer");
82 expect(placed.map((token) => token.id).toSorted()).toEqual(
83 SIDEBAR_ROW_ITEMS.map((item) => item.id)
84 .filter((id) => id !== "spacer")
85 .toSorted(),
86 );
87 expect(layout.hidden.map((token) => token.id)).not.toContain("title");
88 expect(layout.keepLines).toEqual([]);
89 expect(resolveSidebarRowLayout(storedSidebarRowLayout(layout))).toEqual(layout);
90 });
91});
92
93// Mulberry32: seeded, so a failure reproduces from its iteration.
94function random(seed: number) {
95 let state = seed >>> 0;
96 return () => {
97 state = (state + 0x6d2b79f5) >>> 0;
98 let t = Math.imul(state ^ (state >>> 15), state | 1);
99 t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
100 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
101 };
102}
103
104describe.each([
105 { name: "web", defaults: undefined },
106 { name: "mobile", defaults: MOBILE_DEFAULT_STORED_LAYOUT },
107])("$name layout edits", ({ defaults }) => {
108 it("keep every item once, the title visible, and survive a save after any edit sequence", () => {
109 const next = random(42);
110 const pick = <T>(values: ReadonlyArray<T>) => values[Math.floor(next() * values.length)]!;
111 for (let run = 0; run < 60; run++) {
112 let layout: SidebarRowLayout = resolveSidebarRowLayout({}, defaults);
113 for (let step = 0; step < 20; step++) {
114 const keys = LINES.flatMap((line) => layout[line].map((token) => token.key));
115 const index = pick([0, 1, 3, Number.POSITIVE_INFINITY]);
116 layout = pick([
117 () => moveSidebarRowToken(layout, pick(keys), pick(LINES), index),
118 () => setSidebarRowTokenForm(layout, pick(keys), pick(["wide", "compact"] as const)),
119 () => {
120 // Both settings screens toggle both lines together.
121 const kept = next() < 0.5;
122 return setSidebarRowLineKept(
123 setSidebarRowLineKept(layout, "middle", kept),
124 "bottom",
125 kept,
126 );
127 },
128 () => insertSidebarRowSpacer(layout, pick(VISIBLE_LINES), index).layout,
129 () => withoutSidebarRowItem(layout, pick(SIDEBAR_ROW_ITEMS).id),
130 ])();
131 const tokens = LINES.flatMap((line) => layout[line]);
132 const keysAfter = tokens.map((token) => token.key);
133 expect(new Set(keysAfter).size, `run ${run} step ${step}`).toBe(keysAfter.length);
134 expect(tokens.filter((token) => token.id !== "spacer")).toHaveLength(
135 SIDEBAR_ROW_ITEMS.length - 1,
136 );
137 expect(ids(layout.hidden)).not.toContain("title");
138 expect(ids(layout.hidden)).not.toContain("spacer");
139 const saved = storedSidebarRowLayout(layout, defaults);
140 const reloaded = resolveSidebarRowLayout(saved, defaults);
141 expect(storedSidebarRowLayout(reloaded, defaults), `run ${run} step ${step}`).toEqual(
142 saved,
143 );
144 }
145 }
146 });
147});
148
149describe("mobile thread row defaults", () => {
150 it("saves an unchanged mobile layout as empty, so later default changes reach it", () => {
151 const layout = resolveSidebarRowLayout({}, MOBILE_DEFAULT_STORED_LAYOUT);
152 expect(ids(layout.top)).toEqual(["projectIcon", "title", "time"]);
153 expect(storedSidebarRowLayout(layout, MOBILE_DEFAULT_STORED_LAYOUT)).toEqual({});
154 });
155});
156
157describe("item forms", () => {
158 it("saves Environment as icon only, and keeps an unchanged layout saved as empty", () => {
159 const layout = resolveSidebarRowLayout({});
160 expect(storedSidebarRowLayout(layout)).toEqual({});
161 const stored = storedSidebarRowLayout(setSidebarRowTokenForm(layout, "environment", "compact"));
162 expect(stored.bottom?.[0]).toBe("environment:compact");
163 expect(resolveSidebarRowLayout(stored).bottom[0]?.form).toBe("compact");
164 });
165});