|
|
@@ -0,0 +1,116 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 8 |
import { createHmac } from "node:crypto"; |
| 9 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 10 |
import { tmpdir } from "node:os"; |
| 11 |
import { join } from "node:path"; |
| 12 |
import { createApp, type App } from "../src/app.ts"; |
| 13 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 14 |
import { addComment } from "../src/core/issues.ts"; |
| 15 |
import { createSession } from "../src/core/sessions.ts"; |
| 16 |
import { findChromium } from "../src/looks/capture.ts"; |
| 17 |
import { PREVIEW_PULL_PAGE } from "../src/spawn/preview-looks.ts"; |
| 18 |
import { previewAdmin, seedPreview } from "../src/spawn/preview-seed.ts"; |
| 19 |
import { base as elements, utilities as base } from "../src/web/style/base.ts"; |
| 20 |
import { components } from "../src/web/style/components.ts"; |
| 21 |
import { content } from "../src/web/style/content.ts"; |
| 22 |
import { chromiumRuns, evaluateLoaded } from "./helpers/chrome.ts"; |
| 23 |
|
| 24 |
const KEY = "pull-phone-fit-key"; |
| 25 |
const WORD = "w".repeat(300); |
| 26 |
const BRANCH = `feature/${"b".repeat(192)}`; |
| 27 |
const TITLE = `Make every long and winding pull title wrap ${"t".repeat(120)} and many words after it`; |
| 28 |
let dir: string; |
| 29 |
let app: App; |
| 30 |
let servers: Servers; |
| 31 |
let page: string; |
| 32 |
let files: string; |
| 33 |
|
| 34 |
beforeAll(async () => { |
| 35 |
dir = await mkdtemp(join(tmpdir(), "forge-pull-fit-")); |
| 36 |
app = createApp({ dataDir: dir, port: 0, previewIdentityKey: KEY }); |
| 37 |
const seeded = await seedPreview(app, { FORGE_APP_ENV: "preview" }); |
| 38 |
if (!seeded.ok) throw new Error(seeded.error); |
| 39 |
const admin = previewAdmin(app)!; |
| 40 |
const row = app.db.query("SELECT issue_id AS id FROM pull ORDER BY issue_id").all() as { id: string }[]; |
| 41 |
const number = Number(PREVIEW_PULL_PAGE.split("/").pop()); |
| 42 |
const issue = app.db.query("SELECT id FROM issue WHERE number = ?1 AND id IN (SELECT issue_id FROM pull)").get(number) as { id: string }; |
| 43 |
expect(row.length).toBeGreaterThan(0); |
| 44 |
app.db.query("UPDATE issue SET title = ?1 WHERE id = ?2").run(TITLE, issue.id); |
| 45 |
app.db.query("UPDATE pull SET head_branch = ?1 WHERE issue_id = ?2").run(BRANCH, issue.id); |
| 46 |
for (let i = 0; i < 12; i++) addComment(app.db, { issueId: issue.id, authorId: admin.id, body: `Row ${String(i)} ${WORD} https://example.com/${WORD}` }); |
| 47 |
servers = await serve(app); |
| 48 |
const said = `u1|trevato|admin|${Date.now() + 60_000}`; |
| 49 |
const headers = { "x-forge-identity": said, "x-forge-signature": createHmac("sha256", KEY).update(said).digest("base64url") }; |
| 50 |
page = await (await fetch(`${servers.url}${PREVIEW_PULL_PAGE}`, { headers })).text(); |
| 51 |
files = await (await fetch(`${servers.url}${PREVIEW_PULL_PAGE}/files`, { headers })).text(); |
| 52 |
}, 30_000); |
| 53 |
afterAll(async () => { |
| 54 |
servers.stop(); |
| 55 |
app.close(); |
| 56 |
await rm(dir, { recursive: true, force: true }); |
| 57 |
}); |
| 58 |
|
| 59 |
describe("a pull's conversation page at 390 px", () => { |
| 60 |
test("the seeded long content is on the page", () => { |
| 61 |
expect(page).toContain(TITLE); |
| 62 |
expect(page).toContain(WORD); |
| 63 |
|
| 64 |
expect(files).toContain(BRANCH); |
| 65 |
}); |
| 66 |
|
| 67 |
test("no inline width and no rule the page ships fixes a width over 390 px", () => { |
| 68 |
for (const m of page.matchAll(/\swidth=["']?(\d+)/gi)) expect(Number(m[1])).toBeLessThanOrEqual(390); |
| 69 |
expect(page).not.toMatch(/style="[^"]*(?:^|[;\s])min-width\s*:/i); |
| 70 |
for (const m of page.matchAll(/style="([^"]*)"/g)) { |
| 71 |
for (const w of m[1]!.matchAll(/(?<![-\w])(?:min-)?width\s*:\s*(-?[\d.]+)(px|rem|em|ch)\b/g)) { |
| 72 |
expect(Number(w[1]) * (w[2] === "px" ? 1 : w[2] === "ch" ? 8 : 16)).toBeLessThanOrEqual(390); |
| 73 |
} |
| 74 |
} |
| 75 |
const over: string[] = []; |
| 76 |
for (const css of [base, components, content, ...(page.match(/<style[^>]*>[\s\S]*?<\/style>/g) ?? []), ...(files.match(/<style[^>]*>[\s\S]*?<\/style>/g) ?? [])]) { |
| 77 |
for (const m of css.matchAll(/(?<![-\w(])(?:min-)?width\s*:\s*(-?[\d.]+)(px|rem|em|ch)\b/g)) { |
| 78 |
if (Number(m[1]) * (m[2] === "px" ? 1 : m[2] === "ch" ? 8 : 16) > 390) over.push(m[0]); |
| 79 |
} |
| 80 |
} |
| 81 |
expect(over).toEqual([]); |
| 82 |
}); |
| 83 |
|
| 84 |
test("the long-text containers wrap anywhere", () => { |
| 85 |
expect(components).toMatch(/\.page-head h1[^{]*\{[^}]*overflow-wrap:\s*(anywhere|break-word)/); |
| 86 |
expect(elements).toMatch(/\.mono[^{]*\{[^}]*overflow-wrap:\s*(anywhere|break-word)/); |
| 87 |
expect(content).toMatch(/\.prose\s*\{[^}]*overflow-wrap:\s*(anywhere|break-word)/); |
| 88 |
}); |
| 89 |
}); |
| 90 |
|
| 91 |
const found = findChromium(); |
| 92 |
const chromium = found !== undefined && chromiumRuns(found) ? found : undefined; |
| 93 |
if (chromium === undefined) console.log("SKIPPING: no chromium that starts here, so the pull's phone look is not measured"); |
| 94 |
|
| 95 |
describe.skipIf(chromium === undefined)("a pull's conversation page, measured", () => { |
| 96 |
const measure = `(() => { |
| 97 |
const vw = document.documentElement.clientWidth; |
| 98 |
const overflowing = [...document.querySelectorAll("main *, h1")].filter((el) => el.getBoundingClientRect().right > vw + 1).slice(0, 8).map((el) => el.tagName.toLowerCase() + "." + String(el.className)); |
| 99 |
return JSON.stringify({ scrollWidth: document.documentElement.scrollWidth, viewportWidth: vw, overflowing }); |
| 100 |
})()`; |
| 101 |
const look = async (width: number, height: number) => { |
| 102 |
const cookies = [{ name: "forge_session", value: createSession(app.db, previewAdmin(app)!.id).token }]; |
| 103 |
const m = JSON.parse(await evaluateLoaded(chromium!, `${servers.url}${PREVIEW_PULL_PAGE}`, { width, height }, cookies, measure)) as { scrollWidth: number; viewportWidth: number; overflowing: string[] }; |
| 104 |
console.log(`pull look ${width}x${height}: ${JSON.stringify(m)}`); |
| 105 |
return m; |
| 106 |
}; |
| 107 |
test("phone, 390x844: no sideways scroll", async () => { |
| 108 |
const m = await look(390, 844); |
| 109 |
expect(m.overflowing).toEqual([]); |
| 110 |
expect(m.scrollWidth).toBeLessThanOrEqual(m.viewportWidth); |
| 111 |
}, 30_000); |
| 112 |
test("desk, 1280x800: no sideways scroll", async () => { |
| 113 |
const m = await look(1280, 800); |
| 114 |
expect(m.scrollWidth).toBeLessThanOrEqual(m.viewportWidth); |
| 115 |
}, 30_000); |
| 116 |
}); |