// Copyright (c) 2025-2026 GeWuYou // SPDX-License-Identifier: Apache-2.0 using GFramework.Cqrs.Abstractions.Cqrs; namespace GFramework.Core.Tests.Command; /// /// 记录 bridge 执行线程与收到请求的最小 CQRS runtime 测试替身。 /// internal sealed class RecordingCqrsRuntime(Func? responseFactory = null) : ICqrsRuntime { private static readonly Func DefaultResponseFactory = _ => null; private readonly Func _responseFactory = responseFactory ?? DefaultResponseFactory; /// /// 获取最近一次 观察到的同步上下文类型。 /// public Type? ObservedSynchronizationContextType { get; private set; } /// /// 获取最近一次收到的请求实例。 /// public object? LastRequest { get; private set; } /// public ValueTask SendAsync( ICqrsContext context, IRequest request, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(context); ArgumentNullException.ThrowIfNull(request); ObservedSynchronizationContextType = SynchronizationContext.Current?.GetType(); LastRequest = request; object? response = request switch { IRequest => Unit.Value, _ => _responseFactory(request) }; return ValueTask.FromResult((TResponse)response!); } /// public ValueTask PublishAsync( ICqrsContext context, TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification { throw new NotSupportedException(); } /// public IAsyncEnumerable CreateStream( ICqrsContext context, IStreamRequest request, CancellationToken cancellationToken = default) { throw new NotSupportedException(); } }