| 1 | import assert from 'node:assert/strict'; |
| 2 | import { createHash } from 'node:crypto'; |
| 3 | import { mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | import { tmpdir } from 'node:os'; |
| 5 | import { join } from 'node:path'; |
| 6 | import test from 'node:test'; |
| 7 | import { stagePreview, mergeReleaseChecksums } from './publish-platform-preview.mjs'; |
| 8 | const version = '0.1.1-alpha'; |
| 9 | const dependencies = { mihomoSource: { file: 'mihomo-v1.19.33-source.tar.gz' }, qemuSource: { file: 'qemu-' + 'a'.repeat(40) + '-source.tar' } }; |
| 10 | const hash = text => createHash('sha256').update(text).digest('hex'); |
| 11 | function fixture(t) { |
| 12 | const root = mkdtempSync(join(tmpdir(), 'cage-preview-test-')); |
| 13 | t.after(() => rmSync(root, { recursive: true, force: true })); |
| 14 | const windows = join(root, 'windows'), linux = join(root, 'linux'), output = join(root, 'public'); |
| 15 | for (const [directory, names] of [[windows, [`Cage-${version}-windows-amd64-setup-unsigned.exe`, dependencies.mihomoSource.file, dependencies.qemuSource.file + '.gz']], [linux, [`Cage-${version}-linux-amd64.deb`, dependencies.mihomoSource.file]]]) { |
| 16 | mkdirSync(directory); |
| 17 | for (const name of names) writeFileSync(join(directory, name), name); |
| 18 | writeFileSync(join(directory, 'SHA256SUMS'), names.map(name => `${hash(name)} ${name}\n`).join('')); |
| 19 | writeFileSync(join(directory, 'private-report.json'), 'not a release file'); |
| 20 | } |
| 21 | return { windows, linux, output }; |
| 22 | } |
| 23 | test('preview publication stages verified installers and sources only, with one source copy', t => { |
| 24 | const f = fixture(t), files = stagePreview(f.windows, f.linux, f.output, version, dependencies); |
| 25 | assert.equal(files.size, 5); |
| 26 | assert.deepEqual(readdirSync(f.output).sort(), [...files.keys()].sort()); |
| 27 | assert.ok(!files.has('private-report.json')); |
| 28 | for (const [name, file] of files) assert.equal(hash(readFileSync(join(f.output, name))), file.digest); |
| 29 | }); |
| 30 | test('corrupt, missing, duplicate and unexpected checksum entries never become release assets', async t => { |
| 31 | for (const kind of ['corrupt', 'missing', 'duplicate', 'unexpected', 'conflicting-source']) await t.test(kind, t => { |
| 32 | const f = fixture(t), manifest = join(f.windows, 'SHA256SUMS'); |
| 33 | const name = `Cage-${version}-windows-amd64-setup-unsigned.exe`; |
| 34 | if (kind === 'corrupt') writeFileSync(join(f.windows, name), 'changed'); |
| 35 | if (kind === 'missing') rmSync(join(f.windows, name)); |
| 36 | if (kind === 'duplicate') writeFileSync(manifest, readFileSync(manifest, 'utf8') + `${hash(name)} ${name}\n`); |
| 37 | if (kind === 'unexpected') writeFileSync(manifest, readFileSync(manifest, 'utf8') + `${hash('other')} other.exe\n`); |
| 38 | if (kind === 'conflicting-source') { |
| 39 | writeFileSync(join(f.linux, dependencies.mihomoSource.file), 'changed-source'); |
| 40 | const sums = join(f.linux, 'SHA256SUMS'); |
| 41 | writeFileSync(sums, readFileSync(sums, 'utf8').replace(hash(dependencies.mihomoSource.file), hash('changed-source'))); |
| 42 | } |
| 43 | assert.throws(() => stagePreview(f.windows, f.linux, f.output, version, dependencies)); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | test('consolidation preserves macOS checksums and rejects conflicting version assets', () => { |
| 48 | const hash = 'a'.repeat(64), other = 'b'.repeat(64); |
| 49 | const mac = 'Cage-0.1.1-alpha-macos-arm64.dmg', windows = 'Cage-0.1.1-alpha-windows-amd64-setup-unsigned.exe'; |
| 50 | const release = { tag_name: 'v0.1.1-alpha', draft: false, prerelease: true, |
| 51 | assets: [{name: mac, state: 'uploaded', digest: `sha256:${hash}`}] }; |
| 52 | const files = new Map([[windows, {digest: other}], ['SHA256SUMS', {digest: other}]]); |
| 53 | const original = `${hash} ${mac}\n`; |
| 54 | const merged = mergeReleaseChecksums(release, original, files, '0.1.1-alpha'); |
| 55 | assert.equal(merged, original + `${other} ${windows}\n`); |
| 56 | const partial = {...release, assets:[...release.assets, {name:windows, state:'uploaded', digest:`sha256:${other}`}]}; |
| 57 | assert.equal(mergeReleaseChecksums(partial, original, files, '0.1.1-alpha'), merged); |
| 58 | assert.equal(mergeReleaseChecksums(partial, merged, files, '0.1.1-alpha'), merged); |
| 59 | assert.throws(() => mergeReleaseChecksums(release, original + original, files, '0.1.1-alpha')); |
| 60 | assert.throws(() => mergeReleaseChecksums(release, original, files, '0.1.0-alpha.1')); |
| 61 | assert.throws(() => mergeReleaseChecksums(release, original, new Map([[mac,{digest:other}]]), '0.1.1-alpha')); |
| 62 | assert.throws(() => mergeReleaseChecksums({...release,draft:true}, original, files, '0.1.1-alpha')); |
| 63 | assert.throws(() => mergeReleaseChecksums({...release,prerelease:false}, original, files, '0.1.1-alpha')); |
| 64 | assert.throws(() => mergeReleaseChecksums(partial, original, new Map(), '0.1.1-alpha')); |
| 65 | }); |