using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Abstractions.Command;
using GFramework.Core.Abstractions.Enums;
using GFramework.Core.Abstractions.Query;
namespace GFramework.Core.Tests.Architectures;
///
/// 覆盖测试架构上下文替身的共享事件与显式失败契约。
///
[TestFixture]
public class TestArchitectureContextBehaviorTests
{
private static IEnumerable EventContextFactories()
{
yield return new TestCaseData(
new Func(() => new TestArchitectureContext()))
.SetArgDisplayNames(nameof(TestArchitectureContext));
yield return new TestCaseData(
new Func(() => new TestArchitectureContextV3()))
.SetArgDisplayNames(nameof(TestArchitectureContextV3));
}
///
/// 验证测试上下文会把事件注册与发送委托到同一个事件总线实例。
///
[Test]
public void RegisterEvent_And_SendEvent_Should_Use_Shared_EventBus()
{
var context = new TestArchitectureContext();
var eventReceived = false;
context.RegisterEvent(_ => eventReceived = true);
context.SendEvent();
Assert.That(eventReceived, Is.True);
}
///
/// 验证用于 ArchitectureServices 的上下文替身也会把事件注册与发送委托到同一个事件总线实例。
///
[Test]
public void RegisterEvent_And_SendEvent_On_TestArchitectureContextV3_Should_Use_Shared_EventBus()
{
var context = new TestArchitectureContextV3();
var eventReceived = false;
context.RegisterEvent(_ => eventReceived = true);
context.SendEvent();
Assert.That(eventReceived, Is.True);
}
///
/// 验证两类架构测试替身都会对事件 API 的空参数执行显式校验。
///
/// 创建待验证测试上下文的工厂。
[TestCaseSource(nameof(EventContextFactories))]
public void Event_APIs_Should_Validate_Null_Arguments(Func createContext)
{
var context = createContext();
Assert.That(() => context.SendEvent(null!), Throws.TypeOf());
Assert.That(() => context.RegisterEvent(null!), Throws.TypeOf());
Assert.That(() => context.UnRegisterEvent(null!), Throws.TypeOf());
}
///
/// 验证两类架构测试替身在注销事件处理器后都不会继续分发同一回调。
///
/// 创建待验证测试上下文的工厂。
[TestCaseSource(nameof(EventContextFactories))]
public void UnRegisterEvent_Should_Stop_Dispatch(Func createContext)
{
var context = createContext();
var eventReceived = false;
void Handler(TestEventV2 _)
{
eventReceived = true;
}
context.RegisterEvent(Handler);
context.UnRegisterEvent(Handler);
context.SendEvent();
Assert.That(eventReceived, Is.False);
}
///
/// 验证测试上下文的旧版命令与查询入口会显式抛出未支持异常。
///
[Test]
public async Task Legacy_Entries_Should_Throw_Or_Return_Faulted_Tasks()
{
var context = new TestArchitectureContext();
Assert.That(() => context.SendCommand(new TestCommandV2()), Throws.TypeOf());
Assert.That(
() => context.SendCommand(new TestCommandWithResultV2 { Result = 1 }),
Throws.TypeOf());
Assert.That(() => context.SendQuery(new TestQueryV2 { Result = 1 }), Throws.TypeOf());
Assert.That(
async () => await context.SendCommandAsync(new TestAsyncCommand()).ConfigureAwait(false),
Throws.TypeOf());
Assert.That(
async () => await context.SendCommandAsync(new TestAsyncCommandWithResult()).ConfigureAwait(false),
Throws.TypeOf());
Assert.That(
async () => await context.SendQueryAsync(new TestAsyncQuery()).ConfigureAwait(false),
Throws.TypeOf());
}
///
/// 验证用于 ArchitectureServices 的上下文替身也会把旧版入口显式标记为不支持。
///
[Test]
public async Task Legacy_Entries_On_TestArchitectureContextV3_Should_Throw_Or_Return_Faulted_Tasks()
{
var context = new TestArchitectureContextV3();
Assert.That(() => context.SendCommand(new TestCommandV2()), Throws.TypeOf());
Assert.That(
() => context.SendCommand(new TestCommandWithResultV2 { Result = 1 }),
Throws.TypeOf());
Assert.That(() => context.SendQuery(new TestQueryV2 { Result = 1 }), Throws.TypeOf());
Assert.That(
async () => await context.SendCommandAsync(new TestAsyncCommand()).ConfigureAwait(false),
Throws.TypeOf());
Assert.That(
async () => await context.SendCommandAsync(new TestAsyncCommandWithResult()).ConfigureAwait(false),
Throws.TypeOf());
Assert.That(
async () => await context.SendQueryAsync(new TestAsyncQuery()).ConfigureAwait(false),
Throws.TypeOf());
}
///
/// 验证两类架构测试替身在接口视角下都会以 no-op 方式接受生命周期钩子。
///
[Test]
public void RegisterLifecycleHook_Via_Interface_Should_Return_Original_Hook()
{
IArchitecture withRegistry = new TestArchitectureWithRegistry(new TestRegistry());
IArchitecture withoutRegistry = new TestArchitectureWithoutRegistry();
var hook = new NoOpLifecycleHook();
Assert.That(withRegistry.RegisterLifecycleHook(hook), Is.SameAs(hook));
Assert.That(withoutRegistry.RegisterLifecycleHook(hook), Is.SameAs(hook));
}
///
/// 为旧版异步命令入口提供最小实现的测试命令。
///
private sealed class TestAsyncCommand : IAsyncCommand
{
public Task ExecuteAsync()
{
return Task.CompletedTask;
}
public IArchitectureContext GetContext()
{
throw new NotSupportedException();
}
public void SetContext(IArchitectureContext context)
{
ArgumentNullException.ThrowIfNull(context);
}
}
///
/// 为旧版异步命令入口提供最小实现的带结果测试命令。
///
private sealed class TestAsyncCommandWithResult : IAsyncCommand
{
public Task ExecuteAsync()
{
return Task.FromResult(1);
}
public IArchitectureContext GetContext()
{
throw new NotSupportedException();
}
public void SetContext(IArchitectureContext context)
{
ArgumentNullException.ThrowIfNull(context);
}
}
///
/// 为旧版异步查询入口提供最小实现的测试查询。
///
private sealed class TestAsyncQuery : IAsyncQuery
{
public Task DoAsync()
{
return Task.FromResult(1);
}
}
///
/// 为生命周期钩子接口提供空实现的测试替身。
///
private sealed class NoOpLifecycleHook : IArchitectureLifecycleHook
{
public void OnPhase(ArchitecturePhase phase, IArchitecture architecture)
{
ArgumentNullException.ThrowIfNull(architecture);
}
}
}