import { describe, it, expect } from 'vitest';
import { Command } from 'commander';
import { registerStorageCommand } from './cli.js';

describe('registerStorageCommand', () => {
  function buildProgram(): Command {
    const program = new Command();
    program.exitOverride(); // prevent process.exit in tests
    registerStorageCommand(program);
    return program;
  }

  it('registers a "storage" command on the parent', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage');
    expect(storageCmd).toBeDefined();
  });

  it('registers "storage status" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const statusCmd = storageCmd.commands.find((c) => c.name() === 'status');
    expect(statusCmd).toBeDefined();
  });

  it('registers "storage tier" subcommand group', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const tierCmd = storageCmd.commands.find((c) => c.name() === 'tier');
    expect(tierCmd).toBeDefined();
  });

  it('registers "storage tier show" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const tierCmd = storageCmd.commands.find((c) => c.name() === 'tier')!;
    const showCmd = tierCmd.commands.find((c) => c.name() === 'show');
    expect(showCmd).toBeDefined();
  });

  it('registers "storage tier switch" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const tierCmd = storageCmd.commands.find((c) => c.name() === 'tier')!;
    const switchCmd = tierCmd.commands.find((c) => c.name() === 'switch');
    expect(switchCmd).toBeDefined();
  });

  it('registers "storage export" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const exportCmd = storageCmd.commands.find((c) => c.name() === 'export');
    expect(exportCmd).toBeDefined();
  });

  it('registers "storage import" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const importCmd = storageCmd.commands.find((c) => c.name() === 'import');
    expect(importCmd).toBeDefined();
  });

  it('registers "storage migrate" subcommand', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const migrateCmd = storageCmd.commands.find((c) => c.name() === 'migrate');
    expect(migrateCmd).toBeDefined();
  });

  it('has all required subcommands in a single assertion', () => {
    const program = buildProgram();
    const storageCmd = program.commands.find((c) => c.name() === 'storage')!;
    const topLevel = storageCmd.commands.map((c) => c.name());
    expect(topLevel).toContain('status');
    expect(topLevel).toContain('tier');
    expect(topLevel).toContain('export');
    expect(topLevel).toContain('import');
    expect(topLevel).toContain('migrate');

    const tierCmd = storageCmd.commands.find((c) => c.name() === 'tier')!;
    const tierSubcmds = tierCmd.commands.map((c) => c.name());
    expect(tierSubcmds).toContain('show');
    expect(tierSubcmds).toContain('switch');
  });
});
