import { describe, it } from 'node:test';
import { strict as assert } from 'node:assert';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { initCommand } from '../../src/commands/init.js';

describe('mx init command', () => {
  it('creates matrix.json and scaffold directories', async () => {
    const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'mx-init-'));
    try {
      const result = await initCommand({
        cwd,
        name: 'demo-package',
        version: '1.2.3',
        description: 'demo',
        yes: true,
      });

      assert.equal(result.rootDir, cwd);
      assert.equal(fs.existsSync(path.join(cwd, 'matrix.json')), true);
      assert.equal(fs.existsSync(path.join(cwd, 'src')), true);
      assert.equal(fs.existsSync(path.join(cwd, 'examples')), true);
      assert.equal(fs.existsSync(path.join(cwd, 'skills')), true);
      assert.equal(fs.existsSync(path.join(cwd, 'tests')), true);

      const manifest = JSON.parse(fs.readFileSync(path.join(cwd, 'matrix.json'), 'utf8')) as {
        name: string;
        version: string;
        description: string;
        runtime: { language: string; entry: string };
        components: unknown[];
        permissions: { fsPolicy: string };
      };
      assert.equal(manifest.name, 'demo-package');
      assert.equal(manifest.version, '1.2.3');
      assert.equal(manifest.description, 'demo');
      assert.equal(manifest.runtime.entry, 'dist/index.js');
      assert.equal(Array.isArray(manifest.components), true);
      assert.equal(manifest.permissions.fsPolicy, 'none');
    } finally {
      fs.rmSync(cwd, { recursive: true, force: true });
    }
  });
});
