Public: anyone can read this; the crew can change it.

Seed a .docx, .pptx and .ora fixture repository on previews so the office pages can be looked at #12929

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/12928-seed-a-docx-pptx-and-ora-fixture-reposit → main 4 commits fork 3 threads · 3 unresolved

  • Merged. scout/12928-seed-a-docx-pptx-and-ora-fixture-reposit was merged into main as 8764807.

Review

Sign in to review this change.
4 files changed +293 −0
src/spawn/preview-looks.ts +20
@@ -84,6 +84,25 @@ export const PREVIEW_SEARCH_PAGE = "/search?q=sample";
8484 /** The sample's STEP file, whose part card must fit a 390 px phone. */
8585 export const PREVIEW_STEP_PAGE = `${PREVIEW_REPO_HOME}/blob/main/part.step`;
8686
87 /**
88 * The fixture repository (#12928): a .docx, a .pptx and an .ora, each with a
outdated

The comment promises rows with no card and a 360 px column. Nothing checks either, and the phone look is 390 px. Reword to what is actually verified.

89 * desk and a phone look. The seed refuses to finish unless each page answers
90 * 200 and its first-paint HTML holds the marker below, drawn by the office
91 * renderer from the fixture (a plain file page holds none of them).
92 */
93 export const PREVIEW_FIXTURES = "fixtures";
94 export const PREVIEW_FIXTURES_HOME = `/${PREVIEW_ADMIN}/${PREVIEW_FIXTURES}`;
95 export const PREVIEW_OFFICE_FILES: readonly string[] = ["notes.docx", "deck.pptx", "sketch.ora"];
96 export const PREVIEW_OFFICE_PAGES: readonly string[] = PREVIEW_OFFICE_FILES.map(
97 (file) => `${PREVIEW_FIXTURES_HOME}/blob/main/${file}`,
98 );
99 /** A string each office page must hold when its renderer drew the fixture. */
100 export const PREVIEW_OFFICE_MARKERS: Readonly<Record<string, string>> = {
101 "notes.docx": "Heading one",
102 "deck.pptx": "Welcome to the forge",
103 "sketch.ora": "Stripe",
104 };
105
87106 /** The waves page, whose long headlines must wrap at a 390 px phone (#9325). */
88107 export const PREVIEW_WAVES_PAGE = `${PREVIEW_REPO_HOME}/waves`;
89108
@@ -191,6 +210,7 @@ export const PREVIEW_OWNER_LOOKS: readonly OwnerLook[] = [
191210 PREVIEW_MILESTONES_PAGE,
192211 PREVIEW_WAVES_PAGE,
193212 PREVIEW_STEP_PAGE,
213 ...PREVIEW_OFFICE_PAGES,
194214 PREVIEW_PULLS_PAGE,
195215 PREVIEW_CHANGELOG_PAGE,
196216 PREVIEW_NEXT_PAGE,
src/spawn/preview-office.ts added +147
@@ -0,0 +1,147 @@
1 /**
2 * The fixture repository's office files (#12928): a .docx, a .pptx and an
3 * .ora, built in memory so no binary sits in the source tree. Each is small
4 * and holds the worst case its page must draw: a document with headings and
5 * body text, a deck of three slides one of which has no title, and a layered
6 * image whose canvas (640 px) is wider than a 360 px phone column.
7 *
8 * Plain data out: the seed commits the bytes, and the looks name the pages.
9 */
10
11 import { deflateRawSync } from "node:zlib";
12 import { encodePng } from "../design/image/png.ts";
13
14 /** The .ora canvas: wider than a 360 px phone, so the merged preview must shrink to fit. */
15 export const ORA_WIDTH = 640;
16 export const ORA_HEIGHT = 200;
17
18 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
19
20 /** A zip of named parts in order, each deflated, with real CRCs so any reader accepts it. */
21 export function zipParts(parts: readonly (readonly [string, string | Uint8Array])[]): Uint8Array {
22 const out: Uint8Array[] = [];
23 const central: Uint8Array[] = [];
24 let offset = 0;
25 for (const [name, data] of parts) {
26 const raw = typeof data === "string" ? enc(data) : data;
27 const packed = new Uint8Array(deflateRawSync(raw));
28 const crc = Bun.hash.crc32(raw);
29 const n = enc(name);
30 const local = new Uint8Array(30 + n.length);
31 const lv = new DataView(local.buffer);
32 lv.setUint32(0, 0x04034b50, true);
33 lv.setUint16(4, 20, true);
34 lv.setUint16(8, 8, true);
35 lv.setUint32(14, crc, true);
36 lv.setUint32(18, packed.length, true);
37 lv.setUint32(22, raw.length, true);
38 lv.setUint16(26, n.length, true);
39 local.set(n, 30);
40 const head = new Uint8Array(46 + n.length);
41 const cv = new DataView(head.buffer);
42 cv.setUint32(0, 0x02014b50, true);
43 cv.setUint16(4, 20, true);
44 cv.setUint16(6, 20, true);
45 cv.setUint16(10, 8, true);
46 cv.setUint32(16, crc, true);
47 cv.setUint32(20, packed.length, true);
48 cv.setUint32(24, raw.length, true);
49 cv.setUint16(28, n.length, true);
50 cv.setUint32(42, offset, true);
51 head.set(n, 46);
52 out.push(local, packed);
53 central.push(head);
54 offset += local.length + packed.length;
55 }
56 const size = central.reduce((a, c) => a + c.length, 0);
57 const end = new Uint8Array(22);
58 const ev = new DataView(end.buffer);
59 ev.setUint32(0, 0x06054b50, true);
60 ev.setUint16(8, central.length, true);
61 ev.setUint16(10, central.length, true);
62 ev.setUint32(12, size, true);
63 ev.setUint32(16, offset, true);
64 return new Uint8Array(Buffer.concat([...out, ...central, end]));
65 }
66
67 const W = 'xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"';
68 const para = (text: string, style?: string): string =>
69 `<w:p>${style ? `<w:pPr><w:pStyle w:val="${style}"/></w:pPr>` : ""}<w:r><w:t>${text}</w:t></w:r></w:p>`;
70
71 /** A document: a title, two headings and body text under them. */
72 export function buildDocx(): Uint8Array {
73 const body = [
74 para("Field notes", "Title"),
75 para("Heading one", "Heading1"),
76 para("The forge keeps every file as plain rows, so a document reads as an outline."),
77 para("Heading two", "Heading2"),
78 para("A second paragraph, under the second heading, long enough to wrap on a phone: the quick brown fox jumps over the lazy dog."),
79 para("Closing", "Heading1"),
80 para("That is all."),
81 ].join("");
82 return zipParts([
83 ["[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>`],
84 ["_rels/.rels", `<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>`],
85 ["word/document.xml", `<?xml version="1.0" encoding="UTF-8"?><w:document ${W}><w:body>${body}</w:body></w:document>`],
86 ]);
87 }
88
89 const slide = (title: string | undefined): string =>
90 `<?xml version="1.0" encoding="UTF-8"?><p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:cSld><p:spTree>` +
91 (title === undefined
92 ? ""
93 : `<p:sp><p:nvSpPr><p:cNvPr id="2" name="Title 1"/><p:cNvSpPr/><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><p:spPr/><p:txBody><a:bodyPr/><a:p><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp>`) +
94 `<p:sp><p:nvSpPr><p:cNvPr id="3" name="Body 2"/><p:cNvSpPr/><p:nvPr><p:ph idx="1"/></p:nvPr></p:nvSpPr><p:spPr/><p:txBody><a:bodyPr/><a:p><a:r><a:t>Some body text</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
95
96 /** A deck of three slides; the second has no title. */
97 export function buildPptx(): Uint8Array {
98 const ids = [1, 2, 3];
99 const rel = (n: number): string =>
100 `<Relationship Id="rId${String(n)}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide${String(n)}.xml"/>`;
101 return zipParts([
102 ["[Content_Types].xml", `<?xml version="1.0" encoding="UTF-8"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="xml" ContentType="application/xml"/><Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/></Types>`],
103 ["_rels/.rels", `<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/></Relationships>`],
104 ["ppt/presentation.xml", `<?xml version="1.0" encoding="UTF-8"?><p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst>${ids.map((n) => `<p:sldId id="${String(255 + n)}" r:id="rId${String(n)}"/>`).join("")}</p:sldIdLst></p:presentation>`],
105 ["ppt/_rels/presentation.xml.rels", `<?xml version="1.0" encoding="UTF-8"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">${ids.map(rel).join("")}</Relationships>`],
106 ["ppt/slides/slide1.xml", slide("Welcome to the forge")],
107 ["ppt/slides/slide2.xml", slide(undefined)],
108 ["ppt/slides/slide3.xml", slide("What happens next")],
109 ]);
110 }
111
112 /** A solid-colour RGBA PNG of `w` × `h`. */
113 function solid(w: number, h: number, [r, g, b, a]: readonly [number, number, number, number]): Uint8Array {
114 const px = new Uint8Array(w * h * 4);
115 for (let i = 0; i < px.length; i += 4) {
116 px[i] = r;
117 px[i + 1] = g;
118 px[i + 2] = b;
119 px[i + 3] = a;
120 }
121 return encodePng(w, h, px);
122 }
123
124 /** A layered image: two layers and a merged preview, 640 × 200. */
125 export function buildOra(): Uint8Array {
126 const background = solid(ORA_WIDTH, ORA_HEIGHT, [40, 90, 160, 255]);
127 const stripe = solid(320, 80, [240, 180, 40, 255]);
128 const merged = solid(ORA_WIDTH, ORA_HEIGHT, [40, 90, 160, 255]);
129 const stack = `<?xml version="1.0" encoding="UTF-8"?><image version="0.0.3" w="${String(ORA_WIDTH)}" h="${String(ORA_HEIGHT)}"><stack><layer name="Stripe" src="data/stripe.png" x="160" y="60" visibility="visible"/><layer name="Background" src="data/background.png" x="0" y="0" visibility="visible"/></stack></image>`;
130 return zipParts([
131 ["mimetype", "image/openraster"],
132 ["stack.xml", stack],
133 ["data/background.png", background],
134 ["data/stripe.png", stripe],
135 ["mergedimage.png", merged],
136 ]);
137 }
138
139 /** The fixture repository's files, in the order they are committed. */
140 export function officeFixtures(): readonly { readonly path: string; readonly content: Uint8Array }[] {
141 return [
142 { path: "README.md", content: enc("# fixtures\n\nOffice files a preview's file pages are looked at on: a .docx, a .pptx and an .ora.\n") },
143 { path: "notes.docx", content: buildDocx() },
144 { path: "deck.pptx", content: buildPptx() },
145 { path: "sketch.ora", content: buildOra() },
146 ];
147 }
src/spawn/preview-seed.ts +49
@@ -54,17 +54,21 @@ import { PREVIEW_SEEDED, getUserSetting, setUserSetting } from "../core/settings
5454 import { createUser, getUserByName, type User } from "../core/users.ts";
5555 import { diskSummary, recordReading } from "../machines/disk_readings.ts";
5656 import { err, ok, type Result } from "../kernel/result.ts";
57 import { officeFixtures } from "./preview-office.ts";
5758 import { PREVIEW_CONFIG_FILES, PREVIEW_STEP_FILE } from "./preview-config.ts";
5859 import {
5960 PREVIEW_ADMIN,
6061 PREVIEW_APP,
6162 PREVIEW_APP_PAGE,
6263 PREVIEW_DISK_ALERT,
64 PREVIEW_FIXTURES,
6365 PREVIEW_FORKS,
6466 PREVIEW_HELD_PAGE,
6567 PREVIEW_HELD_THEMES,
6668 PREVIEW_ITERATIONS_PAGE,
6769 PREVIEW_MACHINES_PAGE,
70 PREVIEW_OFFICE_MARKERS,
71 PREVIEW_OFFICE_PAGES,
6872 PREVIEW_PULL,
6973 PREVIEW_PULL_PAGE,
7074 PREVIEW_MOVED,
@@ -215,6 +219,7 @@ const PREVIEW_AGENT = "preview-agent";
215219 const PREVIEW_TOKEN = "preview fake token, not a credential";
216220 const SEEDED = PREVIEW_SEEDED;
217221 const CONFIG_SEEDED = "preview_config_seeded";
222 const OFFICE_SEEDED = "preview_office_seeded";
218223 const SETTINGS_SEEDED = "preview_settings_seeded";
219224
220225 type Env = Record<string, string | undefined>;
@@ -328,6 +333,8 @@ export async function seedPreview(
328333 if (!disks.ok) return disks;
329334 const files = await seedConfigFiles(app, admin);
330335 if (!files.ok) return files;
336 const office = await seedOfficeFixtures(app, admin);
337 if (!office.ok) return office;
331338 const newcomer = await seedNewcomer(app);
332339 if (!newcomer.ok) return newcomer;
333340 seedGrace(app);
@@ -344,6 +351,8 @@ export async function seedPreview(
344351 if (!disks.ok) return disks;
345352 const files = await seedConfigFiles(app, admin);
346353 if (!files.ok) return files;
354 const office = await seedOfficeFixtures(app, admin);
355 if (!office.ok) return office;
347356 const newcomer = await seedNewcomer(app);
348357 if (!newcomer.ok) return newcomer;
349358 seedGrace(app);
@@ -443,6 +452,46 @@ async function seedConfigFiles(app: App, admin: User): Promise<Result<void, stri
443452 return ok(undefined);
444453 }
445454
455 /**
456 * The fixture repository, once (#12928): a .docx, a .pptx and an .ora, so
457 * their file pages can be looked at. A sample seeded before it existed gets
458 * it on its next boot. Refused unless every file page answers 200 and holds its marker.
459 */
460 async function seedOfficeFixtures(app: App, admin: User): Promise<Result<void, string>> {
461 if (getUserSetting(app.db, admin.id, OFFICE_SEEDED) !== undefined) return ok(undefined);
462 if (getRepo(app.db, admin.username, PREVIEW_FIXTURES) === undefined) {
463 const made = await createRepo(app, admin, {
464 name: PREVIEW_FIXTURES,
465 visibility: "public",
466 description: "Office files to look at the file pages on",
467 });
468 if (!made.ok) return err(`the fixture repository: ${String(made.error)}`);
469 const added = await commitToBranch(app, admin, made.value, {
470 message: "A .docx, a .pptx and an .ora",
471 files: [...officeFixtures()],
472 });
473 if (!added.ok) return err(`the fixture files: ${JSON.stringify(added.error)}`);
474 }
475 const http = forgeHttp(app);
476 const session = createSession(app.db, admin.id);
477 try {
478 for (const page of PREVIEW_OFFICE_PAGES) {
479 const res = await http.handle(
480 new Request(`http://localhost${page}`, { headers: { cookie: `forge_session=${session.token}` } }),
481 );
482 if (res.status !== 200) return err(`${page} answers ${String(res.status)}, not 200`);
483 const marker = PREVIEW_OFFICE_MARKERS[page.slice(page.lastIndexOf("/") + 1)];
outdated

Checking only status !== 200 does not verify the office renderers drew. Also assert a marker from the body, for example the heading text for the docx, the slide title for the pptx, and a layer name for the ora.

484 if (marker === undefined || !(await res.text()).includes(marker))
485 return err(`${page} does not hold "${String(marker)}": the office renderer did not draw it`);
486 }
487 } finally {
488 destroySession(app.db, session.token);
489 http.close();
490 }
491 setUserSetting(app.db, admin.id, OFFICE_SEEDED, new Date().toISOString());
492 return ok(undefined);
493 }
494
446495 const panel = (title: string, source: string, groupBy: string) => ({
447496 title,
448497 chart: "bar",
test/preview-office.test.ts added +77
@@ -0,0 +1,77 @@
1 /** The fixture repository (#12928): office files on a preview, each with desk and phone looks. */
2 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { PREVIEW_OFFICE_MARKERS, PREVIEW_OFFICE_PAGES, PREVIEW_OWNER_LOOKS, PREVIEW_ADMIN, PREVIEW_FIXTURES } from "../src/spawn/preview-looks.ts";
8 import { ORA_WIDTH, buildDocx, buildOra, buildPptx } from "../src/spawn/preview-office.ts";
9 import { previewAdmin, seedPreview } from "../src/spawn/preview-seed.ts";
10 import { zipEntry } from "../src/web/render/archive.ts";
11 import { pngHeader } from "../src/design/image/png.ts";
12 import { serve, type Servers } from "../src/http/server.ts";
13 import { createSession } from "../src/core/sessions.ts";
14
15 const text = (b: Uint8Array | null): string => new TextDecoder().decode(b ?? undefined);
outdated

Duplicate import from preview-seed.ts (line 11 imports seedPreview from the same module). Merge them.

16
17 test("every office page has a desk and a phone look", () => {
18 expect(PREVIEW_OFFICE_PAGES).toEqual(
19 ["notes.docx", "deck.pptx", "sketch.ora"].map((f) => `/${PREVIEW_ADMIN}/${PREVIEW_FIXTURES}/blob/main/${f}`),
20 );
21 for (const page of PREVIEW_OFFICE_PAGES)
22 for (const kind of ["desktop", "phone"] as const)
23 expect(PREVIEW_OWNER_LOOKS).toContainEqual({ page, kind });
24 });
25
26 describe("the fixtures hold what their pages must draw", () => {
27 test("the docx has headings and text", () => {
28 const xml = text(zipEntry(buildDocx(), "word/document.xml", 1e6));
29 expect(xml).toContain('w:val="Heading1"');
30 expect(xml).toContain("Field notes");
31 });
32 test("the pptx has three slides, one untitled", () => {
33 const deck = buildPptx();
34 const slides = [1, 2, 3].map((n) => text(zipEntry(deck, `ppt/slides/slide${String(n)}.xml`, 1e6)));
35 expect(slides.filter((x) => x.includes('type="title"'))).toHaveLength(2);
36 expect(slides[1]).not.toContain('type="title"');
37 });
38 test("the ora has two layers and a merged preview wider than a phone", () => {
39 const ora = buildOra();
40 const stack = text(zipEntry(ora, "stack.xml", 1e6));
41 expect(stack.match(/<layer /g)).toHaveLength(2);
42 const header = pngHeader(zipEntry(ora, "mergedimage.png", 1e6)!);
43 if (!header.ok) throw new Error("not a png");
44 expect(header.value.width).toBe(ORA_WIDTH);
45 expect(ORA_WIDTH).toBeGreaterThan(390);
46 });
47 });
48
49 describe("the seeded fixture repository", () => {
50 let dir: string;
51 let app: App;
52 let servers: Servers;
53 beforeAll(async () => {
54 dir = await mkdtemp(join(tmpdir(), "forge-preview-office-"));
55 app = createApp({ dataDir: dir, port: 0 });
56 const seeded = await seedPreview(app, { FORGE_APP_ENV: "preview" });
57 if (!seeded.ok) throw new Error(seeded.error);
58 servers = await serve(app);
59 }, 60_000);
60 afterAll(async () => {
61 await servers.stop();
62 await rm(dir, { recursive: true, force: true });
63 });
64 test("each file page answers 200 as the owner and a second seeding changes nothing", async () => {
65 const admin = previewAdmin(app)!;
66 const cookie = `forge_session=${createSession(app.db, admin.id).token}`;
67 for (const page of PREVIEW_OFFICE_PAGES) {
68 const res = await fetch(`${servers.url}${page}`, { headers: { cookie } });
69 expect(res.status).toBe(200);
70 const html = await res.text();
71 expect(html).toContain(PREVIEW_OFFICE_MARKERS[page.slice(page.lastIndexOf("/") + 1)]!);
72 if (page.endsWith(".ora")) expect(html).toContain("<img");
73 }
74 const again = await seedPreview(app, { FORGE_APP_ENV: "preview" });
75 expect(again.ok).toBe(true);
76 });
77 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs