using System.Text.Json;
using Sdt.Core;
using Xunit;

namespace DevTool.Tests;

public sealed class RunEventJsonlRecorderTests
{
    [Fact]
    public void Recorder_WritesJsonlEvents()
    {
        var root = Path.Combine(Path.GetTempPath(), "sdt-events-" + Guid.NewGuid().ToString("N"));
        Directory.CreateDirectory(root);

        string path;
        using (var recorder = RunEventJsonlRecorder.Create(root, "workflow"))
        {
            path = recorder.FilePath;
            recorder.Write(new RunEvent("workflow", RunEventType.WorkflowStarted, "started", WorkflowId: "build"));
            recorder.Write(new RunEvent("workflow", RunEventType.WorkflowCompleted, "done", WorkflowId: "build", Success: true));
        }

        Assert.True(File.Exists(path));
        var lines = File.ReadAllLines(path);
        Assert.Equal(2, lines.Length);

        using var doc = JsonDocument.Parse(lines[0]);
        Assert.Equal("workflow", doc.RootElement.GetProperty("category").GetString());
        Assert.Equal("WorkflowStarted", doc.RootElement.GetProperty("type").GetString());
    }
}
