using ZB.MOM.WW.ScadaBridge.ScriptAnalysis;
namespace ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests;
///
/// Review 2026-10-21 04/P3:
/// is memoised by code hash for the default (policy) reference set, so the bundle
/// importer's preview and apply passes — which call it with no change at their call
/// sites — do not redo a full-framework semantic analysis per script. A cache hit
/// returns the very same verdict instance, which is what these tests observe.
///
public class ScriptTrustValidatorCacheTests
{
/// Verifies that the same violating code analysed twice is served from the cache.
[Fact]
public void FindViolations_SameCodeTwice_SecondCallIsCacheHit()
{
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
var first = ScriptTrustValidator.FindViolations(code);
var second = ScriptTrustValidator.FindViolations(code);
Assert.NotEmpty(second);
Assert.Same(first, second); // pre-fix: a fresh list per call
}
/// Verifies that a clean verdict is cached too.
[Fact]
public void FindViolations_CleanCodeTwice_SecondCallIsCacheHit()
{
var code = $"var x = \"{Guid.NewGuid()}\".Length;";
var first = ScriptTrustValidator.FindViolations(code);
var second = ScriptTrustValidator.FindViolations(code);
Assert.Empty(second);
Assert.Same(first, second);
}
///
/// Code review 2026-12-02 CMN-5: the extra references are part of the cache key, so a
/// call with extras is never served the no-extras verdict (or vice versa), while a
/// repeat call with the same file-backed extras is a cache hit.
///
[Fact]
public void FindViolations_WithExtraReferences_KeyedSeparatelyFromNoExtras()
{
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location) };
// Named argument: a PortableExecutableReference[] positional argument binds to the
// explicit-base-references overload instead (the pre-CMN-5 version of this test did
// exactly that, so it never exercised the extras path).
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
var cached = ScriptTrustValidator.FindViolations(code);
Assert.Same(a, b); // same extras → same key
Assert.NotSame(a, cached); // extras vs none → different keys
Assert.NotEmpty(a);
Assert.NotEmpty(cached);
}
/// An extra reference with no stable identity (an in-memory image) is never cached.
[Fact]
public void FindViolations_WithInMemoryExtraReference_BypassesCache()
{
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
var image = File.ReadAllBytes(typeof(ScriptCompileSurface).Assembly.Location);
var extra = new[] { Microsoft.CodeAnalysis.MetadataReference.CreateFromImage(image) };
Assert.Null(ScriptTrustValidator.DigestExtraReferences(extra));
var a = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
var b = ScriptTrustValidator.FindViolations(code, extraReferences: extra);
Assert.NotSame(a, b);
}
/// Distinct extra-reference sets digest differently; the same set digests the same.
[Fact]
public void DigestExtraReferences_DistinguishesSets()
{
var surface = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
var surfaceAgain = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ScriptCompileSurface).Assembly.Location);
var linq = Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).Assembly.Location);
var aliased = surface.WithAliases(["other"]);
var one = ScriptTrustValidator.DigestExtraReferences([surface]);
Assert.NotNull(one);
Assert.Equal(one, ScriptTrustValidator.DigestExtraReferences([surfaceAgain]));
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([surface, linq]));
Assert.NotEqual(one, ScriptTrustValidator.DigestExtraReferences([aliased]));
}
///
/// The key carries every verdict input: code, the degraded flag, the base reference
/// fingerprint and the extras digest. Changing any one of them changes the key.
///
[Fact]
public void BuildKey_ChangesWithEveryInput()
{
var baseKey = TrustVerdictCache.BuildKey("code", degraded: false, "fp", "");
Assert.Equal(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", ""));
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code2", degraded: false, "fp", ""));
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: true, "fp", ""));
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp2", ""));
Assert.NotEqual(baseKey, TrustVerdictCache.BuildKey("code", degraded: false, "fp", "x"));
// Length-prefixing: moving characters between components cannot collide.
Assert.NotEqual(
TrustVerdictCache.BuildKey("code", degraded: false, "ab", "c"),
TrustVerdictCache.BuildKey("code", degraded: false, "a", "bc"));
}
/// Verifies that the shared verdict cannot be mutated by a caller to poison the cache.
[Fact]
public void FindViolations_ReturnedList_IsReadOnly()
{
var code = $"var x = System.IO.File.Exists(\"{Guid.NewGuid()}\");";
var verdict = ScriptTrustValidator.FindViolations(code);
Assert.False(verdict is List);
Assert.True(verdict is System.Collections.Generic.ICollection { IsReadOnly: true });
}
}