using System.Text.RegularExpressions;
using Shouldly;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns;
///
/// R04 H1 source pin (no bUnit): every destructive button on the equipment page and the script editor
/// opens a confirm dialog instead of deleting on the first click. The live check (each delete confirms)
/// is in the Wave 4 docker-dev pass.
///
[Trait("Category", "Unit")]
public sealed class DeleteConfirmationWiringTests
{
[Fact]
public void Equipment_page_routes_every_destructive_row_action_through_the_confirm()
{
var src = AdminUiSource.Read("Components", "Pages", "Uns", "EquipmentPage.razor");
src.ShouldContain(" ConfirmRemoveReference(r)\"");
src.ShouldContain("@onclick=\"() => ConfirmDeleteVirtualTag(v.VirtualTagId, v.Name)\"");
src.ShouldContain("@onclick=\"() => ConfirmDeleteAlarm(a.ScriptedAlarmId, a.Name)\"");
// No danger button may call a delete handler directly.
Regex.Matches(src, @"btn-outline-danger[^>]*@onclick=""\(\) => (Delete|Remove)\w*\(").Count.ShouldBe(0);
// Positive control: the page still has its three danger buttons.
Regex.Matches(src, "btn-outline-danger").Count.ShouldBe(3);
}
///
/// #832: the shared confirm used to sit INSIDE the Alarms tab branch, so it was on the page only while that
/// tab was showing — the Tags tab's Remove and the Virtual Tags tab's Delete set the dialog visible and
/// nothing appeared. Pinned by brace depth: the dialog must be at the depth of the tab @if chain,
/// not inside any branch of it.
///
[Fact]
public void Equipment_page_confirm_renders_outside_every_tab_branch()
{
var src = AdminUiSource.Read("Components", "Pages", "Uns", "EquipmentPage.razor");
var markup = src[..src.IndexOf("@code", StringComparison.Ordinal)];
int DepthAt(int index) =>
markup[..index].Count(c => c == '{') - markup[..index].Count(c => c == '}');
var tabChain = markup.IndexOf("@if (_activeTab == \"details\")", StringComparison.Ordinal);
var dialog = markup.IndexOf(" _confirmDeleteVisible = true\"");
src.ShouldContain("OnConfirm=\"DeleteAsync\"");
src.ShouldNotContain("@onclick=\"DeleteAsync\"");
}
}