Files
VolcanicCore/tests/VolcanicCore.Tests/ExecutionContextTests.cs

60 lines
2.2 KiB
C#

using System.Text.Json;
using Microsoft.Extensions.Logging.Abstractions;
using VolcanicCore.Api.Engine;
using ExecutionContext = VolcanicCore.Api.Engine.ExecutionContext;
namespace VolcanicCore.Tests;
public class ExecutionContextTests
{
private sealed class Handle { public string Value { get; set; } = ""; }
[Fact]
public void GetConnection_returns_registered_handle_by_id()
{
var handle = new Handle { Value = "live" };
var reg = FakeInstanceRegistry.With("conn-1", handle);
var ctx = new ExecutionContext(
executionId: "e",
workflowId: "w",
nodeId: "n",
config: JsonDocument.Parse("{}").RootElement,
answerChannelId: null,
logger: NullLogger.Instance,
connectionId: "conn-1",
instances: reg);
Assert.Same(handle, ctx.GetConnection<Handle>());
}
[Fact]
public void GetConnection_throws_when_node_has_no_connection_id()
{
var ctx = new ExecutionContext("e", "w", "n",
JsonDocument.Parse("{}").RootElement, null,
NullLogger.Instance, connectionId: null, instances: FakeInstanceRegistry.Empty);
Assert.Throws<InvalidOperationException>(() => ctx.GetConnection<Handle>());
}
[Fact]
public void GetConnection_throws_when_type_mismatches()
{
var reg = FakeInstanceRegistry.With("conn-1", new object());
var ctx = new ExecutionContext("e", "w", "n",
JsonDocument.Parse("{}").RootElement, null,
NullLogger.Instance, connectionId: "conn-1", instances: reg);
Assert.Throws<InvalidCastException>(() => ctx.GetConnection<Handle>());
}
private sealed class FakeInstanceRegistry : IConnectionInstanceRegistry
{
private readonly Dictionary<string, object> _byId;
private FakeInstanceRegistry(Dictionary<string, object> byId) => _byId = byId;
public static FakeInstanceRegistry Empty => new(new());
public static FakeInstanceRegistry With(string id, object h) => new(new() { [id] = h });
public Task<object> GetAsync(string connectionId, CancellationToken ct) => Task.FromResult(_byId[connectionId]);
}
}