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 {
  configGetCommand,
  configListCommand,
  configResetCommand,
  configSetCommand,
} from '../../src/commands/config.js';

describe('matrix config command', () => {
  it('sets, reads, lists, and resets the package registry in a local Matrix config', async () => {
    const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'matrix-sdk-config-'));
    try {
      const configPath = path.join(cwd, '.matrix', 'config.json');

      const set = await configSetCommand('registry', 'https://registry.example.test/npm', { cwd });
      assert.equal(set.configPath, configPath);
      assert.equal(set.value, 'https://registry.example.test/npm/');

      const get = await configGetCommand('registry', { cwd });
      assert.equal(get.value, 'https://registry.example.test/npm/');
      assert.equal(get.source, 'config');

      const list = await configListCommand({ cwd });
      assert.equal(list.values.registry, 'https://registry.example.test/npm/');
      assert.equal(list.effective.registry, 'https://registry.example.test/npm/');
      assert.equal(list.effective.registrySource, 'config');

      const reset = await configResetCommand({ cwd });
      assert.equal(reset.configPath, configPath);
      assert.equal(fs.existsSync(configPath), false);
    } finally {
      fs.rmSync(cwd, { recursive: true, force: true });
    }
  });

  it('rejects unknown config keys', async () => {
    await assert.rejects(
      () => configSetCommand('hostHome', '/var/lib/provider-local'),
      /MX_CONFIG_INVALID/u,
    );
  });
});
