package iace

import (
	"testing"
)

// Spot-check sample of cross-references, asserting a couple of well-known
// regional pendants that I personally vetted. If these break, the matrix
// got corrupted; investigate before just updating the test.

func TestCrossRef_SpotChecks(t *testing.T) {
	cases := []struct {
		normID   string
		region   string
		mustHave string
		desc     string
	}{
		{"IEC-60601-1", "US-ANSI", "ES60601", "medical electrical equipment → ANSI/AAMI ES60601"},
		{"ISO-10218-1", "US-ANSI", "RIA R15.06", "industrial robots → RIA R15.06"},
		{"EN-388", "US-ANSI", "ISEA 105", "mech. gloves → ANSI/ISEA 105"},
		{"EN-352-1", "US-ANSI", "S3.19", "hearing protection → ANSI S3.19/S12.6"},
		{"EN-1176-1", "US-ASTM", "F1487", "playgrounds → ASTM F1487"},
		{"EN-13814", "US-ASTM", "F2291", "amusement rides → ASTM F2291"},
		{"EN-13445-1", "US-ASME", "Section VIII", "pressure vessels → ASME BPVC VIII"},
		{"EN-13480-1", "US-ASME", "B31.3", "process piping → ASME B31.3"},
		{"EN-60204-1", "US-NFPA", "NFPA 79", "industrial electrical → NFPA 79"},
		{"EN-12453", "US-UL", "UL 325", "garage doors → UL 325"},
		{"ISO-11681-1", "US-ANSI", "B175.1", "chainsaws → OPEI B175.1"},
		{"EN-ISO-5395-1", "US-ANSI", "B71.1", "lawnmowers → OPEI B71.1"},
		{"EN-ISO-20345", "US-ASTM", "F2413", "safety shoes → ASTM F2413"},
		{"EN-IEC-61400-1", "INTL-ISO", "IEC 61400-1", "wind turbine design → IEC 61400-1"},
		{"EN-149", "US-NIOSH", "42 CFR", "respirators → NIOSH N95 framework"},
	}

	for _, tc := range cases {
		cr := GetNormCrossRef(tc.normID)
		found := false
		for _, m := range cr.Mappings {
			if m.Region == tc.region && contains(m.Identifier, tc.mustHave) {
				found = true
				break
			}
		}
		if !found {
			t.Errorf("[%s] %s: expected %s mapping containing %q", tc.desc, tc.normID, tc.region, tc.mustHave)
		}
	}
}

func contains(s, sub string) bool {
	if sub == "" {
		return true
	}
	for i := 0; i+len(sub) <= len(s); i++ {
		if s[i:i+len(sub)] == sub {
			return true
		}
	}
	return false
}
