#!/usr/bin/env bats
# Tests for scripts/orca-verify-render.sh (C-16 render-format contract validator).
# Covers happy path (valid input returns 0) and failure paths (schema mismatch,
# missing fields, missing file, missing argument).

load test_helper

VERIFY_RENDER="$SCRIPTS_DIR/orca-verify-render.sh"
TMP_BUNDLE=""

setup() {
	TMP_BUNDLE="$(mktemp)"
}

teardown() {
	[ -n "$TMP_BUNDLE" ] && rm -f "$TMP_BUNDLE"
}

@test "verify-render happy path: valid artifacts return 0" {
	cat >"$TMP_BUNDLE" <<'EOF'
{"schema_version":"orca.emit/v1","kind":"systemd","path":"/etc/systemd/system/x.service","content":"[Service]","mode":"0644"}
{"schema_version":"orca.emit/v1","kind":"traefik","path":"/etc/traefik/dynamic/orca.yml","content":"tls:{}","mode":"0644"}
EOF
	run "$VERIFY_RENDER" "$TMP_BUNDLE"
	assert_status 0 "$status"
	assert_contains "$output" "2 artifacts valid"
}

@test "verify-render failure: schema_version mismatch returns non-zero" {
	cat >"$TMP_BUNDLE" <<'EOF'
{"schema_version":"orca.emit/v2","kind":"systemd","path":"/x","mode":"0644"}
EOF
	run "$VERIFY_RENDER" "$TMP_BUNDLE"
	[ "$status" -ne 0 ]
	assert_contains "$output" "schema_version mismatch"
}

@test "verify-render failure: missing schema_version returns non-zero" {
	cat >"$TMP_BUNDLE" <<'EOF'
{"kind":"systemd","path":"/x","mode":"0644"}
EOF
	run "$VERIFY_RENDER" "$TMP_BUNDLE"
	[ "$status" -ne 0 ]
	assert_contains "$output" "missing schema_version"
}

@test "verify-render failure: missing bundle argument returns 2" {
	run "$VERIFY_RENDER"
	assert_status 2 "$status"
	assert_contains "$output" "usage:"
}

@test "verify-render failure: non-existent bundle returns 2" {
	run "$VERIFY_RENDER" "/nonexistent/bundle.json"
	assert_status 2 "$status"
	assert_contains "$output" "bundle not found"
}

@test "verify-render skips blank lines and comments" {
	cat >"$TMP_BUNDLE" <<'EOF'
# this is a comment

{"schema_version":"orca.emit/v1","kind":"systemd","path":"/x","content":"c","mode":"0644"}

EOF
	run "$VERIFY_RENDER" "$TMP_BUNDLE"
	assert_status 0 "$status"
	assert_contains "$output" "1 artifacts valid"
}