Files
cneicy 2e8cc588c4
Publish UPM package / publish (push) Failing after 1s
chore: initialize standalone UPM package
2026-08-26 02:49:53 +08:00

113 lines
5.9 KiB
C#

#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
{
/// <summary>
/// Basic Starter 默认装配表的端到端验证(阶段 3):
/// 三个功能模块走原生 Cordis 组件,四个跨包集成走按键注入的薄组件;
/// 提供者停用时依赖集成先退出,兼容服务门面也作为可逆效应注销。
/// </summary>
public class NativeWiringTests
{
[Test]
public void BasicComposition_UsesNativeComponents_AndRetiresDependentsReversibly()
{
var settings = ScriptableObject.CreateInstance<ShrinkAppSettings>();
ShrinkAppLoaderHost? host = null;
try
{
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());
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);
}
}
}
}