Files
ShrinkContext.AppAdapter/Tests/NativeWiringTests.cs
T

118 lines
5.9 KiB
C#

#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
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 UnityEngine.TestTools;
using Object = UnityEngine.Object;
namespace ShrinkContext.AppAdapter.Tests
{
/// <summary>
/// Basic Starter 默认装配表的端到端验证(阶段 3):
/// 三个功能模块走原生 Cordis 组件,四个跨包集成走按键注入的薄组件;
/// 提供者停用时依赖集成先退出,兼容服务门面也作为可逆效应注销。
/// </summary>
public class NativeWiringTests
{
private ShrinkAppSettings? _settings;
private ShrinkAppLoaderHost? _host;
[UnityTearDown]
public IEnumerator TearDown()
{
if (_host?.IsRunning == true)
yield return _host.ShutdownAsync().ToCoroutine();
if (_settings != null)
Object.DestroyImmediate(_settings);
}
[UnityTest]
public IEnumerator BasicComposition_UsesNativeComponents_AndRetiresDependentsReversibly()
{
_settings = ScriptableObject.CreateInstance<ShrinkAppSettings>();
var coreInstallers = new List<IShrinkAppModuleInstaller>();
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());
yield return _host.StartAsync().ToCoroutine();
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 _));
yield return _host.SetModuleDisabledAsync("shrink.command", true).ToCoroutine();
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 _),
"兼容门面应随组件退役注销");
yield return _host.SetModuleDisabledAsync("shrink.command", false).ToCoroutine();
Assert.AreEqual(7, _host.ActiveModuleIds.Count);
Assert.IsTrue(_host.Services.TryGet<ShrinkCommandAppService>(out _));
yield return _host.ShutdownAsync().ToCoroutine();
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 _));
}
}
}