fix(uns): reject tag create on non-existent equipment + narrow JSON catch (review)

This commit is contained in:
Joseph Doherty
2026-06-08 13:06:45 -04:00
parent 5a392c5db0
commit 77024f87da
2 changed files with 21 additions and 1 deletions
@@ -523,6 +523,9 @@ public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbF
return new UnsMutationResult(false, "TagConfig is not valid JSON.");
}
if (!await db.Equipment.AnyAsync(e => e.EquipmentId == equipmentId, ct))
return new UnsMutationResult(false, $"Equipment '{equipmentId}' not found.");
var equipmentCluster = await ResolveEquipmentClusterAsync(db, equipmentId, ct);
var guard = await CheckTagDriverGuardAsync(db, input.DriverInstanceId, equipmentCluster, ct);
if (guard is not null)
@@ -648,7 +651,7 @@ public sealed class UnsTreeService(IDbContextFactory<OtOpcUaConfigDbContext> dbF
using var _ = System.Text.Json.JsonDocument.Parse(json);
return true;
}
catch
catch (System.Text.Json.JsonException)
{
return false;
}
@@ -219,6 +219,23 @@ public sealed class UnsTreeServiceTagTests
result.Error.ShouldBe("Tag 'TAG-1' already exists.");
}
/// <summary>Creating a tag for an equipment id that does not exist returns a not-found error.</summary>
[Fact]
public async Task CreateTag_unresolvable_equipment_returns_error()
{
var (service, dbName) = Fresh();
SeedHierarchyAndDrivers(dbName, equipmentCluster: "MAIN", seedEquipmentDriver: true);
var result = await service.CreateTagAsync("EQ-NOPE", Input("TAG-1", "speed", "DRV-EQ"));
result.Ok.ShouldBeFalse();
result.Error.ShouldNotBeNull();
result.Error.ShouldContain("not found");
using var db = UnsTreeTestDb.CreateNamed(dbName);
db.Tags.Any(t => t.TagId == "TAG-1").ShouldBeFalse();
}
/// <summary>Creating a tag whose Name already exists on the same equipment is blocked.</summary>
[Fact]
public async Task CreateTag_duplicate_name_on_equipment_blocked()