From e4e5487efcd1748c0a51e05bb3dce2dc2ce7692b Mon Sep 17 00:00:00 2001 From: nk-batman21 <40377381+nk-batman21@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:12:26 +0700 Subject: [PATCH] fix(migrate): honor --dry-run in migrate run and migrate fix (#4568) The parser stores `--dry-run` (and `-d` on `migrate fix`) as `dryRun`, but both actions read `flags['dry-run']`, which is always undefined. So `migrate run --dry-run` ran the real migration, overwriting an existing .claude-flow/config.json, and `migrate fix --agents --dry-run` wrote the restored agent files. Read `dryRun`, keeping the kebab key as a fallback as update, init and mods do. --- .../cli/__tests__/migrate-dry-run.test.ts | 64 +++++++++++++++++++ v3/@claude-flow/cli/src/commands/migrate.ts | 4 +- 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/migrate-dry-run.test.ts diff --git a/v3/@claude-flow/cli/__tests__/migrate-dry-run.test.ts b/v3/@claude-flow/cli/__tests__/migrate-dry-run.test.ts new file mode 100644 index 0000000000..1e05f33df1 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/migrate-dry-run.test.ts @@ -0,0 +1,64 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const mocks = vi.hoisted(() => ({ + restoreRemovedAgents: vi.fn(async (..._args: unknown[]) => []), +})); + +vi.mock('../src/output.js', () => ({ output: new Proxy({}, { + get: (_target, name) => name === 'writeln' ? () => {} : (value: unknown) => value, +}) })); +vi.mock('../src/commands/migrate-agent-detection.js', () => ({ + detectRemovedAgentGaps: () => [{ agent: 'coder.md', plugin: 'ruflo-core', installCommand: '/plugin install ruflo-core@ruflo' }], +})); +vi.mock('../src/commands/migrate-agent-restore.js', () => ({ + restoreRemovedAgents: mocks.restoreRemovedAgents, +})); +import { migrateCommand } from '../src/commands/migrate.js'; +import { CommandParser } from '../src/parser.js'; + +let root: string; +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'migrate-dry-run-')); + writeFileSync(join(root, 'claude-flow.config.json'), '{"version":"2","swarm":{"mode":"mesh"}}\n'); + mkdirSync(join(root, 'data', 'sessions'), { recursive: true }); + writeFileSync(join(root, 'data', 'sessions', 'session-1.json'), '{}\n'); +}); +afterEach(() => { + rmSync(root, { recursive: true, force: true }); + vi.clearAllMocks(); +}); + +async function migrate(argv: string[]): Promise { + const parser = new CommandParser(); + parser.registerCommand(migrateCommand); + const { flags } = parser.parse(['migrate', ...argv]); + const subcommand = migrateCommand.subcommands!.find((command) => command.name === argv[0])!; + const result = await subcommand.action!({ args: [], flags, cwd: root, interactive: false }); + expect(result?.success).toBe(true); +} + +describe('migrate --dry-run through the real command parser', () => { + it('migrate run --dry-run writes nothing', async () => { + await migrate(['run', '--dry-run']); + expect(existsSync(join(root, '.claude-flow'))).toBe(false); + }); + + it('migrate run still migrates without the flag', async () => { + await migrate(['run']); + expect(existsSync(join(root, '.claude-flow', 'config.json'))).toBe(true); + expect(existsSync(join(root, '.claude-flow', 'sessions', 'session-1.json'))).toBe(true); + }); + + it.each(['--dry-run', '-d'])('migrate fix --agents %s restores as a dry run', async (flag) => { + await migrate(['fix', '--agents', flag]); + expect(mocks.restoreRemovedAgents).toHaveBeenCalledWith(root, expect.any(Array), { dryRun: true }); + }); + + it('migrate fix --agents still restores without the flag', async () => { + await migrate(['fix', '--agents']); + expect(mocks.restoreRemovedAgents).toHaveBeenCalledWith(root, expect.any(Array), { dryRun: false }); + }); +}); diff --git a/v3/@claude-flow/cli/src/commands/migrate.ts b/v3/@claude-flow/cli/src/commands/migrate.ts index e16d1bea6b..4b00c81716 100644 --- a/v3/@claude-flow/cli/src/commands/migrate.ts +++ b/v3/@claude-flow/cli/src/commands/migrate.ts @@ -232,7 +232,7 @@ const runCommand: Command = { ], action: async (ctx: CommandContext): Promise => { const cwd = ctx.cwd || process.cwd(); - const dryRun = ctx.flags['dry-run'] === true; + const dryRun = ctx.flags.dryRun === true || ctx.flags['dry-run'] === true; const skipBackup = ctx.flags.backup === false; const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const v3Dir = path.join(cwd, '.claude-flow'); @@ -794,7 +794,7 @@ const fixCommand: Command = { return { success: true, data: { results: [] } }; } - const dryRun = Boolean(ctx.flags['dry-run']); + const dryRun = ctx.flags.dryRun === true || ctx.flags['dry-run'] === true; const results = await restoreRemovedAgents(cwd, gaps, { dryRun }); if (ctx.flags.format === 'json') {