# Tests that closedness is correctly tracked when combining schemas through
# inheritance and embedding using the & operator. The issue caused CUE to reject
# fields that should be permitted under the merged schema definitions.
# https://cuelang.org/issue/599
-- in.cue --
package deps

a: {
	#A: {
		n: string
		depends_on: [...#AnyA]
	}

	#AnyA: {
		n: string
		depends_on: [...#AnyA]
		...
	}

	#A1: {
		#A
		x: int
		y: z: string // Comment to avoid error
	}

	#A2: {
		#A
		i: string
	}

	s: [Name=string]: #AnyA & {n: Name}

	s: foo: #A1 & {
		x: 10
		y: z: "z" // Comment to avoid error
	}

	s: bar: #A2 & {
		i: "hello bar"
		depends_on: [s.foo]
	}
	@test(eq, {
		#A: {n: string, depends_on: []}
		#AnyA: {n: string, depends_on: []}
		#A1: {
			x: int
			y: {z: string}
			n: string
			depends_on: []
		}
		#A2: {
			i: string
			n: string
			depends_on: []
		}
		s: {
			foo: {
				x: 10
				y: {z: "z"}
				n: "foo"
				depends_on: []
			}
			bar: {
				i: "hello bar"
				depends_on: [{
					x: 10
					y: {z: "z"}
					n: "foo"
					depends_on: []
				}]
				n: "bar"
			}
		}
	})
}

b: {
	#A: {
		f: {...}
	}

	#A1: {
		#A
		// f: {...} // Uncomment to fix
	}

	#C: {
		d: {...}
	}

	s: [string]: #A1

	s: foo: {
		f: #C & {
			d: foo: "bar"
		}
	}
	@test(eq, {
		#A: {f: {}}
		#A1: {f: {}}
		#C: {d: {}}
		s: {foo: {f: {d: {foo: "bar"}}}}
	})
}
-- out/eval/stats --
Leaks:  0
Freed:  47
Reused: 28
Allocs: 19
Retain: 0

Unifications: 47
Conjuncts:    105
Disjuncts:    0

NumCloseIDs: 31

ConjunctInfos:       87
MaxConjunctInfos:    6
MaxReqSets:          11
MaxRedirect:         2
-- out/todo.txt --
Reordering of fields between v2 and v3.
