Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests/NegativeTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

129 lines
5.7 KiB
C#

using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Persistence;
using ZB.MOM.WW.ScadaBridge.TestSupport;
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests;
/// <summary>
/// Negative tests verifying design constraints.
/// </summary>
public class NegativeTests
{
/// <summary>Verifies the case: schema, no Alarm State Table.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Schema_NoAlarmStateTable()
{
// Per design decision: no alarm state table in site SQLite schema.
// The site SQLite stores only deployed configs and static attribute overrides.
//
// SiteStorageService takes an ILocalDb now, and LocalDb has no in-memory mode
// (its Path is a filesystem path), so the service is initialized over a real
// temp file instead of the "Data Source=:memory:" it used before. The manually
// built schema subset below is still a plain in-memory SqliteConnection — it is
// not a LocalDb store, just a scratch database this test asserts against.
var localDb = TestLocalDb.CreateTemp("negative-schema");
try
{
var storage = new SiteStorageService(
localDb.Db,
NullLogger<SiteStorageService>.Instance);
await storage.InitializeAsync();
// Try querying a non-existent alarm_states table — should throw
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
// Re-initialize on this connection to get the schema
await using var initCmd = connection.CreateCommand();
initCmd.CommandText = @"
CREATE TABLE IF NOT EXISTS deployed_configurations (
instance_unique_name TEXT PRIMARY KEY,
config_json TEXT NOT NULL,
deployment_id TEXT NOT NULL,
revision_hash TEXT NOT NULL,
is_enabled INTEGER NOT NULL DEFAULT 1,
deployed_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS static_attribute_overrides (
instance_unique_name TEXT NOT NULL,
attribute_name TEXT NOT NULL,
override_value TEXT NOT NULL,
updated_at TEXT NOT NULL,
PRIMARY KEY (instance_unique_name, attribute_name)
);";
await initCmd.ExecuteNonQueryAsync();
// Verify alarm_states does NOT exist
await using var checkCmd = connection.CreateCommand();
checkCmd.CommandText = "SELECT name FROM sqlite_master WHERE type='table' AND name='alarm_states'";
var result = await checkCmd.ExecuteScalarAsync();
Assert.Null(result);
}
finally
{
// Dispose first — the master connection anchors the WAL, so the sidecars
// cannot be removed while it is open.
var path = localDb.Path;
localDb.Dispose();
TestLocalDb.DeleteFiles(path);
}
}
/// <summary>Verifies the case: schema, no Local Config Authoring.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task Schema_NoLocalConfigAuthoring()
{
// Per design: sites cannot author/modify template configurations locally.
// The SQLite schema has no template tables or editing tables.
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
await using var initCmd = connection.CreateCommand();
initCmd.CommandText = @"
CREATE TABLE IF NOT EXISTS deployed_configurations (
instance_unique_name TEXT PRIMARY KEY,
config_json TEXT NOT NULL,
deployment_id TEXT NOT NULL,
revision_hash TEXT NOT NULL,
is_enabled INTEGER NOT NULL DEFAULT 1,
deployed_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS static_attribute_overrides (
instance_unique_name TEXT NOT NULL,
attribute_name TEXT NOT NULL,
override_value TEXT NOT NULL,
updated_at TEXT NOT NULL,
PRIMARY KEY (instance_unique_name, attribute_name)
);";
await initCmd.ExecuteNonQueryAsync();
// Verify no template editing tables exist
await using var checkCmd = connection.CreateCommand();
checkCmd.CommandText = "SELECT COUNT(*) FROM sqlite_master WHERE type='table'";
var tableCount = (long)(await checkCmd.ExecuteScalarAsync())!;
// Only 2 tables in this manually-created schema (tests the constraint that
// no template editing tables exist in the manually-created subset)
Assert.Equal(2, tableCount);
}
/// <summary>Verifies the case: site Node, does Not Bind Http Ports.</summary>
[Fact]
public void SiteNode_DoesNotBindHttpPorts()
{
// Per design: site nodes use Host.CreateDefaultBuilder (not WebApplication.CreateBuilder).
// This is verified structurally — the site path in Program.cs does not configure Kestrel.
// This test documents the constraint; the actual verification is in the Program.cs code.
// The SiteRuntime project does not reference ASP.NET Core packages
var siteRuntimeAssembly = typeof(SiteRuntimeOptions).Assembly;
var referencedAssemblies = siteRuntimeAssembly.GetReferencedAssemblies();
Assert.DoesNotContain(referencedAssemblies,
a => a.Name != null && a.Name.Contains("AspNetCore"));
}
}