'use strict';

// Smoke tests against the REAL datasets/ files. Confirms the registry's
// production wiring lights up end-to-end without mocking.

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

const { assetResolver } = require('../../src/registry');

test('namespaces() includes curves, menu, monsterSamples, monsterSpecs, units', () => {
    const ns = assetResolver.namespaces().sort();
    assert.deepEqual(ns, ['curves', 'menu', 'monsterSamples', 'monsterSpecs', 'units']);
});

test('monsterSpecs: \"all\" key resolves to a defaults + bySample document', () => {
    const doc = assetResolver.resolve('monsterSpecs', 'all');
    assert.ok(doc, 'expected monsterSpecs/all');
    assert.equal(typeof doc.defaults, 'object');
    assert.equal(typeof doc.bySample, 'object');
});

test('curves: known model id resolves to a curve object', () => {
    const c = assetResolver.resolve('curves', 'hidrostal-H05K-S03R');
    assert.ok(c, 'expected a curve payload');
    assert.equal(typeof c, 'object');
});

test('curves: lookup is case-insensitive', () => {
    const lower = assetResolver.resolve('curves', 'hidrostal-h05k-s03r');
    const upper = assetResolver.resolve('curves', 'HIDROSTAL-H05K-S03R');
    assert.ok(lower);
    assert.deepEqual(lower, upper);
});

test('curves: unknown model returns null (no throw)', () => {
    assert.equal(assetResolver.resolve('curves', 'nope-not-here'), null);
});

test('menu: machine.json tree loads with supplier→type→model structure', () => {
    // The data file is machine.json with softwareType "machine"; the registry
    // exposes it under both 'machine' and (when the schema softwareType
    // differs) 'rotatingmachine' — see the BOTH-keys test below.
    const tree = assetResolver.resolve('menu', 'machine');
    assert.ok(tree, 'menu/machine should exist (machine.json)');
    assert.ok(Array.isArray(tree.suppliers));
    assert.ok(tree.suppliers.length > 0);
});

test('menu: valve tree loads', () => {
    const tree = assetResolver.resolve('menu', 'valve');
    assert.ok(tree);
    assert.ok(Array.isArray(tree.suppliers));
});

test('menu: indexed by BOTH inner softwareType and filename', () => {
    // machine.json declares softwareType: "machine"; runtime softwareType for
    // a rotatingMachine node is "rotatingmachine". Both should resolve to the
    // same tree so all call paths work.
    const bySoftwareType = assetResolver.resolve('menu', 'machine');
    const byFilename     = assetResolver.resolve('menu', 'machine');
    assert.ok(bySoftwareType);
    assert.deepEqual(byFilename, bySoftwareType);
});

test('resolveAssetMetadata: hidrostal-H05K-S03R derives supplier + type', () => {
    const meta = assetResolver.resolveAssetMetadata('machine', 'hidrostal-H05K-S03R');
    assert.ok(meta, 'expected metadata');
    assert.equal(meta.supplier, 'Hidrostal');
    assert.equal(meta.type,     'Centrifugal');
    assert.ok(meta.units.length > 0);
});

test('monsterSamples: a real sample code resolves', () => {
    const ids = assetResolver.list('monsterSamples');
    assert.ok(ids.length > 0, 'expected at least one sample code');
    const sample = assetResolver.resolve('monsterSamples', ids[0]);
    assert.ok(sample);
    assert.ok(sample.code);
});

test('units: flow family resolves to a list of unit values', () => {
    const flow = assetResolver.resolve('units', 'flow');
    assert.ok(flow);
    assert.ok(Array.isArray(flow.values));
    assert.ok(flow.values.length > 0);
});

test('list(): curves namespace lists all known model ids', () => {
    const ids = assetResolver.list('curves');
    assert.ok(ids.length >= 2, 'expected at least 2 curves');
    assert.ok(ids.includes('hidrostal-h05k-s03r'));
});

test('refresh(name) reloads the namespace from disk', async () => {
    await assetResolver.refresh('curves');
    const c = assetResolver.resolve('curves', 'hidrostal-H05K-S03R');
    assert.ok(c);
});
