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