OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git-v3 / test / mirror-onmount.test.js

59 lines · javascript ref:
// Continuous mirror enrollment (the one-shot-announce regression fix).
//
// A mirror's _doAnnounce runs ONCE at boot, so an org/repo mounted LATER was
// never enrolled as a writer until a manual restart. The fix: OrgPuller /
// RepoPuller fire `onMount(name)` after a SUCCESSFUL mount, which the daemon
// debounces into a mirror-writer re-announce (scheduleMirrorReannounce in
// pear-git.js). This proves the trigger fires on success and NOT on failure;
// the announce→enroll path itself is proven by issues-mirror-writer.test.js.

import test from 'brittle'
import { OrgPuller } from '../lib/services/org-mirror.js'
import { RepoPuller } from '../lib/services/repo-mirror.js'

test('OrgPuller fires onMount after a successful org mount, not on failure', async (t) => {
  const ok = []
  const good = new OrgPuller({
    issueRegistry: {},
    orgs: { mountOrg: async () => {} },
    log: { close: async () => {} },
    onMount: (name) => ok.push(name)
  })
  await good._process({ name: 'acme', op: 0, orgBootstrapKey: 'a'.repeat(64) })
  t.alike(ok, ['acme'], 'onMount called with the org name on success')

  const bad = []
  const fails = new OrgPuller({
    issueRegistry: {},
    orgs: { mountOrg: async () => { throw new Error('boom') } },
    log: { close: async () => {} },
    onMount: (name) => bad.push(name)
  })
  await fails._process({ name: 'nope', op: 0, orgBootstrapKey: 'b'.repeat(64) })
  t.alike(bad, [], 'onMount NOT called when the mount fails')
})

test('RepoPuller fires onMount after a successful pull, not on failure', async (t) => {
  const ok = []
  const good = new RepoPuller({
    sync: { pull: async () => {}, remotes: new Map(), addRemote () {} },
    issueRegistry: {},
    log: { close: async () => {} },
    primaryServerKey: 'c'.repeat(64),
    onMount: (name) => ok.push(name)
  })
  await good._process({ name: 'demo', op: 0 }, 0)
  t.alike(ok, ['demo'], 'onMount called with the repo name on success')

  const bad = []
  const fails = new RepoPuller({
    sync: { pull: async () => { throw new Error('boom') }, remotes: new Map(), addRemote () {} },
    issueRegistry: {},
    log: { close: async () => {} },
    primaryServerKey: 'd'.repeat(64),
    onMount: (name) => bad.push(name)
  })
  await fails._process({ name: 'nope', op: 0 }, 0)
  t.alike(bad, [], 'onMount NOT called when the pull fails')
})