package iace

import (
	"strings"
	"testing"
)

// TestDomainGate_NamedLeakersGated pins the concrete cross-domain leakers that
// were observed firing for a Kistenhubgeraet (lift) project: generic patterns
// whose text names a press/welding/glass machine but which carry no machine
// type and only generic tags. After applyDomainGates they MUST require a dom_*
// tag, so they no longer fire for unrelated machines.
func TestDomainGate_NamedLeakersGated(t *testing.T) {
	// Confirmed cross-domain leakers observed firing for a lift project. (Note:
	// "Splitterflug bei Werkzeugbruch" has two patterns sharing the name; the
	// one that leaked carries a "Pressraum" zone and is gated via the zone
	// scan — verified empirically by the project re-seed, not pinned here to
	// avoid catching the unrelated high-pressure plastics variant HP514.)
	leakers := map[string]bool{
		"Quetschen Arm zwischen Pressenteilen":    true,
		"Quetschen durch Punktschweisselektroden": true,
		"Laerm bei Glasschneidemaschine":          true,
		"Laerm bei Blechbearbeitung (Stanzen)":    true,
	}
	seen := map[string]bool{}
	for _, p := range collectAllPatterns() {
		if !leakers[p.NameDE] {
			continue
		}
		seen[p.NameDE] = true
		hasDom := false
		for _, tag := range p.RequiredComponentTags {
			if strings.HasPrefix(tag, "dom_") {
				hasDom = true
				break
			}
		}
		if !hasDom {
			t.Errorf("%s (%q) not domain-gated; RequiredComponentTags=%v", p.ID, p.NameDE, p.RequiredComponentTags)
		}
	}
	for n := range leakers {
		if !seen[n] {
			t.Errorf("leaker pattern %q not found in library", n)
		}
	}
}
