Files
ExecuteR/tests/Backend.Tests/InventoryReportCacheTests.cs
gsadmin cdfe02eb1b
CI / build-test (push) Successful in 11m26s
CI / images (push) Successful in 3m10s
CI / build-test (pull_request) Successful in 3m18s
CI / images (pull_request) Has been skipped
Script Configuration strip with self-headed sections; directory guard
The define strip is titled Script Configuration and every section
carries its own header — Parameters, Secrets, Files (S3), Functions —
instead of one mashed headline. Directory file deliveries now refuse a
leaf destination: the server rejects destinations whose last segment
looks like a file path (extension heuristic that tolerates version-like
dots), and the editor shows the same warning inline as you type.
Screenshots refreshed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-08 11:31:57 -04:00

70 lines
2.3 KiB
C#

using Backend.Groups;
using Xunit;
namespace Backend.Tests;
public class InventoryReportCacheTests
{
private const string MinimalReport = """
{
"identity": { "clientId": "c1", "hostname": "PC-001", "partOfDomain": false },
"os": { "caption": "Windows 11 Pro" },
"cpu": {}, "memory": [], "totalPhysicalMemoryBytes": 0,
"disks": [], "volumes": [], "network": [],
"collectedUtc": "2026-08-08T00:00:00Z"
}
""";
[Fact]
public void HitsOnlyWhileTheSnapshotTimestampMatches()
{
var cache = new InventoryReportCache();
var t1 = DateTimeOffset.UtcNow;
Assert.False(cache.TryGet("c1", t1, out _));
var stored = cache.Store("c1", t1, MinimalReport);
Assert.NotNull(stored);
Assert.True(cache.TryGet("c1", t1, out var hit));
Assert.Equal("Windows 11 Pro", hit!.Os.Caption);
// A newer snapshot timestamp invalidates the entry.
Assert.False(cache.TryGet("c1", t1.AddMinutes(15), out _));
}
[Fact]
public void CorruptJsonCachesAsNullInsteadOfReparsingForever()
{
var cache = new InventoryReportCache();
var t1 = DateTimeOffset.UtcNow;
Assert.Null(cache.Store("c1", t1, "{not json"));
Assert.True(cache.TryGet("c1", t1, out var report));
Assert.Null(report);
}
[Theory]
[InlineData(@"C:\temp\agent.msi", true)]
[InlineData("/opt/tools/archive.tar", true)]
[InlineData(@"%TEMP%\executer", false)]
[InlineData("/opt/tools/", false)]
[InlineData(@"C:\app-1.2", false)] // version-like dots are not extensions
[InlineData(@"C:\payload\v2.hotfix", true)]
public void DirectoryDestinationsRejectLeafFilePaths(string destination, bool looksLikeFile)
=> Assert.Equal(looksLikeFile, Backend.Scripts.ScriptEndpoints.LooksLikeFilePath(destination));
[Fact]
public void PruneDropsDevicesThatNoLongerExist()
{
var cache = new InventoryReportCache();
var t1 = DateTimeOffset.UtcNow;
cache.Store("alive", t1, MinimalReport);
cache.Store("gone", t1, MinimalReport);
cache.Prune(new HashSet<string>(StringComparer.Ordinal) { "alive" });
Assert.True(cache.TryGet("alive", t1, out _));
Assert.False(cache.TryGet("gone", t1, out _));
}
}