Refactor code structure for improved readability and maintainability; removed redundant code blocks and optimized function calls.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Reachability.FixtureTests;
|
||||
|
||||
public class CorpusFixtureTests
|
||||
{
|
||||
private static readonly string RepoRoot = ReachbenchFixtureTests.LocateRepoRoot();
|
||||
private static readonly string CorpusRoot = Path.Combine(RepoRoot, "tests", "reachability", "corpus");
|
||||
|
||||
[Fact]
|
||||
public void ManifestExistsAndIsDeterministic()
|
||||
{
|
||||
var manifestPath = Path.Combine(CorpusRoot, "manifest.json");
|
||||
File.Exists(manifestPath).Should().BeTrue("corpus manifest should exist");
|
||||
|
||||
using var stream = File.OpenRead(manifestPath);
|
||||
using var doc = JsonDocument.Parse(stream);
|
||||
doc.RootElement.ValueKind.Should().Be(JsonValueKind.Array);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CorpusEntriesMatchManifestHashes()
|
||||
{
|
||||
var manifestPath = Path.Combine(CorpusRoot, "manifest.json");
|
||||
var manifest = JsonDocument.Parse(File.ReadAllBytes(manifestPath)).RootElement.EnumerateArray().ToArray();
|
||||
|
||||
manifest.Should().NotBeEmpty("corpus manifest must have entries");
|
||||
|
||||
foreach (var entry in manifest)
|
||||
{
|
||||
var id = entry.GetProperty("id").GetString();
|
||||
var language = entry.GetProperty("language").GetString();
|
||||
var files = entry.GetProperty("files");
|
||||
|
||||
id.Should().NotBeNullOrEmpty();
|
||||
language.Should().NotBeNullOrEmpty();
|
||||
|
||||
var caseDir = Path.Combine(CorpusRoot, language!, id!);
|
||||
Directory.Exists(caseDir).Should().BeTrue($"case folder missing: {caseDir}");
|
||||
|
||||
foreach (var fileProp in files.EnumerateObject())
|
||||
{
|
||||
var filename = fileProp.Name;
|
||||
var expectedHash = fileProp.Value.GetString();
|
||||
File.Exists(Path.Combine(caseDir, filename)).Should().BeTrue($"{id} missing {filename}");
|
||||
|
||||
var actualHash = BitConverter.ToString(SHA256.HashData(File.ReadAllBytes(Path.Combine(caseDir, filename)))).Replace("-", "").ToLowerInvariant();
|
||||
actualHash.Should().Be(expectedHash, $"{id} hash mismatch for {filename}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExpectFilesContainRequiredFields()
|
||||
{
|
||||
var manifestPath = Path.Combine(CorpusRoot, "manifest.json");
|
||||
var manifest = JsonDocument.Parse(File.ReadAllBytes(manifestPath)).RootElement.EnumerateArray().ToArray();
|
||||
var required = new[] { "id", "language", "state", "score" };
|
||||
var idRegex = new Regex(@"^id:\s*(?<id>.+)$", RegexOptions.Multiline);
|
||||
|
||||
foreach (var entry in manifest)
|
||||
{
|
||||
var id = entry.GetProperty("id").GetString()!;
|
||||
var language = entry.GetProperty("language").GetString()!;
|
||||
var expectPath = Path.Combine(CorpusRoot, language, id, "expect.yaml");
|
||||
File.Exists(expectPath).Should().BeTrue($"{id} missing expect.yaml");
|
||||
var text = File.ReadAllText(expectPath);
|
||||
|
||||
foreach (var field in required)
|
||||
{
|
||||
text.Should().Contain($"{field}:", $"{id} expect.yaml missing '{field}:'");
|
||||
}
|
||||
|
||||
var match = idRegex.Match(text);
|
||||
match.Success.Should().BeTrue($"{id} expect.yaml should include matching id");
|
||||
match.Groups["id"].Value.Trim().Should().Be(id, $"{id} expect.yaml id must match manifest id");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user