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

chatCanvasLayout.test.ts

319 lines12.5 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("moves the chat left only as far as a details card it would crowd requires", () => {
108 const { chat } = resolveChatCanvasLayout({
109 container: { width: 1344, height: 988 },
110 maxChatWidth: 736,
111 detailsCard: { left: 1052, right: 1332, bottom: 339 },
112 preview: null,
113 });
114 expect(chat).toMatchObject({ left: 1052 - 32 - 736, width: 736 });
115 });
116 it("keeps the chat canvas-centered when it clears the details card", () => {
117 const result = resolveChatCanvasLayout({
118 container: { width: 2200, height: 988 },
119 maxChatWidth: 736,
120 detailsCard: { left: 1876, right: 2188, bottom: 339 },
121 preview: null,
122 });
123 expect(result.chat.left).toBe((2200 - 736) / 2);
124 });
125 it("moves the chat smoothly as a window with a details card grows", () => {
126 const leftAt = (width: number) =>
127 resolveChatCanvasLayout({
128 container: { width, height: 988 },
129 maxChatWidth: 736,
130 detailsCard: { left: width - 292, right: width - 12, bottom: 339 },
131 preview: null,
132 }).chat.left;
133 for (let width = 1300; width < 1800; width += 1) {
134 expect(Math.abs(leftAt(width + 1) - leftAt(width))).toBeLessThanOrEqual(1);
135 }
136 });
137 it("keeps a dragged or resized preview below the open find bar", () => {
138 const findBar = { left: 1292, right: 1572, bottom: 48 };
139 const layout = (lastInteraction: "drag" | "resize") =>
140 resolveChatCanvasLayout({
141 container: { width: 1584, height: 988 },
142 maxChatWidth: 736,
143 composerHeight: 180,
144 findBar,
145 preview: {
146 ...preview,
147 width: 240,
148 source: { width: 240, height: 365 },
149 position: { x: 1332, y: 12 },
150 lastInteraction,
151 },
152 });
153 for (const lastInteraction of ["drag", "resize"] as const) {
154 const result = layout(lastInteraction);
155 expect(result.frame).toEqual({ x: 1332, y: 60, width: 240, height: 365 });
156 expect(result.overlapsDetailsCard).toBe(false);
157 expect(result.chat).toEqual({ left: 424, width: 736, insetStart: 0, insetEnd: 0 });
158 }
159 });
160 it("uses space beside the full card if a tall preview cannot fit below it", () => {
161 const result = resolveChatCanvasLayout({
162 container: { width: 1584, height: 988 },
163 maxChatWidth: 736,
164 composerHeight: 180,
165 detailsCard: { left: 1260, right: 1572, bottom: 339 },
166 preview: {
167 ...preview,
168 width: 240,
169 source: { width: 240, height: 700 },
170 position: { x: 1260, y: 100 },
171 },
172 });
173 expect(result.frame).toEqual({ x: 1008, y: 100, width: 240, height: 700 });
174 expect(result.overlapsDetailsCard).toBe(false);
175 expectClear(result);
176 });
177 it("keeps a large player when clearing the full card would leave no readable chat", () => {
178 const container = { width: 1584, height: 988 };
179 const source = { width: 1000, height: 1523 };
180 const layout = (width: number) => {
181 const resized = resizePreviewMiniPlayer({
182 start: { x: 1332, y: 610, width: 240, height: 366 },
183 direction: "northwest",
184 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
185 container,
186 source,
187 });
188 return resolveChatCanvasLayout({
189 container,
190 maxChatWidth: 736,
191 composerHeight: 206,
192 detailsCard: { left: 1260, right: 1572, bottom: 339 },
193 preview: { ...preview, width: resized.width, position: resized, source },
194 });
195 };
196 const shifted = layout(460);
197 expect(shifted.frame!.width).toBe(460);
198 expect(shifted.chat.left).toBeLessThan(424);
199 expect(shifted.overlapsDetailsCard).toBe(false);
200 expectClear(shifted);
201​
202 const narrowed = layout(532);
203 expect(narrowed.frame!.width).toBe(532);
204 expect(narrowed.chat.width).toBe(656);
205 expect(narrowed.overlapsDetailsCard).toBe(false);
206 expectClear(narrowed);
207​
208 const grown = layout(600);
209 expect(grown.frame!.width).toBe(600);
210 expect(grown.overlapsDetailsCard).toBe(true);
211 expect(grown.overlapsChat).toBe(false);
212 expectClear(grown);
213 expect(layout(532)).toEqual(narrowed);
214 });
215 it.each(["west", "north"] as const)(
216 "folds the card before moving a %s resize beside it",
217 (direction) => {
218 const container = { width: 1584, height: 988 };
219 const source = { width: 1000, height: 1523 };
220 const layout = (width: number) => {
221 const resized = resizePreviewMiniPlayer({
222 start: { x: 1332, y: 611, width: 240, height: 365 },
223 direction,
224 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
225 container,
226 source,
227 });
228 return resolveChatCanvasLayout({
229 container,
230 maxChatWidth: 736,
231 composerHeight: 206,
232 detailsCard: { left: 1260, right: 1572, bottom: 339 },
233 preview: {
234 ...preview,
235 width: resized.width,
236 position: resized,
237 source,
238 lastInteraction: "resize",
239 },
240 });
241 };
242 const density = (result: ReturnType<typeof layout>) => {
243 const card = resolveThreadDetailsCardLayout({
244 container,
245 lane: { padding: 48, minChatWidth: 640 },
246 frame: result.frame,
247 overlapsDetailsCard: result.overlapsDetailsCard,
248 });
249 return card
250 ? resolveThreadDetailsCardDensity(card.height, { full: 327, compact: 182 })
251 : "hidden";
252 };
253 expect(density(layout(400))).toBe("full");
254 const compact = layout(460);
255 expect(compact.frame!.x + compact.frame!.width).toBe(1572);
256 expect(compact.frame!.width).toBe(460);
257 expect(compact.chat.left).toBe(364);
258 expect(compact.overlapsDetailsCard).toBe(true);
259 expect(density(compact)).toBe("compact");
260 expectClear(compact);
261 expect(density(layout(500))).toBe("compact");
262 expect(density(layout(520))).toBe("essential");
263 expect(density(layout(540))).toBe("hidden");
264 expect(layout(600).frame!.width).toBe(600);
265 expect(layout(460)).toEqual(compact);
266 expect(density(layout(400))).toBe("full");
267 },
268 );
269 it("requests card folding only when no full-card slot fits and keeps the composer clear", () => {
270 const result = resolveChatCanvasLayout({
271 container: { width: 1584, height: 988 },
272 maxChatWidth: 736,
273 composerHeight: 180,
274 detailsCard: { left: 1260, right: 1572, bottom: 600 },
275 preview: {
276 ...preview,
277 width: 1300,
278 source: { width: 1300, height: 400 },
279 position: { x: 12, y: 12 },
280 },
281 });
282 expect(result.overlapsDetailsCard).toBe(true);
283 expect(result.overlapsChat).toBe(true);
284 expect(result.frame).toEqual({ x: 12, y: 396, width: 1300, height: 400 });
285 });
286});
287​
288describe("workspace card beside chat", () => {
289 const withCard = (width: number, player: ChatCanvasPreview | null = null, maxChatWidth = 736) =>
290 resolveChatCanvasLayout({
291 container: { width, height: 900 },
292 maxChatWidth,
293 composerHeight: 180,
294 detailsCard: { left: width - 292, right: width - 12, bottom: 400 },
295 preview: player,
296 });
297 it("keeps chat centered while the card fits beside it", () => {
298 expect(withCard(1500).chat).toEqual({ left: 382, width: 736, insetStart: 0, insetEnd: 0 });
299 });
300 it("narrows chat only after it reaches the left padding", () => {
301 expect(withCard(1108).chat).toMatchObject({ left: 48, width: 736 });
302 expect(withCard(1028).chat).toEqual({ left: 48, width: 656, insetStart: 0, insetEnd: 276 });
303 expect(withCard(1012).chat).toMatchObject({ left: 48, width: 640 });
304 });
305 it("reserves the marker gutter across widths where the details card fits", () => {
306 for (let width = 1012; width <= 1440; width++) {
307 const container = { width, height: 900 };
308 const card = resolveThreadDetailsCardLayout({
309 container,
310 lane: { padding: 48, minChatWidth: 640 },
311 frame: null,
312 })!;
313 const chat = withCard(width).chat;
314 expect(chat.left).toBeGreaterThanOrEqual(48);
315 expect(chat.width).toBeGreaterThanOrEqual(640);
316 expect(card.x - chat.left - chat.width).toBeGreaterThanOrEqual(32);
317 }
318 });
319});