# When a script is specified in a watch, updates should be taken into account
# See https://github.com/elastic/x-plugins/issues/4237
---
"Test transform scripts are updated on execution":
  - do:
      cluster.health:
        wait_for_status: yellow

  - do:
      put_script:
        id: transform-script
        body: >
          {
            "script":{
              "lang": "painless",
              "source":"return [ 'email': 'foo@bar.org' ]"
            }
          }

  - do:
      watcher.put_watch:
        id: "my_watch"
        body:  >
          {
            "trigger": {
              "schedule": {
                "interval": "1h"
              }
            },
            "input": {
              "simple": {}
            },
            "transform": {
              "script": {
                "id": "transform-script"
              }
            },
            "actions": {
              "my_log": {
                "logging": {
                  "text": "{{ctx}}"
                }
              }
            }
          }

  - match: { _id: "my_watch" }

  - do:
      watcher.execute_watch:
        id: "my_watch"

  - match: { "watch_record.watch_id": "my_watch" }
  - match: { "watch_record.result.transform.payload.email": "foo@bar.org" }

  - do:
      put_script:
        id: transform-script
        body: >
          {
            "script":
            {
              "lang": "painless",
              "source":"return [ 'email': 'foo@example.org' ]"
            }
          }

  - do:
      watcher.execute_watch:
        id: "my_watch"

  - match: { "watch_record.watch_id": "my_watch" }
  - match: { "watch_record.result.transform.payload.email": "foo@example.org" }

---
"Test condition scripts are updated on execution":
  - do:
      cluster.health:
        wait_for_status: yellow

  - do:
      put_script:
        id: condition-script
        body: >
          {
            "script":
            {
              "lang": "painless",
              "source": "return false"
            }
          }

  - do:
      watcher.put_watch:
        id: "my_watch"
        body:  >
          {
            "trigger": {
              "schedule": {
                "interval": "1h"
              }
            },
            "input": {
              "simple": {}
            },
            "condition": {
              "script": {
                "id": "condition-script"
              }
            },
            "actions": {
              "my_log": {
                "logging": {
                  "text": "{{ctx}}"
                }
              }
            }
          }

  - match: { _id: "my_watch" }

  - do:
      watcher.execute_watch:
        id: "my_watch"

  - match: { "watch_record.watch_id": "my_watch" }
  - match: { "watch_record.result.condition.met": false }

  - do:
      put_script:
        id: condition-script
        body: >
          {
            "script": {
              "lang": "painless",
              "source": "return true"
            }
          }

  - do:
      watcher.execute_watch:
        id: "my_watch"

  - match: { "watch_record.watch_id": "my_watch" }
  - match: { "watch_record.result.condition.met": true }

