package contract import ( "os" "path/filepath" "strings" "testing" ) func TestRejectsRetiredCallerProfiles(t *testing.T) { for _, tc := range []struct { name, file, from, to string }{ {"config-read", "config-read-sip.json", `"caller_id":"BD00000000"`, `"caller_profiles":[{"caller_profile_id":"legacy","caller_id":"BD00000000"}]`}, {"config-read", "config-read-task-asr.json", `"route_policy_id":"route-mock"`, `"route_policy_id":"route-mock","caller_profile_id":"legacy"`}, {"mq", "mq-result-no-recording.json", `"task_id":"task-asr"`, `"task_id":"task-asr","caller_profile_id":"legacy"`}, } { raw, err := os.ReadFile(filepath.Join("..", "..", "contracts", "local", "examples", tc.file)) if err != nil { t.Fatal(err) } if !strings.Contains(string(raw), tc.from) { t.Fatalf("missing test fixture field in %s", tc.file) } changed := strings.Replace(string(raw), tc.from, tc.to, 1) if err := ValidateCurrent(tc.name, []byte(changed)); err == nil { t.Fatalf("retired caller profile accepted by %s", tc.file) } } } func TestValidateCurrent(t *testing.T) { for _, tc := range []struct { name string file string valid bool }{ {"config-read", "config-read-task-asr.json", true}, {"config-read", "config-read-task-full.json", true}, {"config-read", "config-read-sip.json", true}, {"config-read", "config-read-providers.json", true}, {"config-read", "config-read-quota.json", true}, {"task-discovery", "task-discovery-end.json", true}, {"mq", "mq-execute.json", true}, {"config-read", "invalid/config-read-string-tenant.json", false}, {"mq", "invalid/mq-legacy-schema-version.json", false}, } { t.Run(tc.file, func(t *testing.T) { raw, err := os.ReadFile(filepath.Join("..", "..", "contracts", "local", "examples", tc.file)) if err != nil { t.Fatal(err) } if err := ValidateCurrent(tc.name, raw); (err == nil) != tc.valid { t.Fatalf("valid=%v, err=%v", tc.valid, err) } }) } for _, name := range []string{"", "config-read-v0.3", "../upstream/v1/mq"} { if err := ValidateCurrent(name, []byte(`{}`)); err == nil || !strings.Contains(err.Error(), "invalid") { t.Fatalf("accepted unknown contract %q: %v", name, err) } } }