#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using ShrinkContext;
namespace ShrinkContext.Tests
{
/// 提供者测试组件:把 Value 设置到供给键上;OnApply 作为装载闸门。
public sealed class ProviderComponent : IShrinkComponent
{
private readonly string[] _provide;
public ProviderComponent(string name, string key, object? value = null)
{
Name = name;
Value = value;
_provide = new[] { key };
}
public string Name { get; }
public object? Value { get; set; }
public IReadOnlyList Inject { get; set; } = Array.Empty();
public IReadOnlyList Provide => _provide;
public Func? OnApply { get; set; }
public int ApplyCount { get; private set; }
public async UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
ApplyCount++;
if (OnApply != null)
await OnApply();
foreach (var key in _provide)
ctx.Set(key, Value);
}
}
/// 消费者测试组件:装载时读取依赖键的当前值;OnApply 作为装载闸门。
public sealed class ConsumerComponent : IShrinkComponent
{
private readonly string[] _inject;
public ConsumerComponent(string name, string key)
{
Name = name;
Key = key;
_inject = new[] { key };
}
public string Name { get; }
public string Key { get; }
public IReadOnlyList Inject => _inject;
public IReadOnlyList Provide => Array.Empty();
public Func? OnApply { get; set; }
public int LoadCount { get; private set; }
public object? LastSeen { get; private set; }
public async UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
LoadCount++;
if (OnApply != null)
await OnApply();
LastSeen = ctx.Get