import assert from 'node:assert/strict'; import test from 'node:test'; import {dockerClient, imageTag, images, promote} from './release-images.mjs'; test('SemVer release and prerelease tags', () => { for (const version of ['0.1.0', '1.0.0', '1.2.3-rc.1', '1.0.0-0', '1.0.0-01a', '1.0.0-x-y-z.--', '12345678901234567890.0.1']) { assert.equal(imageTag(`refs/tags/v${version}`), `v${version}`); } assert.equal(imageTag('refs/tags/v1.2.3-rc.1+build.001'), 'v1.2.3-rc.1_build.001'); }); test('reject malformed SemVer and ambiguous registry tags', () => { for (const version of ['1', '1.2', '01.2.3', '1.02.3', '1.2.03', '1.2.3-01', '1.2.3-a.01', '1.2.3-', '1.2.3-a..b', '1.2.3+', '1.2.3+foo..bar', '1.2.3+foo+bar', '1.2.3_bad', '1.2.3\n', '1.2.3-RC.1', '1.2.3+BUILD', '1.2.3-'+ 'a'.repeat(128)]) { assert.throws(() => imageTag(`refs/tags/v${version}`)); } assert.throws(() => imageTag('refs/heads/v1.2.3')); assert.throws(() => imageTag('refs/tags/1.2.3')); }); const revision = 'a'.repeat(40); const options = {ref: 'refs/tags/v0.1.0', revision, prefix: 'registry.example.test/anya'}; function registry() { const entries = new Map(images.map((image, index) => [`${options.prefix}/${image}:sha-${revision}`, `sha256:${String(index + 1).repeat(64)}`])); const copies = []; const client = { digest(ref, optional = false) { if (entries.has(ref)) return entries.get(ref); if (optional) return null; throw new Error('source missing'); }, revision: () => revision, copy(source, destination) { copies.push({source, destination}); entries.set(destination, source.split('@')[1]); }, }; return {client, entries, copies}; } test('promotes three sources by digest and retry does not rewrite', () => { const {client, copies} = registry(); const plan = promote(options, client, () => {}); assert.equal(copies.length, 3); for (const item of plan) assert.ok(item.source.endsWith(`@${item.digest}`)); promote(options, client, () => {}); assert.equal(copies.length, 3); }); test('missing source, wrong revision or existing release conflict prevents all writes', () => { for (const failure of ['missing', 'revision', 'conflict']) { const {client, entries, copies} = registry(); const last = `${options.prefix}/${images[2]}`; if (failure === 'missing') entries.delete(`${last}:sha-${revision}`); if (failure === 'revision') client.revision = () => 'b'.repeat(40); if (failure === 'conflict') entries.set(`${last}:v0.1.0`, `sha256:${'f'.repeat(64)}`); assert.throws(() => promote(options, client)); assert.equal(copies.length, 0); } }); test('partial promotion is safely resumable', () => { const {client, copies} = registry(); const copy = client.copy; client.copy = (source, destination) => { if (copies.length === 1) throw new Error('registry unavailable'); copy(source, destination); }; assert.throws(() => promote(options, client, () => {}), /unavailable/); client.copy = copy; promote(options, client, () => {}); assert.equal(copies.length, 3); }); test('destination digest mismatch and concurrent overwrite are failures', () => { const {client} = registry(); client.copy = () => {}; assert.throws(() => promote(options, client, () => {})); const other = registry(); const get = other.client.digest; let reads = 0; other.client.digest = (ref, optional) => { if (ref.endsWith(':v0.1.0') && ++reads > 3) return `sha256:${'f'.repeat(64)}`; return get(ref, optional); }; assert.throws(() => promote(options, other.client), /concurrently/); assert.equal(other.copies.length, 0); }); test('inspect distinguishes missing manifests from authorization and network errors', () => { const ref = 'registry.example.test/anya/image:v0.1.0'; for (const message of [`docker failed: ERROR: ${ref}: not found`, 'docker failed: manifest unknown']) { const client = dockerClient(() => { throw new Error(message); }); assert.equal(client.digest(ref, true), null); assert.throws(() => client.digest(ref)); } for (const message of ['unauthorized', 'TLS handshake timeout', 'no such host', '404 page not found']) { const client = dockerClient(() => { throw new Error(message); }); assert.throws(() => client.digest(ref, true)); } }); test('Docker adapter preserves manifest format and validates metadata', () => { const calls = []; const client = dockerClient((command, args) => { calls.push([command, ...args]); if (args.includes('{{json .Manifest}}')) return JSON.stringify({digest: `sha256:${'a'.repeat(64)}`}); if (args.includes('{{json .Image}}')) return JSON.stringify({os: 'linux', architecture: 'amd64', config: {Labels: {'org.opencontainers.image.revision': revision}}}); return ''; }); assert.equal(client.revision('source'), revision); assert.equal(client.digest('source'), `sha256:${'a'.repeat(64)}`); client.copy('source@sha256:abc', 'destination:v0.1.0'); assert.deepEqual(calls.at(-1), ['docker', 'buildx', 'imagetools', 'create', '--prefer-index=false', '--tag', 'destination:v0.1.0', 'source@sha256:abc']); assert.throws(() => dockerClient(() => '{}').digest('source'), /Invalid registry digest/); assert.throws(() => dockerClient(() => '{}').revision('source'), /linux\/amd64/); });