# snouty api webhook
#
# Submit raw parameters to an Antithesis webhook endpoint.

# 1. Requires credentials from environment.
! snouty api webhook -w basic_test --antithesis.duration 30
stderr 'missing environment variable.*'

# 2. Requires --webhook/-w.
mock-server 200 '{}'
! snouty api webhook --antithesis.duration 30
stderr '--webhook.*'

# 3. At least one parameter is required.
mock-server 200 '{}'
! snouty api webhook -w basic_test
stderr 'no parameters provided.*'

# 4. Parameters are provided as --key value pairs and printed to stderr
#    with sensitive values redacted.
mock-server 200 '{"status": "ok"}'
snouty api webhook -w basic_test --antithesis.test_name my-test --antithesis.description 'nightly test run' --antithesis.config_image config:latest --antithesis.images app:latest --antithesis.duration 30 --antithesis.report.recipients team@example.com
stderr '"antithesis.test_name": "my-test"'
stderr '"antithesis.description": "nightly test run"'
stderr '"antithesis.config_image": "config:latest"'
stderr '"antithesis.images": "app:latest"'
stderr '"antithesis.duration": "30"'
stderr '"antithesis.report.recipients": "\[REDACTED\]"'

# 5. Parameters can be read from stdin via --stdin as JSON.
mock-server 200 '{"launched": true}'
stdin webhook_stdin.json
snouty api webhook -w basic_test --stdin
stderr '"antithesis.duration": "60"'
stderr '"antithesis.is_ephemeral": "true"'

# 6. Custom (non-antithesis) properties are accepted.
mock-server 200 '{"ok": true}'
snouty api webhook -w basic_test --antithesis.duration 30 --my.custom.prop value
stderr '"my.custom.prop": "value"'

# 7. Without --stdin flag, stdin content is ignored.
mock-server 200 '{"ok": true}'
stdin ignored_stdin.json
snouty api webhook -w basic_test --antithesis.duration 30
stderr '"antithesis.duration": "30"'
! stderr '.*SHOULD_BE_IGNORED.*'

# 8. --webhook long flag form accepted.
mock-server 200 '{"status": "ok"}'
snouty api webhook --webhook basic_k8s_test --antithesis.duration 30

# 9. Custom webhook names accepted.
mock-server 200 '{"status": "ok"}'
snouty api webhook -w my_custom_webhook --antithesis.duration 30

# 10. Invalid JSON on stdin fails.
mock-server 200 '{}'
stdin invalid_json.txt
! snouty api webhook -w basic_test --stdin
stderr 'invalid JSON.*'

# 11. Missing value for a --key flag fails.
mock-server 200 '{}'
! snouty api webhook -w basic_test --antithesis.duration
stderr 'missing value.*'

# 12. Unexpected positional argument fails.
mock-server 200 '{}'
! snouty api webhook -w basic_test notaflag
stderr 'unexpected argument.*'

# 13. API errors are reported with status code.
mock-server 400 '{"error": "bad request"}'
! snouty api webhook -w basic_test --antithesis.duration 30
stderr 'API error: 400.*'

# 14. On API error, nothing is printed to stdout.
mock-server 500 '{"error": "something went wrong"}'
! snouty api webhook -w basic_test --antithesis.duration 30
stderr 'API error: 500.*'
! stdout .+

# 15. Stdin JSON is merged with CLI args; CLI args take priority.
mock-server 200 '{"ok": true}'
stdin merge_stdin.json
snouty api webhook -w basic_test --stdin --antithesis.report.recipients team@example.com
stderr '"antithesis.duration": "60"'
stderr '"antithesis.description": "from stdin"'
stderr '"antithesis.report.recipients": "\[REDACTED\]"'

# 16. CLI args override values from stdin.
mock-server 200 '{"ok": true}'
stdin merge_stdin.json
snouty api webhook -w basic_test --stdin --antithesis.duration 120
stderr '"antithesis.duration": "120"'
stderr '"antithesis.description": "from stdin"'

# 17. On success, response body is printed as JSON to stdout.
mock-server 200 '{"session_id": "abc123", "status": "launched"}'
snouty api webhook -w basic_test --antithesis.duration 30
stdout '"session_id".*'
stdout '"status".*'

# 18. On success, no report email ETA is printed (unlike `run`).
mock-server 200 '{"ok": true}'
snouty api webhook -w basic_test --antithesis.duration 30
! stderr 'Expect a report email.*'

# 19. -c/--config flag is not accepted on api webhook.
! snouty api webhook -w basic_test -c /some/path --antithesis.duration 30

-- webhook_stdin.json --
{"antithesis.duration": "60", "antithesis.is_ephemeral": "true"}
-- ignored_stdin.json --
{"antithesis.duration": "SHOULD_BE_IGNORED"}
-- invalid_json.txt --
not valid json
-- merge_stdin.json --
{"antithesis.duration": "60", "antithesis.description": "from stdin"}
