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