---
cases:
  - note: refheads/general, single var
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r := i if q := ["a", "b", "c"][i]
    want_result:
      - x:
          a:
            r: 0
          b:
            r: 1
          c:
            r: 2
  - note: refheads/general, multiple vars
    query: data.test.p = x
    modules:
      - |
        package test

        p[q][r] if q := ["a", "b", "c"][r]
    want_result:
      - x:
          a:
            "0": true
          b:
            "1": true
          c:
            "2": true
  - note: refheads/general, deep query
    query: data.test.p.b = x
    modules:
      - |
        package test

        p[q][r] if q := ["a", "b", "c"][r]
    want_result:
      - x:
          "1": true
  - note: refheads/general, overlapping rule, no conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r := i if q := ["a", "b", "c"][i]

        p.a.r := 0
    want_result:
      - x:
          a:
            r: 0
          b:
            r: 1
          c:
            r: 2
  - note: refheads/general, overlapping rule, different dynamic depths, no conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r.s := i if q := ["a", "b", "c"][i]

        p.d.r := 3

        p.e := 4
    want_result:
      - x:
          a:
            r:
              s: 0
          b:
            r:
              s: 1
          c:
            r:
              s: 2
          d:
            r: 3
          e: 4
  - note: refheads/general, self-conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r.s := i if q := ["a", "b", "c", "b"][i]
    want_error_code: eval_conflict_error
  - note: refheads/general, overlapping rule, conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r.s := i if q := ["a", "b", "c"][i]

        p.a.r := 42
    want_error_code: eval_conflict_error
  - note: refheads/general, overlapping rule, deep override inside other rule object value, conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r := v if {
        	q := "q"
        	v := {"s": {"t": 1}}
        }

        p.q.r.s.t := 42
    want_error_code: eval_conflict_error
  - note: refheads/general, overlapping rule, deep injection into other rule object value, conflict
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r := v if {
        	q := "q"
        	v := {"s": {"t": 1}}
        }

        p.q.r.s.u := 42
    want_error_code: eval_conflict_error
  - note: refheads/general, set leaf (shallow ref)
    query: data.test.p = x
    modules:
      - |
        package test

        p[q] contains r if {
        	x := ["a", "b", "c"]
        	q := x[_]
        	r := x[_]
        	q != r
        }

        p.b contains "foo"
    want_result:
      - x:
          a:
            - b
            - c
          b:
            - a
            - c
            - foo
          c:
            - a
            - b
  - note: refheads/general, set leaf (other rule defines dynamic ref portion)
    query: data.test.p = x
    modules:
      - |
        package test

        p.q contains r if {
            r := ["a", "b", "c"][_]
        }

        p[q] := r if {
            q := "foo"
            r := "bar"
        }
    want_result:
      - x:
          foo: bar
          q:
            - a
            - b
            - c
  - note: refheads/general, set leaf
    query: data.test.p = x
    modules:
      - |
        package test

        p[q].r contains s if {
        	x := ["a", "b", "c"]
        	q := x[_]
        	s := x[_]
        	q != s
        }

        p.b.r contains "foo"
    want_result:
      - x:
          a:
            r:
              - b
              - c
          b:
            r:
              - a
              - c
              - foo
          c:
            r:
              - a
              - b
  - note: refheads/general, set leaf, deep query
    query: data.test.p.b.r.c = x
    modules:
      - |
        package test

        p[q].r contains s if {
        	x := ["a", "b", "c"]
        	q := x[_]
        	s := x[_]
        	q != s
        }

        p.b.r contains "foo"
    want_result:
      - x: c
  - note: refheads/general, input var
    query: data.test.p = x
    modules:
      - |
        package test

        p[input.x].r := "foo"
    input:
      x: bar
    want_result:
      - x:
          bar:
            r: foo
  - note: refheads/general, external non-ground var
    query: data.test.p = x
    modules:
      - |
        package test

        a := [x | x := input.x[_]]

        b := input.y

        p[a[b]].r[s] := i if {
        	s := a[i]
        }
    input:
      x:
        - foo
        - bar
        - baz
      "y": 1
    want_result:
      - x:
          bar:
            r:
              bar: 1
              baz: 2
              foo: 0
  - note: refheads/general, multiple result-set entries
    query: data.test.p.q[i].s = x
    modules:
      - |
        package test

        p.q[r].s := 1 if r := "foo"

        p.q[r].s := 2 if r := "bar"
    want_result:
      - i: foo
        x: 1
      - i: bar
        x: 2
