Skip to content

AI-native design editor. Open-source Figma alternative.

2.9 KB
Raw Blame
import { expect, test } from 'bun:test'

import {
  publishReleasePackages,
  type PublicationOperations,
  type PublicationPlanEntry
} from '#release/workflow'

function fixture() {
  const plan: PublicationPlanEntry[] = ['one', 'two'].map((name) => ({
    package: { directory: name, manifest: { name, version: '1.0.0' } },
    status: 'unpublished'
  }))
  const published: string[][] = []
  const artifacts = new Map([
    ['[email protected]', '/one.tgz'],
    ['[email protected]', '/two.tgz']
  ])
  const operations: PublicationOperations = {
    plan: async () => plan,
    artifacts: async () => artifacts,
    verify: async (_root, paths) => {
      expect(paths).toHaveLength(2)
    },
    execute: async ({ args }) => {
      published.push(args ?? [])
      return { stdout: '', stderr: '' }
    }
  }
  return { plan, published, artifacts, operations }
}

test('verification failure prevents every registry write', async () => {
  const state = fixture()
  state.operations.verify = async () => {
    throw new Error('broken consumer')
  }
  await expect(publishReleasePackages('/repo', state.operations)).rejects.toThrow('broken consumer')
  expect(state.published).toEqual([])
})

test('a partial retry verifies retained artifacts and publishes only pending versions', async () => {
  const state = fixture()
  const first = state.plan[0]
  if (!first) throw new Error('Missing fixture entry')
  first.status = 'published'
  let verified = false
  state.operations.verify = async (_root, artifacts) => {
    expect(artifacts).toEqual(['/one.tgz', '/two.tgz'])
    verified = true
  }
  state.operations.execute = async ({ args }) => {
    expect(verified).toBe(true)
    state.published.push(args ?? [])
    return { stdout: '', stderr: '' }
  }
  await publishReleasePackages('/repo', state.operations)
  expect(state.published).toHaveLength(1)
  expect(state.published[0]).toContain('/two.tgz')
})

test('mismatched artifacts prevent verification and publication', async () => {
  const state = fixture()
  state.artifacts.delete('[email protected]')
  state.artifacts.set('[email protected]', '/wrong.tgz')
  let verified = false
  state.operations.verify = async () => {
    verified = true
  }
  await expect(publishReleasePackages('/repo', state.operations)).rejects.toThrow(
    'Missing verified package artifact'
  )
  expect(verified).toBe(false)
  expect(state.published).toEqual([])
})

test('publishes a beta under the next dist-tag and a stable version under latest', async () => {
  const state = fixture()
  const [stable, beta] = state.plan
  if (!stable || !beta) throw new Error('Missing fixture entry')
  beta.package.manifest.version = '1.1.0-beta.1'
  state.artifacts.delete('[email protected]')
  state.artifacts.set('[email protected]', '/two.tgz')
  await publishReleasePackages('/repo', state.operations)
  expect(state.published[0]).not.toContain('--tag')
  expect(state.published[1]?.slice(-2)).toEqual(['--tag', 'next'])
})