Files
git.stella-ops.org/src/__Libraries/__Tests/StellaOps.Signals.Tests/SyntheticRuntimeProbeTests.cs

59 lines
2.4 KiB
C#

using System.Net;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using StellaOps.Signals.Models;
using StellaOps.Signals.Tests.TestInfrastructure;
using Xunit;
using StellaOps.TestKit;
namespace StellaOps.Signals.Tests;
public class SyntheticRuntimeProbeTests : IClassFixture<SignalsTestFactory>
{
private readonly SignalsTestFactory factory;
public SyntheticRuntimeProbeTests(SignalsTestFactory factory)
{
this.factory = factory;
}
[Trait("Category", TestCategories.Unit)]
[Fact]
public async Task SyntheticProbe_Generates_Runtime_Facts_And_Scoring()
{
using var client = factory.CreateClient();
client.DefaultRequestHeaders.Add("X-Scopes", "signals:write signals:read");
var callgraphRequest = CallgraphIngestionTests.CreateRequest("java", component: "synthetic-probe", version: "1.0.0");
var callgraphRes = await client.PostAsJsonAsync("/signals/callgraphs", callgraphRequest);
Assert.Equal(HttpStatusCode.Accepted, callgraphRes.StatusCode);
var callgraphBody = await callgraphRes.Content.ReadFromJsonAsync<CallgraphIngestResponse>();
Assert.NotNull(callgraphBody);
var probeRequest = new SyntheticRuntimeProbeRequest
{
CallgraphId = callgraphBody!.CallgraphId,
Subject = new ReachabilitySubject { ScanId = "scan-synthetic-001" },
EventCount = 3
};
var probeRes = await client.PostAsJsonAsync("/signals/runtime-facts/synthetic", probeRequest);
Assert.Equal(HttpStatusCode.Accepted, probeRes.StatusCode);
var ingestBody = await probeRes.Content.ReadFromJsonAsync<RuntimeFactsIngestResponse>();
Assert.NotNull(ingestBody);
Assert.Equal("scan-synthetic-001", ingestBody!.SubjectKey);
Assert.True(ingestBody.RuntimeFactCount > 0);
var factRes = await client.GetAsync($"/signals/facts/{ingestBody.SubjectKey}");
Assert.Equal(HttpStatusCode.OK, factRes.StatusCode);
var factJson = await factRes.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(factJson);
Assert.True(doc.RootElement.TryGetProperty("states", out var states));
Assert.True(states.GetArrayLength() > 0);
Assert.True(doc.RootElement.TryGetProperty("runtimeFacts", out var runtimeFacts));
Assert.True(runtimeFacts.GetArrayLength() > 0);
}
}