feat(cordis): 完成阶段5访问介导与诊断

This commit is contained in:
2026-08-17 01:09:32 +08:00
parent de5ab449cd
commit 8738e633ee
35 changed files with 1540 additions and 50 deletions
@@ -1,4 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
@@ -95,5 +96,56 @@ namespace ShrinkCommand.Integration.Network.Tests
Assert.IsFalse(_runtime.TryGetRaw<object>(_runtime.RootContext,
ShrinkDataSaverAppComponent.ServiceKey, out _));
}
[Test]
public void DataSaverIntercept_ReadOnlyViewAllowsReadsAndDeniesWriterCapability()
{
var provider = _runtime.Use(new ShrinkDataSaverAppComponent());
Assert.AreEqual(ShrinkFiberState.Active, provider.State);
var intercept = new Dictionary<string, IReadOnlyDictionary<string, object?>>
{
[ShrinkDataSaverAppComponent.ServiceKey] = new Dictionary<string, object?>
{
[ShrinkDataSaverAccess.MetadataKey] = ShrinkDataSaverAccess.ReadOnly
}
};
var consumerComponent = new DataSaverAccessConsumer();
var consumer = _runtime.Use(consumerComponent, intercept: intercept);
Assert.AreEqual(ShrinkFiberState.Active, consumer.State);
var reader = consumerComponent.GetReader();
Assert.IsNotNull(reader);
Assert.IsFalse(reader is IShrinkDataSaverWriter,
"只读上下文取得的对象不应再暴露写接口");
Assert.Throws<ShrinkCoeffectAccessDeniedException>(() => consumerComponent.GetWriter());
Assert.IsTrue(_runtime.TryGetRaw<ShrinkDataSaverService>(_runtime.RootContext,
ShrinkDataSaverAppComponent.ServiceKey, out _),
"编排层 raw read 保留原始服务;intercept 只介导组件访问");
}
private sealed class DataSaverAccessConsumer : IShrinkComponent
{
private ShrinkCtx? _ctx;
public string Name => "test.datasaver.read-only-consumer";
public IReadOnlyList<string> Inject { get; } = new[] { ShrinkDataSaverAppComponent.ServiceKey };
public IReadOnlyList<string> Provide => Array.Empty<string>();
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
{
_ctx = ctx;
return UniTask.CompletedTask;
}
public IShrinkDataSaverReader GetReader() => Context.Get<IShrinkDataSaverReader>(
ShrinkDataSaverAppComponent.ServiceKey);
public IShrinkDataSaverWriter GetWriter() => Context.Get<IShrinkDataSaverWriter>(
ShrinkDataSaverAppComponent.ServiceKey);
private ShrinkCtx Context => _ctx ?? throw new InvalidOperationException("Consumer is not active.");
}
}
}