GFramework/GFramework.Core.Tests/StateManagement/StoreEventBusExtensionsTests.cs
gewuyou ff553977e3 chore(license): 补齐 Apache-2.0 文件头治理
- 新增许可证文件头检查与修复脚本

- 补充维护者手动修复 PR 工作流和 CI 校验

- 更新贡献指南中的文件头说明

- 补齐仓库维护源码和配置文件的许可证声明
2026-05-03 19:39:49 +08:00

95 lines
3.6 KiB
C#

// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using GFramework.Core.Events;
namespace GFramework.Core.Tests.StateManagement;
/// <summary>
/// Store 到 EventBus 桥接扩展的单元测试。
/// 这些测试验证旧模块兼容桥接能够正确转发 dispatch 和状态变化事件,并支持运行时拆除。
/// </summary>
[TestFixture]
public class StoreEventBusExtensionsTests
{
/// <summary>
/// 测试桥接会发布每次 dispatch 事件,并对批处理后的状态变化只发送一次最终状态事件。
/// </summary>
[Test]
public void BridgeToEventBus_Should_Publish_Dispatches_And_Collapsed_State_Changes()
{
var eventBus = new EventBus();
var store = CreateStore();
var dispatchedEvents = new List<StoreDispatchedEvent<CounterState>>();
var stateChangedEvents = new List<StoreStateChangedEvent<CounterState>>();
eventBus.Register<StoreDispatchedEvent<CounterState>>(dispatchedEvents.Add);
eventBus.Register<StoreStateChangedEvent<CounterState>>(stateChangedEvents.Add);
store.BridgeToEventBus(eventBus);
store.Dispatch(new IncrementAction(1));
store.RunInBatch(() =>
{
store.Dispatch(new IncrementAction(1));
store.Dispatch(new IncrementAction(1));
});
Assert.That(dispatchedEvents.Count, Is.EqualTo(3));
Assert.That(dispatchedEvents[0].DispatchRecord.NextState.Count, Is.EqualTo(1));
Assert.That(dispatchedEvents[2].DispatchRecord.NextState.Count, Is.EqualTo(3));
Assert.That(stateChangedEvents.Count, Is.EqualTo(2));
Assert.That(stateChangedEvents[0].State.Count, Is.EqualTo(1));
Assert.That(stateChangedEvents[1].State.Count, Is.EqualTo(3));
}
/// <summary>
/// 测试桥接句柄注销后不会再继续向 EventBus 发送事件。
/// </summary>
[Test]
public void BridgeToEventBus_UnRegister_Should_Stop_Future_Publications()
{
var eventBus = new EventBus();
var store = CreateStore();
var dispatchedEvents = new List<StoreDispatchedEvent<CounterState>>();
var stateChangedEvents = new List<StoreStateChangedEvent<CounterState>>();
eventBus.Register<StoreDispatchedEvent<CounterState>>(dispatchedEvents.Add);
eventBus.Register<StoreStateChangedEvent<CounterState>>(stateChangedEvents.Add);
var bridge = store.BridgeToEventBus(eventBus);
store.Dispatch(new IncrementAction(1));
bridge.UnRegister();
store.Dispatch(new IncrementAction(1));
Assert.That(dispatchedEvents.Count, Is.EqualTo(1));
Assert.That(stateChangedEvents.Count, Is.EqualTo(1));
Assert.That(stateChangedEvents[0].State.Count, Is.EqualTo(1));
}
/// <summary>
/// 创建一个带基础 reducer 的测试 Store。
/// </summary>
/// <returns>测试用 Store 实例。</returns>
private static Store<CounterState> CreateStore()
{
var store = new Store<CounterState>(new CounterState(0, "Player"));
store.RegisterReducer<IncrementAction>((state, action) => state with { Count = state.Count + action.Amount });
return store;
}
/// <summary>
/// 用于桥接测试的状态类型。
/// </summary>
/// <param name="Count">当前计数值。</param>
/// <param name="Name">当前名称。</param>
private sealed record CounterState(int Count, string Name);
/// <summary>
/// 用于桥接测试的计数 action。
/// </summary>
/// <param name="Amount">要增加的数量。</param>
private sealed record IncrementAction(int Amount);
}