Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composerFooterLayout.test.ts

426 lines14.2 KB

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