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