---
setup:
  - do:
        indices.create:
          index: test-1
          body:
            mappings:
              properties:
                timestamp:
                  type: date

  - do:
      index:
        index:  test-1
        body:   { timestamp: "2015-01-02" }

  - do:
      indices.refresh:
        index: [test-1]

---
"Field caps with runtime mappings section":

  - requires:
      cluster_features: ["gte_v7.12.0"]
      reason: Runtime mappings support was added in 7.12

  - do:
      field_caps:
        index: test-*
        fields: "*"
        body:
          runtime_mappings:
            day_of_week:
              type: keyword
              script:
                source: "emit(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"

  - match: {indices:                             ["test-1"]}
  - length: {fields.timestamp:                            1}
  - match: {fields.timestamp.date.type:                date}
  - match: {fields.timestamp.date.searchable:          true}
  - match: {fields.timestamp.date.aggregatable:        true}
  - length: {fields.day_of_week:                          1}
  - match: {fields.day_of_week.keyword.type:           keyword}
  - match: {fields.day_of_week.keyword.searchable:        true}
  - match: {fields.day_of_week.keyword.aggregatable:      true}

---
"Field caps with runtime mappings section overwriting existing mapping":

  - requires:
      cluster_features: ["gte_v7.10.0"]
      reason: Runtime mappings support was added in 7.10

  - do:
      index:
        index:  test-2
        body:   { day_of_week: 123 }

  - do:
      field_caps:
        index: test-*
        fields: "day*"

  - match: {indices:                    ["test-1", "test-2"]}
  - length: {fields.day_of_week:                          1}
  - match: {fields.day_of_week.long.type:          long}
  - match: {fields.day_of_week.long.searchable:        true}
  - match: {fields.day_of_week.long.aggregatable:      true}

  - do:
      field_caps:
        index: test-*
        fields: "day*"
        body:
          runtime_mappings:
            day_of_week:
              type: keyword
              script:
                source: "emit(doc['timestamp'].value.dayOfWeekEnum.getDisplayName(TextStyle.FULL, Locale.ENGLISH))"

  - match: {indices:                    ["test-1", "test-2"]}
  - length: {fields.day_of_week:                          1}
  - match: {fields.day_of_week.keyword.type:          keyword}
  - match: {fields.day_of_week.keyword.searchable:        true}
  - match: {fields.day_of_week.keyword.aggregatable:      true}

---
"Field caps with errors in runtime mappings section throws":

  - requires:
      cluster_features: ["gte_v7.12.0"]
      reason: Runtime mappings support was added in 7.12

  - do:
      catch: bad_request
      field_caps:
        index: test-*
        fields: "*"
        body:
          runtime_mappings:
            day_of_week:
              type: keyword
              script:
                source: "bad syntax"

  - match: { error.type: "script_exception" }
  - match: { error.reason: "compile error" }
  - match: { error.script : "bad syntax" }
  - match: { error.lang : "painless" }
