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