# Tests that recursive definitions using disjunctions of validators and self-references
# do not produce duplicate constraints. A regression caused nested applications to
# accumulate redundant conjunctions instead of preserving the recursive structure.
# https://cuelang.org/issue/545
title: Simplification of validators.
-- in.cue --
package main

import (
	"strings"
	"time"
)

t1: {
	#Test: {
		#HTTP:  =~"^http://"
		#SSH:   !~"^ssh://"
		#USER:  strings.MinRunes(3)
		source: #HTTP | #SSH | #USER | #Test
	}

	foo: #Test & {
		source: "http://blablabla"
	}

	bar: #Test & {
		source: foo
	}
	@test(eq, {
		#Test: {
			#HTTP: =~"^http://"
			#SSH: !~"^ssh://"
			#USER: strings.MinRunes(3)
			source: =~"^http://" | !~"^ssh://" | strings.MinRunes(3)
		}
		foo: {
			source: "http://blablabla"
			#HTTP: =~"^http://"
			#SSH: !~"^ssh://"
			#USER: strings.MinRunes(3)
		}
		bar: {
			source: {
				source: "http://blablabla"
				#HTTP: =~"^http://"
				#SSH: !~"^ssh://"
				#USER: strings.MinRunes(3)
			}
			#HTTP: =~"^http://"
			#SSH: !~"^ssh://"
			#USER: strings.MinRunes(3)
		}
	})
}

t2: {
	str: "foo"
	a:   =~str
	b:   =~"foo"
	c:   a & b & a & b

	d: time.Time
	e: time.Time()
	f: d & e & d & e
	@test(eq, {
		str: "foo"
		a: =~"foo"
		b: =~"foo"
		c: =~"foo"
		d: time.Time()
		e: time.Time()
		f: time.Time()
	})
}
-- out/eval/stats --
Leaks:  7
Freed:  62
Reused: 41
Allocs: 28
Retain: 0

Unifications: 29
Conjuncts:    79
Disjuncts:    16
Notifications: 3

NumCloseIDs: 22
-- out/todo.txt --
Reordering of fields in t1.foo and t1.bar between v2 and v3 (source field order differs).
