|
| 1 | +import type {Config} from '@oclif/core' |
| 2 | + |
| 3 | +import {Config as OclifConfig} from '@oclif/core' |
| 4 | +import {expect} from 'chai' |
| 5 | +import {restore, stub} from 'sinon' |
| 6 | + |
| 7 | +import type {MigrateRunReport} from '../../src/shared/transport/events/migrate-events.js' |
| 8 | + |
| 9 | +import Migrate from '../../src/oclif/commands/migrate.js' |
| 10 | + |
| 11 | +// Tests the gate logic in displayForwardResult that controls when the |
| 12 | +// VC-sync hint fires. Daemon transport is bypassed by calling the |
| 13 | +// `protected` display method directly with a synthetic report — the |
| 14 | +// gate decisions are pure functions of (format, dryRun, summary) and |
| 15 | +// don't need a live daemon round-trip. |
| 16 | + |
| 17 | +class TestableMigrate extends Migrate { |
| 18 | + public exerciseDisplay(report: MigrateRunReport, format: string, dryRun: boolean): void { |
| 19 | + return this.displayForwardResult(report, format, dryRun) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function makeReport(overrides: {failed?: number; migrated?: number;} = {}): MigrateRunReport { |
| 24 | + return { |
| 25 | + archiveRoot: '/tmp/archive', |
| 26 | + completedAt: '2026-05-26T00:00:00.000Z', |
| 27 | + dryRun: false, |
| 28 | + files: [], |
| 29 | + projectRoot: '/tmp/proj', |
| 30 | + startedAt: '2026-05-26T00:00:00.000Z', |
| 31 | + summary: { |
| 32 | + archived: 0, |
| 33 | + failed: overrides.failed ?? 0, |
| 34 | + migrated: overrides.migrated ?? 0, |
| 35 | + skipped: 0, |
| 36 | + }, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe('brv migrate — VC-sync hint gate', () => { |
| 41 | + let config: Config |
| 42 | + let stdout: string[] |
| 43 | + let stderr: string[] |
| 44 | + let warnings: string[] |
| 45 | + |
| 46 | + before(async () => { |
| 47 | + config = await OclifConfig.load(import.meta.url) |
| 48 | + }) |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + stdout = [] |
| 52 | + stderr = [] |
| 53 | + warnings = [] |
| 54 | + }) |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + restore() |
| 58 | + }) |
| 59 | + |
| 60 | + function buildCommand(): TestableMigrate { |
| 61 | + const cmd = new TestableMigrate([], config) |
| 62 | + stub(cmd, 'log').callsFake((msg?: string) => { |
| 63 | + if (msg !== undefined) stdout.push(msg) |
| 64 | + }) |
| 65 | + stub(cmd, 'logToStderr').callsFake((msg?: string) => { |
| 66 | + if (msg !== undefined) stderr.push(msg) |
| 67 | + }) |
| 68 | + stub(cmd, 'warn').callsFake((msg: Error | string) => { |
| 69 | + warnings.push(typeof msg === 'string' ? msg : msg.message) |
| 70 | + return msg |
| 71 | + }) |
| 72 | + return cmd |
| 73 | + } |
| 74 | + |
| 75 | + it('text + real + migrated>0 + failed=0: prints the hint on stderr', () => { |
| 76 | + const cmd = buildCommand() |
| 77 | + cmd.exerciseDisplay(makeReport({failed: 0, migrated: 5}), 'text', false) |
| 78 | + |
| 79 | + const stderrJoined = stderr.join('\n') |
| 80 | + expect(stderrJoined).to.include('Tip: the context tree was successfully migrated') |
| 81 | + expect(stderrJoined).to.include('brv vc status') |
| 82 | + expect(stderrJoined).to.include('brv vc add') |
| 83 | + expect(stderrJoined).to.include('brv vc push') |
| 84 | + expect(stderrJoined).to.include('brv vc remote add origin') |
| 85 | + }) |
| 86 | + |
| 87 | + it('text + dry-run + migrated>0: does NOT print the hint', () => { |
| 88 | + const cmd = buildCommand() |
| 89 | + cmd.exerciseDisplay(makeReport({failed: 0, migrated: 5}), 'text', true) |
| 90 | + |
| 91 | + expect(stderr.join('\n')).to.not.include('Tip:') |
| 92 | + }) |
| 93 | + |
| 94 | + it('text + real + migrated=0: does NOT print the hint', () => { |
| 95 | + const cmd = buildCommand() |
| 96 | + cmd.exerciseDisplay(makeReport({failed: 0, migrated: 0}), 'text', false) |
| 97 | + |
| 98 | + expect(stderr.join('\n')).to.not.include('Tip:') |
| 99 | + }) |
| 100 | + |
| 101 | + it('text + real + migrated>0 + failed>0: does NOT print the hint (exit-code contradiction guard)', () => { |
| 102 | + const cmd = buildCommand() |
| 103 | + cmd.exerciseDisplay(makeReport({failed: 2, migrated: 5}), 'text', false) |
| 104 | + |
| 105 | + // Warnings about failures still fire — only the success hint is suppressed. |
| 106 | + expect(warnings.join('\n')).to.include('2 file(s) failed') |
| 107 | + expect(stderr.join('\n')).to.not.include('Tip:') |
| 108 | + }) |
| 109 | + |
| 110 | + it('json + real + migrated>0: emits the JSON envelope on stdout, nothing on stderr', () => { |
| 111 | + const cmd = buildCommand() |
| 112 | + cmd.exerciseDisplay(makeReport({failed: 0, migrated: 5}), 'json', false) |
| 113 | + |
| 114 | + expect(stdout).to.have.lengthOf(1) |
| 115 | + expect(() => JSON.parse(stdout[0])).to.not.throw() |
| 116 | + expect(stderr).to.have.lengthOf(0) |
| 117 | + }) |
| 118 | +}) |
0 commit comments