Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { resolveContextStripLabelsCompact } from "./BranchToolbar.logic";
4import {
5 COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX,
6 COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX,
7 getRestingComposerImagePreviewCounts,
8 resolveRestingComposerControlsLayout,
9 resolveRestingComposerControlsNaturalWidth,
10 shouldAnimateComposerRestingTransition,
11 shouldUseCompactComposerPrimaryActions,
12 shouldUseCompactComposerFooter,
13 shouldUseRestingComposerLayout,
14} from "./composerFooterLayout";
15
16describe("getRestingComposerImagePreviewCounts", () => {
17 it("shows at most three thumbnails and counts the remainder", () => {
18 expect(getRestingComposerImagePreviewCounts(0)).toEqual({
19 visibleCount: 0,
20 overflowCount: 0,
21 });
22 expect(getRestingComposerImagePreviewCounts(3)).toEqual({
23 visibleCount: 3,
24 overflowCount: 0,
25 });
26 expect(getRestingComposerImagePreviewCounts(7)).toEqual({
27 visibleCount: 3,
28 overflowCount: 4,
29 });
30 });
31});
32
33describe("shouldUseCompactComposerFooter", () => {
34 it("stays expanded without a measured width", () => {
35 expect(shouldUseCompactComposerFooter(null)).toBe(false);
36 });
37
38 it("switches to compact mode below the breakpoint", () => {
39 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX - 1)).toBe(true);
40 });
41
42 it("stays expanded at and above the breakpoint", () => {
43 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX)).toBe(false);
44 expect(shouldUseCompactComposerFooter(COMPOSER_FOOTER_COMPACT_BREAKPOINT_PX + 48)).toBe(false);
45 });
46
47 it("uses a higher breakpoint for wide action states", () => {
48 expect(
49 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1, {
50 hasWideActions: true,
51 }),
52 ).toBe(true);
53 expect(
54 shouldUseCompactComposerFooter(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
55 hasWideActions: true,
56 }),
57 ).toBe(false);
58 });
59});
60
61describe("shouldUseCompactComposerPrimaryActions", () => {
62 it("matches the wide footer breakpoint", () => {
63 expect(
64 shouldUseCompactComposerPrimaryActions(
65 COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX - 1,
66 { hasWideActions: true },
67 ),
68 ).toBe(true);
69 expect(
70 shouldUseCompactComposerPrimaryActions(COMPOSER_FOOTER_WIDE_ACTIONS_COMPACT_BREAKPOINT_PX, {
71 hasWideActions: true,
72 }),
73 ).toBe(false);
74 });
75});
76
77describe("shouldUseRestingComposerLayout", () => {
78 const resting = {
79 isExistingThread: true,
80 isMobileViewport: false,
81 isFocused: false,
82 isScrollCollapsed: false,
83 hasExpandedChrome: false,
84 collapseOnBlur: true,
85 };
86
87 it("uses the resting layout for an unfocused desktop composer", () => {
88 expect(shouldUseRestingComposerLayout(resting)).toBe(true);
89 });
90
91 it("keeps an unfocused composer expanded when blur collapse is off", () => {
92 expect(shouldUseRestingComposerLayout({ ...resting, collapseOnBlur: false })).toBe(false);
93 });
94
95 it("rests a scroll-collapsed composer even while focused", () => {
96 expect(
97 shouldUseRestingComposerLayout({ ...resting, isFocused: true, isScrollCollapsed: true }),
98 ).toBe(true);
99 });
100
101 it("rests a scroll-collapsed composer regardless of the blur preference", () => {
102 expect(
103 shouldUseRestingComposerLayout({
104 ...resting,
105 isFocused: true,
106 isScrollCollapsed: true,
107 collapseOnBlur: false,
108 }),
109 ).toBe(true);
110 });
111
112 it("keeps new-thread composers expanded", () => {
113 expect(shouldUseRestingComposerLayout({ ...resting, isExistingThread: false })).toBe(false);
114 });
115
116 it("leaves responsive mobile on its existing collapse path", () => {
117 expect(shouldUseRestingComposerLayout({ ...resting, isMobileViewport: true })).toBe(false);
118 });
119
120 it("expands when focus is anywhere in the composer", () => {
121 expect(shouldUseRestingComposerLayout({ ...resting, isFocused: true })).toBe(false);
122 });
123
124 it("keeps drawers and composer-owned menus expanded", () => {
125 expect(shouldUseRestingComposerLayout({ ...resting, hasExpandedChrome: true })).toBe(false);
126 });
127});
128
129describe("shouldAnimateComposerRestingTransition", () => {
130 it("does not animate layout measurements that settle during initial mount", () => {
131 expect(
132 shouldAnimateComposerRestingTransition({
133 hasCompletedInitialLayout: false,
134 stateChanged: true,
135 hasInterruptedAnimation: false,
136 }),
137 ).toBe(false);
138 });
139
140 it("animates later resting-state changes and interrupted transitions", () => {
141 expect(
142 shouldAnimateComposerRestingTransition({
143 hasCompletedInitialLayout: true,
144 stateChanged: true,
145 hasInterruptedAnimation: false,
146 }),
147 ).toBe(true);
148 expect(
149 shouldAnimateComposerRestingTransition({
150 hasCompletedInitialLayout: true,
151 stateChanged: false,
152 hasInterruptedAnimation: true,
153 }),
154 ).toBe(true);
155 });
156});
157
158describe("resolveRestingComposerControlsLayout", () => {
159 // Picker 140 natural / 96 minimum, plus a 9px separator. Traits 60,
160 // mode 140, overflow 24, gap 4.
161 const base = {
162 gap: 4,
163 naturalFixedWidth: 149,
164 minimumFixedWidth: 105,
165 blockWidths: [60, 140],
166 overflowWidth: 24,
167 };
168
169 it("shows everything when the host has room", () => {
170 // 149 + 60 + 140 + 4 * 2 = 357
171 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
172 hiddenCount: 0,
173 visible: true,
174 });
175 });
176
177 it("moves trailing blocks into the overflow menu until the rest fits", () => {
178 // 149 + 60 + 24 + 4 * 2 = 241
179 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 356 })).toEqual({
180 hiddenCount: 1,
181 visible: true,
182 });
183 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
184 hiddenCount: 1,
185 visible: true,
186 });
187 // 149 + 24 + 4 = 177
188 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
189 hiddenCount: 2,
190 visible: true,
191 });
192 });
193
194 it("shrinks the picker after moving every trailing block into overflow", () => {
195 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 176 })).toEqual({
196 hiddenCount: 2,
197 visible: true,
198 });
199 // 105 + 24 + 4 = 133
200 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 133 })).toEqual({
201 hiddenCount: 2,
202 visible: true,
203 });
204 });
205
206 it("hides the whole cluster below the picker's minimum readable width", () => {
207 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 132 })).toEqual({
208 hiddenCount: 2,
209 visible: false,
210 });
211 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 0 })).toEqual({
212 hiddenCount: 2,
213 visible: false,
214 });
215 });
216
217 it("uses the same thresholds while shrinking and growing", () => {
218 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
219 hiddenCount: 2,
220 visible: true,
221 });
222 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
223 hiddenCount: 1,
224 visible: true,
225 });
226 });
227
228 it("supports a single leading control without overflow blocks", () => {
229 expect(
230 resolveRestingComposerControlsLayout({
231 ...base,
232 naturalFixedWidth: 140,
233 minimumFixedWidth: 140,
234 blockWidths: [],
235 overflowWidth: 0,
236 hostWidth: 139,
237 }),
238 ).toEqual({ hiddenCount: 0, visible: false });
239 });
240});
241
242describe("context strip labels and resting composer controls", () => {
243 // Widths captured from a desktop renderer that crashed with React error
244 // 185. The strip is 724px wide. Its expanded labels need 327px, and the
245 // rest of its chrome needs 125px. The composer controls sit in the host
246 // that takes whatever is left.
247 const stripWidth = 724;
248 const labelWidth = 327;
249 const chromeWidth = 125;
250 const measurement = {
251 gap: 4,
252 naturalFixedWidth: 96.3828125 + 5 + 4,
253 minimumFixedWidth: 52 + 5 + 4,
254 blockWidths: [130.6953125, 119.671875],
255 overflowWidth: 28,
256 };
257 const naturalWidth = resolveRestingComposerControlsNaturalWidth(measurement);
258
259 function hostWidth(compact: boolean): number {
260 return stripWidth - chromeWidth - (compact ? 0 : labelWidth);
261 }
262
263 it("keeps the labels compact when the full controls only fit beside compact labels", () => {
264 // Compact labels leave 599px, so the composer shows every block.
265 const layout = resolveRestingComposerControlsLayout({
266 ...measurement,
267 hostWidth: hostWidth(true),
268 });
269 expect(layout).toEqual({ hiddenCount: 0, visible: true });
270
271 // The strip reserves the natural controls width, so expanding the
272 // labels is off the table: 125 + 327 + 364 > 724.
273 const compact = resolveContextStripLabelsCompact({
274 compact: true,
275 neededWidth: chromeWidth + labelWidth + naturalWidth,
276 availableWidth: stripWidth,
277 });
278 expect(compact).toBe(true);
279
280 // The next pass sees the same inputs and lands on the same answer.
281 expect(
282 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(compact) }),
283 ).toEqual(layout);
284 });
285
286 it("does not settle when the strip only reserves the visible controls", () => {
287 // Regression guard for the alternating layout. Reserving only the
288 // controls left visible after two blocks moved into overflow makes the
289 // strip expand its labels, which shrinks the host below what the full
290 // controls need, which hides the blocks again.
291 const hiddenControlsWidth = 137;
292 const expands = !resolveContextStripLabelsCompact({
293 compact: true,
294 neededWidth: chromeWidth + labelWidth + hiddenControlsWidth,
295 availableWidth: stripWidth,
296 });
297 expect(expands).toBe(true);
298 expect(
299 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
300 ).toEqual({ hiddenCount: 2, visible: true });
301 });
302});
303
304describe("resolveRestingComposerControlsLayout hysteresis", () => {
305 // Same cluster as above: picker 140 natural / 96 minimum plus a 9px
306 // separator, traits 60, mode 140, overflow 24, gap 4.
307 const base = {
308 gap: 4,
309 naturalFixedWidth: 149,
310 minimumFixedWidth: 105,
311 blockWidths: [60, 140],
312 overflowWidth: 24,
313 };
314
315 it("keeps a block in overflow when re-showing it would leave no slack", () => {
316 // 149 + 60 + 140 + 4 * 2 = 357 fills the host exactly.
317 expect(
318 resolveRestingComposerControlsLayout({
319 ...base,
320 hostWidth: 357,
321 previous: { hiddenCount: 1, visible: true },
322 }),
323 ).toEqual({ hiddenCount: 1, visible: true });
324 });
325
326 it("re-shows a block once the host clears the slack margin", () => {
327 expect(
328 resolveRestingComposerControlsLayout({
329 ...base,
330 hostWidth: 358,
331 previous: { hiddenCount: 1, visible: true },
332 }),
333 ).toEqual({ hiddenCount: 0, visible: true });
334 });
335
336 it("restores the blocks that fit when the full cluster has no slack", () => {
337 const partlyRestored = resolveRestingComposerControlsLayout({
338 ...base,
339 hostWidth: 357,
340 previous: { hiddenCount: 2, visible: true },
341 });
342 expect(partlyRestored).toEqual({ hiddenCount: 1, visible: true });
343 expect(
344 resolveRestingComposerControlsLayout({ ...base, hostWidth: 357, previous: partlyRestored }),
345 ).toEqual(partlyRestored);
346 expect(
347 resolveRestingComposerControlsLayout({ ...base, hostWidth: 358, previous: partlyRestored }),
348 ).toEqual({ hiddenCount: 0, visible: true });
349 });
350
351 it("requires slack before partially restoring a cluster", () => {
352 // One inline block, the picker, and overflow need 149 + 60 + 24 + 8 = 241px.
353 const previous = { hiddenCount: 2, visible: true };
354 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241, previous })).toEqual(
355 previous,
356 );
357 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 242, previous })).toEqual({
358 hiddenCount: 1,
359 visible: true,
360 });
361 });
362
363 it("still resolves from scratch when there is no previous layout", () => {
364 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
365 hiddenCount: 0,
366 visible: true,
367 });
368 });
369
370 it("settles when the measured picker width jitters below a pixel", () => {
371 // Regression guard for React error #185 ("Maximum update depth
372 // exceeded"). The composer re-measures on every render, so the picker's
373 // recovered natural width can land a fraction of a pixel apart between
374 // renders. Without hysteresis that flips hiddenCount forever.
375 let layout = { hiddenCount: 0, visible: true };
376 const settled: string[] = [];
377 for (let index = 0; index < 10; index += 1) {
378 layout = resolveRestingComposerControlsLayout({
379 ...base,
380 // The picker's recovered natural width lands a half pixel apart
381 // between renders.
382 naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
383 hostWidth: 357,
384 previous: layout,
385 });
386 if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
387 }
388 expect(new Set(settled).size).toBe(1);
389 });
390
391 it("keeps the cluster hidden until its minimum width clears the slack", () => {
392 // 105 + 24 + 4 = 133 is the exact minimum for the hidden cluster.
393 expect(
394 resolveRestingComposerControlsLayout({
395 ...base,
396 hostWidth: 133,
397 previous: { hiddenCount: 2, visible: false },
398 }),
399 ).toEqual({ hiddenCount: 2, visible: false });
400 expect(
401 resolveRestingComposerControlsLayout({
402 ...base,
403 hostWidth: 134,
404 previous: { hiddenCount: 2, visible: false },
405 }),
406 ).toEqual({ hiddenCount: 2, visible: true });
407 });
408});