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