using System.IO; using System.Text.Json.Nodes; using FluentAssertions; using StellaOps.Graph.Indexer.Ingestion.Sbom; using Xunit; namespace StellaOps.Graph.Indexer.Tests; public sealed class FileSystemSnapshotFileWriterTests : IDisposable { private readonly string _root = Path.Combine(Path.GetTempPath(), $"graph-snapshots-{Guid.NewGuid():N}"); [Fact] public async Task WriteJsonAsync_writes_canonical_json() { var writer = new FileSystemSnapshotFileWriter(_root); var json = new JsonObject { ["b"] = "value2", ["a"] = "value1" }; await writer.WriteJsonAsync("manifest.json", json, CancellationToken.None); var content = await File.ReadAllTextAsync(Path.Combine(_root, "manifest.json")); content.Should().Be("{\"a\":\"value1\",\"b\":\"value2\"}"); } [Fact] public async Task WriteJsonLinesAsync_writes_each_object_on_new_line() { var writer = new FileSystemSnapshotFileWriter(_root); var items = new[] { new JsonObject { ["id"] = "1", ["kind"] = "component" }, new JsonObject { ["id"] = "2", ["kind"] = "artifact" } }; await writer.WriteJsonLinesAsync("nodes.jsonl", items, CancellationToken.None); var lines = await File.ReadAllLinesAsync(Path.Combine(_root, "nodes.jsonl")); lines.Should().HaveCount(2); lines[0].Should().Be("{\"id\":\"1\",\"kind\":\"component\"}"); lines[1].Should().Be("{\"id\":\"2\",\"kind\":\"artifact\"}"); } public void Dispose() { if (Directory.Exists(_root)) { Directory.Delete(_root, recursive: true); } } }