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 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
72describe("resizePreviewMiniPlayer", () => {
73 const start = { x: 300, y: 200, width: 320, height: 200 };
74
75 it("does not jump when corner motion crosses opposing dominant axes", () => {
76 const frames = [-99, -100, -101].map((y) =>
77 resizePreviewMiniPlayer({
78 start,
79 direction: "southeast",
80 delta: { x: 160, y },
81 source,
82 container,
83 }),
84 );
85 expect(Math.abs(frames[0]!.width - frames[2]!.width)).toBeLessThanOrEqual(2);
86 });
87
88 it("projects corner motion onto the aspect-ratio diagonal", () => {
89 expect(
90 resizePreviewMiniPlayer({
91 start,
92 direction: "southeast",
93 delta: { x: 20, y: 100 },
94 source,
95 container,
96 }),
97 ).toEqual({ x: 300, y: 200, width: 379, height: 237 });
98 });
99
100 it("stops at the container edge in the drag direction", () => {
101 expect(
102 resizePreviewMiniPlayer({
103 start: { x: 600, y: 12, width: 320, height: 200 },
104 direction: "east",
105 delta: { x: 500, y: 0 },
106 source,
107 container,
108 }),
109 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
110 });
111
112 it("shifts the player when the free axis would overflow", () => {
113 expect(
114 resizePreviewMiniPlayer({
115 start: { x: 12, y: 400, width: 320, height: 200 },
116 direction: "east",
117 delta: { x: 300, y: 0 },
118 source,
119 container,
120 }),
121 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
122 });
123
124 it("lets a player beside a tall composer keep its height on an edge drag", () => {
125 const phone = { width: 390, height: 844 };
126 const start = { x: 12, y: 100, width: 240, height: 519 };
127 const beside = { composer: { left: 300, right: 900, height: 300 }, detailsCard: null };
128 expect(
129 resizePreviewMiniPlayer({
130 start,
131 direction: "east",
132 delta: { x: 10, y: 0 },
133 source: phone,
134 container,
135 obstacles: beside,
136 }),
137 ).toEqual({ x: 12, y: 100, width: 250, height: 541 });
138 expect(
139 resizePreviewMiniPlayer({
140 start: { ...start, x: 400 },
141 direction: "east",
142 delta: { x: 10, y: 0 },
143 source: phone,
144 container,
145 obstacles: beside,
146 }),
147 ).toMatchObject({ height: 376 });
148 });
149
150 it("stops growing downward at the composer beneath the player's columns", () => {
151 expect(
152 resizePreviewMiniPlayer({
153 start: { x: 300, y: 100, width: 320, height: 200 },
154 direction: "south",
155 delta: { x: 0, y: 400 },
156 source,
157 container,
158 obstacles: tallComposer,
159 }),
160 ).toEqual({ x: 300, y: 100, width: 461, height: 288 });
161 });
162});
163
164describe("resolvePreviewMiniPlayerFrame with the details card", () => {
165 it("opens under the inline details card with right edges aligned", () => {
166 expect(
167 resolvePreviewMiniPlayerFrame({
168 width: null,
169 position: null,
170 source,
171 container,
172 obstacles: withCard,
173 }),
174 ).toEqual({ x: 988 - 320, y: 260 + gap, width: 320, height: 200 });
175 });
176
177 it("opens left of the card when it cannot fit beneath it", () => {
178 const tallCard = { ...withCard, detailsCard: { ...detailsCard, bottom: 480 } };
179 expect(
180 resolvePreviewMiniPlayerFrame({
181 width: null,
182 position: null,
183 source,
184 container,
185 obstacles: tallCard,
186 }),
187 ).toEqual({ x: 680 - gap - 320, y: 700 - 150 - gap - 200, width: 320, height: 200 });
188 });
189
190 it("keeps a stored player's size when the card covers its columns", () => {
191 const frame = resolvePreviewMiniPlayerFrame({
192 width: 480,
193 position: { x: 700, y: 280 },
194 source,
195 container,
196 obstacles: withCard,
197 });
198 expect(frame).toEqual({
199 x: 680 - gap - 480,
200 y: 700 - 150 - gap - 300,
201 width: 480,
202 height: 300,
203 });
204 });
205});
206
207describe("clampPreviewMiniPlayerPosition", () => {
208 const player = { width: 360, height: 240 };
209
210 it("lets the player drop into the margin beside the composer", () => {
211 expect(
212 clampPreviewMiniPlayerPosition(
213 { x: 20, y: 500 },
214 container,
215 { width: 60, height: 150 },
216 obstacles,
217 ),
218 ).toEqual({ x: 20, y: 500 });
219 });
220
221 it("slides sideways past the composer when that is the shorter move", () => {
222 expect(
223 clampPreviewMiniPlayerPosition(
224 { x: 850, y: 500 },
225 container,
226 { width: 60, height: 150 },
227 obstacles,
228 ),
229 ).toEqual({ x: composer.right + gap, y: 500 });
230 });
231
232 it("clears the card and the composer together in a tight column", () => {
233 const tight = { composer: { left: 0, right: 1_000, height: 300 }, detailsCard };
234 expect(clampPreviewMiniPlayerPosition({ x: 700, y: 600 }, container, player, tight)).toEqual({
235 x: 680 - gap - 360,
236 y: 700 - 300 - gap - 240,
237 });
238 expect(
239 clampPreviewMiniPlayerPosition(
240 { x: 700, y: 600 },
241 container,
242 { width: 360, height: 400 },
243 tight,
244 ),
245 ).toEqual({ x: 628, y: 260 + gap });
246 });
247
248 it("stays inside the container when a full-width card leaves no open rows", () => {
249 const wall = {
250 composer,
251 detailsCard: { left: 0, right: 1_000, bottom: 690 },
252 };
253 expect(clampPreviewMiniPlayerPosition({ x: 300, y: 500 }, container, player, wall)).toEqual({
254 x: 300,
255 y: 700 - gap - player.height,
256 });
257 });
258
259 it("sits above the composer when it is too wide for either margin", () => {
260 expect(
261 clampPreviewMiniPlayerPosition(
262 { x: 100, y: 100 },
263 container,
264 { width: 976, height: 500 },
265 obstacles,
266 ),
267 ).toEqual({ x: gap, y: 700 - 150 - gap - 500 });
268 });
269});