Communication Layer (WP-1–5): - 8 message patterns with correlation IDs, per-pattern timeouts - Central/Site communication actors, transport heartbeat config - Connection failure handling (no central buffering, debug streams killed) Data Connection Layer (WP-6–14, WP-34): - Connection actor with Become/Stash lifecycle (Connecting/Connected/Reconnecting) - OPC UA + LmxProxy adapters behind IDataConnection - Auto-reconnect, bad quality propagation, transparent re-subscribe - Write-back, tag path resolution with retry, health reporting - Protocol extensibility via DataConnectionFactory Site Runtime (WP-15–25, WP-32–33): - ScriptActor/ScriptExecutionActor (triggers, concurrent execution, blocking I/O dispatcher) - AlarmActor/AlarmExecutionActor (ValueMatch/RangeViolation/RateOfChange, in-memory state) - SharedScriptLibrary (inline execution), ScriptRuntimeContext (API) - ScriptCompilationService (Roslyn, forbidden API enforcement, execution timeout) - Recursion limit (default 10), call direction enforcement - SiteStreamManager (per-subscriber bounded buffers, fire-and-forget) - Debug view backend (snapshot + stream), concurrency serialization - Local artifact storage (4 SQLite tables) Health Monitoring (WP-26–28): - SiteHealthCollector (thread-safe counters, connection state) - HealthReportSender (30s interval, monotonic sequence numbers) - CentralHealthAggregator (offline detection 60s, online recovery) Site Event Logging (WP-29–31): - SiteEventLogger (SQLite, 6 event categories, ISO 8601 UTC) - EventLogPurgeService (30-day retention, 1GB cap) - EventLogQueryService (filters, keyword search, keyset pagination) 541 tests pass, zero warnings.
157 lines
5.0 KiB
C#
157 lines
5.0 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using ScadaLink.SiteRuntime.Persistence;
|
|
|
|
namespace ScadaLink.SiteRuntime.Tests.Persistence;
|
|
|
|
/// <summary>
|
|
/// WP-33: Local Artifact Storage tests — shared scripts, external systems,
|
|
/// database connections, notification lists.
|
|
/// </summary>
|
|
public class ArtifactStorageTests : IAsyncLifetime, IDisposable
|
|
{
|
|
private readonly string _dbFile;
|
|
private SiteStorageService _storage = null!;
|
|
|
|
public ArtifactStorageTests()
|
|
{
|
|
_dbFile = Path.Combine(Path.GetTempPath(), $"artifact-test-{Guid.NewGuid():N}.db");
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
_storage = new SiteStorageService(
|
|
$"Data Source={_dbFile}",
|
|
NullLogger<SiteStorageService>.Instance);
|
|
await _storage.InitializeAsync();
|
|
}
|
|
|
|
public Task DisposeAsync() => Task.CompletedTask;
|
|
|
|
public void Dispose()
|
|
{
|
|
try { File.Delete(_dbFile); } catch { /* cleanup */ }
|
|
}
|
|
|
|
// ── Shared Script Storage ──
|
|
|
|
[Fact]
|
|
public async Task StoreSharedScript_RoundTrips()
|
|
{
|
|
await _storage.StoreSharedScriptAsync("CalcAvg", "return 42;", "{}", "int");
|
|
|
|
var scripts = await _storage.GetAllSharedScriptsAsync();
|
|
Assert.Single(scripts);
|
|
Assert.Equal("CalcAvg", scripts[0].Name);
|
|
Assert.Equal("return 42;", scripts[0].Code);
|
|
Assert.Equal("{}", scripts[0].ParameterDefinitions);
|
|
Assert.Equal("int", scripts[0].ReturnDefinition);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreSharedScript_Upserts_OnConflict()
|
|
{
|
|
await _storage.StoreSharedScriptAsync("CalcAvg", "return 1;", null, null);
|
|
await _storage.StoreSharedScriptAsync("CalcAvg", "return 2;", "{\"x\":\"int\"}", "int");
|
|
|
|
var scripts = await _storage.GetAllSharedScriptsAsync();
|
|
Assert.Single(scripts);
|
|
Assert.Equal("return 2;", scripts[0].Code);
|
|
Assert.Equal("{\"x\":\"int\"}", scripts[0].ParameterDefinitions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreSharedScript_MultipleScripts()
|
|
{
|
|
await _storage.StoreSharedScriptAsync("Script1", "1", null, null);
|
|
await _storage.StoreSharedScriptAsync("Script2", "2", null, null);
|
|
await _storage.StoreSharedScriptAsync("Script3", "3", null, null);
|
|
|
|
var scripts = await _storage.GetAllSharedScriptsAsync();
|
|
Assert.Equal(3, scripts.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreSharedScript_NullableFields()
|
|
{
|
|
await _storage.StoreSharedScriptAsync("Simple", "42", null, null);
|
|
|
|
var scripts = await _storage.GetAllSharedScriptsAsync();
|
|
Assert.Single(scripts);
|
|
Assert.Null(scripts[0].ParameterDefinitions);
|
|
Assert.Null(scripts[0].ReturnDefinition);
|
|
}
|
|
|
|
// ── External System Storage ──
|
|
|
|
[Fact]
|
|
public async Task StoreExternalSystem_DoesNotThrow()
|
|
{
|
|
await _storage.StoreExternalSystemAsync(
|
|
"WeatherAPI", "https://api.weather.com",
|
|
"ApiKey", "{\"key\":\"abc\"}", "{\"getForecast\":{}}");
|
|
|
|
// No exception = success. Query verification would need a Get method.
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreExternalSystem_Upserts()
|
|
{
|
|
await _storage.StoreExternalSystemAsync("API1", "https://v1", "Basic", null, null);
|
|
await _storage.StoreExternalSystemAsync("API1", "https://v2", "ApiKey", "{}", null);
|
|
|
|
// Upsert should not throw
|
|
}
|
|
|
|
// ── Database Connection Storage ──
|
|
|
|
[Fact]
|
|
public async Task StoreDatabaseConnection_DoesNotThrow()
|
|
{
|
|
await _storage.StoreDatabaseConnectionAsync(
|
|
"MainDB", "Server=localhost;Database=main", 3, TimeSpan.FromSeconds(1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreDatabaseConnection_Upserts()
|
|
{
|
|
await _storage.StoreDatabaseConnectionAsync(
|
|
"DB1", "Server=old", 3, TimeSpan.FromSeconds(1));
|
|
await _storage.StoreDatabaseConnectionAsync(
|
|
"DB1", "Server=new", 5, TimeSpan.FromSeconds(2));
|
|
|
|
// Upsert should not throw
|
|
}
|
|
|
|
// ── Notification List Storage ──
|
|
|
|
[Fact]
|
|
public async Task StoreNotificationList_DoesNotThrow()
|
|
{
|
|
await _storage.StoreNotificationListAsync(
|
|
"Ops Team", ["ops@example.com", "admin@example.com"]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task StoreNotificationList_Upserts()
|
|
{
|
|
await _storage.StoreNotificationListAsync("Team1", ["a@b.com"]);
|
|
await _storage.StoreNotificationListAsync("Team1", ["x@y.com", "z@w.com"]);
|
|
|
|
// Upsert should not throw
|
|
}
|
|
|
|
// ── Schema includes all WP-33 tables ──
|
|
|
|
[Fact]
|
|
public async Task Initialize_CreatesAllArtifactTables()
|
|
{
|
|
// The initialize already ran. Verify by storing to each table.
|
|
await _storage.StoreSharedScriptAsync("s", "code", null, null);
|
|
await _storage.StoreExternalSystemAsync("e", "url", "None", null, null);
|
|
await _storage.StoreDatabaseConnectionAsync("d", "connstr", 1, TimeSpan.Zero);
|
|
await _storage.StoreNotificationListAsync("n", ["email@test.com"]);
|
|
|
|
// All succeeded without exceptions = tables exist
|
|
}
|
|
}
|