|
|
@@ -0,0 +1,101 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 8 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 9 |
import { tmpdir } from "node:os"; |
| 10 |
import { join } from "node:path"; |
| 11 |
import { createRepo } from "../src/actions/repos.ts"; |
| 12 |
import { addComment, openIssue } from "../src/actions/issues.ts"; |
| 13 |
import { createApp, type App } from "../src/app.ts"; |
| 14 |
import { createSession } from "../src/core/sessions.ts"; |
| 15 |
import { createUser } from "../src/core/users.ts"; |
| 16 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 17 |
import { PREVIEW_ISSUE_PAGE, PREVIEW_OWNER_LOOKS } from "../src/spawn/preview-looks.ts"; |
| 18 |
import { utilities as base } from "../src/web/style/base.ts"; |
| 19 |
import { components } from "../src/web/style/components.ts"; |
| 20 |
import { content } from "../src/web/style/content.ts"; |
| 21 |
import { findChromium } from "../src/looks/capture.ts"; |
| 22 |
import { chromiumRuns, evaluateLoaded } from "./helpers/chrome.ts"; |
| 23 |
|
| 24 |
const WORD = "W".repeat(300); |
| 25 |
const TITLE = "A very long title ".repeat(10).trim(); |
| 26 |
let dir: string; |
| 27 |
let app: App; |
| 28 |
let servers: Servers; |
| 29 |
let page: string; |
| 30 |
let cookie: string; |
| 31 |
|
| 32 |
const must = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => { |
| 33 |
if (!r.ok) throw new Error(JSON.stringify(r.error)); |
| 34 |
return r.value; |
| 35 |
}; |
| 36 |
|
| 37 |
beforeAll(async () => { |
| 38 |
dir = await mkdtemp(join(tmpdir(), "forge-issue-fit-")); |
| 39 |
app = createApp({ dataDir: dir, port: 0 }); |
| 40 |
servers = await serve(app); |
| 41 |
const u = must(await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true })); |
| 42 |
const repo = must(await createRepo(app, u, { name: "fit", visibility: "public" })); |
| 43 |
const issue = must(openIssue(app, u, repo, { title: TITLE, body: `${WORD}\n\n\`\`\`\n${"code ".repeat(80)}\n\`\`\`` })); |
| 44 |
for (let i = 0; i < 15; i++) must(addComment(app, u, repo, issue, i === 0 ? WORD : `Comment ${i} ${"words ".repeat(20)}`)); |
| 45 |
cookie = `forge_session=${createSession(app.db, u.id).token}`; |
| 46 |
page = await (await fetch(`${servers.url}/alice/fit/issues/${issue.number}`, { headers: { cookie } })).text(); |
| 47 |
}, 60_000); |
| 48 |
afterAll(async () => { |
| 49 |
servers.stop(); |
| 50 |
app.close(); |
| 51 |
await rm(dir, { recursive: true, force: true }); |
| 52 |
}); |
| 53 |
|
| 54 |
describe("an issue's page at 390 px", () => { |
| 55 |
test("the long content is on the page, in many comments", () => { |
| 56 |
expect(page).toContain(WORD); |
| 57 |
expect(page).toContain("A very long title"); |
| 58 |
expect(page.match(/data-kind="comment"/g)?.length ?? 0).toBeGreaterThanOrEqual(10); |
| 59 |
}); |
| 60 |
|
| 61 |
test("no inline width and no shipped rule fixes a width over 390 px", () => { |
| 62 |
expect(page).not.toMatch(/style="[^"]*(?:^|[;\s])(?:min-|max-)?width\s*:/i); |
| 63 |
for (const m of page.matchAll(/\swidth=["']?(\d+)/gi)) expect(Number(m[1])).toBeLessThanOrEqual(390); |
| 64 |
const over: string[] = []; |
| 65 |
for (const css of [base, components, content, ...(page.match(/<style[^>]*>[\s\S]*?<\/style>/g) ?? [])]) { |
| 66 |
for (const m of css.matchAll(/(?<![-\w(])(?:min-)?width\s*:\s*(-?[\d.]+)(px|rem|em|ch)\b/g)) { |
| 67 |
if (Number(m[1]) * (m[2] === "px" ? 1 : m[2] === "ch" ? 8 : 16) > 390) over.push(m[0]); |
| 68 |
} |
| 69 |
} |
| 70 |
expect(over).toEqual([]); |
| 71 |
}); |
| 72 |
|
| 73 |
test("the container wraps anywhere, and code scrolls inside its own box", () => { |
| 74 |
expect(page).toContain("data-tell"); |
| 75 |
const css = content; |
| 76 |
expect(css).toMatch(/\[data-tell\]\s*\{[^}]*overflow-wrap:\s*(anywhere|break-word)/); |
| 77 |
expect(css).toMatch(/\[data-tell\] \.split > \*\s*\{[^}]*min-width:\s*0/); |
| 78 |
expect(css).toMatch(/\[data-tell\] \.page-head-sub\s*\{[^}]*flex-wrap:\s*wrap/); |
| 79 |
expect(css).toMatch(/\[data-tell\] \.comment-body img\s*\{[^}]*max-width:\s*100%/); |
| 80 |
expect(css).toMatch(/\[data-tell\] pre\s*\{[^}]*overflow-x:\s*auto/); |
| 81 |
}); |
| 82 |
}); |
| 83 |
|
| 84 |
test("the owner's phone look takes an issue's page", () => { |
| 85 |
expect(PREVIEW_ISSUE_PAGE).toBe("/preview-admin/sample/issues/1"); |
| 86 |
expect(PREVIEW_OWNER_LOOKS).toContainEqual({ page: PREVIEW_ISSUE_PAGE, kind: "phone" }); |
| 87 |
}); |
| 88 |
|
| 89 |
const found = findChromium(); |
| 90 |
const chromium = found !== undefined && chromiumRuns(found) ? found : undefined; |
| 91 |
if (chromium === undefined) console.log("SKIPPING: no chromium that starts here, so the issue page is not measured"); |
| 92 |
|
| 93 |
describe.skipIf(chromium === undefined)("measured at 390x844", () => { |
| 94 |
test("no sideways scroll", async () => { |
| 95 |
const measure = `(() => JSON.stringify({ scrollWidth: document.documentElement.scrollWidth, viewportWidth: document.documentElement.clientWidth, |
| 96 |
comments: document.querySelectorAll(".comment").length }))()`; |
| 97 |
const m = JSON.parse(await evaluateLoaded(chromium!, `${servers.url}/alice/fit/issues/1`, { width: 390, height: 844 }, [{ name: "forge_session", value: cookie.split("=")[1]! }], measure)); |
| 98 |
console.log(`issue look 390x844: ${JSON.stringify(m)}`); |
| 99 |
expect(m.scrollWidth).toBeLessThanOrEqual(m.viewportWidth); |
| 100 |
}, 30_000); |
| 101 |
}); |
The
.page-head-suband.comment-body imgrules have no assertion in test/issue-fit.test.ts. Add them to the test or remove them.