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

chatCanvasLayout.test.ts

323 lines12.7 KB

1import { describe, expect, it } from "vite-plus/test";
2import { resolveChatCanvasLayout, type ChatCanvasPreview } from "./chatCanvasLayout";
3import { resizePreviewMiniPlayer } from "../preview/previewMiniPlayerLayout";
4import {
5 resolveThreadDetailsCardDensity,
6 resolveThreadDetailsCardLayout,
7} from "./threadDetailsCardLayout";
8​
9const preview: ChatCanvasPreview = {
10 key: "browser:one",
11 width: 320,
12 position: null,
13 source: { width: 1600, height: 1000 },
14};
15const resolve = (width: number, player: ChatCanvasPreview | null = preview, height = 900) =>
16 resolveChatCanvasLayout({ container: { width, height }, preview: player, composerHeight: 180 });
17const expectClear = (result: ReturnType<typeof resolve>) => {
18 const frame = result.frame!;
19 const chat = result.chat;
20 expect(frame.x + frame.width <= chat.left - 12 || frame.x >= chat.left + chat.width + 12).toBe(
21 true,
22 );
23};
24​
25describe("chat canvas layout", () => {
26 it("lifts a growing preview above the composer without snapping at the chat boundary", () => {
27 let previous: ReturnType<typeof resolve> | undefined;
28 for (let width = 480; width <= 1100; width++) {
29 const result = resolve(1344, {
30 ...preview,
31 width,
32 lastInteraction: "resize",
33 position: { x: 1332 - width, y: 888 - Math.round(width / 1.6) },
34 });
35 const frame = result.frame!;
36 if (frame.x < result.chat.left + result.chat.width + 12) {
37 expect(frame.y + frame.height).toBeLessThanOrEqual(708);
38 }
39 expect(result.chat.left).toBeLessThanOrEqual(288);
40 expect(result.chat.width).toBeGreaterThanOrEqual(640);
41 if (previous) {
42 expect(Math.abs(frame.y - previous.frame!.y)).toBeLessThanOrEqual(3);
43 expect(Math.abs(frame.width - previous.frame!.width)).toBeLessThanOrEqual(2);
44 expect(Math.abs(result.chat.width - previous.chat.width)).toBeLessThanOrEqual(1);
45 }
46 previous = result;
47 }
48 expect(previous!.overlapsChat).toBe(true);
49 expect(previous!.frame!.width).toBe(1100);
50 });
51 it.each([900, 1000, 1004])("allows dragging across a %spx chat canvas", (width) => {
52 const start = resolve(width, { ...preview, position: { x: width - 332, y: 200 } }).frame!;
53 const moved = resolve(width, {
54 ...preview,
55 position: { x: start.x - 300, y: start.y },
56 lastInteraction: "drag",
57 });
58 expect(moved.frame).toEqual({ ...start, x: start.x - 300 });
59 const returned = resolve(width, {
60 ...preview,
61 position: { x: start.x, y: start.y },
62 lastInteraction: "drag",
63 });
64 expect(returned.frame).toEqual(start);
65 });
66 it("stops leftward dragging before the player can push chat to the right", () => {
67 const centered = resolve(1344, null).chat;
68 for (const x of [1100, 800, 700, 600, 400, 12, -100]) {
69 const result = resolve(1344, { ...preview, position: { x, y: 500 } });
70 expect(result.frame!.x).toBeGreaterThanOrEqual(700);
71 expect(result.chat.left).toBeLessThanOrEqual(centered.left);
72 expect(result.chat.insetStart).toBe(0);
73 expect(result.chat.width).toBeGreaterThanOrEqual(640);
74 expectClear(result);
75 }
76 expect(resolve(1344, { ...preview, position: { x: 12, y: 500 } }).frame!.x).toBe(700);
77 });
78 it("keeps a dragged player above the composer when no readable lane fits beside it", () => {
79 const result = resolve(1000, { ...preview, position: { x: 12, y: 700 } });
80 expect(result.chat).toEqual(resolve(1000, null).chat);
81 expect(result.frame!.x).toBe(12);
82 expect(result.frame!.y + result.frame!.height).toBeLessThanOrEqual(708);
83 });
84 it("restores the preferred width when the container grows again", () => {
85 const player = { ...preview, width: 480 };
86 expect(resolve(1200, player).chat.width).toBeLessThan(768);
87 expect(resolve(1800, player).chat.width).toBe(768);
88 expect(player.width).toBe(480);
89 });
90 it("constrains a dragged preview below the full top-right card instead of folding it", () => {
91 const result = resolveChatCanvasLayout({
92 container: { width: 1584, height: 988 },
93 maxChatWidth: 736,
94 composerHeight: 180,
95 detailsCard: { left: 1260, right: 1572, bottom: 339 },
96 preview: {
97 ...preview,
98 width: 240,
99 source: { width: 240, height: 365 },
100 position: { x: 1260, y: 240 },
101 },
102 });
103 expect(result.frame).toEqual({ x: 1260, y: 351, width: 240, height: 365 });
104 expect(result.overlapsDetailsCard).toBe(false);
105 expect(result.chat).toEqual({ left: 424, width: 736, insetStart: 0, insetEnd: 0 });
106 });
107 it("centers the chat beside a details card it would crowd, when asked", () => {
108 const layout = (centerBesideDetails: boolean) =>
109 resolveChatCanvasLayout({
110 container: { width: 1344, height: 988 },
111 maxChatWidth: 736,
112 detailsCard: { left: 1052, right: 1332, bottom: 339 },
113 preview: null,
114 centerBesideDetails,
115 }).chat;
116 // Pushed left just enough to clear the card.
117 expect(layout(false).left).toBe(1052 - 32 - 736);
118 // Equal space on both sides of the chat.
119 expect(layout(true)).toMatchObject({ left: (1052 - 736) / 2, width: 736 });
120 });
121 it("keeps the chat canvas-centered when it clears the details card", () => {
122 const result = resolveChatCanvasLayout({
123 container: { width: 2200, height: 988 },
124 maxChatWidth: 736,
125 detailsCard: { left: 1876, right: 2188, bottom: 339 },
126 preview: null,
127 centerBesideDetails: true,
128 });
129 expect(result.chat.left).toBe((2200 - 736) / 2);
130 });
131 it("moves the chat smoothly as a window with a details card grows", () => {
132 const leftAt = (width: number) =>
133 resolveChatCanvasLayout({
134 container: { width, height: 988 },
135 maxChatWidth: 736,
136 detailsCard: { left: width - 292, right: width - 12, bottom: 339 },
137 preview: null,
138 centerBesideDetails: true,
139 }).chat.left;
140 for (let width = 1300; width < 1800; width += 1) {
141 expect(Math.abs(leftAt(width + 1) - leftAt(width))).toBeLessThanOrEqual(1);
142 }
143 });
144 it("keeps a dragged or resized preview below the open find bar", () => {
145 const findBar = { left: 1292, right: 1572, bottom: 48 };
146 const layout = (lastInteraction: "drag" | "resize") =>
147 resolveChatCanvasLayout({
148 container: { width: 1584, height: 988 },
149 maxChatWidth: 736,
150 composerHeight: 180,
151 findBar,
152 preview: {
153 ...preview,
154 width: 240,
155 source: { width: 240, height: 365 },
156 position: { x: 1332, y: 12 },
157 lastInteraction,
158 },
159 });
160 for (const lastInteraction of ["drag", "resize"] as const) {
161 const result = layout(lastInteraction);
162 expect(result.frame).toEqual({ x: 1332, y: 60, width: 240, height: 365 });
163 expect(result.overlapsDetailsCard).toBe(false);
164 expect(result.chat).toEqual({ left: 424, width: 736, insetStart: 0, insetEnd: 0 });
165 }
166 });
167 it("uses space beside the full card if a tall preview cannot fit below it", () => {
168 const result = resolveChatCanvasLayout({
169 container: { width: 1584, height: 988 },
170 maxChatWidth: 736,
171 composerHeight: 180,
172 detailsCard: { left: 1260, right: 1572, bottom: 339 },
173 preview: {
174 ...preview,
175 width: 240,
176 source: { width: 240, height: 700 },
177 position: { x: 1260, y: 100 },
178 },
179 });
180 expect(result.frame).toEqual({ x: 1008, y: 100, width: 240, height: 700 });
181 expect(result.overlapsDetailsCard).toBe(false);
182 expectClear(result);
183 });
184 it("keeps a large player when clearing the full card would leave no readable chat", () => {
185 const container = { width: 1584, height: 988 };
186 const source = { width: 1000, height: 1523 };
187 const layout = (width: number) => {
188 const resized = resizePreviewMiniPlayer({
189 start: { x: 1332, y: 610, width: 240, height: 366 },
190 direction: "northwest",
191 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
192 container,
193 source,
194 });
195 return resolveChatCanvasLayout({
196 container,
197 maxChatWidth: 736,
198 composerHeight: 206,
199 detailsCard: { left: 1260, right: 1572, bottom: 339 },
200 preview: { ...preview, width: resized.width, position: resized, source },
201 });
202 };
203 const shifted = layout(460);
204 expect(shifted.frame!.width).toBe(460);
205 expect(shifted.chat.left).toBeLessThan(424);
206 expect(shifted.overlapsDetailsCard).toBe(false);
207 expectClear(shifted);
208​
209 const narrowed = layout(532);
210 expect(narrowed.frame!.width).toBe(532);
211 expect(narrowed.chat.width).toBe(656);
212 expect(narrowed.overlapsDetailsCard).toBe(false);
213 expectClear(narrowed);
214​
215 const grown = layout(600);
216 expect(grown.frame!.width).toBe(600);
217 expect(grown.overlapsDetailsCard).toBe(true);
218 expect(grown.overlapsChat).toBe(false);
219 expectClear(grown);
220 expect(layout(532)).toEqual(narrowed);
221 });
222 it.each(["west", "north"] as const)(
223 "folds the card before moving a %s resize beside it",
224 (direction) => {
225 const container = { width: 1584, height: 988 };
226 const source = { width: 1000, height: 1523 };
227 const layout = (width: number) => {
228 const resized = resizePreviewMiniPlayer({
229 start: { x: 1332, y: 611, width: 240, height: 365 },
230 direction,
231 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
232 container,
233 source,
234 });
235 return resolveChatCanvasLayout({
236 container,
237 maxChatWidth: 736,
238 composerHeight: 206,
239 detailsCard: { left: 1260, right: 1572, bottom: 339 },
240 preview: {
241 ...preview,
242 width: resized.width,
243 position: resized,
244 source,
245 lastInteraction: "resize",
246 },
247 });
248 };
249 const density = (result: ReturnType<typeof layout>) => {
250 const card = resolveThreadDetailsCardLayout({
251 container,
252 lane: { padding: 48, minChatWidth: 640 },
253 frame: result.frame,
254 overlapsDetailsCard: result.overlapsDetailsCard,
255 });
256 return card
257 ? resolveThreadDetailsCardDensity(card.height, { full: 327, compact: 182 })
258 : "hidden";
259 };
260 expect(density(layout(400))).toBe("full");
261 const compact = layout(460);
262 expect(compact.frame!.x + compact.frame!.width).toBe(1572);
263 expect(compact.frame!.width).toBe(460);
264 expect(compact.chat.left).toBe(364);
265 expect(compact.overlapsDetailsCard).toBe(true);
266 expect(density(compact)).toBe("compact");
267 expectClear(compact);
268 expect(density(layout(500))).toBe("compact");
269 expect(density(layout(520))).toBe("essential");
270 expect(density(layout(540))).toBe("hidden");
271 expect(layout(600).frame!.width).toBe(600);
272 expect(layout(460)).toEqual(compact);
273 expect(density(layout(400))).toBe("full");
274 },
275 );
276 it("requests card folding only when no full-card slot fits and keeps the composer clear", () => {
277 const result = resolveChatCanvasLayout({
278 container: { width: 1584, height: 988 },
279 maxChatWidth: 736,
280 composerHeight: 180,
281 detailsCard: { left: 1260, right: 1572, bottom: 600 },
282 preview: {
283 ...preview,
284 width: 1300,
285 source: { width: 1300, height: 400 },
286 position: { x: 12, y: 12 },
287 },
288 });
289 expect(result.overlapsDetailsCard).toBe(true);
290 expect(result.overlapsChat).toBe(true);
291 expect(result.frame).toEqual({ x: 12, y: 396, width: 1300, height: 400 });
292 });
293});
294​
295describe("workspace card beside chat", () => {
296 const withCard = (width: number, player: ChatCanvasPreview | null = null, maxChatWidth = 736) =>
297 resolveChatCanvasLayout({
298 container: { width, height: 900 },
299 maxChatWidth,
300 composerHeight: 180,
301 detailsCard: { left: width - 292, right: width - 12, bottom: 400 },
302 preview: player,
303 });
304 it("narrows chat only after it reaches the left padding", () => {
305 expect(withCard(1108).chat).toMatchObject({ left: 48, width: 736 });
306 expect(withCard(1028).chat).toEqual({ left: 48, width: 656, insetStart: 0, insetEnd: 276 });
307 expect(withCard(1012).chat).toMatchObject({ left: 48, width: 640 });
308 });
309 it("reserves the marker gutter across widths where the details card fits", () => {
310 for (let width = 1012; width <= 1440; width++) {
311 const container = { width, height: 900 };
312 const card = resolveThreadDetailsCardLayout({
313 container,
314 lane: { padding: 48, minChatWidth: 640 },
315 frame: null,
316 })!;
317 const chat = withCard(width).chat;
318 expect(chat.left).toBeGreaterThanOrEqual(48);
319 expect(chat.width).toBeGreaterThanOrEqual(640);
320 expect(card.x - chat.left - chat.width).toBeGreaterThanOrEqual(32);
321 }
322 });
323});