.*?<\/p>/g), ['
', '']) } }) // Top-level MP4 boxes of [start, end): { type, start, end } with start at the size field. function mp4Boxes(bytes, start = 0, end = bytes.length) { const boxes = [] for (let offset = start; offset + 8 <= end;) { const size = bytes.readUInt32BE(offset) assert.ok(size >= 8 && offset + size <= end, "valid MP4 box size") boxes.push({ type: bytes.toString("latin1", offset + 4, offset + 8), start: offset, end: offset + size }) offset += size } return boxes } test("the tour clip is a silent faststart H.264 4:2:0 loop of 40-50 s at 1280x720 within 3.5 MB, and its poster a 1280x720 JPEG within 200 KB", async () => { const video = await readFile(resolve(root, "docs/screenshots/codenomad-tour.mp4")) assert.ok(video.length <= 3_500_000, `clip is ${video.length} bytes`) const boxes = mp4Boxes(video) assert.equal(boxes[0].type, "ftyp") const moov = boxes.find((box) => box.type === "moov") assert.ok(moov && boxes.indexOf(moov) < boxes.findIndex((box) => box.type === "mdat"), "faststart: metadata before media data") const tracks = mp4Boxes(video, moov.start + 8, moov.end).filter((box) => box.type === "trak") assert.equal(tracks.length, 1, "one track, no audio") const trak = video.subarray(tracks[0].start, tracks[0].end) const hdlr = trak.indexOf("hdlr") assert.equal(trak.toString("latin1", hdlr + 12, hdlr + 16), "vide") const tkhd = trak.indexOf("tkhd") + 4 const dimensions = tkhd + (trak[tkhd] === 1 ? 88 : 76) assert.deepEqual([trak.readUInt32BE(dimensions) / 65536, trak.readUInt32BE(dimensions + 4) / 65536], [1280, 720]) const avcC = trak.indexOf("avcC") + 4 assert.ok(trak.indexOf("avc1") > 0 && avcC > 4, "H.264") assert.ok([66, 77, 100].includes(trak[avcC + 1]), "Baseline, Main or High profile: 4:2:0 chroma") const mvhd = video.indexOf("mvhd", moov.start) + 4 assert.equal(video[mvhd], 0) const seconds = video.readUInt32BE(mvhd + 16) / video.readUInt32BE(mvhd + 12) assert.ok(seconds >= 40 && seconds <= 50, `clip lasts ${seconds} s`) const poster = await readFile(resolve(root, "docs/screenshots/codenomad-tour-poster.jpg")) assert.ok(poster.length <= 200_000, `poster is ${poster.length} bytes`) assert.equal(poster.readUInt16BE(0), 0xffd8, "JPEG") let offset = 2 while (offset < poster.length && ![0xc0, 0xc1, 0xc2].includes(poster[offset + 1])) offset += 2 + poster.readUInt16BE(offset + 2) assert.deepEqual([poster.readUInt16BE(offset + 7), poster.readUInt16BE(offset + 5)], [1280, 720], "poster matches the 16:9 video box") }) const LATEST_RELEASE = "https://github.com/NeuralNomadsAI/CodeNomad/releases/latest" const releaseFixture = JSON.parse(await readFile(resolve(root, "scripts/fixtures/help-release.json"), "utf8")) const downloads = JSON.parse(await readFile(resolve(root, "docs/help/downloads.json"), "utf8")) const megabytes = (locale, bytes) => new Intl.NumberFormat(locale, { style: "unit", unit: "megabyte", maximumFractionDigits: 0 }).format(Math.round(bytes / 1e6)) const chromeFor = async (locale) => JSON.parse(await readFile(resolve(root, `docs/help/i18n/${locale}.json`), "utf8")) // The install page of the generic fixture gets a release line and one link per downloads.json asset. async function releaseFixturePages(ctx) { const paths = await fixture(ctx) const links = downloads.map((entry) => `[${entry.id}](release:${entry.id})`).join(" · ") for (const locale of LOCALES) { await writeFile(join(paths.source, locale, "install.md"), `# ${locale} install\n\n{{release}}\n\nLead paragraph.\n\n## Desktop {#desktop}\n\n${links}\n`) } return paths } test("release links use resolved asset URLs with localized sizes, and the latest release page without data", async (ctx) => { const { source, output } = await releaseFixturePages(ctx) assert.deepEqual((await buildHelp({ content: source, output, release: releaseFixture })).problems, []) for (const locale of LOCALES) { await checkHtml(output, locale, "install", LOCALES) const html = await readFile(join(output, locale, "install.html"), "utf8") for (const entry of downloads) { const asset = releaseFixture.assets[entry.id] assert.ok(html.includes(`${entry.id} ${escapeHtml(megabytes(locale, asset.size))}`), `${locale}: ${entry.id}`) } const date = new Intl.DateTimeFormat(locale, { dateStyle: "long", timeZone: "UTC" }).format(new Date(releaseFixture.publishedAt)) const line = escapeHtml((await chromeFor(locale)).release.published).replace("{version}", releaseFixture.version).replace("{date}", escapeHtml(date)) assert.equal(html.split(`${line}
`).length, 2, `${locale}: one release line`) assert.equal(html.split(escapeHtml(date)).length, 2, `${locale}: the date appears once`) assert.ok(html.includes(''), "the generated line never becomes the description") } assert.deepEqual((await buildHelp({ content: source, output })).problems, []) for (const locale of LOCALES) { const html = await readFile(join(output, locale, "install.html"), "utf8") for (const entry of downloads) assert.ok(html.includes(`${entry.id}`), `${locale}: ${entry.id}`) assert.ok(!html.includes("download-size") && !html.includes("/releases/download/"), "no asset URL or size without release data") assert.ok(html.includes(`${escapeHtml((await chromeFor(locale)).release.unavailable)}
`)) } }) test("directive and release-link problems are fatal: unknown, misplaced, duplicate and drifting across locales", async (ctx) => { const { source, output } = await releaseFixturePages(ctx) const file = join(source, "en/install.md"), original = await readFile(file, "utf8") for (const [kind, markdown] of [ ["unknown-release-asset", original.replace("(release:tauri-windows-x64)", "(release:tauri-windows-arm64)")], ["unknown-directive", original.replace("Lead paragraph.", "{{donate}}")], ["unknown-directive", original.replace("Lead paragraph.", "{{ release }}")], ["unknown-directive", original.replace("Lead paragraph.", "Before {{release}}")], ["unknown-directive", original.replace("Lead paragraph.", "{{release")], ["unknown-directive", `${original}\n- {{release}}\n\n- loose item\n`], ["unknown-directive", `${original}\n| Release |\n| - |\n| {{release}} |\n`], ["unknown-directive", `${original}\n> {{release}}\n`], ["duplicate-directive", `${original}\n{{release}}\n`], ["misplaced-directive", original.replace("Lead paragraph.", "{{download-button}}")], ["misplaced-directive", original.replace("Lead paragraph.", "{{tour-video}}")], ["misplaced-directive", original.replace("Lead paragraph.", "{{funding}}")], ]) { await writeFile(file, markdown) const { problems } = await buildHelp({ content: source, output }) assert.ok(problems.some((problem) => problem.kind === kind && problem.locale === "en" && problem.page === "install"), `${kind}: ${markdown}`) assert.ok(!TRANSLATION_PROBLEMS.has(kind)) } await writeFile(file, `${original}\nWrite \`{{release}}\` on a line of its own:\n\n\`\`\`md\n{{release}}\n{{anything}}\n\`\`\`\n\n {{indented}}\n`) const french = join(source, "fr/install.md") await writeFile(french, `${await readFile(french, "utf8")}\n\`{{release}}\`\n`) assert.deepEqual((await buildHelp({ content: source, output })).problems, [], "code samples are never directives") await writeFile(file, original) await writeFile(french, original.replace("{{release}}\n\n", "")) const { problems } = await buildHelp({ content: source, output }) assert.deepEqual(problems.map(({ kind, locale, page }) => ({ kind, locale, page })), [{ kind: "directive-mismatch", locale: "fr", page: "install" }]) }) test("release data is revalidated: exact repository download URLs, positive sizes, valid tag and UTC date", async (ctx) => { const { source, output } = await fixture(ctx) const base = "https://github.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/" const name = "CodeNomad-Tauri-windows-x64-1.0.0.exe" const changed = (change) => { const value = structuredClone(releaseFixture); change(value.assets["tauri-windows-x64"], value); return value } const invalid = [ {}, [], "v1.0.0", ...[ `http://github.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://github.com.evil.example/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://evil.example/github.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://user:token@github.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://github.com:443/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://GitHub.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, `https://github.com/neuralnomadsai/codenomad/releases/download/v1.0.0/${name}`, `https://github.com/Other/CodeNomad/releases/download/v1.0.0/${name}`, `${base}${name}?download=1`, `${base}${name}#asset`, ` ${base}${name}`, `${base}../v0.9.0/${name}`, `${base}..%2Fv0.9.0%2F${name}`, `${base}%43odeNomad-Tauri-windows-x64-1.0.0.exe`, `https://github.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.1/${name}`, `${base}CodeNomad-Tauri-windows-x64-1.0.1.exe`, `${base}${name}.sig`, `${base}CodeNomad-Electron-windows-x64-1.0.0.zip`, `https://objects.githubusercontent.com/NeuralNomadsAI/CodeNomad/releases/download/v1.0.0/${name}`, "javascript:alert(1)", 42, ].map((url) => changed((asset) => { asset.url = url })), ...[0, -1, 1.5, "60272894", Number.MAX_SAFE_INTEGER + 1, Number.NaN, Infinity, null].map((size) => changed((asset) => { asset.size = size })), changed((asset) => { asset.name = name }), changed((asset, release) => { delete release.assets["electron-linux-x64"] }), changed((asset, release) => { release.assets["tauri-linux-arm64"] = { ...asset } }), changed((asset, release) => { release.assets["tauri-macos-x64"] = release.assets["tauri-macos-arm64"] }), changed((asset, release) => { release.version = "1.0.1" }), changed((asset, release) => { release.tag = "1.0.0" }), changed((asset, release) => { release.tag = "v1.0.0-beta.1"; release.version = "1.0.0-beta.1" }), changed((asset, release) => { release.tag = "v1.0.0" }), ] for (const release of invalid) { await assert.rejects(buildHelp({ content: source, output, release }), /release data:/, JSON.stringify(release)?.slice(0, 300)) } const valid = validateRelease(releaseFixture, await readDownloads()) assert.equal(valid.version, "1.0.0") assert.equal([...valid.assets.keys()].join(), downloads.map((entry) => entry.id).join(), "the fixture covers every downloads.json asset") }) test("downloads.json lists the stable assets in display order and rejects unsafe templates", async (ctx) => { assert.deepEqual(downloads.map((entry) => entry.id), ["tauri-windows-x64", "tauri-macos-arm64", "tauri-macos-x64", "tauri-linux-x64", "electron-windows-x64", "electron-macos-arm64", "electron-macos-x64", "electron-linux-x64"], "Tauri is the recommended build and comes first") const directory = await temporary(ctx) const entry = downloads[0] for (const manifest of [ [], {}, [{ ...entry }, { ...entry }], [{ ...entry, extra: 1 }], [{ ...entry, id: "Tauri Windows" }], [{ ...entry, platform: "android" }], [{ ...entry, host: "web" }], [{ ...entry, arch: "ia32" }], [{ ...entry, template: "CodeNomad.exe" }], [{ ...entry, template: "{version}-{version}.exe" }], [{ ...entry, template: "../CodeNomad-{version}.exe" }], [{ ...entry, template: "CodeNomad {version}.exe" }], [entry, { ...entry, id: "other" }], ]) { await writeFile(join(directory, "downloads.json"), JSON.stringify(manifest)) await assert.rejects(readDownloads(directory), /downloads\.json/, JSON.stringify(manifest)) } }) // The generic fixture's sponsor page gets a channel section, and its download page a support section. async function fundingFixturePages(ctx) { const paths = await fixture(ctx) for (const locale of LOCALES) { await writeFile(join(paths.source, locale, "sponsor.md"), `# ${locale} sponsor\n\nLead paragraph.\n\n## Channels {#channels}\n\n{{funding}}\n`) const download = join(paths.source, locale, "download.md") await writeFile(download, `${await readFile(download, "utf8")}\n{{download-button}}\n\n## Support development {#support-development}\n\nRead the [sponsor page](sponsor.md).\n\n{{funding}}\n`) } return paths } test("funding channels render in order and escaped on the sponsor and download pages, with the header Sponsor link", async (ctx) => { const { source, output } = await fundingFixturePages(ctx) const channels = [...funding, { id: "second", name: `Fund & "x"`, url: "https://github.com/sponsors/Shantur-R", recipient: "R & D- [\s\S]*?<\/ul>/)
for (const [page, original, changed, pattern] of [
["install", install, install.replace("
