OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-deploy / test / config.test.js

177 lines · javascript ref:
import test from 'brittle'
import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
import { mkdtempSync } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { DeployConfig, DeployLog, getDeployStatus } from '../lib/config.js'

function tmpBee () {
  const store = new Corestore(mkdtempSync(join(tmpdir(), 'deploy-config-')))
  const bee = new Hyperbee(store.get({ name: 'deploy' }), { keyEncoding: 'utf-8', valueEncoding: 'json' })
  return bee
}

// ── DeployConfig ─────────────────────────────────────────────

test('DeployConfig — set and get', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)

  const result = await config.set('my-app', { enabled: true, mode: 'git', channel: 'release', autoDeploy: true })
  t.is(result.enabled, true)
  t.is(result.mode, 'git')
  t.is(result.repoName, 'my-app')
  t.ok(result.configuredAt)

  const fetched = await config.get('my-app')
  t.is(fetched.enabled, true)
  t.is(fetched.channel, 'release')

  await bee.close()
})

test('DeployConfig — set merges with existing', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)

  await config.set('app', { enabled: true, mode: 'git' })
  await config.set('app', { autoDeploy: true })

  const fetched = await config.get('app')
  t.is(fetched.enabled, true, 'preserved from first set')
  t.is(fetched.autoDeploy, true, 'added in second set')

  await bee.close()
})

test('DeployConfig — list all configs', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)

  await config.set('app-a', { enabled: true })
  await config.set('app-b', { enabled: false })

  const all = await config.list()
  t.is(all.length, 2)

  await bee.close()
})

test('DeployConfig — isEnabled and shouldAutoDeploy', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)

  t.is(await config.isEnabled('missing'), false)
  t.is(await config.shouldAutoDeploy('missing'), false)

  await config.set('app', { enabled: true, autoDeploy: false })
  t.is(await config.isEnabled('app'), true)
  t.is(await config.shouldAutoDeploy('app'), false)

  await bee.close()
})

test('DeployConfig — delete', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)

  await config.set('app', { enabled: true })
  t.ok(await config.get('app'))
  await config.delete('app')
  t.is(await config.get('app'), null)

  await bee.close()
})

// ── DeployLog ────────────────────────────────────────────────

test('DeployLog — append and getLog', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const log = new DeployLog(bee)

  await log.append('app', { status: 'success', commitHash: 'abc123', duration: 5000 })
  await log.append('app', { status: 'failed', error: 'timeout', duration: 120000 })

  const entries = await log.getLog('app')
  t.is(entries.length, 2)
  t.is(entries[0].status, 'failed', 'newest first (reverse)')
  t.is(entries[1].status, 'success')

  await bee.close()
})

test('DeployLog — getLatest', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const log = new DeployLog(bee)

  t.is(await log.getLatest('app'), null, 'null when empty')

  await log.append('app', { status: 'success', commitHash: 'first' })
  await log.append('app', { status: 'success', commitHash: 'second' })

  const latest = await log.getLatest('app')
  t.is(latest.commitHash, 'second')

  await bee.close()
})

test('DeployLog — clear', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const log = new DeployLog(bee)

  await log.append('app', { status: 'success' })
  await log.append('app', { status: 'success' })
  t.is((await log.getLog('app')).length, 2)

  await log.clear('app')
  t.is((await log.getLog('app')).length, 0)

  await bee.close()
})

// ── getDeployStatus ──────────────────────────────────────────

test('getDeployStatus — assembles from config + log', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)
  const log = new DeployLog(bee)

  await config.set('app', { enabled: true, mode: 'git', autoDeploy: true })
  await log.append('app', { status: 'success', commitHash: 'abc', duration: 3000 })

  const status = await getDeployStatus(config, log, 'app')
  t.is(status.repoName, 'app')
  t.is(status.configured, true)
  t.is(status.enabled, true)
  t.is(status.autoDeploy, true)
  t.ok(status.lastDeploy)
  t.is(status.lastDeploy.status, 'success')
  t.is(status.lastDeploy.commitHash, 'abc')

  await bee.close()
})

test('getDeployStatus — unconfigured repo', async (t) => {
  const bee = tmpBee()
  await bee.ready()
  const config = new DeployConfig(bee)
  const log = new DeployLog(bee)

  const status = await getDeployStatus(config, log, 'unknown')
  t.is(status.configured, false)
  t.is(status.enabled, false)
  t.is(status.lastDeploy, null)

  await bee.close()
})