|
| 1 | +import { expect, test, type Page } from "@playwright/test"; |
| 2 | + |
| 3 | +import { estimateTimelineMessageHeight } from "../src/components/timelineHeight"; |
| 4 | + |
| 5 | +interface HeightCase { |
| 6 | + timelineWidthPx: number; |
| 7 | + text: string; |
| 8 | + attachmentCount: number; |
| 9 | +} |
| 10 | + |
| 11 | +async function measureUserRowHeight(page: Page, testCase: HeightCase) { |
| 12 | + const { timelineWidthPx, text, attachmentCount } = testCase; |
| 13 | + await page.setContent("<!doctype html><html><body style='margin:0'></body></html>"); |
| 14 | + |
| 15 | + return page.evaluate(({ width, messageText, attachments }) => { |
| 16 | + const timeline = document.createElement("div"); |
| 17 | + timeline.style.width = `${width}px`; |
| 18 | + timeline.style.maxWidth = `${width}px`; |
| 19 | + |
| 20 | + const row = document.createElement("div"); |
| 21 | + row.style.paddingBottom = "16px"; |
| 22 | + |
| 23 | + const alignment = document.createElement("div"); |
| 24 | + alignment.style.display = "flex"; |
| 25 | + alignment.style.justifyContent = "flex-end"; |
| 26 | + |
| 27 | + const bubble = document.createElement("div"); |
| 28 | + bubble.style.boxSizing = "border-box"; |
| 29 | + bubble.style.maxWidth = "80%"; |
| 30 | + bubble.style.padding = "12px 16px"; |
| 31 | + bubble.style.border = "1px solid rgba(0, 0, 0, 0.12)"; |
| 32 | + bubble.style.borderRadius = "16px"; |
| 33 | + bubble.style.background = "rgba(0, 0, 0, 0.03)"; |
| 34 | + |
| 35 | + if (attachments > 0) { |
| 36 | + const attachmentGrid = document.createElement("div"); |
| 37 | + attachmentGrid.style.marginBottom = "8px"; |
| 38 | + attachmentGrid.style.maxWidth = "420px"; |
| 39 | + attachmentGrid.style.display = "grid"; |
| 40 | + attachmentGrid.style.gridTemplateColumns = "repeat(2, minmax(0, 1fr))"; |
| 41 | + attachmentGrid.style.gap = "8px"; |
| 42 | + for (let index = 0; index < attachments; index += 1) { |
| 43 | + const tile = document.createElement("div"); |
| 44 | + tile.style.height = "220px"; |
| 45 | + tile.style.border = "1px solid rgba(0, 0, 0, 0.12)"; |
| 46 | + tile.style.borderRadius = "8px"; |
| 47 | + tile.style.background = "rgba(0, 0, 0, 0.06)"; |
| 48 | + attachmentGrid.append(tile); |
| 49 | + } |
| 50 | + bubble.append(attachmentGrid); |
| 51 | + } |
| 52 | + |
| 53 | + if (messageText.length > 0) { |
| 54 | + const pre = document.createElement("pre"); |
| 55 | + pre.textContent = messageText; |
| 56 | + pre.style.margin = "0"; |
| 57 | + pre.style.whiteSpace = "pre-wrap"; |
| 58 | + pre.style.overflowWrap = "anywhere"; |
| 59 | + pre.style.fontFamily = |
| 60 | + "ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, monospace"; |
| 61 | + pre.style.fontSize = "14px"; |
| 62 | + pre.style.lineHeight = "22px"; |
| 63 | + bubble.append(pre); |
| 64 | + } |
| 65 | + |
| 66 | + const meta = document.createElement("div"); |
| 67 | + meta.style.marginTop = "6px"; |
| 68 | + meta.style.height = "16px"; |
| 69 | + bubble.append(meta); |
| 70 | + |
| 71 | + alignment.append(bubble); |
| 72 | + row.append(alignment); |
| 73 | + timeline.append(row); |
| 74 | + document.body.append(timeline); |
| 75 | + return row.getBoundingClientRect().height; |
| 76 | + }, { width: timelineWidthPx, messageText: text, attachments: attachmentCount }); |
| 77 | +} |
| 78 | + |
| 79 | +function estimatedHeight(testCase: HeightCase): number { |
| 80 | + return estimateTimelineMessageHeight( |
| 81 | + { |
| 82 | + role: "user", |
| 83 | + text: testCase.text, |
| 84 | + attachments: Array.from({ length: testCase.attachmentCount }, (_value, index) => ({ |
| 85 | + id: String(index), |
| 86 | + })), |
| 87 | + }, |
| 88 | + { timelineWidthPx: testCase.timelineWidthPx }, |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +test.describe("timeline height estimator parity", () => { |
| 93 | + test("tracks long wrapped text growth at desktop width", async ({ page }) => { |
| 94 | + const baselineCase: HeightCase = { timelineWidthPx: 960, text: "", attachmentCount: 0 }; |
| 95 | + const longCase: HeightCase = { |
| 96 | + timelineWidthPx: 960, |
| 97 | + text: "x".repeat(1200), |
| 98 | + attachmentCount: 0, |
| 99 | + }; |
| 100 | + |
| 101 | + const baselineMeasured = await measureUserRowHeight(page, baselineCase); |
| 102 | + const longMeasured = await measureUserRowHeight(page, longCase); |
| 103 | + const measuredDelta = longMeasured - baselineMeasured; |
| 104 | + |
| 105 | + const estimatedDelta = estimatedHeight(longCase) - estimatedHeight(baselineCase); |
| 106 | + expect(Math.abs(measuredDelta - estimatedDelta)).toBeLessThanOrEqual(22); |
| 107 | + }); |
| 108 | + |
| 109 | + test("tracks additional wrapping when viewport narrows", async ({ page }) => { |
| 110 | + const desktopCase: HeightCase = { |
| 111 | + timelineWidthPx: 960, |
| 112 | + text: "x".repeat(1000), |
| 113 | + attachmentCount: 0, |
| 114 | + }; |
| 115 | + const mobileCase: HeightCase = { |
| 116 | + timelineWidthPx: 360, |
| 117 | + text: desktopCase.text, |
| 118 | + attachmentCount: 0, |
| 119 | + }; |
| 120 | + |
| 121 | + const desktopMeasured = await measureUserRowHeight(page, desktopCase); |
| 122 | + const mobileMeasured = await measureUserRowHeight(page, mobileCase); |
| 123 | + const measuredDelta = mobileMeasured - desktopMeasured; |
| 124 | + |
| 125 | + const estimatedDelta = estimatedHeight(mobileCase) - estimatedHeight(desktopCase); |
| 126 | + expect(Math.abs(measuredDelta - estimatedDelta)).toBeLessThanOrEqual(44); |
| 127 | + }); |
| 128 | + |
| 129 | + test("tracks attachment row growth", async ({ page }) => { |
| 130 | + const withoutAttachmentsCase: HeightCase = { |
| 131 | + timelineWidthPx: 960, |
| 132 | + text: "hello", |
| 133 | + attachmentCount: 0, |
| 134 | + }; |
| 135 | + const withAttachmentsCase: HeightCase = { |
| 136 | + ...withoutAttachmentsCase, |
| 137 | + attachmentCount: 3, |
| 138 | + }; |
| 139 | + |
| 140 | + const withoutMeasured = await measureUserRowHeight(page, withoutAttachmentsCase); |
| 141 | + const withMeasured = await measureUserRowHeight(page, withAttachmentsCase); |
| 142 | + const measuredDelta = withMeasured - withoutMeasured; |
| 143 | + |
| 144 | + const estimatedDelta = |
| 145 | + estimatedHeight(withAttachmentsCase) - estimatedHeight(withoutAttachmentsCase); |
| 146 | + expect(Math.abs(measuredDelta - estimatedDelta)).toBeLessThanOrEqual(4); |
| 147 | + }); |
| 148 | +}); |
0 commit comments