junction/tests/Junction.Tests/Unit/ResultTests.cs
dtrentin 507753f82e init: scaffold Junction multi-protocol machine monitor (M1 walking skeleton)
Greenfield C# app reading industrial machine data over pluggable protocols.
M1 walking skeleton: MTConnect plugin polls a machine, Avalonia dashboard
shows live last datum. Build 0 warn/0 err on net48 + net8.0 (Linux),
115 unit tests + 2 docker-gated integration tests.

Projects:
- Junction.Domain (netstandard2.0): Result<T>, models, IProtocolDriver,
  IMachineRepository, plugin manifest. Zero package deps.
- Junction.Core (netstandard2.0): PollingEngine, PluginLoader
  (Assembly.LoadFrom), MachineMonitor, DI extensions.
- Junction.Persistence (netstandard2.0): SqliteMachineRepository (Dapper),
  schema, connection factory. Provider-swap seam to SQL Server.
- Junction.Protocols.MTConnect (netstandard2.0): HTTP driver + namespace-
  version-agnostic parser (MTConnect 1.7 + 2.0). Runtime plugin.
- Junction.App (net48;net8.0): Avalonia MVVM, live dashboard, NLog, DI root.
- Junction.Tests (net8.0): xUnit + Moq, 117 tests.
- mock/: docker-compose MTConnect agent (ladder99/agent).

Target net48 for Windows 7/8 fleet compatibility. Avalonia pinned 11.3.x.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 23:32:56 +02:00

127 lines
3.4 KiB
C#

using System.Collections.Generic;
using Junction.Domain;
using Xunit;
namespace Junction.Tests.Unit
{
public class ResultTests
{
private static OperationError Err(string msg = "boom") =>
new OperationError("E", "Src", msg);
// ---- Result<T> ----
[Fact]
public void Generic_Ok_HasValue_IsSuccess_EmptyErrors()
{
var r = Result<int>.Ok(42);
Assert.True(r.IsSuccess);
Assert.False(r.WasCancelled);
Assert.Equal(42, r.Value);
Assert.NotNull(r.Errors);
Assert.Empty(r.Errors);
}
[Fact]
public void Generic_FailSingle_CarriesError_NotSuccess()
{
var r = Result<int>.Fail(Err());
Assert.False(r.IsSuccess);
Assert.False(r.WasCancelled);
Assert.Single(r.Errors);
Assert.NotNull(r.Errors);
}
[Fact]
public void Generic_FailMany_CarriesAllErrors()
{
var errors = new List<OperationError> { Err("a"), Err("b") };
var r = Result<int>.Fail(errors);
Assert.False(r.IsSuccess);
Assert.Equal(2, r.Errors.Count);
}
[Fact]
public void Generic_Failed_ValueIsDefault_NoThrow()
{
var rInt = Result<int>.Fail(Err());
var rRef = Result<string>.Fail(Err());
Assert.Equal(0, rInt.Value);
Assert.Null(rRef.Value);
}
[Fact]
public void Generic_Cancelled_IsCancelled_NotSuccess_NotTreatedAsFailure()
{
var r = Result<int>.Cancelled();
Assert.True(r.WasCancelled);
Assert.False(r.IsSuccess);
// cancellation is NOT a failure: no errors recorded
Assert.Empty(r.Errors);
Assert.NotNull(r.Errors);
}
[Fact]
public void Generic_ErrorsNeverNull_InAnyState()
{
Assert.NotNull(Result<int>.Ok(1).Errors);
Assert.NotNull(Result<int>.Fail(Err()).Errors);
Assert.NotNull(Result<int>.Cancelled().Errors);
}
// ---- non-generic Result ----
[Fact]
public void Void_Ok_IsSuccess_EmptyErrors()
{
var r = Result.Ok();
Assert.True(r.IsSuccess);
Assert.False(r.WasCancelled);
Assert.NotNull(r.Errors);
Assert.Empty(r.Errors);
}
[Fact]
public void Void_FailSingle_CarriesError_NotSuccess()
{
var r = Result.Fail(Err());
Assert.False(r.IsSuccess);
Assert.False(r.WasCancelled);
Assert.Single(r.Errors);
}
[Fact]
public void Void_FailMany_CarriesAllErrors()
{
var r = Result.Fail(new[] { Err("a"), Err("b") });
Assert.False(r.IsSuccess);
Assert.Equal(2, r.Errors.Count);
}
[Fact]
public void Void_Cancelled_IsCancelled_NotSuccess_NotTreatedAsFailure()
{
var r = Result.Cancelled();
Assert.True(r.WasCancelled);
Assert.False(r.IsSuccess);
Assert.Empty(r.Errors);
}
[Fact]
public void Void_ErrorsNeverNull_InAnyState()
{
Assert.NotNull(Result.Ok().Errors);
Assert.NotNull(Result.Fail(Err()).Errors);
Assert.NotNull(Result.Cancelled().Errors);
}
}
}