Open History to see the last commit
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
52 it("centers chat in the whole container without a preview", () => {
53 expect(resolve(1344, null).chat).toEqual({ left: 288, width: 768, insetStart: 0, insetEnd: 0 });
54 expect(
55 resolveChatCanvasLayout({
56 container: { width: 390, height: 900 },
57 preview: null,
58 padding: 12,
59 }).chat,
60 ).toEqual({ left: 12, width: 366, insetStart: 0, insetEnd: 0 });
61 });
62 it("does not move chat when a bottom-right preview fits in its margin", () => {
63 const result = resolve(1600);
64 expect(result.chat.insetEnd).toBe(0);
65 expect(result.frame!.y + result.frame!.height).toBe(888);
66 expectClear(result);
67 });
68 it("moves chat only as far as the preview requires", () => {
69 const result = resolve(1344);
70 expect(result.chat).toEqual({ left: 232, width: 768, insetStart: 0, insetEnd: 112 });
71 expectClear(result);
72 });
73 it("shrinks chat modestly after using the left margin", () => {
74 const result = resolve(1200, { ...preview, width: 480 });
75 expect(result.chat.width).toBe(648);
76 expect(result.chat.left).toBe(48);
77 expectClear(result);
78 });
79 it("stops leftward dragging before the player can push chat to the right", () => {
80 const centered = resolve(1344, null).chat;
81 for (const x of [1100, 800, 700, 600, 400, 12, -100]) {
82 const result = resolve(1344, { ...preview, position: { x, y: 500 } });
83 expect(result.frame!.x).toBeGreaterThanOrEqual(700);
84 expect(result.chat.left).toBeLessThanOrEqual(centered.left);
85 expect(result.chat.insetStart).toBe(0);
86 expect(result.chat.width).toBeGreaterThanOrEqual(640);
87 expectClear(result);
88 }
89 expect(resolve(1344, { ...preview, position: { x: 12, y: 500 } }).frame!.x).toBe(700);
90 });
91 it("keeps a dragged player above the composer when no readable lane fits beside it", () => {
92 const result = resolve(1000, { ...preview, position: { x: 12, y: 700 } });
93 expect(result.chat).toEqual(resolve(1000, null).chat);
94 expect(result.frame!.x).toBe(668);
95 expect(result.frame!.y + result.frame!.height).toBeLessThanOrEqual(708);
96 });
97 it("allows message overlap on narrow screens while excluding the composer", () => {
98 const result = resolve(1000);
99 expect(result.overlapsChat).toBe(true);
100 expect(result.chat.insetStart).toBe(0);
101 expect(result.chat.insetEnd).toBe(0);
102 expect(result.frame!.y + result.frame!.height).toBeLessThanOrEqual(900 - 180 - 12);
103 expect(result.frame!.width / result.frame!.height).toBeCloseTo(1.6);
104 });
105 it("restores the preferred width when the container grows again", () => {
106 const player = { ...preview, width: 480 };
107 expect(resolve(1200, player).chat.width).toBeLessThan(768);
108 expect(resolve(1800, player).chat.width).toBe(768);
109 expect(player.width).toBe(480);
110 });
111 it("uses the same stable side-by-side frame even as the composer grows", () => {
112 const result = resolve(1344);
113 expect(
114 resolveChatCanvasLayout({
115 container: { width: 1344, height: 900 },
116 preview,
117 composerHeight: 400,
118 }),
119 ).toEqual(result);
120 });
121 it("constrains a dragged preview below the full top-right card instead of folding it", () => {
122 const result = resolveChatCanvasLayout({
123 container: { width: 1584, height: 988 },
124 maxChatWidth: 736,
125 composerHeight: 180,
126 detailsCard: { left: 1260, right: 1572, bottom: 339 },
127 preview: {
128 ...preview,
129 width: 240,
130 source: { width: 240, height: 365 },
131 position: { x: 1260, y: 240 },
132 },
133 });
134 expect(result.frame).toEqual({ x: 1260, y: 351, width: 240, height: 365 });
135 expect(result.overlapsDetailsCard).toBe(false);
136 expect(result.chat).toEqual({ left: 424, width: 736, insetStart: 0, insetEnd: 0 });
137 });
138 it("uses space beside the full card if a tall preview cannot fit below it", () => {
139 const result = resolveChatCanvasLayout({
140 container: { width: 1584, height: 988 },
141 maxChatWidth: 736,
142 composerHeight: 180,
143 detailsCard: { left: 1260, right: 1572, bottom: 339 },
144 preview: {
145 ...preview,
146 width: 240,
147 source: { width: 240, height: 700 },
148 position: { x: 1260, y: 100 },
149 },
150 });
151 expect(result.frame).toEqual({ x: 1008, y: 100, width: 240, height: 700 });
152 expect(result.overlapsDetailsCard).toBe(false);
153 expectClear(result);
154 });
155 it("keeps a large player when clearing the full card would leave no readable chat", () => {
156 const container = { width: 1584, height: 988 };
157 const source = { width: 1000, height: 1523 };
158 const layout = (width: number) => {
159 const resized = resizePreviewMiniPlayer({
160 start: { x: 1332, y: 610, width: 240, height: 366 },
161 direction: "northwest",
162 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
163 container,
164 source,
165 });
166 return resolveChatCanvasLayout({
167 container,
168 maxChatWidth: 736,
169 composerHeight: 206,
170 detailsCard: { left: 1260, right: 1572, bottom: 339 },
171 preview: { ...preview, width: resized.width, position: resized, source },
172 });
173 };
174 const shifted = layout(460);
175 expect(shifted.frame!.width).toBe(460);
176 expect(shifted.chat.left).toBeLessThan(424);
177 expect(shifted.overlapsDetailsCard).toBe(false);
178 expectClear(shifted);
179
180 const narrowed = layout(532);
181 expect(narrowed.frame!.width).toBe(532);
182 expect(narrowed.chat.width).toBe(656);
183 expect(narrowed.overlapsDetailsCard).toBe(false);
184 expectClear(narrowed);
185
186 const grown = layout(600);
187 expect(grown.frame!.width).toBe(600);
188 expect(grown.overlapsDetailsCard).toBe(true);
189 expect(grown.overlapsChat).toBe(false);
190 expectClear(grown);
191 expect(layout(532)).toEqual(narrowed);
192 });
193 it.each(["west", "north"] as const)(
194 "folds the card before moving a %s resize beside it",
195 (direction) => {
196 const container = { width: 1584, height: 988 };
197 const source = { width: 1000, height: 1523 };
198 const layout = (width: number) => {
199 const resized = resizePreviewMiniPlayer({
200 start: { x: 1332, y: 611, width: 240, height: 365 },
201 direction,
202 delta: { x: 240 - width, y: ((240 - width) * source.height) / source.width },
203 container,
204 source,
205 });
206 return resolveChatCanvasLayout({
207 container,
208 maxChatWidth: 736,
209 composerHeight: 206,
210 detailsCard: { left: 1260, right: 1572, bottom: 339 },
211 preview: {
212 ...preview,
213 width: resized.width,
214 position: resized,
215 source,
216 lastInteraction: "resize",
217 },
218 });
219 };
220 const density = (result: ReturnType<typeof layout>) => {
221 const card = resolveThreadDetailsCardLayout({
222 container,
223 lane: { padding: 48, minChatWidth: 640 },
224 frame: result.frame,
225 overlapsDetailsCard: result.overlapsDetailsCard,
226 });
227 return card
228 ? resolveThreadDetailsCardDensity(card.height, { full: 327, compact: 182 })
229 : "hidden";
230 };
231 expect(density(layout(400))).toBe("full");
232 const compact = layout(460);
233 expect(compact.frame!.x + compact.frame!.width).toBe(1572);
234 expect(compact.frame!.width).toBe(460);
235 expect(compact.chat.left).toBe(364);
236 expect(compact.overlapsDetailsCard).toBe(true);
237 expect(density(compact)).toBe("compact");
238 expectClear(compact);
239 expect(density(layout(500))).toBe("compact");
240 expect(density(layout(520))).toBe("essential");
241 expect(density(layout(540))).toBe("hidden");
242 expect(layout(600).frame!.width).toBe(600);
243 expect(layout(460)).toEqual(compact);
244 expect(density(layout(400))).toBe("full");
245 },
246 );
247 it("requests card folding only when no full-card slot fits and keeps the composer clear", () => {
248 const result = resolveChatCanvasLayout({
249 container: { width: 1584, height: 988 },
250 maxChatWidth: 736,
251 composerHeight: 180,
252 detailsCard: { left: 1260, right: 1572, bottom: 600 },
253 preview: {
254 ...preview,
255 width: 1300,
256 source: { width: 1300, height: 400 },
257 position: { x: 12, y: 12 },
258 },
259 });
260 expect(result.overlapsDetailsCard).toBe(true);
261 expect(result.overlapsChat).toBe(true);
262 expect(result.frame).toEqual({ x: 272, y: 396, width: 1300, height: 400 });
263 });
264});
265
266describe("workspace card beside chat", () => {
267 const withCard = (width: number, player: ChatCanvasPreview | null = null, maxChatWidth = 736) =>
268 resolveChatCanvasLayout({
269 container: { width, height: 900 },
270 maxChatWidth,
271 composerHeight: 180,
272 detailsCard: { left: width - 292, right: width - 12, bottom: 400 },
273 preview: player,
274 });
275 it("keeps chat centered while the card fits beside it", () => {
276 expect(withCard(1384).chat).toEqual({ left: 324, width: 736, insetStart: 0, insetEnd: 0 });
277 });
278 it("moves chat left only as far as the card requires", () => {
279 expect(withCard(1383).chat).toEqual({ left: 323, width: 736, insetStart: 0, insetEnd: 1 });
280 expect(withCard(1147).chat).toEqual({ left: 87, width: 736, insetStart: 0, insetEnd: 237 });
281 });
282 it("narrows chat only after it reaches the left padding", () => {
283 expect(withCard(1108).chat).toMatchObject({ left: 48, width: 736 });
284 expect(withCard(1028).chat).toEqual({ left: 48, width: 656, insetStart: 0, insetEnd: 276 });
285 expect(withCard(1012).chat).toMatchObject({ left: 48, width: 640 });
286 });
287 it("reserves the marker gutter across widths where the details card fits", () => {
288 for (let width = 1012; width <= 1440; width++) {
289 const container = { width, height: 900 };
290 const card = resolveThreadDetailsCardLayout({
291 container,
292 lane: { padding: 48, minChatWidth: 640 },
293 frame: null,
294 })!;
295 const chat = withCard(width).chat;
296 expect(chat.left).toBeGreaterThanOrEqual(48);
297 expect(chat.width).toBeGreaterThanOrEqual(640);
298 expect(card.x - chat.left - chat.width).toBeGreaterThanOrEqual(32);
299 }
300 });
301 it("keeps a full-width chat clear of the card", () => {
302 expect(withCard(1147, null, 10_000).chat).toEqual({
303 left: 48,
304 width: 775,
305 insetStart: 0,
306 insetEnd: 276,
307 });
308 });
309 it("keeps centered chat in place when a new preview opens below the card", () => {
310 expect(withCard(1440, preview)).toEqual({
311 chat: { left: 352, width: 736, insetStart: 0, insetEnd: 0 },
312 frame: { x: 1108, y: 688, width: 320, height: 200 },
313 overlapsChat: false,
314 overlapsDetailsCard: false,
315 });
316 });
317});