import { describe, it, expect, beforeEach } from 'vitest';
import { CommandRegistryService } from './command-registry.service.js';
import type { CommandDef } from '@mosaicstack/types';

const mockCmd: CommandDef = {
  name: 'test',
  description: 'Test command',
  aliases: ['t'],
  scope: 'core',
  execution: 'local',
  available: true,
};

describe('CommandRegistryService', () => {
  let service: CommandRegistryService;

  beforeEach(() => {
    service = new CommandRegistryService();
  });

  it('starts with empty manifest', () => {
    expect(service.getManifest().commands).toHaveLength(0);
  });

  it('registers a command', () => {
    service.registerCommand(mockCmd);
    expect(service.getManifest().commands).toHaveLength(1);
  });

  it('updates existing command by name', () => {
    service.registerCommand(mockCmd);
    service.registerCommand({ ...mockCmd, description: 'Updated' });
    expect(service.getManifest().commands).toHaveLength(1);
    expect(service.getManifest().commands[0]?.description).toBe('Updated');
  });

  it('onModuleInit registers core commands', () => {
    service.onModuleInit();
    const manifest = service.getManifest();
    expect(manifest.commands.length).toBeGreaterThan(5);
    expect(manifest.commands.some((c) => c.name === 'model')).toBe(true);
    expect(manifest.commands.some((c) => c.name === 'help')).toBe(true);
  });

  it('manifest includes skills array', () => {
    const manifest = service.getManifest();
    expect(Array.isArray(manifest.skills)).toBe(true);
  });

  it('manifest version is 1', () => {
    expect(service.getManifest().version).toBe(1);
  });
});
