Public: anyone can read this; the crew can change it.
A file that opens as plain source says in one line why it was not drawn #15723
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15719-a-file-that-opens-as-plain-source-says-i → main 0 commits fork 4 threads · 4 unresolved
9 files
changed +304
−10
docs/features/plain-source-reason.md added +18
| @@ -0,0 +1,18 @@ | ||
| 1 | --- | |
| 2 | topic: Files in the browser | |
| 3 | row: Why a file opened as plain source | |
| 4 | --- | |
| 5 | # Why a file opened as plain source | |
| 6 | ||
| 7 | When a file's name says it is a drawable kind (a picture, a table, a PSD) but its bytes do not look like it, | |
| 8 | for example a `.png` whose bytes are text, the file opens as plain source and one quiet line says why: | |
| 9 | `its bytes do not look like image`. A kind the forge has no viewer for says `no viewer for <kind> yet`. | |
| 10 | A file over its kind's size limit says `too large (2.1 MB; the limit for html is 2 MB)`, and the | |
| 11 | empty state under it only offers the download, so the size and the limit are said once. | |
| 12 | Ordinary text files show no line. | |
| 13 | ||
| 14 | On the web, open `/<owner>/<repo>/blob/<ref>/<path>`: the line sits above the source. | |
| 15 | In the terminal (`FORGE_APP_SSH`), open the file in the workspace: the same words stand as the first row | |
| 16 | of the file view, text or hex, and stay while you scroll; a too-large file says them in its note. | |
| 17 | ||
| 18 | Try it: commit a `.png` containing text, then open it on either door. |
src/tui/screens/workspace.ts +19 −2
| @@ -132,6 +132,7 @@ import { archiveNote } from "../ui/archive-note.ts"; | ||
| 132 | 132 | import { holdsExif, readExif } from "../../web/render/exif.ts"; |
| 133 | 133 | import { psdNote } from "../ui/psd-note.ts"; |
| 134 | 134 | import { mediaNote } from "../ui/media-note.ts"; |
| 135 | import { plainReason } from "../../web/render/renderers.ts"; | |
| 135 | 136 | import { undrawable, UNDRAWN } from "../../web/render/undrawable.ts"; |
| 136 | 137 | import { tokColor, type Tokens } from "../ui/theme.ts"; |
| 137 | 138 | import { card, emptyState, input, noteLines, scrollbar } from "../ui/widgets.ts"; |
| @@ -872,6 +873,8 @@ class Tab { | ||
| 872 | 873 | note = ""; |
| 873 | 874 | noteTitle = "Not shown"; |
| 874 | 875 | noteDetail = ""; |
| 876 | /** The one line saying why this file is plain source; empty for ordinary text. */ | |
| 877 | why = ""; | |
| 875 | 878 | /** |
| 876 | 879 | * Non-empty when `raw` is only the start of a larger file: the sentence that says so. It is never |
| 877 | 880 | * edited. |
| @@ -1229,6 +1232,8 @@ interface CacheEntry { | ||
| 1229 | 1232 | /** A last line under the note: where to get the file. */ |
| 1230 | 1233 | readonly noteDetail: string; |
| 1231 | 1234 | readonly cut?: string; |
| 1235 | /** Why the file is plain source though its name says otherwise; stands above the text. */ | |
| 1236 | readonly why?: string; | |
| 1232 | 1237 | readonly raw: Uint8Array | null; |
| 1233 | 1238 | readonly picture: Uint8Array | null; |
| 1234 | 1239 | readonly sheet: Table | null; |
| @@ -1548,7 +1553,9 @@ export function workspaceScreen( | ||
| 1548 | 1553 | } else if (read.value.kind === "too_large") { |
| 1549 | 1554 | // One line a part, so the pane keeps the whole reason and the way to the download. |
| 1550 | 1555 | const said = undrawable(t.path, read.value, formatSize); |
| 1551 | const lines = said === undefined ? [`${formatSize(read.value.size)} — over the render cap.`] : [said.name, said.size, said.reason]; | |
| 1556 | // The registry's too-large line, when it has one, stands in for the reason: said once. | |
| 1557 | const why = plainReason(t.path, read.value); | |
| 1558 | const lines = said === undefined ? [why ?? `${formatSize(read.value.size)} — over the render cap.`] : [said.name, said.size, why ?? said.reason]; | |
| 1552 | 1559 | // A photo's EXIF is in its head, so its facts are read even when the file is not. |
| 1553 | 1560 | const photo = holdsExif(t.path) && read.value.head !== undefined ? readExif(read.value.head).map((f) => `${f.label}: ${f.value}`) : []; |
| 1554 | 1561 | note = `${[...lines, ...photo].join("\n")}\n\n${downloadAt(t.path)}`; |
| @@ -1589,7 +1596,9 @@ export function workspaceScreen( | ||
| 1589 | 1596 | cut = `Showing the first ${formatSize(read.value.body.length)} of ${formatSize(read.value.size)}, read-only. The rest is in the raw file.`; |
| 1590 | 1597 | } |
| 1591 | 1598 | } |
| 1592 | entry = { note, noteTitle, noteDetail, cut, raw, picture, sheet, db, ...(big === undefined ? {} : { big }) }; | |
| 1599 | // A too-large file says its reason in the note; the line above the source is for the rest. | |
| 1600 | const why = read.ok && read.value.kind !== "too_large" ? (plainReason(t.path, read.value) ?? "") : ""; | |
| 1601 | entry = { note, noteTitle, noteDetail, cut, why, raw, picture, sheet, db, ...(big === undefined ? {} : { big }) }; | |
| 1593 | 1602 | remember(key, entry); |
| 1594 | 1603 | } |
| 1595 | 1604 | if (stale(mine)) return; |
| @@ -1599,6 +1608,7 @@ export function workspaceScreen( | ||
| 1599 | 1608 | t.noteDetail = entry.noteDetail; |
| 1600 | 1609 | t.raw = entry.raw; |
| 1601 | 1610 | t.cut = entry.cut ?? ""; |
| 1611 | t.why = entry.why ?? ""; | |
| 1602 | 1612 | if (t.cut !== "") ctx.toast(t.cut, "attn"); |
| 1603 | 1613 | t.sheet = entry.sheet; |
| 1604 | 1614 | t.db = entry.db; |
| @@ -3264,6 +3274,13 @@ export function workspaceScreen( | ||
| 3264 | 3274 | drawTable(s, r, c, t); |
| 3265 | 3275 | return; |
| 3266 | 3276 | } |
| 3277 | if (t.why !== "" && r.h > 1) { | |
| 3278 | // Cut with an ellipsis, never mid-word unannounced; the code under it starts one row down, | |
| 3279 | // and the wheel and keys scroll by lines, so nothing measures from the pane's top row. | |
| 3280 | s.text(r.x, r.y, truncate(t.why, Math.max(1, r.w - 1)), style(c.muted, c.bg), r.x + Math.max(0, r.w - 1)); | |
| 3281 | drawCode(s, { x: r.x, y: r.y + 1, w: r.w, h: r.h - 1 }, c, t); | |
| 3282 | return; | |
| 3283 | } | |
| 3267 | 3284 | drawCode(s, r, c, t); |
| 3268 | 3285 | } |
| 3269 | 3286 |
src/web/pages/repo.ts +12 −2
| @@ -118,7 +118,7 @@ import { EXIF_WINDOW, holdsExif } from "../render/exif.ts"; | ||
| 118 | 118 | import { READ_CEILING } from "../render/psd-read.ts"; |
| 119 | 119 | import { NO_MEMBER, openMember } from "../render/json-big.ts"; |
| 120 | 120 | import { CSV_PREFIX_BYTES, fileKind, HEAD_BYTES, isJsonPath, isTablePath, JSON_PREFIX_BYTES, kindByName, needsPaths, RENDER_CAP, sniffType } from "../render/kinds.ts"; |
| 121 | import { RENDERERS, rendererFor } from "../render/renderers.ts"; | |
| 121 | import { plainReason, RENDERERS, rendererFor, TOO_LARGE } from "../render/renderers.ts"; | |
| 122 | 122 | import { downloadHref, mediaHref } from "./media.ts"; |
| 123 | 123 | import { browseBase, localScope, type RepoScope } from "../scope.ts"; |
| 124 | 124 | import { repoForkOf } from "../../core/origins.ts"; |
| @@ -1202,6 +1202,9 @@ function blobRenderer(scope: RepoScope, path: string, blob: Blob): ReturnType<ty | ||
| 1202 | 1202 | return found?.render === RENDERERS.pdf && scope.kind !== "local" ? undefined : found; |
| 1203 | 1203 | } |
| 1204 | 1204 | |
| 1205 | /** The empty state's note when the reason line above it already says the size and the limit. */ | |
| 1206 | const SAID_ABOVE = "Download it to read it."; | |
| 1207 | ||
| 1205 | 1208 | /** Why an archive too big to list is not listed, as one sentence. */ |
| 1206 | 1209 | function overCap(path: string, blob: Blob): string | undefined { |
| 1207 | 1210 | if (kindByName(path) === "notebook" && blob.size > RENDER_CAP.notebook) |
| @@ -1308,11 +1311,17 @@ export function renderBlob( | ||
| 1308 | 1311 | }) |
| 1309 | 1312 | : undefined; |
| 1310 | 1313 | const fellBack = drawn?.fallback; |
| 1314 | // A kind's own over-the-cap sentence (G-code, a notebook, an archive) already says why. | |
| 1315 | const capSays = overCap(o.path, blob); | |
| 1316 | // The registry's own reason a file is plain source; nothing for ordinary text. | |
| 1317 | const plainWhy = renderer === undefined && capSays === undefined ? plainReason(o.path, blob, RENDERERS, { media: scope.kind === "local" }) : undefined; | |
| 1318 | // The line above says the size and the limit, so the empty state under it does not (say it once). | |
| 1319 | const saysSize = plainWhy?.startsWith(TOO_LARGE) === true; | |
| 1311 | 1320 | const { content, lines, summary } = |
| 1312 | 1321 | drawn !== undefined && fellBack === undefined |
| 1313 | 1322 | ? drawn |
| 1314 | 1323 | : { |
| 1315 | ...blobBody(base, o.ref, o.path, blob, o.highlighted, o.all, o.rows, fellBack ?? overCap(o.path, blob)), | |
| 1324 | ...blobBody(base, o.ref, o.path, blob, o.highlighted, o.all, o.rows, fellBack ?? capSays ?? (saysSize && blob.kind === "too_large" ? SAID_ABOVE : undefined)), | |
| 1316 | 1325 | summary: undefined, |
| 1317 | 1326 | }; |
| 1318 | 1327 | const sizeText = |
| @@ -1378,6 +1387,7 @@ export function renderBlob( | ||
| 1378 | 1387 | fellBack !== undefined, |
| 1379 | 1388 | () => html`<p class="muted small mb">Showing the source: ${fellBack}</p>`, |
| 1380 | 1389 | )} |
| 1390 | ${when(plainWhy !== undefined, () => html`<p class="muted small mb" data-plain-why>${plainWhy}</p>`)} | |
| 1381 | 1391 | ${content} |
| 1382 | 1392 | </div>`; |
| 1383 | 1393 | } |
src/web/render/renderers.ts +51 −4
| @@ -49,6 +49,7 @@ import { htmlPreview } from "./html_preview.ts"; | ||
| 49 | 49 | import { image } from "./image.ts"; |
| 50 | 50 | import { holdsExif } from "./exif.ts"; |
| 51 | 51 | import { fileKind, HEAD_BYTES, isJsonPath, kindByName, NO_BYTES, RENDER_CAP, type FileKind } from "./kinds.ts"; |
| 52 | import { limit } from "./undrawable.ts"; | |
| 52 | 53 | import { log, drawsLog } from "./log.ts"; |
| 53 | 54 | import { makefile } from "./makefile.ts"; |
| 54 | 55 | import { markdown } from "./markdown.ts"; |
| @@ -235,6 +236,43 @@ export function rendererFor( | ||
| 235 | 236 | renderers: Partial<Readonly<Record<FileKind, Renderer>>> = RENDERERS, |
| 236 | 237 | where: { readonly media: boolean } = { media: true }, |
| 237 | 238 | ): { render: Renderer; body: Uint8Array } | undefined { |
| 239 | const d = decide(path, blob, renderers, where); | |
| 240 | return "render" in d ? d : undefined; | |
| 241 | } | |
| 242 | ||
| 243 | /** | |
| 244 | * Why a file that is not plain text opens as plain source, in one line, or undefined when it is | |
| 245 | * drawn or needs no renderer (ordinary text, a binary with its own view). Decided by the same | |
| 246 | * call as `rendererFor`, never re-derived by a page. | |
| 247 | */ | |
| 248 | export function plainReason( | |
| 249 | path: string, | |
| 250 | blob: Blob, | |
| 251 | renderers: Partial<Readonly<Record<FileKind, Renderer>>> = RENDERERS, | |
| 252 | where: { readonly media: boolean } = { media: true }, | |
| 253 | ): string | undefined { | |
| 254 | const d = decide(path, blob, renderers, where); | |
| 255 | return "render" in d ? undefined : d.why; | |
| 256 | } | |
| 257 | ||
| 258 | /** | |
| 259 | * The too-large reason, as the issue words it: `too large (3.2 MB; the limit for html is 2 MB)`. | |
| 260 | */ | |
| 261 | function tooLarge(size: number, kind: FileKind): string { | |
| 262 | return `${TOO_LARGE} (${limit(size)}; the limit for ${kind} is ${limit(RENDER_CAP[kind])})`; | |
| 263 | } | |
| 264 | ||
| 265 | /** How a too-large reason begins, so a door can tell it from the others and not say it twice. */ | |
| 266 | export const TOO_LARGE = "too large"; | |
| 267 | ||
| 268 | type Decision = { render: Renderer; body: Uint8Array } | { why?: string }; | |
| 269 | ||
| 270 | function decide( | |
| 271 | path: string, | |
| 272 | blob: Blob, | |
| 273 | renderers: Partial<Readonly<Record<FileKind, Renderer>>>, | |
| 274 | where: { readonly media: boolean }, | |
| 275 | ): Decision { | |
| 238 | 276 | // A file the source gave no bytes of is taken at its name: STL, OBJ and |
| 239 | 277 | // glTF have no signature to read anyway. |
| 240 | 278 | const named = |
| @@ -245,12 +283,21 @@ export function rendererFor( | ||
| 245 | 283 | : kindByName(path); |
| 246 | 284 | // A bill of materials too big to parse keeps the JSON view it had, outline and all. |
| 247 | 285 | const kind = named === "sbom" && blob.size > RENDER_CAP.sbom ? "config" : named; |
outdated
Principle 1 / issue box 1: too-large returns {} so no |
||
| 286 | // A file the source did not read whole has no bytes to compare with its name. | |
| 287 | const said = blob.kind !== "too_large"; | |
| 288 | const byName = kindByName(path); | |
| 289 | const plain = kind === "text" || kind === "binary"; | |
| 248 | 290 | // A table's cap is lifted only for a body the source already cut to a prefix. |
| 249 | 291 | const prefixed = kind === "csv" && blob.kind === "text" && blob.partial === true; |
| 250 | 292 | const outlined = kind === "config" && blob.kind === "text" && blob.partial === true && isJsonPath(path); |
| 251 | if (blob.size > RENDER_CAP[kind] && !prefixed && !outlined) return undefined; | |
| 252 | if (MEDIA_KINDS.has(kind) && !where.media) return undefined; | |
| 253 | if (blob.kind === "too_large" && !MEDIA_KINDS.has(kind) && kind !== "psd" && kind !== "cad") return undefined; | |
| 293 | if (said && plain && byName !== "text" && byName !== "binary" && renderers[byName] !== undefined) | |
| 294 | return { why: `its bytes do not look like ${byName}` }; | |
| 295 | // Over its kind's own limit: the line says both sizes, and the doors' empty states do not repeat | |
| 296 | // it. | |
| 297 | if (blob.size > RENDER_CAP[kind] && !prefixed && !outlined) | |
| 298 | return plain ? {} : { why: tooLarge(blob.size, kind) }; | |
| 299 | if (MEDIA_KINDS.has(kind) && !where.media) return {}; | |
| 300 | if (blob.kind === "too_large" && !MEDIA_KINDS.has(kind) && kind !== "psd" && kind !== "cad") return {}; | |
| 254 | 301 | // A kind may draw only some of its files: `config` draws JSON, YAML and |
| 255 | 302 | // TOML as a form, and leaves the rest (INI, .env …) their source. |
| 256 | 303 | // A log with no escape and no marked line keeps the source view, exactly as plain text. |
| @@ -260,7 +307,7 @@ export function rendererFor( | ||
| 260 | 307 | (kind === "log" && blob.kind !== "too_large" && !drawsLog(blob.body)) || |
| 261 | 308 | (kind === "package" && blob.kind !== "too_large" && readPackage(blob.body) === undefined); |
| 262 | 309 | const render = declined ? undefined : renderers[kind]; |
| 263 | if (render === undefined) return undefined; | |
| 310 | if (render === undefined) return said && !plain && !declined ? { why: `no viewer for ${kind} yet` } : {}; | |
| 264 | 311 | // A PSD over the read ceiling is drawn from its head: the header holds the facts. So is a |
| 265 | 312 | // photo's: its EXIF is in the first 64 KiB. |
| 266 | 313 | const fromHead = kind === "psd" || kind === "cad" || (kind === "image" && holdsExif(path)); |
src/web/render/undrawable.ts +2 −1
| @@ -19,7 +19,8 @@ export interface UndrawnBlob { | ||
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | const MB = 1_000_000; |
| 22 | const limit = (bytes: number): string => (bytes >= MB ? `${+(bytes / MB).toFixed(1)} MB` : `${Math.round(bytes / 1000)} kB`); | |
| 22 | /** A size as the limits are written: `2 MB`, `800 kB`. */ | |
| 23 | export const limit = (bytes: number): string => (bytes >= MB ? `${+(bytes / MB).toFixed(1)} MB` : `${Math.round(bytes / 1000)} kB`); | |
| 23 | 24 | const MEDIA: ReadonlySet<FileKind> = new Set<FileKind>(["image", "svg", "pdf", "html", "audio", "video", "model3d"]); |
| 24 | 25 | |
| 25 | 26 | const baseName = (path: string): string => path.slice(path.lastIndexOf("/") + 1); |
test/blob-media-ceiling.test.ts +3 −1
| @@ -192,7 +192,9 @@ describe("a large blob on the page", () => { | ||
| 192 | 192 | test("a 3 MB .mp4 of words, and a 3 MB Markdown file, keep the over-cap note", async () => { |
| 193 | 193 | for (const path of ["words.mp4", "notes.md"]) { |
| 194 | 194 | const text = await (await fetch(`${base}/alice/big/blob/main/${path}`)).text(); |
| 195 | expect(text).toContain("Over the 1 MB limit"); | |
| 195 | // The limit is said once: the reason line above, or the empty state's note when there is | |
| 196 | // none. | |
| 197 | expect((text.match(/Over the 1 MB limit|too large \(/g) ?? []).length).toBe(1); | |
| 196 | 198 | expect(text).not.toContain("<video"); |
| 197 | 199 | } |
| 198 | 200 | }); |
test/blob-notebook-page.test.ts +3
| @@ -62,7 +62,10 @@ describe("a .ipynb on its blob page", () => { | ||
| 62 | 62 | const res = await fetch(`${base}/alice/p/blob/main/big.ipynb`); |
| 63 | 63 | const text = await res.text(); |
| 64 | 64 | expect(res.status).toBe(200); |
| 65 | // The notebook's own sentence says the limit, once; the generic reason line does not repeat it | |
| 66 | // (#15719). | |
| 65 | 67 | expect(text).toContain("This notebook is over 1 MB, so it is not drawn as cells"); |
| 68 | expect(text).not.toContain("data-plain-why"); | |
| 66 | 69 | expect(text).not.toContain("In ["); |
| 67 | 70 | }); |
| 68 | 71 |
test/gcode-page.test.ts +2
| @@ -120,6 +120,8 @@ describe("a G-code file on the page", () => { | ||
| 120 | 120 | expect(text).toContain("Download"); |
| 121 | 121 | expect(text).not.toContain("gcode-view"); |
| 122 | 122 | expect(text).not.toContain('name="layer"'); |
| 123 | // The G-code sentence says the limit; the generic reason line does not say it again (#15719). | |
| 124 | expect(text).not.toContain("data-plain-why"); | |
| 123 | 125 | }); |
| 124 | 126 | |
| 125 | 127 | test("the local source reads a G-code file past 5 MB, up to the cap, and no further", async () => { |
test/plain-reason.test.ts added +194
| @@ -0,0 +1,194 @@ | ||
| 1 | /** | |
| 2 | * A file that opens as plain source says in one line why it was not drawn (#15719): too large, | |
| 3 | * bytes that do not match its name, a kind with no renderer. Ordinary text says nothing. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { repoDir } from "../src/git/repos.ts"; | |
| 12 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { plainReason, RENDERERS } from "../src/web/render/renderers.ts"; | |
| 14 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | import { workspaceFixture, type Fixture } from "./helpers/tui-workspace.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | const enc = (s: string): Uint8Array => new TextEncoder().encode(s); | |
| 21 | const text = (s: string, size = s.length) => ({ kind: "text" as const, body: enc(s), size }); | |
| 22 | ||
| 23 | /** Commits a 2.1 MB `big.html` onto main in the repository at `dir`, with git plumbing. */ | |
| 24 | function addBig(dir: string): void { | |
| 25 | const run = (args: string[], stdin?: Uint8Array | string): string => { | |
| 26 | const out = Bun.spawnSync(["git", ...args], { | |
| 27 | cwd: dir, | |
| 28 | stdin: typeof stdin === "string" ? enc(stdin) : (stdin ?? "ignore"), | |
| 29 | env: { ...process.env, GIT_AUTHOR_NAME: "alice", GIT_AUTHOR_EMAIL: "[email protected]", GIT_COMMITTER_NAME: "alice", GIT_COMMITTER_EMAIL: "[email protected]" }, | |
| 30 | }); | |
| 31 | if (out.exitCode !== 0) throw new Error(`git ${args[0]}: ${out.stderr.toString()}`); | |
| 32 | return out.stdout.toString().trim(); | |
| 33 | }; | |
| 34 | const blob = run(["hash-object", "-w", "--stdin"], enc("<p>" + "x".repeat(2_100_000) + "</p>")); | |
| 35 | const tree = run(["mktree"], `${run(["ls-tree", "main"])}\n100644 blob ${blob}\tbig.html\n`); | |
| 36 | run(["update-ref", "refs/heads/main", run(["commit-tree", tree, "-p", "main", "-m", "big"])]); | |
| 37 | } | |
| 38 | ||
| 39 | describe("plainReason", () => { | |
| 40 | test("too large names the size and its kind's limit", () => { | |
| 41 | expect(plainReason("a.html", text("<p>x</p>", 2_500_000))).toBe("too large (2.5 MB; the limit for html is 2 MB)"); | |
| 42 | expect(plainReason("a.html", { kind: "too_large", size: 3_000_000 })).toBe("too large (3 MB; the limit for html is 2 MB)"); | |
| 43 | }); | |
| 44 | test("bytes that do not look like the name", () => { | |
| 45 | expect(plainReason("a.png", text("just words"))).toBe("its bytes do not look like image"); | |
| 46 | }); | |
| 47 | test("a kind with no renderer", () => { | |
| 48 | const { markdown: _gone, ...rest } = RENDERERS; | |
| 49 | expect(plainReason("a.md", text("# hi"), rest)).toBe("no viewer for markdown yet"); | |
| 50 | }); | |
| 51 | test("ordinary text, and a drawn file, say nothing", () => { | |
| 52 | expect(plainReason("a.txt", text("hello"))).toBeUndefined(); | |
| 53 | expect(plainReason("a.ts", text("let x = 1"))).toBeUndefined(); | |
| 54 | expect(plainReason("a.md", text("# hi"))).toBeUndefined(); | |
| 55 | expect(plainReason("a.txt", text("hello", 900_000))).toBeUndefined(); | |
| 56 | }); | |
| 57 | }); | |
| 58 | ||
| 59 | describe("the file page and the terminal", () => { | |
| 60 | let dataDir: string; | |
| 61 | let app: App; | |
| 62 | let servers: Servers; | |
| 63 | let fx: Fixture; | |
| 64 | ||
| 65 | beforeAll(async () => { | |
| 66 | dataDir = await mkdtemp(join(tmpdir(), "forge-plain-")); | |
| 67 | app = createApp({ dataDir, port: 0 }); | |
| 68 | servers = await serve(app); | |
| 69 | const a = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "a-long-plain-passphrase" }); | |
| 70 | if (!a.ok) throw new Error("signup failed"); | |
| 71 | const r = await createRepo(app, a.value, { name: "p", visibility: "public" }); | |
| 72 | if (!r.ok) throw new Error("repo failed"); | |
| 73 | const made = await commitToBranch(app, a.value, r.value, { | |
| 74 | message: "seed", | |
| 75 | files: [ | |
| 76 | { path: "fake.png", content: "<b>not a picture</b>" }, | |
| 77 | { path: "notes.txt", content: "plain" }, | |
| 78 | { path: "bad.pem", content: "-----BEGIN CERTIFICATE-----\nnot a certificate\n-----END CERTIFICATE-----\n" }, | |
| 79 | ], | |
| 80 | }); | |
| 81 | if (!made.ok) throw new Error("commit failed " + JSON.stringify(made.error)); | |
| 82 | // Past the commit limit, so written with git itself. | |
| 83 | addBig(repoDir(app.reposRoot, "alice", "p")); | |
| 84 | fx = await workspaceFixture(); | |
| 85 | const tl = await createRepo(fx.forge, fx.alice, { name: "tl", visibility: "public", description: "t" }); | |
| 86 | if (!tl.ok) throw new Error("repo failed"); | |
| 87 | const long = Array.from({ length: 200 }, (_, i) => `row ${i + 1}`).join("\n"); | |
| 88 | await commitToBranch(fx.forge, fx.alice, tl.value, { message: "seed", files: [{ path: "long.png", content: long }] }); | |
| 89 | addBig(repoDir(fx.forge.reposRoot, "alice", "tl")); | |
| 90 | }, 60_000); | |
| 91 | ||
| 92 | afterAll(async () => { | |
| 93 | await servers?.stop(); | |
| 94 | app?.close(); | |
| 95 | await fx?.close(); | |
| 96 | await rm(dataDir, { recursive: true, force: true }); | |
| 97 | }); | |
| 98 | ||
| 99 | const page = async (path: string) => (await fetch(`${servers.url}/alice/p/blob/main/${path}`)).text(); | |
| 100 | ||
| 101 | test("bytes that do not match: one line, escaped", async () => { | |
| 102 | const body = await page("fake.png"); | |
| 103 | expect(body).toContain(`data-plain-why>its bytes do not look like image</p>`); | |
| 104 | expect(body).not.toContain("<b>not a picture</b>"); | |
| 105 | }); | |
| 106 | test("too large: one line says the size and the limit, and the empty state does not repeat it", async () => { | |
| 107 | const body = await page("big.html"); | |
| 108 | expect(body).toContain(`data-plain-why>too large (2.1 MB; the limit for html is 2 MB)</p>`); | |
| 109 | expect(body.match(/limit for html/g)).toHaveLength(1); | |
| 110 | expect(body).toContain("Download it to read it."); | |
| 111 | }); | |
| 112 | test("a file that falls back and has a reason shows at most one reason line", async () => { | |
| 113 | const body = await page("bad.pem"); | |
| 114 | const lines = (body.match(/data-plain-why>/g) ?? []).length + (body.match(/Showing the source: /g) ?? []).length; | |
| 115 | expect(lines).toBe(1); | |
| 116 | }); | |
| 117 | test("plain text adds no line", async () => { | |
| 118 | expect(await page("notes.txt")).not.toContain("data-plain-why"); | |
| 119 | }); | |
| 120 | ||
| 121 | test("the terminal keeps the line above a binary file too, not as a toast", async () => { | |
| 122 | const made = await createRepo(fx.forge, fx.alice, { name: "tb", visibility: "public", description: "t" }); | |
| 123 | if (!made.ok) throw new Error("repo failed"); | |
| 124 | await commitToBranch(fx.forge, fx.alice, made.value, { message: "seed", files: [{ path: "bin.pcap", content: new Uint8Array([0, 1, 2, 0, 0, 255]) }] }); | |
| 125 | const h = fx.boot(); | |
| 126 | try { | |
| 127 | await h.settle(); | |
| 128 | await fx.open(h, "tb"); | |
| 129 | await fx.openFile(h, "bin.pcap"); | |
| 130 | expect(h.text()).toContain("its bytes do not look like pcap"); | |
| 131 | await h.settle(); | |
| 132 | expect(h.text()).toContain("its bytes do not look like pcap"); | |
| 133 | } finally { | |
| 134 | h.stop(); | |
| 135 | } | |
| 136 | }); | |
| 137 | ||
| 138 | test("the terminal: under the reason row, the wheel scrolls by lines and the rows are the right ones", async () => { | |
| 139 | const h = fx.boot(undefined, 120, 20); | |
| 140 | try { | |
| 141 | await h.settle(); | |
| 142 | await fx.open(h, "tl"); | |
| 143 | await fx.openFile(h, "long.png"); | |
| 144 | const screen = (): string[] => h.text().split("\n"); | |
| 145 | const why = screen().findIndex((r) => r.includes("its bytes do not look like image")); | |
| 146 | expect(why).toBeGreaterThanOrEqual(0); | |
| 147 | // The row under the reason is line 1, not line 2: the code starts one row down, nothing | |
| 148 | // skipped. | |
| 149 | expect(screen()[why + 1]).toMatch(/\b1 row 1\b/); | |
| 150 | h.app.dispatch({ type: "mouse", kind: "wheel-down", button: 0, x: 100, y: why + 3, ctrl: false, alt: false, shift: false }); | |
| 151 | await h.settle(); | |
| 152 | const top = screen()[why + 1]!; | |
| 153 | const n = Number(/(\d+) row (\d+)/.exec(top)?.[1]); | |
| 154 | expect(n).toBeGreaterThan(1); | |
| 155 | expect(top).toContain(`row ${n}`); | |
| 156 | // The reason row holds still, and the last line is reachable: the pane is one row shorter, | |
| 157 | // not cut. | |
| 158 | expect(screen()[why]).toContain("its bytes do not look like image"); | |
| 159 | await h.press("G"); | |
| 160 | expect(h.text()).toContain("row 200"); | |
| 161 | } finally { | |
| 162 | h.stop(); | |
| 163 | } | |
| 164 | }); | |
| 165 | ||
| 166 | test("the terminal says the too-large words once, in the note", async () => { | |
| 167 | const h = fx.boot(); | |
| 168 | try { | |
| 169 | await h.settle(); | |
| 170 | await fx.open(h, "tl"); | |
| 171 | await fx.openFile(h, "big.html"); | |
| 172 | const screen = h.text(); | |
| 173 | expect(screen).toContain("too large (2.1 MB; the limit for html is 2 MB)"); | |
| 174 | expect(screen.match(/limit for html/g)).toHaveLength(1); | |
| 175 | } finally { | |
| 176 | h.stop(); | |
| 177 | } | |
| 178 | }); | |
| 179 | ||
| 180 | test("the terminal says the same words", async () => { | |
| 181 | const made = await createRepo(fx.forge, fx.alice, { name: "tp", visibility: "public", description: "t" }); | |
| 182 | if (!made.ok) throw new Error("repo failed"); | |
| 183 | await commitToBranch(fx.forge, fx.alice, made.value, { message: "seed", files: [{ path: "fake.png", content: "just words" }] }); | |
| 184 | const h = fx.boot(); | |
| 185 | try { | |
| 186 | await h.settle(); | |
| 187 | await fx.open(h, "tp"); | |
| 188 | await fx.openFile(h, "fake.png"); | |
| 189 | expect(h.text()).toContain("its bytes do not look like image"); | |
| 190 | } finally { | |
| 191 | h.stop(); | |
| 192 | } | |
| 193 | }); | |
| 194 | }); |
new above
offline
The extra row shifts drawCode by one; confirm click/scroll hit-testing assumes the old offset and test it. Also long reasons are clipped to the width with no ellipsis.