| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtempSync, mkdirSync, readFileSync, symlinkSync, writeFileSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import test from "node:test"; |
| 6 | import { generateReport, inspectTree } from "./size-report.mjs"; |
| 7 | |
| 8 | test("size report classifies files, duplicates and links without following links", () => { |
| 9 | const root = mkdtempSync(join(tmpdir(), "reasonix-size-report-")); |
| 10 | mkdirSync(join(root, "bundle", "service"), { recursive: true }); |
| 11 | writeFileSync(join(root, "bundle", "service", "reasonix-desktop"), "service"); |
| 12 | writeFileSync(join(root, "bundle", "reasonix"), "same"); |
| 13 | writeFileSync(join(root, "bundle", "copy"), "same"); |
| 14 | symlinkSync("service/reasonix-desktop", join(root, "bundle", "service-link")); |
| 15 | mkdirSync(join(root, "dist")); |
| 16 | writeFileSync(join(root, "dist", "Reasonix-linux-amd64.tar.gz"), "archive"); |
| 17 | |
| 18 | const tree = inspectTree(join(root, "bundle")); |
| 19 | assert.equal(tree.fileCount, 3); |
| 20 | assert.equal(tree.symlinks[0].target, "service/reasonix-desktop"); |
| 21 | assert.equal(tree.duplicates.length, 1); |
| 22 | |
| 23 | const report = generateReport({ |
| 24 | platform: "linux/amd64", version: "v1.2.3", sourceSHA: "a".repeat(40), |
| 25 | bundle: join(root, "bundle"), dist: join(root, "dist"), output: join(root, "out"), |
| 26 | }); |
| 27 | assert.equal(report.artifacts[0].name, "Reasonix-linux-amd64.tar.gz"); |
| 28 | assert.match(readFileSync(join(root, "out", "size-report.md"), "utf8"), /Bundle categories/); |
| 29 | |
| 30 | const baseline = join(root, "baseline.json"); |
| 31 | writeFileSync(baseline, JSON.stringify(report)); |
| 32 | const compared = generateReport({ |
| 33 | platform: "linux/amd64", version: "v1.2.4", sourceSHA: "b".repeat(40), |
| 34 | bundle: join(root, "bundle"), dist: join(root, "dist"), output: join(root, "compared"), baseline, |
| 35 | }); |
| 36 | assert.equal(compared.comparison.downloadBytesDelta, 0); |
| 37 | assert.equal(compared.comparison.bundleLogicalBytesDelta, 0); |
| 38 | }); |
| 39 |