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
145describe("shouldUseRestingComposerLayout", () => {
146 const resting = {
147 isExistingThread: true,
148 isMobileViewport: false,
149 isScrollCollapsed: true,
150 hasExpandedChrome: false,
151 hasMultilinePrompt: false,
152 timelineOverflows: true,
153 };
154
155 it("uses the resting layout after a timeline scroll", () => {
156 expect(shouldUseRestingComposerLayout(resting)).toBe(true);
157 });
158
159 it("keeps the composer expanded until the timeline is scrolled", () => {
160 expect(shouldUseRestingComposerLayout({ ...resting, isScrollCollapsed: false })).toBe(false);
161 });
162
163 it("keeps the composer expanded while the timeline fits above it", () => {
164 expect(shouldUseRestingComposerLayout({ ...resting, timelineOverflows: false })).toBe(false);
165 });
166
167 it("keeps new-thread composers expanded", () => {
168 expect(shouldUseRestingComposerLayout({ ...resting, isExistingThread: false })).toBe(false);
169 });
170
171 it("leaves responsive mobile on its existing collapse path", () => {
172 expect(shouldUseRestingComposerLayout({ ...resting, isMobileViewport: true })).toBe(false);
173 });
174
175 it("keeps drawers and composer-owned menus expanded", () => {
176 expect(shouldUseRestingComposerLayout({ ...resting, hasExpandedChrome: true })).toBe(false);
177 });
178
179 it.each([false, true])(
180 "keeps multiline drafts expanded when scroll collapsed is %s",
181 (isScrollCollapsed) => {
182 expect(
183 shouldUseRestingComposerLayout({
184 ...resting,
185 hasMultilinePrompt: true,
186 isScrollCollapsed,
187 }),
188 ).toBe(false);
189 },
190 );
191});
192
193describe("shouldAnimateComposerRestingTransition", () => {
194 it("does not animate layout measurements that settle during initial mount", () => {
195 expect(
196 shouldAnimateComposerRestingTransition({
197 hasCompletedInitialLayout: false,
198 stateChanged: true,
199 hasInterruptedAnimation: false,
200 }),
201 ).toBe(false);
202 });
203
204 it("animates later resting-state changes and interrupted transitions", () => {
205 expect(
206 shouldAnimateComposerRestingTransition({
207 hasCompletedInitialLayout: true,
208 stateChanged: true,
209 hasInterruptedAnimation: false,
210 }),
211 ).toBe(true);
212 expect(
213 shouldAnimateComposerRestingTransition({
214 hasCompletedInitialLayout: true,
215 stateChanged: false,
216 hasInterruptedAnimation: true,
217 }),
218 ).toBe(true);
219 });
220});
221
222describe("resolveRestingComposerControlsLayout", () => {
223 const base = {
224 gap: 4,
225 naturalFixedWidth: 149,
226 minimumFixedWidth: 105,
227 blockWidths: [60, 140],
228 overflowWidth: 24,
229 };
230
231 it("shows everything when the host has room", () => {
232 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
233 hiddenCount: 0,
234 visible: true,
235 });
236 });
237
238 it("moves trailing blocks into the overflow menu until the rest fits", () => {
239 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 356 })).toEqual({
240 hiddenCount: 1,
241 visible: true,
242 });
243 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
244 hiddenCount: 1,
245 visible: true,
246 });
247 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
248 hiddenCount: 2,
249 visible: true,
250 });
251 });
252
253 it("shrinks the picker after moving every trailing block into overflow", () => {
254 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 176 })).toEqual({
255 hiddenCount: 2,
256 visible: true,
257 });
258 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 133 })).toEqual({
259 hiddenCount: 2,
260 visible: true,
261 });
262 });
263
264 it("hides the whole cluster below the picker's minimum readable width", () => {
265 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 132 })).toEqual({
266 hiddenCount: 2,
267 visible: false,
268 });
269 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 0 })).toEqual({
270 hiddenCount: 2,
271 visible: false,
272 });
273 });
274
275 it("uses the same thresholds while shrinking and growing", () => {
276 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
277 hiddenCount: 2,
278 visible: true,
279 });
280 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
281 hiddenCount: 1,
282 visible: true,
283 });
284 });
285
286 it("supports a single leading control without overflow blocks", () => {
287 expect(
288 resolveRestingComposerControlsLayout({
289 ...base,
290 naturalFixedWidth: 140,
291 minimumFixedWidth: 140,
292 blockWidths: [],
293 overflowWidth: 0,
294 hostWidth: 139,
295 }),
296 ).toEqual({ hiddenCount: 0, visible: false });
297 });
298});
299
300describe("context strip labels and resting composer controls", () => {
301 const stripWidth = 724;
302 const labelWidth = 327;
303 const chromeWidth = 125;
304 const measurement = {
305 gap: 4,
306 naturalFixedWidth: 96.3828125 + 5 + 4,
307 minimumFixedWidth: 52 + 5 + 4,
308 blockWidths: [130.6953125, 119.671875],
309 overflowWidth: 28,
310 };
311 const naturalWidth = resolveRestingComposerControlsNaturalWidth(measurement);
312
313 function hostWidth(compact: boolean): number {
314 return stripWidth - chromeWidth - (compact ? 0 : labelWidth);
315 }
316
317 it("keeps the labels compact when the full controls only fit beside compact labels", () => {
318 const layout = resolveRestingComposerControlsLayout({
319 ...measurement,
320 hostWidth: hostWidth(true),
321 });
322 expect(layout).toEqual({ hiddenCount: 0, visible: true });
323
324 const compact = resolveContextStripLabelsCompact({
325 compact: true,
326 neededWidth: chromeWidth + labelWidth + naturalWidth,
327 availableWidth: stripWidth,
328 });
329 expect(compact).toBe(true);
330
331 expect(
332 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(compact) }),
333 ).toEqual(layout);
334 });
335
336 it("does not settle when the strip only reserves the visible controls", () => {
337 const hiddenControlsWidth = 137;
338 const expands = !resolveContextStripLabelsCompact({
339 compact: true,
340 neededWidth: chromeWidth + labelWidth + hiddenControlsWidth,
341 availableWidth: stripWidth,
342 });
343 expect(expands).toBe(true);
344 expect(
345 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
346 ).toEqual({ hiddenCount: 2, visible: true });
347 });
348});
349
350describe("resolveRestingComposerControlsLayout hysteresis", () => {
351 const base = {
352 gap: 4,
353 naturalFixedWidth: 149,
354 minimumFixedWidth: 105,
355 blockWidths: [60, 140],
356 overflowWidth: 24,
357 };
358
359 it("keeps a block in overflow when re-showing it would leave no slack", () => {
360 expect(
361 resolveRestingComposerControlsLayout({
362 ...base,
363 hostWidth: 357,
364 previous: { hiddenCount: 1, visible: true },
365 }),
366 ).toEqual({ hiddenCount: 1, visible: true });
367 });
368
369 it("re-shows a block once the host clears the slack margin", () => {
370 expect(
371 resolveRestingComposerControlsLayout({
372 ...base,
373 hostWidth: 358,
374 previous: { hiddenCount: 1, visible: true },
375 }),
376 ).toEqual({ hiddenCount: 0, visible: true });
377 });
378
379 it("restores the blocks that fit when the full cluster has no slack", () => {
380 const partlyRestored = resolveRestingComposerControlsLayout({
381 ...base,
382 hostWidth: 357,
383 previous: { hiddenCount: 2, visible: true },
384 });
385 expect(partlyRestored).toEqual({ hiddenCount: 1, visible: true });
386 expect(
387 resolveRestingComposerControlsLayout({ ...base, hostWidth: 357, previous: partlyRestored }),
388 ).toEqual(partlyRestored);
389 expect(
390 resolveRestingComposerControlsLayout({ ...base, hostWidth: 358, previous: partlyRestored }),
391 ).toEqual({ hiddenCount: 0, visible: true });
392 });
393
394 it("requires slack before partially restoring a cluster", () => {
395 const previous = { hiddenCount: 2, visible: true };
396 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241, previous })).toEqual(
397 previous,
398 );
399 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 242, previous })).toEqual({
400 hiddenCount: 1,
401 visible: true,
402 });
403 });
404
405 it("still resolves from scratch when there is no previous layout", () => {
406 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
407 hiddenCount: 0,
408 visible: true,
409 });
410 });
411
412 it("settles when the measured picker width jitters below a pixel", () => {
413 // The composer re-measures on every render, so the picker's recovered
414 // natural width can land a fraction of a pixel apart between renders.
415 // Without hysteresis that flips hiddenCount forever.
416 let layout = { hiddenCount: 0, visible: true };
417 const settled: string[] = [];
418 for (let index = 0; index < 10; index += 1) {
419 layout = resolveRestingComposerControlsLayout({
420 ...base,
421 naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
422 hostWidth: 357,
423 previous: layout,
424 });
425 if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
426 }
427 expect(new Set(settled).size).toBe(1);
428 });
429
430 it("keeps the cluster hidden until its minimum width clears the slack", () => {
431 expect(
432 resolveRestingComposerControlsLayout({
433 ...base,
434 hostWidth: 133,
435 previous: { hiddenCount: 2, visible: false },
436 }),
437 ).toEqual({ hiddenCount: 2, visible: false });
438 expect(
439 resolveRestingComposerControlsLayout({
440 ...base,
441 hostWidth: 134,
442 previous: { hiddenCount: 2, visible: false },
443 }),
444 ).toEqual({ hiddenCount: 2, visible: true });
445 });
446});
447
448describe("resolveScrollToEndClearance", () => {
449 it("removes the side tab gap in both composer states while clearing overlapping attachments", () => {
450 for (const overlayHeight of [120, 214]) {
451 const layout = {
452 overlayHeight,
453 mainSurfaceTop: 534,
454 button: { left: 340, right: 460 },
455 attachments: [{ top: 500, left: 600, right: 700 }],
456 };
457 expect(resolveScrollToEndClearance(layout)).toBe(overlayHeight - 34);
458 expect(resolveScrollToEndClearance({ ...layout, attachments: [] })).toBe(overlayHeight);
459 expect(
460 resolveScrollToEndClearance({
461 ...layout,
462 attachments: [...layout.attachments, { top: 500, left: 100, right: 700 }],
463 }),
464 ).toBe(overlayHeight);
465 expect(resolveScrollToEndClearance({ ...layout, button: { left: 590, right: 710 } })).toBe(
466 overlayHeight,
467 );
468 }
469 });
470});
471
472describe("progressive composer controls", () => {
473 const measurement = {
474 gap: 4,
475 naturalFixedWidth: 140,
476 minimumFixedWidth: 80,
477 blockWidths: [80, 140],
478 iconOnlyBlockWidths: [40, 60],
479 overflowWidth: 24,
480 };
481
482 it("keeps labels while they fit and removes trailing labels before controls", () => {
483 for (const [hostWidth, iconOnlyCount, hiddenCount] of [
484 [368, 0, 0],
485 [367, 1, 0],
486 [288, 1, 0],
487 [287, 2, 0],
488 [248, 2, 0],
489 [247, 2, 1],
490 [211, 2, 2],
491 ] as const) {
492 expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth })).toEqual({
493 hiddenCount,
494 iconOnlyCount,
495 visible: true,
496 });
497 }
498 });
499
500 it("requires slack to restore labels and controls", () => {
501 for (const [hostWidth, previous, promoted] of [
502 [
503 368,
504 { hiddenCount: 0, iconOnlyCount: 1, visible: true },
505 { hiddenCount: 0, iconOnlyCount: 0, visible: true },
506 ],
507 [
508 288,
509 { hiddenCount: 0, iconOnlyCount: 2, visible: true },
510 { hiddenCount: 0, iconOnlyCount: 1, visible: true },
511 ],
512 [
513 248,
514 { hiddenCount: 1, iconOnlyCount: 2, visible: true },
515 { hiddenCount: 0, iconOnlyCount: 2, visible: true },
516 ],
517 ] as const) {
518 expect(resolveRestingComposerControlsLayout({ ...measurement, hostWidth, previous })).toEqual(
519 previous,
520 );
521 expect(
522 resolveRestingComposerControlsLayout({
523 ...measurement,
524 hostWidth: hostWidth + 1,
525 previous,
526 }),
527 ).toEqual(promoted);
528 }
529 });
530
531 it("settles through fractional label-width changes at each threshold", () => {
532 for (const hostWidth of [368, 288, 248]) {
533 let previous = resolveRestingComposerControlsLayout({ ...measurement, hostWidth });
534 for (let index = 0; index < 10; index += 1) {
535 const next = resolveRestingComposerControlsLayout({
536 ...measurement,
537 hostWidth,
538 previous,
539 naturalFixedWidth: 140 + (index % 2) * 0.5,
540 });
541 if (index > 1) expect(next).toEqual(previous);
542 previous = next;
543 }
544 }
545 });
546});