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("uses the measured expanded height while the resting strip host is mounting", () => {
94 expect(
95 resolveComposerTimelineInset({ currentInset: 172, overlayHeight: 110, isResting: true }),
96 ).toBe(172);
97 });
98
99 it("keeps timeline padding stable when the model-only strip appears on collapse", () => {
100 const expanded = resolveComposerTimelineInset({
101 currentInset: 0,
102 overlayHeight: 172,
103 isResting: false,
104 });
105 const collapsed = resolveComposerTimelineInset({
106 currentInset: expanded,
107 overlayHeight: 110,
108 isResting: true,
109 restingOnlyHeight: 32,
110 });
111 expect(collapsed).toBe(expanded);
112 expect(
113 resolveComposerTimelineInset({
114 currentInset: collapsed,
115 overlayHeight: 172,
116 isResting: false,
117 }),
118 ).toBe(expanded);
119 });
120
121 it("reserves the empty expansion when no larger height is known", () => {
122 expect(
123 resolveComposerTimelineInset({ currentInset: 0, overlayHeight: 60, isResting: true }),
124 ).toBe(60 + COMPOSER_RESTING_EXPANSION_MIN_PX);
125 });
126});
127
128describe("shouldUseRestingComposerLayout", () => {
129 const resting = {
130 isExistingThread: true,
131 isMobileViewport: false,
132 isScrollCollapsed: true,
133 hasExpandedChrome: false,
134 hasMultilinePrompt: false,
135 timelineOverflows: true,
136 };
137
138 it("uses the resting layout after a timeline scroll", () => {
139 expect(shouldUseRestingComposerLayout(resting)).toBe(true);
140 });
141
142 it("keeps the composer expanded until the timeline is scrolled", () => {
143 expect(shouldUseRestingComposerLayout({ ...resting, isScrollCollapsed: false })).toBe(false);
144 });
145
146 it("keeps the composer expanded while the timeline fits above it", () => {
147 expect(shouldUseRestingComposerLayout({ ...resting, timelineOverflows: false })).toBe(false);
148 });
149
150 it("keeps new-thread composers expanded", () => {
151 expect(shouldUseRestingComposerLayout({ ...resting, isExistingThread: false })).toBe(false);
152 });
153
154 it("leaves responsive mobile on its existing collapse path", () => {
155 expect(shouldUseRestingComposerLayout({ ...resting, isMobileViewport: 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 it.each([false, true])(
163 "keeps multiline drafts expanded when scroll collapsed is %s",
164 (isScrollCollapsed) => {
165 expect(
166 shouldUseRestingComposerLayout({
167 ...resting,
168 hasMultilinePrompt: true,
169 isScrollCollapsed,
170 }),
171 ).toBe(false);
172 },
173 );
174});
175
176describe("shouldAnimateComposerRestingTransition", () => {
177 it("does not animate layout measurements that settle during initial mount", () => {
178 expect(
179 shouldAnimateComposerRestingTransition({
180 hasCompletedInitialLayout: false,
181 stateChanged: true,
182 hasInterruptedAnimation: false,
183 }),
184 ).toBe(false);
185 });
186
187 it("animates later resting-state changes and interrupted transitions", () => {
188 expect(
189 shouldAnimateComposerRestingTransition({
190 hasCompletedInitialLayout: true,
191 stateChanged: true,
192 hasInterruptedAnimation: false,
193 }),
194 ).toBe(true);
195 expect(
196 shouldAnimateComposerRestingTransition({
197 hasCompletedInitialLayout: true,
198 stateChanged: false,
199 hasInterruptedAnimation: true,
200 }),
201 ).toBe(true);
202 });
203});
204
205describe("resolveRestingComposerControlsLayout", () => {
206 // Picker 140 natural / 96 minimum, plus a 9px separator. Traits 60,
207 // mode 140, overflow 24, gap 4.
208 const base = {
209 gap: 4,
210 naturalFixedWidth: 149,
211 minimumFixedWidth: 105,
212 blockWidths: [60, 140],
213 overflowWidth: 24,
214 };
215
216 it("shows everything when the host has room", () => {
217 // 149 + 60 + 140 + 4 * 2 = 357
218 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
219 hiddenCount: 0,
220 visible: true,
221 });
222 });
223
224 it("moves trailing blocks into the overflow menu until the rest fits", () => {
225 // 149 + 60 + 24 + 4 * 2 = 241
226 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 356 })).toEqual({
227 hiddenCount: 1,
228 visible: true,
229 });
230 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
231 hiddenCount: 1,
232 visible: true,
233 });
234 // 149 + 24 + 4 = 177
235 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
236 hiddenCount: 2,
237 visible: true,
238 });
239 });
240
241 it("shrinks the picker after moving every trailing block into overflow", () => {
242 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 176 })).toEqual({
243 hiddenCount: 2,
244 visible: true,
245 });
246 // 105 + 24 + 4 = 133
247 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 133 })).toEqual({
248 hiddenCount: 2,
249 visible: true,
250 });
251 });
252
253 it("hides the whole cluster below the picker's minimum readable width", () => {
254 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 132 })).toEqual({
255 hiddenCount: 2,
256 visible: false,
257 });
258 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 0 })).toEqual({
259 hiddenCount: 2,
260 visible: false,
261 });
262 });
263
264 it("uses the same thresholds while shrinking and growing", () => {
265 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 240 })).toEqual({
266 hiddenCount: 2,
267 visible: true,
268 });
269 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241 })).toEqual({
270 hiddenCount: 1,
271 visible: true,
272 });
273 });
274
275 it("supports a single leading control without overflow blocks", () => {
276 expect(
277 resolveRestingComposerControlsLayout({
278 ...base,
279 naturalFixedWidth: 140,
280 minimumFixedWidth: 140,
281 blockWidths: [],
282 overflowWidth: 0,
283 hostWidth: 139,
284 }),
285 ).toEqual({ hiddenCount: 0, visible: false });
286 });
287});
288
289describe("context strip labels and resting composer controls", () => {
290 // Widths captured from a desktop renderer that crashed with React error
291 // 185. The strip is 724px wide. Its expanded labels need 327px, and the
292 // rest of its chrome needs 125px. The composer controls sit in the host
293 // that takes whatever is left.
294 const stripWidth = 724;
295 const labelWidth = 327;
296 const chromeWidth = 125;
297 const measurement = {
298 gap: 4,
299 naturalFixedWidth: 96.3828125 + 5 + 4,
300 minimumFixedWidth: 52 + 5 + 4,
301 blockWidths: [130.6953125, 119.671875],
302 overflowWidth: 28,
303 };
304 const naturalWidth = resolveRestingComposerControlsNaturalWidth(measurement);
305
306 function hostWidth(compact: boolean): number {
307 return stripWidth - chromeWidth - (compact ? 0 : labelWidth);
308 }
309
310 it("keeps the labels compact when the full controls only fit beside compact labels", () => {
311 // Compact labels leave 599px, so the composer shows every block.
312 const layout = resolveRestingComposerControlsLayout({
313 ...measurement,
314 hostWidth: hostWidth(true),
315 });
316 expect(layout).toEqual({ hiddenCount: 0, visible: true });
317
318 // The strip reserves the natural controls width, so expanding the
319 // labels is off the table: 125 + 327 + 364 > 724.
320 const compact = resolveContextStripLabelsCompact({
321 compact: true,
322 neededWidth: chromeWidth + labelWidth + naturalWidth,
323 availableWidth: stripWidth,
324 });
325 expect(compact).toBe(true);
326
327 // The next pass sees the same inputs and lands on the same answer.
328 expect(
329 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(compact) }),
330 ).toEqual(layout);
331 });
332
333 it("does not settle when the strip only reserves the visible controls", () => {
334 // Regression guard for the alternating layout. Reserving only the
335 // controls left visible after two blocks moved into overflow makes the
336 // strip expand its labels, which shrinks the host below what the full
337 // controls need, which hides the blocks again.
338 const hiddenControlsWidth = 137;
339 const expands = !resolveContextStripLabelsCompact({
340 compact: true,
341 neededWidth: chromeWidth + labelWidth + hiddenControlsWidth,
342 availableWidth: stripWidth,
343 });
344 expect(expands).toBe(true);
345 expect(
346 resolveRestingComposerControlsLayout({ ...measurement, hostWidth: hostWidth(false) }),
347 ).toEqual({ hiddenCount: 2, visible: true });
348 });
349});
350
351describe("resolveRestingComposerControlsLayout hysteresis", () => {
352 // Same cluster as above: picker 140 natural / 96 minimum plus a 9px
353 // separator, traits 60, mode 140, overflow 24, gap 4.
354 const base = {
355 gap: 4,
356 naturalFixedWidth: 149,
357 minimumFixedWidth: 105,
358 blockWidths: [60, 140],
359 overflowWidth: 24,
360 };
361
362 it("keeps a block in overflow when re-showing it would leave no slack", () => {
363 // 149 + 60 + 140 + 4 * 2 = 357 fills the host exactly.
364 expect(
365 resolveRestingComposerControlsLayout({
366 ...base,
367 hostWidth: 357,
368 previous: { hiddenCount: 1, visible: true },
369 }),
370 ).toEqual({ hiddenCount: 1, visible: true });
371 });
372
373 it("re-shows a block once the host clears the slack margin", () => {
374 expect(
375 resolveRestingComposerControlsLayout({
376 ...base,
377 hostWidth: 358,
378 previous: { hiddenCount: 1, visible: true },
379 }),
380 ).toEqual({ hiddenCount: 0, visible: true });
381 });
382
383 it("restores the blocks that fit when the full cluster has no slack", () => {
384 const partlyRestored = resolveRestingComposerControlsLayout({
385 ...base,
386 hostWidth: 357,
387 previous: { hiddenCount: 2, visible: true },
388 });
389 expect(partlyRestored).toEqual({ hiddenCount: 1, visible: true });
390 expect(
391 resolveRestingComposerControlsLayout({ ...base, hostWidth: 357, previous: partlyRestored }),
392 ).toEqual(partlyRestored);
393 expect(
394 resolveRestingComposerControlsLayout({ ...base, hostWidth: 358, previous: partlyRestored }),
395 ).toEqual({ hiddenCount: 0, visible: true });
396 });
397
398 it("requires slack before partially restoring a cluster", () => {
399 // One inline block, the picker, and overflow need 149 + 60 + 24 + 8 = 241px.
400 const previous = { hiddenCount: 2, visible: true };
401 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 241, previous })).toEqual(
402 previous,
403 );
404 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 242, previous })).toEqual({
405 hiddenCount: 1,
406 visible: true,
407 });
408 });
409
410 it("still resolves from scratch when there is no previous layout", () => {
411 expect(resolveRestingComposerControlsLayout({ ...base, hostWidth: 357 })).toEqual({
412 hiddenCount: 0,
413 visible: true,
414 });
415 });
416
417 it("settles when the measured picker width jitters below a pixel", () => {
418 // Regression guard for React error #185 ("Maximum update depth
419 // exceeded"). The composer re-measures on every render, so the picker's
420 // recovered natural width can land a fraction of a pixel apart between
421 // renders. Without hysteresis that flips hiddenCount forever.
422 let layout = { hiddenCount: 0, visible: true };
423 const settled: string[] = [];
424 for (let index = 0; index < 10; index += 1) {
425 layout = resolveRestingComposerControlsLayout({
426 ...base,
427 // The picker's recovered natural width lands a half pixel apart
428 // between renders.
429 naturalFixedWidth: index % 2 === 0 ? 149 : 149.5,
430 hostWidth: 357,
431 previous: layout,
432 });
433 if (index >= 2) settled.push(`${layout.hiddenCount}:${layout.visible}`);
434 }
435 expect(new Set(settled).size).toBe(1);
436 });
437
438 it("keeps the cluster hidden until its minimum width clears the slack", () => {
439 // 105 + 24 + 4 = 133 is the exact minimum for the hidden cluster.
440 expect(
441 resolveRestingComposerControlsLayout({
442 ...base,
443 hostWidth: 133,
444 previous: { hiddenCount: 2, visible: false },
445 }),
446 ).toEqual({ hiddenCount: 2, visible: false });
447 expect(
448 resolveRestingComposerControlsLayout({
449 ...base,
450 hostWidth: 134,
451 previous: { hiddenCount: 2, visible: false },
452 }),
453 ).toEqual({ hiddenCount: 2, visible: true });
454 });
455});
456
457describe("resolveScrollToEndClearance", () => {
458 it("removes the side tab gap in both composer states while clearing overlapping attachments", () => {
459 for (const overlayHeight of [120, 214]) {
460 const layout = {
461 overlayHeight,
462 mainSurfaceTop: 534,
463 button: { left: 340, right: 460 },
464 attachments: [{ top: 500, left: 600, right: 700 }],
465 };
466 expect(resolveScrollToEndClearance(layout)).toBe(overlayHeight - 34);
467 expect(resolveScrollToEndClearance({ ...layout, attachments: [] })).toBe(overlayHeight);
468 expect(
469 resolveScrollToEndClearance({
470 ...layout,
471 attachments: [...layout.attachments, { top: 500, left: 100, right: 700 }],
472 }),
473 ).toBe(overlayHeight);
474 expect(resolveScrollToEndClearance({ ...layout, button: { left: 590, right: 710 } })).toBe(
475 overlayHeight,
476 );
477 }
478 });
479});