| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { arrange, formatBytes, installCommands, shortCount, shortDigest } from "./packages.ts"; |
| 5 | |
| 6 | test("sizes read as registries show them", () => { |
| 7 | assert.equal(formatBytes(0), "0 B"); |
| 8 | assert.equal(formatBytes(812), "812 B"); |
| 9 | assert.equal(formatBytes(12_340_000), "12.3 MB"); |
| 10 | assert.equal(formatBytes(1_400_000_000), "1.4 GB"); |
| 11 | assert.equal(formatBytes(250_000_000), "250 MB"); |
| 12 | }); |
| 13 | |
| 14 | test("digests shorten for lists", () => { |
| 15 | assert.equal(shortDigest("sha256:3f2a9c1b7d0e55aa"), "3f2a9c1b7d0e"); |
| 16 | }); |
| 17 | |
| 18 | test("an npm package is installed after .npmrc names its scope's registry, and a token for private ones", () => { |
| 19 | const pkg = { ecosystem: "npm" as const, address: "g1t.sh/-/npm/@acme/ui", name: "ui", workspace: "acme" }; |
| 20 | assert.deepEqual(installCommands(pkg, "1.2.0", "ada"), { |
| 21 | registry: "npm config set @acme:registry=https://g1t.sh/-/npm/", |
| 22 | login: "npm config set //g1t.sh/-/npm/:_authToken=YOUR_TOKEN", |
| 23 | install: "npm install @acme/ui@1.2.0", |
| 24 | }); |
| 25 | assert.equal(installCommands(pkg, null, "ada").install, "npm install @acme/ui"); |
| 26 | }); |
| 27 | |
| 28 | test("a Composer package is required after its workspace's repository is added, with credentials for private ones", () => { |
| 29 | const pkg = { ecosystem: "composer" as const, address: "g1t.sh/-/composer/acme/acme/lib", name: "acme/lib", workspace: "acme" }; |
| 30 | assert.deepEqual(installCommands(pkg, "v1.1.0", "ada"), { |
| 31 | registry: "composer config repositories.acme composer https://g1t.sh/-/composer/acme/", |
| 32 | login: "composer config --global --auth http-basic.g1t.sh ada YOUR_TOKEN", |
| 33 | install: "composer require acme/lib:v1.1.0", |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | test("a container image is pulled by its address and tag", () => { |
| 38 | const pkg = { ecosystem: "container" as const, address: "g1t.sh/acme/web", name: "web", workspace: "acme" }; |
| 39 | assert.deepEqual(installCommands(pkg, "latest", "ada"), { |
| 40 | login: "docker login g1t.sh -u ada", |
| 41 | install: "docker pull g1t.sh/acme/web:latest", |
| 42 | }); |
| 43 | assert.equal(installCommands(pkg, null, "ada").install, "docker pull g1t.sh/acme/web"); |
| 44 | }); |
| 45 | |
| 46 | test("packages are listed by the visibility and order asked for", () => { |
| 47 | const list = [ |
| 48 | { name: "web", visibility: "private" as const, updated_at: "2026-10-01T00:00:00Z", downloads: 5 }, |
| 49 | { name: "api", visibility: "public" as const, updated_at: "2026-10-03T00:00:00Z", downloads: 9 }, |
| 50 | { name: "cli", visibility: "public" as const, updated_at: "2026-10-02T00:00:00Z", downloads: 9 }, |
| 51 | ]; |
| 52 | assert.deepEqual(arrange(list, "all", "updated").map((p) => p.name), ["api", "cli", "web"]); |
| 53 | assert.deepEqual(arrange(list, "all", "downloads").map((p) => p.name), ["api", "cli", "web"]); |
| 54 | assert.deepEqual(arrange(list, "all", "name").map((p) => p.name), ["api", "cli", "web"]); |
| 55 | assert.deepEqual(arrange(list, "private", "updated").map((p) => p.name), ["web"]); |
| 56 | assert.deepEqual(arrange(list, "public", "name").map((p) => p.name), ["api", "cli"]); |
| 57 | }); |
| 58 | |
| 59 | test("download counts read short", () => { |
| 60 | assert.equal(shortCount(940), "940"); |
| 61 | assert.equal(shortCount(16_100), "16.1k"); |
| 62 | assert.equal(shortCount(2_000_000), "2M"); |
| 63 | }); |