const test = require('node:test');
const assert = require('node:assert/strict');

const ConfigManager = require('../src/configs/index.js');

test('can read known config and report existence', () => {
  const manager = new ConfigManager('.');
  assert.equal(manager.hasConfig('measurement'), true);

  const config = manager.getConfig('measurement');
  assert.ok(config.functionality);
  assert.ok(config.functionality.softwareType);
});

test('getAvailableConfigs includes known names', () => {
  const manager = new ConfigManager('.');
  const configs = manager.getAvailableConfigs();
  assert.ok(configs.includes('measurement'));
  assert.ok(configs.includes('rotatingMachine'));
});

test('createEndpoint creates executable JS payload shell', () => {
  const manager = new ConfigManager('.');
  const script = manager.createEndpoint('measurement');

  assert.match(script, /window\.EVOLV\.nodes\.measurement/);
  assert.match(script, /config loaded and endpoint created/);
});

test('getConfig throws on missing config', () => {
  const manager = new ConfigManager('.');
  assert.throws(() => manager.getConfig('definitely-not-real'), /Failed to load config/);
});
