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

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

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

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

# 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\]"'

# 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"'

# 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"'

# 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.*'

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

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

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

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

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

# 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.*'

# 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 .+

# 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\]"'

# 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"'

# 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".*'

# Username/password auth works when ANTITHESIS_API_KEY is not set.
mock-server 200 '{"ok": true}'
snouty api webhook -w basic_test --antithesis.duration 30

# ANTITHESIS_API_KEY switches authentication to bearer auth and removes the
#    ANTITHESIS_USERNAME / ANTITHESIS_PASSWORD requirement.
mock-server 200 '{"ok": true}'
env ANTITHESIS_API_KEY=test-api-key
env ANTITHESIS_USERNAME=
env ANTITHESIS_PASSWORD=
snouty api webhook -w basic_test --antithesis.duration 30

# When both auth styles are present, ANTITHESIS_API_KEY still takes precedence.
mock-server 200 '{"ok": true}'
env ANTITHESIS_USERNAME=testuser
env ANTITHESIS_PASSWORD=testpass
snouty api webhook -w basic_test --antithesis.duration 30

# -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"}
