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;
18// A centered composer stack with margins on each side.
19const composer = { left: 100, right: 900, height: 150 };
20const obstacles: PreviewMiniPlayerObstacles = { composer };
21const tallComposer: PreviewMiniPlayerObstacles = { composer: { ...composer, height: 300 } };
22
23describe("resolvePreviewMiniPlayerSourceSize", () => {
24 it("uses the device viewport scaled by zoom", () => {
25 expect(
26 resolvePreviewMiniPlayerSourceSize({ _tag: "freeform", width: 390, height: 844 }, null, 2),
27 ).toEqual({ width: 780, height: 1_688 });
28 });
29
30 it("uses the size the fill viewport had when it was floated", () => {
31 expect(
32 resolvePreviewMiniPlayerSourceSize(
33 FILL_PREVIEW_VIEWPORT,
34 { x: 0, y: 0, width: 640, height: 400, scale: 0.5, scrollLeft: 0, scrollTop: 0 },
35 1,
36 ),
37 ).toEqual({ width: 1_280, height: 800 });
38 });
39});
40
41describe("resolveDeviceMiniPlayerSourceSize", () => {
42 it("stands in with the platform's phone shape until the stream reports a size", () => {
43 const ios = resolveDeviceMiniPlayerSourceSize("ios", null);
44 expect(ios.width / ios.height).toBeCloseTo(9 / 19.5);
45 const android = resolveDeviceMiniPlayerSourceSize("android", null);
46 expect(android.width / android.height).toBeCloseTo(9 / 20);
47 });
48
49 it("turns a rotated screen into a landscape box", () => {
50 const screen = { width: 1_179, height: 2_556, orientation: "landscape_left" } as const;
51 expect(resolveDeviceMiniPlayerSourceSize("ios", screen)).toEqual({
52 width: 2_556,
53 height: 1_179,
54 });
55 expect(
56 resolveDeviceMiniPlayerSourceSize("android", { ...screen, orientation: "portrait" }),
57 ).toEqual({ width: 1_179, height: 2_556 });
58 });
59
60 it("floats a phone at the minimum width rather than the default box", () => {
61 expect(
62 resolvePreviewMiniPlayerFrame({
63 width: null,
64 position: null,
65 source: resolveDeviceMiniPlayerSourceSize("ios", null),
66 container,
67 }),
68 ).toMatchObject({ width: 240, height: 520 });
69 });
70});
71
72describe("resolveDeviceMiniPlayerCornerRadius", () => {
73 it("rounds an Android player like a phone, scaled with its short side", () => {
74 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 240, height: 520 })).toBe(34);
75 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 520, height: 240 })).toBe(34);
76 expect(resolveDeviceMiniPlayerCornerRadius("android", { width: 60, height: 130 })).toBe(12);
77 });
78
79 it("keeps the frame radius for iOS, whose stream has square corners", () => {
80 expect(resolveDeviceMiniPlayerCornerRadius("ios", { width: 240, height: 520 })).toBe(12);
81 expect(resolveDeviceMiniPlayerCornerRadius("ios", { width: 720, height: 1_000 })).toBe(12);
82 });
83});
84
85describe("resolvePreviewMiniPlayerFrame", () => {
86 it("opens at the source aspect ratio in the top-right corner", () => {
87 expect(
88 resolvePreviewMiniPlayerFrame({ width: null, position: null, source, container }),
89 ).toEqual({ x: 668, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 320, height: 200 });
90 });
91
92 it("keeps a tall source at the minimum width instead of the default box", () => {
93 expect(
94 resolvePreviewMiniPlayerFrame({
95 width: null,
96 position: null,
97 source: { width: 390, height: 844 },
98 container,
99 }),
100 ).toEqual({ x: 748, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 240, height: 519 });
101 });
102
103 it("derives height from the stored width", () => {
104 expect(
105 resolvePreviewMiniPlayerFrame({ width: 480, position: { x: 100, y: 80 }, source, container }),
106 ).toEqual({ x: 100, y: 80, width: 480, height: 300 });
107 });
108
109 it("shrinks to the space above the composer without losing the stored width", () => {
110 const frame = resolvePreviewMiniPlayerFrame({
111 width: 800,
112 position: { x: 100, y: 80 },
113 source,
114 container,
115 obstacles: tallComposer,
116 });
117 expect(frame).toEqual({ x: 100, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 602, height: 376 });
118 });
119
120 it("keeps a tall frame parked beside the composer across layout passes", () => {
121 // The frame an edge resize produced in the left margin, resolved again from
122 // the stored width and position on the next render.
123 const phone = { width: 390, height: 844 };
124 const beside = { composer: { left: 300, right: 900, height: 300 } };
125 const resized = resizePreviewMiniPlayer({
126 start: { x: 12, y: 100, width: 240, height: 519 },
127 direction: "east",
128 delta: { x: 10, y: 0 },
129 source: phone,
130 container,
131 obstacles: beside,
132 });
133 expect(
134 resolvePreviewMiniPlayerFrame({
135 width: resized.width,
136 position: { x: resized.x, y: resized.y },
137 source: phone,
138 container,
139 obstacles: beside,
140 }),
141 ).toEqual(resized);
142 });
143
144 it("never grows past the source's own rendered size", () => {
145 expect(
146 resolvePreviewMiniPlayerFrame({
147 width: 900,
148 position: { x: 12, y: 12 },
149 source: { width: 480, height: 320 },
150 container,
151 }),
152 ).toMatchObject({ width: 480, height: 320 });
153 });
154});
155
156describe("resizePreviewMiniPlayer", () => {
157 const start = { x: 300, y: 200, width: 320, height: 200 };
158
159 it("keeps the aspect ratio when dragging the right edge", () => {
160 expect(
161 resizePreviewMiniPlayer({
162 start,
163 direction: "east",
164 delta: { x: 160, y: 0 },
165 source,
166 container,
167 }),
168 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
169 });
170
171 it("anchors the right edge when dragging from the left", () => {
172 expect(
173 resizePreviewMiniPlayer({
174 start,
175 direction: "west",
176 delta: { x: -160, y: 0 },
177 source,
178 container,
179 }),
180 ).toEqual({ x: 140, y: 200, width: 480, height: 300 });
181 });
182
183 it("anchors the bottom edge when dragging the top up", () => {
184 expect(
185 resizePreviewMiniPlayer({
186 start,
187 direction: "north",
188 delta: { x: 0, y: -100 },
189 source,
190 container,
191 }),
192 ).toEqual({ x: 300, y: 100, width: 480, height: 300 });
193 });
194
195 it("follows the dominant axis on a corner drag", () => {
196 expect(
197 resizePreviewMiniPlayer({
198 start,
199 direction: "southeast",
200 delta: { x: 20, y: 100 },
201 source,
202 container,
203 }),
204 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
205 });
206
207 it("stops at the container edge in the drag direction", () => {
208 expect(
209 resizePreviewMiniPlayer({
210 start: { x: 600, y: 12, width: 320, height: 200 },
211 direction: "east",
212 delta: { x: 500, y: 0 },
213 source,
214 container,
215 }),
216 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
217 });
218
219 it("shifts the player when the free axis would overflow", () => {
220 expect(
221 resizePreviewMiniPlayer({
222 start: { x: 12, y: 400, width: 320, height: 200 },
223 direction: "east",
224 delta: { x: 300, y: 0 },
225 source,
226 container,
227 }),
228 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
229 });
230
231 it("lets a player beside a tall composer keep its height on an edge drag", () => {
232 // A portrait player parked in the margin left of the composer, already
233 // taller than the rows above the composer, nudged from its right edge.
234 const phone = { width: 390, height: 844 };
235 const start = { x: 12, y: 100, width: 240, height: 519 };
236 const beside = { composer: { left: 300, right: 900, height: 300 } };
237 expect(
238 resizePreviewMiniPlayer({
239 start,
240 direction: "east",
241 delta: { x: 10, y: 0 },
242 source: phone,
243 container,
244 obstacles: beside,
245 }),
246 ).toEqual({ x: 12, y: 100, width: 250, height: 541 });
247 // The same drag with the composer under the player is still held above it.
248 expect(
249 resizePreviewMiniPlayer({
250 start: { ...start, x: 400 },
251 direction: "east",
252 delta: { x: 10, y: 0 },
253 source: phone,
254 container,
255 obstacles: beside,
256 }),
257 ).toMatchObject({ height: 376 });
258 });
259
260 it("stops growing downward at the composer beneath the player's columns", () => {
261 expect(
262 resizePreviewMiniPlayer({
263 start: { x: 300, y: 100, width: 320, height: 200 },
264 direction: "south",
265 delta: { x: 0, y: 400 },
266 source,
267 container,
268 obstacles: tallComposer,
269 }),
270 ).toEqual({ x: 300, y: 100, width: 461, height: 288 });
271 });
272
273 it("respects the minimum size", () => {
274 expect(
275 resizePreviewMiniPlayer({
276 start,
277 direction: "southeast",
278 delta: { x: -300, y: -300 },
279 source,
280 container,
281 }),
282 ).toEqual({ x: 300, y: 200, width: 240, height: 150 });
283 });
284});
285
286describe("clampPreviewMiniPlayerPosition", () => {
287 const player = { width: 360, height: 240 };
288
289 it("keeps a dragged player within the chat viewport", () => {
290 expect(clampPreviewMiniPlayerPosition({ x: 900, y: -40 }, container, player)).toEqual({
291 x: 628,
292 y: gap,
293 });
294 });
295
296 it("keeps the player above a growing composer", () => {
297 expect(
298 clampPreviewMiniPlayerPosition({ x: 500, y: 448 }, container, player, {
299 composer: { ...composer, height: 160 },
300 }),
301 ).toEqual({ x: 500, y: 288 });
302 });
303
304 it("lets the player drop into the margin beside the composer", () => {
305 expect(
306 clampPreviewMiniPlayerPosition(
307 { x: 20, y: 500 },
308 container,
309 { width: 60, height: 150 },
310 obstacles,
311 ),
312 ).toEqual({ x: 20, y: 500 });
313 });
314
315 it("slides sideways past the composer when that is the shorter move", () => {
316 expect(
317 clampPreviewMiniPlayerPosition(
318 { x: 850, y: 500 },
319 container,
320 { width: 60, height: 150 },
321 obstacles,
322 ),
323 ).toEqual({ x: composer.right + gap, y: 500 });
324 });
325
326 it("sits above the composer when it is too wide for either margin", () => {
327 expect(
328 clampPreviewMiniPlayerPosition(
329 { x: 100, y: 100 },
330 container,
331 { width: 976, height: 500 },
332 obstacles,
333 ),
334 ).toEqual({ x: gap, y: 700 - 150 - gap - 500 });
335 });
336});