dsh-brain/verify-skill-sync-conflict-...

34 lines
2.6 KiB
JavaScript

// Conflict confirmation must bind the preview scope and cloud revision.
import { mkdtempSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const home = mkdtempSync(join(tmpdir(), 'skill-conflict-r5-'))
process.env.DSH_HOME = home
const results = []
function check(name, ok, detail = '') { results.push({ name, ok }); console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail ? ' :: ' + detail : '')) }
const { SkillSync } = await import('./lib/skill-sync.js')
let scope = { endpoint: 'https://stub-a', accountToken: 'token-a', root: '/tmp/skills' }
let revision = 1; let manifest = 'manifest-a'
const repo = { read: async (name) => ({ name, area: 'root', files: [{ path: 'SKILL.md', sha256: 'abc', size: 3, data: 'eA==' }], delivery: undefined }) }
const client = { listSkills: async () => ({ ok: true, data: { items: [{ name: 'alpha', revision, manifest_sha256: manifest }] } }) }
const engine = new SkillSync({ client: () => client, settings: () => ({ enabled: true, skillsSyncEnabled: true }), repository: () => repo, log: () => {}, scope: () => scope })
engine.current.conflicts = [{ name: 'alpha', reason: 'diverged', scopeKey: undefined }]
const preview = await engine.conflictDetails()
check('conflict preview exposes a stable conflict id and versions', typeof preview[0]?.conflictId === 'string' && preview[0].localVersion.length > 0 && preview[0].cloudVersion.includes('1'))
const manual = await engine.resolveConflict('alpha', 'manual', preview[0].conflictId)
check('valid preview confirmation reaches manual action', manual.applied === false && manual.action === 'manual')
revision = 2; manifest = 'manifest-b'
let stale = ''
try { await engine.resolveConflict('alpha', 'manual', preview[0].conflictId) } catch (error) { stale = String(error) }
check('changed cloud revision rejects stale confirmation', stale.includes('stale_conflict'), stale)
const preview2 = await engine.conflictDetails()
check('a refreshed preview gets a different conflict id', preview2[0].conflictId !== preview[0].conflictId)
scope = { endpoint: 'https://stub-b', accountToken: 'token-b', root: '/tmp/skills' }
let scopeStale = ''
try { await engine.resolveConflict('alpha', 'manual', preview2[0].conflictId) } catch (error) { scopeStale = String(error) }
check('scope change rejects confirmation from the old scope', scopeStale.includes('stale_conflict'), scopeStale)
const failed = results.filter((result) => !result.ok)
console.log('')
console.log(failed.length ? 'FAILED ' + failed.length + '/' + results.length + ': ' + failed.map((result) => result.name).join(', ') : 'ALL ' + results.length + ' CHECKS PASSED')
process.exit(failed.length ? 1 : 0)