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

chatCanvasLayout.test.ts

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