OverviewFilesCommitsIssuesDocsReleasesPeers

pgh / test / conflict-policy.test.js

69 lines · javascript ref:
// conflict-policy — app categories (compartments). detectConflicts only
// compares apps in the SAME category; cross-category divergence (experimental
// vs production, external canaries) is expected and never flagged.

import test from 'brittle'
import fs from '#node:fs'
import os from '#node:os'
import path from '#node:path'
import { detectConflicts, CONFLICT_VERSION } from '../lib/conflict.js'
import { loadPolicy, DEFAULT_CATEGORY } from '../lib/conflict-policy.js'

// Minimal fake PublishedIndex over an in-memory list of {appName, lockfile}.
function fakeIndex (entries) {
  return { allLockfiles () { return entries } }
}
const dep = (name, version, tree) => ({ name, version, tree })

test('loadPolicy: missing file ⇒ default production, empty overrides', (t) => {
  const p = loadPolicy(path.join(os.tmpdir(), 'pgh-no-such-policy-' + 'x.json'))
  t.is(p.default, DEFAULT_CATEGORY)
  t.is(p.categoryOf('anything'), 'production')
})

test('loadPolicy: overrides + self-declared lockfile.category', (t) => {
  const f = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'pgh-pol-')), 'conflict-policy.json')
  fs.writeFileSync(f, JSON.stringify({ default: 'production', overrides: { 'keet-daemon': 'external' } }))
  const p = loadPolicy(f)
  t.is(p.categoryOf('keet-daemon'), 'external', 'override map')
  t.is(p.categoryOf('pgh'), 'production', 'falls to default')
  t.is(p.categoryOf('pgh', { category: 'experimental' }), 'experimental', 'self-declared wins over default')
  t.is(p.categoryOf('keet-daemon', { category: 'production' }), 'production', 'self-declared wins over override')
})

test('detectConflicts: no categoryOf ⇒ compares against everything (back-compat)', (t) => {
  const mine = { name: 'pgh', deps: [dep('foo', '2.0.0', 't1')] }
  const index = fakeIndex([
    { appName: 'keet-daemon', lockfile: { deps: [dep('foo', '1.0.0', 't2')] } }
  ])
  const c = detectConflicts(mine, index)
  t.is(c.length, 1, 'foo version split is flagged when no policy')
})

test('detectConflicts: category scoping suppresses cross-category divergence', (t) => {
  const mine = { name: 'pgh', deps: [dep('foo', '2.0.0', 't1')] }
  const index = fakeIndex([
    { appName: 'pear-git-v3', lockfile: { deps: [dep('foo', '1.0.0', 't2')] } }, // production → compared
    { appName: 'keet-daemon', lockfile: { deps: [dep('foo', '0.5.0', 't3')] } }, // external → ignored
    { appName: 'pgh-experimental', lockfile: { deps: [dep('foo', '9.9.9', 't4')] } } // experimental → ignored
  ])
  const categoryOf = (name) => ({ 'keet-daemon': 'external', 'pgh-experimental': 'experimental' }[name] || 'production')
  const c = detectConflicts(mine, index, { categoryOf })
  t.is(c.length, 1, 'only the production rival (pear-git-v3) conflicts')
  t.is(c[0].type, CONFLICT_VERSION)
  const rivalApps = c[0].existing.map(e => e.appName)
  t.alike(rivalApps, ['pear-git-v3'], 'external + experimental rivals excluded')
})

test('detectConflicts: a publishing experimental app only compares to experimental', (t) => {
  const mine = { name: 'pgh-experimental', category: 'experimental', deps: [dep('foo', '9.0.0', 't1')] }
  const index = fakeIndex([
    { appName: 'pgh', lockfile: { deps: [dep('foo', '2.0.0', 't2')] } }, // production → ignored for an experimental publisher
    { appName: 'pear-git-v3-experimental', lockfile: { category: 'experimental', deps: [dep('foo', '8.0.0', 't3')] } }
  ])
  const categoryOf = (name, lf) => (lf && lf.category) || ({ 'pgh-experimental': 'experimental', 'pear-git-v3-experimental': 'experimental' }[name] || 'production')
  const c = detectConflicts(mine, index, { categoryOf })
  t.is(c.length, 1)
  t.alike(c[0].existing.map(e => e.appName), ['pear-git-v3-experimental'])
})