feat(cordis): 接入上下文组合与模组事务热替换

This commit is contained in:
2026-08-16 23:20:40 +08:00
commit ad256f109b
676 changed files with 52168 additions and 0 deletions
@@ -0,0 +1,100 @@
#nullable enable
using NUnit.Framework;
using ShrinkApp.Starter.Basic;
using ShrinkCommand.Integration.App;
using ShrinkContext;
using ShrinkDataSaver.Integration.App;
using ShrinkNetwork;
using ShrinkNetwork.Integration.App;
using ShrinkApp;
using UnityEngine;
using Object = UnityEngine.Object;
namespace ShrinkContext.AppAdapter.Tests
{
/// <summary>
/// Basic Starter 默认装配表的端到端验证(阶段 3):
/// 三个功能模块走原生 Cordis 组件,四个跨包集成走按键注入的薄组件;
/// 提供者停用时依赖集成先退出,兼容服务门面也作为可逆效应注销。
/// </summary>
public class NativeWiringTests
{
[Test]
public void BasicComposition_UsesNativeComponents_AndRetiresDependentsReversibly()
{
var settings = ScriptableObject.CreateInstance<ShrinkAppSettings>();
ShrinkAppLoaderHost? host = null;
try
{
host = new ShrinkAppLoaderHost(settings);
ShrinkAppBasicContextComposition.Configure(host);
TestAwait.Run(host.StartAsync());
Assert.AreEqual(7, host.ModuleIds.Count);
Assert.AreEqual(7, host.ActiveModuleIds.Count);
Assert.IsTrue(host.IsModuleActive("shrink.network"));
Assert.IsTrue(host.IsModuleActive("shrink.command"));
Assert.IsTrue(host.IsModuleActive("shrink.datasaver"));
Assert.IsTrue(host.IsModuleActive("shrink.integration.command-network"));
Assert.IsTrue(host.IsModuleActive("shrink.integration.command-eventbus"));
Assert.IsTrue(host.IsModuleActive("shrink.integration.datasaver-eventbus"));
Assert.IsTrue(host.IsModuleActive("shrink.integration.network-eventbus"));
var root = host.Context.RootContext;
Assert.IsTrue(host.Context.TryGetRaw<ShrinkNetworkService>(root,
ShrinkNetworkAppComponent.ServiceKey, out _), "网络服务键由原生组件提供");
Assert.IsTrue(host.Context.TryGetRaw<ShrinkCommand.ShrinkCommandService>(root,
ShrinkCommandAppComponent.ServiceKey, out _), "命令服务键由原生组件提供");
Assert.IsTrue(host.Context.TryGetRaw<object>(root,
ShrinkDataSaverAppComponent.ServiceKey, out _), "存档服务键由原生组件提供");
Assert.IsTrue(host.Context.TryGetRaw<object>(root,
"shrink.integration.command-network", out _));
Assert.IsTrue(host.Context.TryGetRaw<object>(root,
"shrink.integration.command-eventbus", out _));
Assert.IsTrue(host.Context.TryGetRaw<object>(root,
"shrink.integration.datasaver-eventbus", out _));
Assert.IsTrue(host.Context.TryGetRaw<object>(root,
"shrink.integration.network-eventbus", out _));
Assert.IsTrue(host.Services.TryGet<ShrinkNetworkAppService>(out _));
Assert.IsTrue(host.Services.TryGet<ShrinkCommandAppService>(out _));
Assert.IsTrue(host.Services.TryGet<ShrinkDataSaverService>(out _));
TestAwait.Run(host.SetModuleDisabledAsync("shrink.command", true));
Assert.IsFalse(host.IsModuleActive("shrink.command"));
Assert.IsFalse(host.IsModuleActive("shrink.integration.command-network"));
Assert.IsFalse(host.IsModuleActive("shrink.integration.command-eventbus"));
Assert.IsTrue(host.IsModuleActive("shrink.network"), "无关提供者不得重载");
Assert.IsTrue(host.IsModuleActive("shrink.datasaver"), "无关提供者不得重载");
Assert.IsTrue(host.IsModuleActive("shrink.integration.network-eventbus"));
Assert.IsTrue(host.IsModuleActive("shrink.integration.datasaver-eventbus"));
Assert.IsFalse(host.Context.TryGetRaw<object>(root,
ShrinkCommandAppComponent.ServiceKey, out _));
Assert.IsFalse(host.Context.TryGetRaw<object>(root,
"shrink.integration.command-network", out _));
Assert.IsFalse(host.Context.TryGetRaw<object>(root,
"shrink.integration.command-eventbus", out _));
Assert.IsFalse(host.Services.TryGet<ShrinkCommandAppService>(out _),
"兼容门面应随组件退役注销");
TestAwait.Run(host.SetModuleDisabledAsync("shrink.command", false));
Assert.AreEqual(7, host.ActiveModuleIds.Count);
Assert.IsTrue(host.Services.TryGet<ShrinkCommandAppService>(out _));
TestAwait.Run(host.ShutdownAsync());
Assert.AreEqual(0, host.ActiveModuleIds.Count);
Assert.IsFalse(host.Services.TryGet<ShrinkNetworkAppService>(out _));
Assert.IsFalse(host.Services.TryGet<ShrinkCommandAppService>(out _));
Assert.IsFalse(host.Services.TryGet<ShrinkDataSaverService>(out _));
}
finally
{
if (host?.IsRunning == true)
TestAwait.Run(host.ShutdownAsync());
Object.DestroyImmediate(settings);
}
}
}
}