#nullable enable using System; using System.Collections.Generic; 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 { /// /// Basic Starter 默认装配表的端到端验证(阶段 3): /// 三个功能模块走原生 Cordis 组件,四个跨包集成走按键注入的薄组件; /// 提供者停用时依赖集成先退出,兼容服务门面也作为可逆效应注销。 /// public class NativeWiringTests { [Test] public void BasicComposition_UsesNativeComponents_AndRetiresDependentsReversibly() { var settings = ScriptableObject.CreateInstance(); ShrinkAppLoaderHost? host = null; try { var coreInstallers = new List(); foreach (var installerType in ShrinkAppInstallers.GetDiscoveredInstallerTypes()) { if (Activator.CreateInstance(installerType) is not IShrinkAppModuleInstaller installer) continue; if (installer.ModuleId is "shrink.network" or "shrink.command" or "shrink.datasaver") coreInstallers.Add(installer); } host = new ShrinkAppLoaderHost(settings, coreInstallers); ShrinkAppBasicContextComposition.Configure(host); host.ApplyComposition(ShrinkAppBasicContextComposition.CreateDefaultDocument()); 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(root, ShrinkNetworkAppComponent.ServiceKey, out _), "网络服务键由原生组件提供"); Assert.IsTrue(host.Context.TryGetRaw(root, ShrinkCommandAppComponent.ServiceKey, out _), "命令服务键由原生组件提供"); Assert.IsTrue(host.Context.TryGetRaw(root, ShrinkDataSaverAppComponent.ServiceKey, out _), "存档服务键由原生组件提供"); Assert.IsTrue(host.Context.TryGetRaw(root, "shrink.integration.command-network", out _)); Assert.IsTrue(host.Context.TryGetRaw(root, "shrink.integration.command-eventbus", out _)); Assert.IsTrue(host.Context.TryGetRaw(root, "shrink.integration.datasaver-eventbus", out _)); Assert.IsTrue(host.Context.TryGetRaw(root, "shrink.integration.network-eventbus", out _)); Assert.IsTrue(host.Services.TryGet(out _)); Assert.IsTrue(host.Services.TryGet(out _)); Assert.IsTrue(host.Services.TryGet(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(root, ShrinkCommandAppComponent.ServiceKey, out _)); Assert.IsFalse(host.Context.TryGetRaw(root, "shrink.integration.command-network", out _)); Assert.IsFalse(host.Context.TryGetRaw(root, "shrink.integration.command-eventbus", out _)); Assert.IsFalse(host.Services.TryGet(out _), "兼容门面应随组件退役注销"); TestAwait.Run(host.SetModuleDisabledAsync("shrink.command", false)); Assert.AreEqual(7, host.ActiveModuleIds.Count); Assert.IsTrue(host.Services.TryGet(out _)); TestAwait.Run(host.ShutdownAsync()); Assert.AreEqual(0, host.ActiveModuleIds.Count); Assert.IsFalse(host.Services.TryGet(out _)); Assert.IsFalse(host.Services.TryGet(out _)); Assert.IsFalse(host.Services.TryGet(out _)); } finally { if (host?.IsRunning == true) TestAwait.Run(host.ShutdownAsync()); Object.DestroyImmediate(settings); } } } }