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 resizePreviewMiniPlayer,
8 resolvePreviewMiniPlayerFrame,
9 resolvePreviewMiniPlayerSourceSize,
10} from "./previewMiniPlayerLayout";
11
12const container = { width: 1_000, height: 700 };
13const source = { width: 1_600, height: 1_000 };
14
15describe("resolvePreviewMiniPlayerSourceSize", () => {
16 it("uses the device viewport scaled by zoom", () => {
17 expect(
18 resolvePreviewMiniPlayerSourceSize({ _tag: "freeform", width: 390, height: 844 }, null, 2),
19 ).toEqual({ width: 780, height: 1_688 });
20 });
21
22 it("uses the size the fill viewport had when it was floated", () => {
23 expect(
24 resolvePreviewMiniPlayerSourceSize(
25 FILL_PREVIEW_VIEWPORT,
26 { x: 0, y: 0, width: 640, height: 400, scale: 0.5, scrollLeft: 0, scrollTop: 0 },
27 1,
28 ),
29 ).toEqual({ width: 1_280, height: 800 });
30 });
31});
32
33describe("resolvePreviewMiniPlayerFrame", () => {
34 it("opens at the source aspect ratio in the top-right corner", () => {
35 expect(
36 resolvePreviewMiniPlayerFrame({ width: null, position: null, source, container }),
37 ).toEqual({ x: 668, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 320, height: 200 });
38 });
39
40 it("keeps a tall source at the minimum width instead of the default box", () => {
41 expect(
42 resolvePreviewMiniPlayerFrame({
43 width: null,
44 position: null,
45 source: { width: 390, height: 844 },
46 container,
47 }),
48 ).toEqual({ x: 748, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 240, height: 519 });
49 });
50
51 it("derives height from the stored width", () => {
52 expect(
53 resolvePreviewMiniPlayerFrame({ width: 480, position: { x: 100, y: 80 }, source, container }),
54 ).toEqual({ x: 100, y: 80, width: 480, height: 300 });
55 });
56
57 it("shrinks to the space above the composer without losing the stored width", () => {
58 const frame = resolvePreviewMiniPlayerFrame({
59 width: 800,
60 position: { x: 100, y: 80 },
61 source,
62 container,
63 bottomInset: 300,
64 });
65 expect(frame).toEqual({ x: 100, y: PREVIEW_MINI_PLAYER_EDGE_GAP, width: 602, height: 376 });
66 });
67
68 it("never grows past the source's own rendered size", () => {
69 expect(
70 resolvePreviewMiniPlayerFrame({
71 width: 900,
72 position: { x: 12, y: 12 },
73 source: { width: 480, height: 320 },
74 container,
75 }),
76 ).toMatchObject({ width: 480, height: 320 });
77 });
78});
79
80describe("resizePreviewMiniPlayer", () => {
81 const start = { x: 300, y: 200, width: 320, height: 200 };
82
83 it("keeps the aspect ratio when dragging the right edge", () => {
84 expect(
85 resizePreviewMiniPlayer({
86 start,
87 direction: "east",
88 delta: { x: 160, y: 0 },
89 source,
90 container,
91 }),
92 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
93 });
94
95 it("anchors the right edge when dragging from the left", () => {
96 expect(
97 resizePreviewMiniPlayer({
98 start,
99 direction: "west",
100 delta: { x: -160, y: 0 },
101 source,
102 container,
103 }),
104 ).toEqual({ x: 140, y: 200, width: 480, height: 300 });
105 });
106
107 it("anchors the bottom edge when dragging the top up", () => {
108 expect(
109 resizePreviewMiniPlayer({
110 start,
111 direction: "north",
112 delta: { x: 0, y: -100 },
113 source,
114 container,
115 }),
116 ).toEqual({ x: 300, y: 100, width: 480, height: 300 });
117 });
118
119 it("follows the dominant axis on a corner drag", () => {
120 expect(
121 resizePreviewMiniPlayer({
122 start,
123 direction: "southeast",
124 delta: { x: 20, y: 100 },
125 source,
126 container,
127 }),
128 ).toEqual({ x: 300, y: 200, width: 480, height: 300 });
129 });
130
131 it("stops at the container edge in the drag direction", () => {
132 expect(
133 resizePreviewMiniPlayer({
134 start: { x: 600, y: 12, width: 320, height: 200 },
135 direction: "east",
136 delta: { x: 500, y: 0 },
137 source,
138 container,
139 }),
140 ).toEqual({ x: 600, y: 12, width: 388, height: 243 });
141 });
142
143 it("shifts the player when the free axis would overflow", () => {
144 expect(
145 resizePreviewMiniPlayer({
146 start: { x: 12, y: 400, width: 320, height: 200 },
147 direction: "east",
148 delta: { x: 300, y: 0 },
149 source,
150 container,
151 bottomInset: 0,
152 }),
153 ).toEqual({ x: 12, y: 300, width: 620, height: 388 });
154 });
155
156 it("respects the minimum size", () => {
157 expect(
158 resizePreviewMiniPlayer({
159 start,
160 direction: "southeast",
161 delta: { x: -300, y: -300 },
162 source,
163 container,
164 }),
165 ).toEqual({ x: 300, y: 200, width: 240, height: 150 });
166 });
167});
168
169describe("clampPreviewMiniPlayerPosition", () => {
170 it("keeps a dragged player within the chat viewport", () => {
171 expect(
172 clampPreviewMiniPlayerPosition({ x: 900, y: -40 }, container, { width: 360, height: 240 }),
173 ).toEqual({ x: 628, y: PREVIEW_MINI_PLAYER_EDGE_GAP });
174 });
175
176 it("keeps the player above a growing composer inset", () => {
177 expect(
178 clampPreviewMiniPlayerPosition(
179 { x: 500, y: 448 },
180 container,
181 { width: 360, height: 240 },
182 160,
183 ),
184 ).toEqual({ x: 500, y: 288 });
185 });
186});