using System.Text;
using NodePipeline.Configuration.Yaml.Exceptions;
using NodePipeline.Configuration.Yaml.Loader;

namespace NodePipeline.Configuration.Yaml.Tests;

public class YamlGraphLoaderTests
{
    [Fact]
    public void ConvertsEditorConfigToRuntimeConfig()
    {
        const string yaml = """
                            name: runtime-test
                            nodes:
                              - id: node1
                                type: FileImageSource
                                parameters:
                                  FileName:
                                    value: "input.jpg"
                                outputs:
                                  Output:
                                    label: "Выход"
                            """;

        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(yaml));
        var loader = new YamlGraphLoader();
        var config = loader.Load(stream);

        Assert.Equal("runtime-test", config.Name);
        Assert.Single(config.Nodes);

        var node = config.Nodes[0];
        Assert.Equal("node1", node.Id);
        Assert.Equal("FileImageSource", node.Type);
        Assert.Single(node.Parameters);
        Assert.Equal("input.jpg", node.Parameters["FileName"]);
        Assert.Single(node.Outputs);
        Assert.Contains("Output", node.Outputs);
    }

    [Fact]
    public void ThrowsOnEmptyYaml()
    {
        const string emptyYaml = "";
        var loader = new YamlGraphLoader();
        var ex = Assert.Throws<YamlFileIsEmptyException>(() =>
        {
            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(emptyYaml));
            loader.Load(stream);
        });
        Assert.Null(ex.FileName);
    }
}