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

previewMiniPlayerLayout.test.ts

280 lines8.4 KB

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 resolvePreviewMiniPlayerFrame,
10 resolvePreviewMiniPlayerSourceSize,
11} from "./previewMiniPlayerLayout";
12​
13const container = { width: 1_000, height: 700 };
14const source = { width: 1_600, height: 1_000 };
15const gap = PREVIEW_MINI_PLAYER_EDGE_GAP;
16const composer = { left: 100, right: 900, height: 150 };
17const obstacles: PreviewMiniPlayerObstacles = { composer, detailsCard: null };
18const tallComposer: PreviewMiniPlayerObstacles = {
19 composer: { ...composer, height: 300 },
20 detailsCard: null,
21};
22const detailsCard = { left: 680, right: 988, bottom: 260 };
23const withCard: PreviewMiniPlayerObstacles = { composer, detailsCard };
24​
25describe("resolvePreviewMiniPlayerSourceSize", () => {
26 it("uses the size the fill viewport had when it was floated", () => {
27 expect(
28 resolvePreviewMiniPlayerSourceSize(
29 FILL_PREVIEW_VIEWPORT,
30 { x: 0, y: 0, width: 640, height: 400, scale: 0.5, scrollLeft: 0, scrollTop: 0 },
31 1,
32 ),
33 ).toEqual({ width: 1_280, height: 800 });
34 });
35});
36​
37describe("resolvePreviewMiniPlayerFrame", () => {
38 it("shrinks to the space above the composer without losing the stored width", () => {
39 const frame = resolvePreviewMiniPlayerFrame({
40 width: 800,
41 position: { x: 100, y: 80 },
42 source,
43 container,
44 obstacles: tallComposer,
45 });
46 expect(frame).toEqual({ x: 100, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 602, height: 376 });
47 });
48​
49 it("keeps a tall frame parked beside the composer across layout passes", () => {
50 const phone = { width: 390, height: 844 };
51 const beside = { composer: { left: 300, right: 900, height: 300 }, detailsCard: null };
52 const resized = resizePreviewMiniPlayer({
53 start: { x: 12, y: 100, width: 240, height: 519 },
54 direction: "east",
55 delta: { x: 10, y: 0 },
56 source: phone,
57 container,
58 obstacles: beside,
59 });
60 expect(
61 resolvePreviewMiniPlayerFrame({
62 width: resized.width,
63 position: { x: resized.x, y: resized.y },
64 source: phone,
65 container,
66 obstacles: beside,
67 }),
68 ).toEqual(resized);
69 });
70​
71 it("fits above the composer when a narrower container pulls a stored frame over it", () => {
72 const frame = resolvePreviewMiniPlayerFrame({
73 width: 300,
74 position: { x: 948, y: gap },
75 source: { width: 390, height: 844 },
76 container: { width: 600, height: 700 },
77 obstacles: { composer: { left: 0, right: 600, height: 300 }, detailsCard: null },
78 });
79 expect(frame.y + frame.height).toBeLessThanOrEqual(700 - 300 - gap);
80 });
81});
82​
83describe("resizePreviewMiniPlayer", () => {
84 const start = { x: 300, y: 200, width: 320, height: 200 };
85​
86 it("does not jump when corner motion crosses opposing dominant axes", () => {
87 const frames = [-99, -100, -101].map((y) =>
88 resizePreviewMiniPlayer({
89 start,
90 direction: "southeast",
91 delta: { x: 160, y },
92 source,
93 container,
94 }),
95 );
96 expect(Math.abs(frames[0]!.width - frames[2]!.width)).toBeLessThanOrEqual(2);
97 });
98​
99 it("projects corner motion onto the aspect-ratio diagonal", () => {
100 expect(
101 resizePreviewMiniPlayer({
102 start,
103 direction: "southeast",
104 delta: { x: 20, y: 100 },
105 source,
106 container,
107 }),
108 ).toEqual({ x: 300, y: 200, width: 379, height: 237 });
109 });
110​
111 it("stops at the container edge in the drag direction", () => {
112 expect(
113 resizePreviewMiniPlayer({
114 start: { x: 600, y: 12, width: 320, height: 200 },
115 direction: "east",
116 delta: { x: 500, y: 0 },
117 source,
118 container,
119 }),
120 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
121 });
122​
123 it("shifts the player when the free axis would overflow", () => {
124 expect(
125 resizePreviewMiniPlayer({
126 start: { x: 12, y: 400, width: 320, height: 200 },
127 direction: "east",
128 delta: { x: 300, y: 0 },
129 source,
130 container,
131 }),
132 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
133 });
134​
135 it("lets a player beside a tall composer keep its height on an edge drag", () => {
136 const phone = { width: 390, height: 844 };
137 const start = { x: 12, y: 100, width: 240, height: 519 };
138 const beside = { composer: { left: 300, right: 900, height: 300 }, detailsCard: null };
139 expect(
140 resizePreviewMiniPlayer({
141 start,
142 direction: "east",
143 delta: { x: 10, y: 0 },
144 source: phone,
145 container,
146 obstacles: beside,
147 }),
148 ).toEqual({ x: 12, y: 100, width: 250, height: 541 });
149 expect(
150 resizePreviewMiniPlayer({
151 start: { ...start, x: 400 },
152 direction: "east",
153 delta: { x: 10, y: 0 },
154 source: phone,
155 container,
156 obstacles: beside,
157 }),
158 ).toMatchObject({ height: 376 });
159 });
160​
161 it("stops growing downward at the composer beneath the player's columns", () => {
162 expect(
163 resizePreviewMiniPlayer({
164 start: { x: 300, y: 100, width: 320, height: 200 },
165 direction: "south",
166 delta: { x: 0, y: 400 },
167 source,
168 container,
169 obstacles: tallComposer,
170 }),
171 ).toEqual({ x: 300, y: 100, width: 461, height: 288 });
172 });
173});
174​
175describe("resolvePreviewMiniPlayerFrame with the details card", () => {
176 it("opens under the inline details card with right edges aligned", () => {
177 expect(
178 resolvePreviewMiniPlayerFrame({
179 width: null,
180 position: null,
181 source,
182 container,
183 obstacles: withCard,
184 }),
185 ).toEqual({ x: 988 - 320, y: 260 + gap, width: 320, height: 200 });
186 });
187​
188 it("opens left of the card when it cannot fit beneath it", () => {
189 const tallCard = { ...withCard, detailsCard: { ...detailsCard, bottom: 480 } };
190 expect(
191 resolvePreviewMiniPlayerFrame({
192 width: null,
193 position: null,
194 source,
195 container,
196 obstacles: tallCard,
197 }),
198 ).toEqual({ x: 680 - gap - 320, y: 700 - 150 - gap - 200, width: 320, height: 200 });
199 });
200​
201 it("keeps a stored player's size when the card covers its columns", () => {
202 const frame = resolvePreviewMiniPlayerFrame({
203 width: 480,
204 position: { x: 700, y: 280 },
205 source,
206 container,
207 obstacles: withCard,
208 });
209 expect(frame).toEqual({
210 x: 680 - gap - 480,
211 y: 700 - 150 - gap - 300,
212 width: 480,
213 height: 300,
214 });
215 });
216});
217​
218describe("clampPreviewMiniPlayerPosition", () => {
219 const player = { width: 360, height: 240 };
220​
221 it("lets the player drop into the margin beside the composer", () => {
222 expect(
223 clampPreviewMiniPlayerPosition(
224 { x: 20, y: 500 },
225 container,
226 { width: 60, height: 150 },
227 obstacles,
228 ),
229 ).toEqual({ x: 20, y: 500 });
230 });
231​
232 it("slides sideways past the composer when that is the shorter move", () => {
233 expect(
234 clampPreviewMiniPlayerPosition(
235 { x: 850, y: 500 },
236 container,
237 { width: 60, height: 150 },
238 obstacles,
239 ),
240 ).toEqual({ x: composer.right + gap, y: 500 });
241 });
242​
243 it("clears the card and the composer together in a tight column", () => {
244 const tight = { composer: { left: 0, right: 1_000, height: 300 }, detailsCard };
245 expect(clampPreviewMiniPlayerPosition({ x: 700, y: 600 }, container, player, tight)).toEqual({
246 x: 680 - gap - 360,
247 y: 700 - 300 - gap - 240,
248 });
249 expect(
250 clampPreviewMiniPlayerPosition(
251 { x: 700, y: 600 },
252 container,
253 { width: 360, height: 400 },
254 tight,
255 ),
256 ).toEqual({ x: 628, y: 260 + gap });
257 });
258​
259 it("stays inside the container when a full-width card leaves no open rows", () => {
260 const wall = {
261 composer,
262 detailsCard: { left: 0, right: 1_000, bottom: 690 },
263 };
264 expect(clampPreviewMiniPlayerPosition({ x: 300, y: 500 }, container, player, wall)).toEqual({
265 x: 300,
266 y: 700 - gap - player.height,
267 });
268 });
269​
270 it("sits above the composer when it is too wide for either margin", () => {
271 expect(
272 clampPreviewMiniPlayerPosition(
273 { x: 100, y: 100 },
274 container,
275 { width: 976, height: 500 },
276 obstacles,
277 ),
278 ).toEqual({ x: gap, y: 700 - 150 - gap - 500 });
279 });
280});