Open History to see the last commit
1import { FILL_PREVIEW_VIEWPORT } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 clampPreviewMiniPlayerPosition,
6 PREVIEW_MINI_PLAYER_EDGE_GAP,
7 type PreviewMiniPlayerObstacles,
8 resizePreviewMiniPlayer,
9 resolveDeviceMiniPlayerCornerRadius,
10 resolveDeviceMiniPlayerSourceSize,
11 resolvePreviewMiniPlayerFrame,
12 resolvePreviewMiniPlayerSourceSize,
13} from "./previewMiniPlayerLayout";
14
15const container = { width: 1_000, height: 700 };
16const source = { width: 1_600, height: 1_000 };
17const gap = PREVIEW_MINI_PLAYER_EDGE_GAP;
18const composer = { left: 100, right: 900, height: 150 };
19const obstacles: PreviewMiniPlayerObstacles = { composer, detailsCard: null };
20const tallComposer: PreviewMiniPlayerObstacles = {
21 composer: { ...composer, height: 300 },
22 detailsCard: null,
23};
24const detailsCard = { left: 680, right: 988, bottom: 260 };
25const withCard: PreviewMiniPlayerObstacles = { composer, detailsCard };
26
27describe("resolvePreviewMiniPlayerSourceSize", () => {
28 it("uses the device viewport scaled by zoom", () => {
29 expect(
30 resolvePreviewMiniPlayerSourceSize({ _tag: "freeform", width: 390, height: 844 }, null, 2),
31 ).toEqual({ width: 780, height: 1_688 });
32 });
33
34 it("uses the size the fill viewport had when it was floated", () => {
35 expect(
36 resolvePreviewMiniPlayerSourceSize(
37 FILL_PREVIEW_VIEWPORT,
38 { x: 0, y: 0, width: 640, height: 400, scale: 0.5, scrollLeft: 0, scrollTop: 0 },
39 1,
40 ),
41 ).toEqual({ width: 1_280, height: 800 });
42 });
43});
44
45describe("resolveDeviceMiniPlayerSourceSize", () => {
46 it("stands in with the platform's phone shape until the stream reports a size", () => {
47 const ios = resolveDeviceMiniPlayerSourceSize("ios", null);
48 expect(ios.width / ios.height).toBeCloseTo(9 / 19.5);
49 const android = resolveDeviceMiniPlayerSourceSize("android", null);
50 expect(android.width / android.height).toBeCloseTo(9 / 20);
51 });
52
53 it("turns a rotated screen into a landscape box", () => {
54 const screen = { width: 1_179, height: 2_556, orientation: "landscape_left" } as const;
55 expect(resolveDeviceMiniPlayerSourceSize("ios", screen)).toEqual({
56 width: 2_556,
57 height: 1_179,
58 });
59 expect(
60 resolveDeviceMiniPlayerSourceSize("android", { ...screen, orientation: "portrait" }),
61 ).toEqual({ width: 1_179, height: 2_556 });
62 });
63
64 it("floats a phone at the minimum width rather than the default box", () => {
65 expect(
66 resolvePreviewMiniPlayerFrame({
67 width: null,
68 position: null,
69 source: resolveDeviceMiniPlayerSourceSize("ios", null),
70 container,
71 }),
72 ).toMatchObject({ width: 240, height: 520 });
73 });
74});
75
76describe("resolveDeviceMiniPlayerCornerRadius", () => {
77 it("rounds an Android player like a phone, scaled with its short side", () => {
78 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 240, height: 520 })).toBe(34);
79 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 520, height: 240 })).toBe(34);
80 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 60, height: 130 })).toBe(12);
81 });
82
83 it("keeps the frame radius for iOS, whose stream has square corners", () => {
84 expect(resolveDeviceMiniPlayerCornerRadius("ios", { width: 240, height: 520 })).toBe(12);
85 expect(resolveDeviceMiniPlayerCornerRadius("ios", { width: 720, height: 1_000 })).toBe(12);
86 });
87});
88
89describe("resolvePreviewMiniPlayerFrame", () => {
90 it("opens at the source aspect ratio in the top-right corner", () => {
91 expect(
92 resolvePreviewMiniPlayerFrame({ width: null, position: null, source, container }),
93 ).toEqual({ x: 668, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 320, height: 200 });
94 });
95
96 it("keeps a tall source at the minimum width instead of the default box", () => {
97 expect(
98 resolvePreviewMiniPlayerFrame({
99 width: null,
100 position: null,
101 source: { width: 390, height: 844 },
102 container,
103 }),
104 ).toEqual({ x: 748, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 240, height: 519 });
105 });
106
107 it("derives height from the stored width", () => {
108 expect(
109 resolvePreviewMiniPlayerFrame({ width: 480, position: { x: 100, y: 80 }, source, container }),
110 ).toEqual({ x: 100, y: 80, width: 480, height: 300 });
111 });
112
113 it("shrinks to the space above the composer without losing the stored width", () => {
114 const frame = resolvePreviewMiniPlayerFrame({
115 width: 800,
116 position: { x: 100, y: 80 },
117 source,
118 container,
119 obstacles: tallComposer,
120 });
121 expect(frame).toEqual({ x: 100, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 602, height: 376 });
122 });
123
124 it("keeps a tall frame parked beside the composer across layout passes", () => {
125 const phone = { width: 390, height: 844 };
126 const beside = { composer: { left: 300, right: 900, height: 300 }, detailsCard: null };
127 const resized = resizePreviewMiniPlayer({
128 start: { x: 12, y: 100, width: 240, height: 519 },
129 direction: "east",
130 delta: { x: 10, y: 0 },
131 source: phone,
132 container,
133 obstacles: beside,
134 });
135 expect(
136 resolvePreviewMiniPlayerFrame({
137 width: resized.width,
138 position: { x: resized.x, y: resized.y },
139 source: phone,
140 container,
141 obstacles: beside,
142 }),
143 ).toEqual(resized);
144 });
145
146 it("can grow beyond the source rendered size", () => {
147 expect(
148 resolvePreviewMiniPlayerFrame({
149 width: 900,
150 position: { x: 12, y: 12 },
151 source: { width: 480, height: 320 },
152 container,
153 }),
154 ).toMatchObject({ width: 900, height: 600 });
155 });
156});
157
158describe("resizePreviewMiniPlayer", () => {
159 const start = { x: 300, y: 200, width: 320, height: 200 };
160
161 it("keeps the aspect ratio when dragging the right edge", () => {
162 expect(
163 resizePreviewMiniPlayer({
164 start,
165 direction: "east",
166 delta: { x: 160, y: 0 },
167 source,
168 container,
169 }),
170 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
171 });
172
173 it("anchors the right edge when dragging from the left", () => {
174 expect(
175 resizePreviewMiniPlayer({
176 start,
177 direction: "west",
178 delta: { x: -160, y: 0 },
179 source,
180 container,
181 }),
182 ).toEqual({ x: 140, y: 200, width: 480, height: 300 });
183 });
184
185 it("anchors the bottom edge when dragging the top up", () => {
186 expect(
187 resizePreviewMiniPlayer({
188 start,
189 direction: "north",
190 delta: { x: 0, y: -100 },
191 source,
192 container,
193 }),
194 ).toEqual({ x: 300, y: 100, width: 480, height: 300 });
195 });
196
197 it("does not jump when corner motion crosses opposing dominant axes", () => {
198 const frames = [-99, -100, -101].map((y) =>
199 resizePreviewMiniPlayer({
200 start,
201 direction: "southeast",
202 delta: { x: 160, y },
203 source,
204 container,
205 }),
206 );
207 expect(Math.abs(frames[0]!.width - frames[2]!.width)).toBeLessThanOrEqual(2);
208 });
209
210 it("projects corner motion onto the aspect-ratio diagonal", () => {
211 expect(
212 resizePreviewMiniPlayer({
213 start,
214 direction: "southeast",
215 delta: { x: 20, y: 100 },
216 source,
217 container,
218 }),
219 ).toEqual({ x: 300, y: 200, width: 379, height: 237 });
220 });
221
222 it("stops at the container edge in the drag direction", () => {
223 expect(
224 resizePreviewMiniPlayer({
225 start: { x: 600, y: 12, width: 320, height: 200 },
226 direction: "east",
227 delta: { x: 500, y: 0 },
228 source,
229 container,
230 }),
231 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
232 });
233
234 it("shifts the player when the free axis would overflow", () => {
235 expect(
236 resizePreviewMiniPlayer({
237 start: { x: 12, y: 400, width: 320, height: 200 },
238 direction: "east",
239 delta: { x: 300, y: 0 },
240 source,
241 container,
242 }),
243 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
244 });
245
246 it("lets a player beside a tall composer keep its height on an edge drag", () => {
247 const phone = { width: 390, height: 844 };
248 const start = { x: 12, y: 100, width: 240, height: 519 };
249 const beside = { composer: { left: 300, right: 900, height: 300 }, detailsCard: null };
250 expect(
251 resizePreviewMiniPlayer({
252 start,
253 direction: "east",
254 delta: { x: 10, y: 0 },
255 source: phone,
256 container,
257 obstacles: beside,
258 }),
259 ).toEqual({ x: 12, y: 100, width: 250, height: 541 });
260 expect(
261 resizePreviewMiniPlayer({
262 start: { ...start, x: 400 },
263 direction: "east",
264 delta: { x: 10, y: 0 },
265 source: phone,
266 container,
267 obstacles: beside,
268 }),
269 ).toMatchObject({ height: 376 });
270 });
271
272 it("stops growing downward at the composer beneath the player's columns", () => {
273 expect(
274 resizePreviewMiniPlayer({
275 start: { x: 300, y: 100, width: 320, height: 200 },
276 direction: "south",
277 delta: { x: 0, y: 400 },
278 source,
279 container,
280 obstacles: tallComposer,
281 }),
282 ).toEqual({ x: 300, y: 100, width: 461, height: 288 });
283 });
284
285 it("respects the minimum size", () => {
286 expect(
287 resizePreviewMiniPlayer({
288 start,
289 direction: "southeast",
290 delta: { x: -300, y: -300 },
291 source,
292 container,
293 }),
294 ).toEqual({ x: 300, y: 200, width: 240, height: 150 });
295 });
296});
297
298describe("resolvePreviewMiniPlayerFrame with the details card", () => {
299 it("opens under the inline details card with right edges aligned", () => {
300 expect(
301 resolvePreviewMiniPlayerFrame({
302 width: null,
303 position: null,
304 source,
305 container,
306 obstacles: withCard,
307 }),
308 ).toEqual({ x: 988 - 320, y: 260 + gap, width: 320, height: 200 });
309 });
310
311 it("opens left of the card when it cannot fit beneath it", () => {
312 const tallCard = { ...withCard, detailsCard: { ...detailsCard, bottom: 480 } };
313 expect(
314 resolvePreviewMiniPlayerFrame({
315 width: null,
316 position: null,
317 source,
318 container,
319 obstacles: tallCard,
320 }),
321 ).toEqual({ x: 680 - gap - 320, y: 700 - 150 - gap - 200, width: 320, height: 200 });
322 });
323
324 it("keeps a stored player's size when the card covers its columns", () => {
325 const frame = resolvePreviewMiniPlayerFrame({
326 width: 480,
327 position: { x: 700, y: 280 },
328 source,
329 container,
330 obstacles: withCard,
331 });
332 expect(frame).toEqual({
333 x: 680 - gap - 480,
334 y: 700 - 150 - gap - 300,
335 width: 480,
336 height: 300,
337 });
338 });
339});
340
341describe("clampPreviewMiniPlayerPosition", () => {
342 const player = { width: 360, height: 240 };
343
344 it("keeps a dragged player within the chat viewport", () => {
345 expect(clampPreviewMiniPlayerPosition({ x: 900, y: -40 }, container, player)).toEqual({
346 x: 628,
347 y: gap,
348 });
349 });
350
351 it("keeps the player above a growing composer", () => {
352 expect(
353 clampPreviewMiniPlayerPosition({ x: 500, y: 448 }, container, player, {
354 composer: { ...composer, height: 160 },
355 detailsCard: null,
356 }),
357 ).toEqual({ x: 500, y: 288 });
358 });
359
360 it("lets the player drop into the margin beside the composer", () => {
361 expect(
362 clampPreviewMiniPlayerPosition(
363 { x: 20, y: 500 },
364 container,
365 { width: 60, height: 150 },
366 obstacles,
367 ),
368 ).toEqual({ x: 20, y: 500 });
369 });
370
371 it("slides sideways past the composer when that is the shorter move", () => {
372 expect(
373 clampPreviewMiniPlayerPosition(
374 { x: 850, y: 500 },
375 container,
376 { width: 60, height: 150 },
377 obstacles,
378 ),
379 ).toEqual({ x: composer.right + gap, y: 500 });
380 });
381
382 it("keeps the player out from under the details card", () => {
383 expect(clampPreviewMiniPlayerPosition({ x: 700, y: 40 }, container, player, withCard)).toEqual({
384 x: 628,
385 y: 260 + gap,
386 });
387 });
388
389 it("slides sideways out from under the card when that is the shorter move", () => {
390 expect(
391 clampPreviewMiniPlayerPosition(
392 { x: 660, y: 40 },
393 container,
394 { width: 60, height: 150 },
395 withCard,
396 ),
397 ).toEqual({ x: 680 - gap - 60, y: 40 });
398 });
399
400 it("clears the card and the composer together in a tight column", () => {
401 const tight = { composer: { left: 0, right: 1_000, height: 300 }, detailsCard };
402 expect(clampPreviewMiniPlayerPosition({ x: 700, y: 600 }, container, player, tight)).toEqual({
403 x: 680 - gap - 360,
404 y: 700 - 300 - gap - 240,
405 });
406 expect(
407 clampPreviewMiniPlayerPosition(
408 { x: 700, y: 600 },
409 container,
410 { width: 360, height: 400 },
411 tight,
412 ),
413 ).toEqual({ x: 628, y: 260 + gap });
414 });
415
416 it("stays inside the container when a full-width card leaves no open rows", () => {
417 const wall = {
418 composer,
419 detailsCard: { left: 0, right: 1_000, bottom: 690 },
420 };
421 expect(clampPreviewMiniPlayerPosition({ x: 300, y: 500 }, container, player, wall)).toEqual({
422 x: 300,
423 y: 700 - gap - player.height,
424 });
425 });
426
427 it("sits above the composer when it is too wide for either margin", () => {
428 expect(
429 clampPreviewMiniPlayerPosition(
430 { x: 100, y: 100 },
431 container,
432 { width: 976, height: 500 },
433 obstacles,
434 ),
435 ).toEqual({ x: gap, y: 700 - 150 - gap - 500 });
436 });
437});