import { describe, expect, it } from 'vitest';
import { Command } from 'commander';
import { registerBrainCommand } from '@mosaicstack/brain';
import { registerForgeCommand } from '@mosaicstack/forge';
import { registerLogCommand } from '@mosaicstack/log';
import { registerMacpCommand } from '@mosaicstack/macp';
import { registerMemoryCommand } from '@mosaicstack/memory';
import { registerQueueCommand } from '@mosaicstack/queue';
import { registerStorageCommand } from '@mosaicstack/storage';
import { registerAuthCommand } from './commands/auth.js';
import { registerConfigCommand } from './commands/config.js';

// CU-05-10 — integration smoke test
// Asserts every sub-package CLI registered via register<Name>Command() attaches
// a top-level command to the root program and that its help output renders
// without throwing. This is the "mosaic <cmd> --help exits 0" gate that
// guards the sub-package CLI surface (CU-05-01..08) from silent breakage.

const REGISTRARS: Array<[string, (program: Command) => void]> = [
  ['auth', registerAuthCommand],
  ['brain', registerBrainCommand],
  ['config', registerConfigCommand],
  ['forge', registerForgeCommand],
  ['log', registerLogCommand],
  ['macp', registerMacpCommand],
  ['memory', registerMemoryCommand],
  ['queue', registerQueueCommand],
  ['storage', registerStorageCommand],
];

describe('sub-package CLI smoke (CU-05-10)', () => {
  for (const [name, register] of REGISTRARS) {
    it(`registers the "${name}" command on the root program`, () => {
      const program = new Command();
      register(program);
      const cmd = program.commands.find((c) => c.name() === name);
      expect(cmd, `expected top-level "${name}" command`).toBeDefined();
    });

    it(`"${name}" help output renders without throwing`, () => {
      const program = new Command().exitOverride();
      register(program);
      const cmd = program.commands.find((c) => c.name() === name);
      expect(cmd).toBeDefined();
      expect(() => cmd!.helpInformation()).not.toThrow();
    });
  }

  it('all nine sub-package commands coexist on a single program', () => {
    const program = new Command();
    for (const [, register] of REGISTRARS) register(program);
    const names = program.commands.map((c) => c.name()).sort();
    expect(names).toEqual([
      'auth',
      'brain',
      'config',
      'forge',
      'log',
      'macp',
      'memory',
      'queue',
      'storage',
    ]);
  });
});
