# Test that error() in a disjunction used as a pattern constraint key
# surfaces the custom error message instead of the generic "field not allowed".
#
# Issue: https://cuelang.org/issue/4208
-- in.cue --
#Environment: "sbx" | "stg" | "pro" | error("Must specify a valid environment")
#Envs: [Env=#Environment]: {}

// Valid key should work fine.
valid: #Envs   @test(eq, {["sbx"|"stg"|"pro"]: {}, sbx: {}}, hint="user error should be preserved in pattern")
valid: sbx: {}

// Invalid key should show the custom error message.
invalid: #Envs
invalid: foo: {} @test(err, code=eval, contains="field not allowed", incorrect) @test(err:todo, code=eval, contains="Must specify a valid environment")


@test(debug, """
	(_|_){
	  // [eval]
	  #Environment: (string){ |((string){ "sbx" }, (string){ "stg" }, (string){ "pro" }) }
	  #Envs: (#struct){
	  }
	  valid: (#struct){
	    sbx: (#struct){
	    }
	  }
	  invalid: (_|_){
	    // [eval]
	    foo: (_|_){
	      // [eval] invalid.foo: field not allowed:
	      //     in.cue:10:10
	    }
	  }
	}
	""")

-- out/errors.txt --
[eval] invalid.foo: field not allowed:
    ./in.cue:10:10
-- out/eval/stats --
Leaks:  0
Freed:  11
Reused: 2
Allocs: 9
Retain: 0

Unifications: 7
Conjuncts:    16
Disjuncts:    4

NumCloseIDs: 2
