test(core): 补强 legacy bridge 上下文断言

- 补充 AsyncQueryExecutor 与 CommandExecutor bridge 测试的上下文保留断言

- 优化 RecordingCqrsRuntime 的 bridge 执行模拟与响应类型诊断

- 更新 cqrs-rewrite active tracking 与 trace 的 RP-097 验证记录
This commit is contained in:
gewuyou 2026-05-07 20:17:46 +08:00
parent cca413042f
commit 44d1a89a0b
5 changed files with 179 additions and 5 deletions

View File

@ -126,6 +126,7 @@ public class CommandExecutorTests
{
Assert.That(result, Is.EqualTo(123));
Assert.That(runtime.LastRequest, Is.TypeOf<GFramework.Core.Cqrs.LegacyCommandResultDispatchRequest>());
Assert.That(command.ObservedContext, Is.SameAs(expectedContext));
});
}

View File

@ -1,6 +1,8 @@
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Core.Abstractions.Rule;
using GFramework.Core.Cqrs;
using GFramework.Cqrs.Abstractions.Cqrs;
namespace GFramework.Core.Tests.Command;
@ -25,7 +27,7 @@ internal sealed class RecordingCqrsRuntime(Func<object?, object?>? responseFacto
public object? LastRequest { get; private set; }
/// <inheritdoc />
public ValueTask<TResponse> SendAsync<TResponse>(
public async ValueTask<TResponse> SendAsync<TResponse>(
ICqrsContext context,
IRequest<TResponse> request,
CancellationToken cancellationToken = default)
@ -38,11 +40,34 @@ internal sealed class RecordingCqrsRuntime(Func<object?, object?>? responseFacto
object? response = request switch
{
LegacyCommandDispatchRequest legacyCommandDispatchRequest => ExecuteLegacyCommand(context, legacyCommandDispatchRequest),
LegacyCommandResultDispatchRequest legacyCommandResultDispatchRequest => ExecuteContextAwareRequest(
context,
legacyCommandResultDispatchRequest.Target,
legacyCommandResultDispatchRequest.Execute),
LegacyQueryDispatchRequest legacyQueryDispatchRequest => ExecuteContextAwareRequest(
context,
legacyQueryDispatchRequest.Target,
legacyQueryDispatchRequest.Execute),
LegacyAsyncCommandDispatchRequest legacyAsyncCommandDispatchRequest => await ExecuteLegacyAsyncCommandAsync(
context,
legacyAsyncCommandDispatchRequest,
cancellationToken).ConfigureAwait(false),
LegacyAsyncCommandResultDispatchRequest legacyAsyncCommandResultDispatchRequest => await ExecuteContextAwareRequestAsync(
context,
legacyAsyncCommandResultDispatchRequest.Target,
legacyAsyncCommandResultDispatchRequest.ExecuteAsync,
cancellationToken).ConfigureAwait(false),
LegacyAsyncQueryDispatchRequest legacyAsyncQueryDispatchRequest => await ExecuteContextAwareRequestAsync(
context,
legacyAsyncQueryDispatchRequest.Target,
legacyAsyncQueryDispatchRequest.ExecuteAsync,
cancellationToken).ConfigureAwait(false),
IRequest<Unit> => Unit.Value,
_ => _responseFactory(request)
};
return ValueTask.FromResult((TResponse)response!);
return ConvertResponse<TResponse>(request, response);
}
/// <inheritdoc />
@ -63,4 +88,120 @@ internal sealed class RecordingCqrsRuntime(Func<object?, object?>? responseFacto
{
throw new NotSupportedException();
}
/// <summary>
/// 将测试替身工厂返回的装箱结果显式还原为目标类型,并在类型不匹配时给出可诊断异常。
/// </summary>
/// <typeparam name="TResponse">当前请求声明的响应类型。</typeparam>
/// <param name="request">触发响应工厂的请求实例。</param>
/// <param name="response">响应工厂返回的装箱结果。</param>
/// <returns>还原后的目标类型响应。</returns>
/// <exception cref="InvalidOperationException">
/// 响应工厂返回 <see langword="null" /> 或错误类型,导致无法还原为 <typeparamref name="TResponse" />。
/// </exception>
private static TResponse ConvertResponse<TResponse>(IRequest<TResponse> request, object? response)
{
if (response is TResponse typedResponse)
{
return typedResponse;
}
if (response is null && !typeof(TResponse).IsValueType)
{
return (TResponse)response!;
}
string actualType = response?.GetType().FullName ?? "null";
throw new InvalidOperationException(
$"RecordingCqrsRuntime 无法将响应类型从 '{actualType}' 转换为 '{typeof(TResponse).FullName}'。"
+ $" 请求类型:'{request.GetType().FullName}'。");
}
/// <summary>
/// 按 bridge handler 语义为 legacy 无返回值命令注入上下文并执行。
/// </summary>
/// <param name="context">当前运行时接收到的架构上下文。</param>
/// <param name="request">待执行的 legacy 命令桥接请求。</param>
/// <returns>桥接后的 <see cref="Unit" /> 响应。</returns>
private static Unit ExecuteLegacyCommand(
ICqrsContext context,
LegacyCommandDispatchRequest request)
{
PrepareTarget(context, request.Command);
request.Command.Execute();
return Unit.Value;
}
/// <summary>
/// 按 bridge handler 语义为 legacy 异步无返回值命令注入上下文并执行。
/// </summary>
/// <param name="context">当前运行时接收到的架构上下文。</param>
/// <param name="request">待执行的 legacy 异步命令桥接请求。</param>
/// <param name="cancellationToken">调用方传入的取消令牌。</param>
/// <returns>表示 bridge 执行完成的异步结果。</returns>
private static async Task<Unit> ExecuteLegacyAsyncCommandAsync(
ICqrsContext context,
LegacyAsyncCommandDispatchRequest request,
CancellationToken cancellationToken)
{
PrepareTarget(context, request.Command);
await request.Command.ExecuteAsync().WaitAsync(cancellationToken).ConfigureAwait(false);
return Unit.Value;
}
/// <summary>
/// 按 bridge handler 语义为带返回值 legacy 请求注入上下文并执行同步委托。
/// </summary>
/// <param name="context">当前运行时接收到的架构上下文。</param>
/// <param name="target">需要接收上下文注入的 legacy 目标对象。</param>
/// <param name="execute">实际执行 legacy 目标逻辑的同步委托。</param>
/// <returns>同步执行结果。</returns>
private static object? ExecuteContextAwareRequest(
ICqrsContext context,
object target,
Func<object?> execute)
{
PrepareTarget(context, target);
return execute();
}
/// <summary>
/// 按 bridge handler 语义为带返回值 legacy 请求注入上下文并执行异步委托。
/// </summary>
/// <param name="context">当前运行时接收到的架构上下文。</param>
/// <param name="target">需要接收上下文注入的 legacy 目标对象。</param>
/// <param name="executeAsync">实际执行 legacy 目标逻辑的异步委托。</param>
/// <param name="cancellationToken">调用方传入的取消令牌。</param>
/// <returns>异步执行结果。</returns>
private static async Task<object?> ExecuteContextAwareRequestAsync(
ICqrsContext context,
object target,
Func<Task<object?>> executeAsync,
CancellationToken cancellationToken)
{
PrepareTarget(context, target);
return await executeAsync().WaitAsync(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// 模拟 legacy bridge handler 的上下文注入语义,使测试替身与生产桥接行为保持一致。
/// </summary>
/// <param name="context">当前运行时接收到的架构上下文。</param>
/// <param name="target">即将执行的 legacy 目标对象。</param>
private static void PrepareTarget(ICqrsContext context, object target)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(target);
if (context is not GFramework.Core.Abstractions.Architectures.IArchitectureContext architectureContext)
{
throw new InvalidOperationException(
$"RecordingCqrsRuntime 期望收到 IArchitectureContext但实际为 '{context.GetType().FullName}'。");
}
if (target is IContextAware contextAware)
{
contextAware.SetContext(architectureContext);
}
}
}

View File

@ -159,6 +159,7 @@ public class AsyncQueryExecutorTests
{
Assert.That(result, Is.EqualTo(64));
Assert.That(runtime.LastRequest, Is.TypeOf<GFramework.Core.Cqrs.LegacyAsyncQueryDispatchRequest>());
Assert.That(query.ObservedContext, Is.SameAs(expectedContext));
});
}

View File

@ -7,7 +7,7 @@ CQRS 迁移与收敛。
## 当前恢复点
- 恢复点编号:`CQRS-REWRITE-RP-096`
- 恢复点编号:`CQRS-REWRITE-RP-097`
- 当前阶段:`Phase 8`
- 当前 PR 锚点:`PR #334`
- 当前结论:
@ -31,8 +31,9 @@ CQRS 迁移与收敛。
- `RP-093` 已把 `GFramework.Core` 的 legacy `SendCommand` / `SendQuery` 兼容入口收敛到底层统一 `GFramework.Cqrs` runtime同时补充 `Mediator` 未吸收能力差距复核
- `RP-094` 已按 `PR #334` latest-head review 收口 legacy bridge 的测试注册方式、模块运行时依赖契约、异步取消语义、XML 文档缺口与兼容文档回退边界
- `RP-095` 已继续收口 `PR #334` 剩余 review把 legacy 同步 bridge 的阻塞等待统一切到线程池隔离 helper、补齐 `ArchitectureContext` / executor 共享 dispatch helper、修正 bridge fixture 的并行与容器释放约束,并为 runtime bridge 与 async void command cancellation 增补回归测试
- 当前 `RP-096` 已再次使用 `$gframework-pr-review` 复核 `PR #334` latest-head review确认仍显示为 open 的 AI threads 在本地代码中已无新增仍成立的运行时 / 测试 / 文档缺陷,剩余差异主要是 GitHub thread 未 resolve 的状态滞后
- `ai-plan` active 入口现以 `RP-096` 为最新恢复锚点;`PR #334``PR #331``PR #326``PR #323``PR #307` 与其他更早阶段细节均以下方归档或说明为准
- `RP-096` 已再次使用 `$gframework-pr-review` 复核 `PR #334` latest-head review确认仍显示为 open 的 AI threads 在本地代码中已无新增仍成立的运行时 / 测试 / 文档缺陷,剩余差异主要是 GitHub thread 未 resolve 的状态滞后
- 当前 `RP-097` 已继续收口 `PR #334` latest-head nitpick`AsyncQueryExecutorTests` / `CommandExecutorTests` 补齐可观察的上下文保留断言,并让 `RecordingCqrsRuntime` 在测试替身返回错误响应类型时抛出带请求/类型信息的诊断异常
- `ai-plan` active 入口现以 `RP-097` 为最新恢复锚点;`PR #334``PR #331``PR #326``PR #323``PR #307` 与其他更早阶段细节均以下方归档或说明为准
## 当前活跃事实
@ -62,6 +63,8 @@ CQRS 迁移与收敛。
- 远端 `CTRF` 最新汇总为 `2311/2311` passedrun `#1079`, 2026-05-07
- `MegaLinter` 当前只暴露 `dotnet-format``Restore operation failed` 环境噪音,尚未提供本地仍成立的文件级格式诊断
- `PR #334` 当前 latest-head open AI feedback 经过本轮本地复核与修复后,应主要剩余待 GitHub 重新索引的状态差异或已实质关闭但未 resolve 的 thread
- `GFramework.Core.Tests` 中 legacy bridge 的“保留上下文”回归现在同时断言 bridge request 类型与目标对象执行期观察到的 `IArchitectureContext`
- `RecordingCqrsRuntime` 对非 `Unit` 响应已显式校验返回值类型;若测试工厂返回了 `null` 或错误装箱类型,异常会直接指出 request 类型与期望/实际响应类型
## 当前风险
@ -97,6 +100,14 @@ CQRS 迁移与收敛。
- 备注:确认当前分支对应 `PR #334``CodeRabbit` latest review 已 `APPROVED`,但 latest-head 仍显示 `10` 个 open thread、`Greptile` 仍显示 `3` 个 open thread本地逐项复核后未发现新的仍成立缺陷最新 CI 测试汇总为 `2311/2311` passed`MegaLinter` 仅剩 `dotnet-format` restore 环境噪音
- `dotnet build GFramework.Core/GFramework.Core.csproj -c Release`
- 结果:通过,`0 warning / 0 error`
- `dotnet build GFramework.Core.Tests/GFramework.Core.Tests.csproj -c Release`
- 结果:通过,`0 warning / 0 error`
- `dotnet test GFramework.Core.Tests/GFramework.Core.Tests.csproj -c Release --filter "FullyQualifiedName~CommandExecutorTests|FullyQualifiedName~AsyncQueryExecutorTests"`
- 结果:通过,`19/19` passed
- `python3 scripts/license-header.py --check`
- 结果:通过
- `git diff --check`
- 结果:通过
- `python3 .agents/skills/gframework-pr-review/scripts/fetch_current_pr_review.py --format json --json-output <temporary-json-output>`
- 结果:通过
- 备注:确认当前分支对应 `PR #331`,本轮 latest-head open AI feedback 已收敛到 `dotnet pack --no-build`、共享包校验脚本跨平台兼容性与 active 文档 PR 锚点同步

View File

@ -2,6 +2,26 @@
## 2026-05-07
### 阶段PR #334 nitpick 测试收尾CQRS-REWRITE-RP-097
- 继续处理 `PR #334` latest-head review 中仍值得本地吸收的轻量 nitpick范围限定在 legacy bridge 测试可观察性与测试替身诊断质量:
- `AsyncQueryExecutorTests.SendAsync_Should_Bridge_Through_Runtime_And_Preserve_Context` 标题声明“保留上下文”,但此前只断言了返回值与 bridge request 类型
- `CommandExecutorTests.Send_WithResult_Should_Bridge_Through_Runtime_And_Preserve_Context` 同样缺少可观察的上下文注入断言
- `RecordingCqrsRuntime` 直接强转响应对象,若测试工厂回错类型,失败信息不够聚焦
- 本轮主线程决策:
- 为两个 “Preserve_Context” 用例补齐 `ObservedContext``expectedContext` 的同一实例断言,使测试标题、注释与断言对象保持一致
- 让 `RecordingCqrsRuntime` 通过私有 helper 显式执行响应类型还原;当工厂返回 `null` 或错误装箱类型时,抛出包含 request 类型与期望/实际响应类型的 `InvalidOperationException`
- 同步刷新 `cqrs-rewrite` active tracking把本轮 nitpick 收敛与验证结果记录为新的恢复锚点 `RP-097`
- 本轮权威验证:
- `dotnet build GFramework.Core.Tests/GFramework.Core.Tests.csproj -c Release`
- 结果:通过,`0 warning / 0 error`
- `dotnet test GFramework.Core.Tests/GFramework.Core.Tests.csproj -c Release --filter "FullyQualifiedName~CommandExecutorTests|FullyQualifiedName~AsyncQueryExecutorTests"`
- 结果:通过,`19/19` passed
- `python3 scripts/license-header.py --check`
- 结果:通过
- `git diff --check`
- 结果:通过
### 阶段PR #334 latest-head review 复核CQRS-REWRITE-RP-096
- 再次使用 `$gframework-pr-review` 抓取 `feat/cqrs-optimization` 对应的 `PR #334` latest-head review并读取 `/tmp/current-pr-review.json` 中的 `review_agents``latest_commit_review``megalinter_report``test_reports`