#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
TMP_ROOT="$TEST_ROOT/tmp"
API_BASE="http://localhost:31300/api/v1/master"
NODE_ID="$(cat "$TMP_ROOT/node_id")"

# 使用相同 ID 重注册，同时修改部分 meta/version 字段
payload=$(cat <<JSON
{
  "id": "$NODE_ID",
  "name": "dev-testuser-testinst-pod-0",
  "type": "agent",
  "meta_data": {
    "hostname": "dev-testuser-testinst-pod-0",
    "ip": "10.0.0.11",
    "env": "dev",
    "user": "testuser",
    "instance": "testinst",
    "cpu_number": 8,
    "memory_in_bytes": 2147483648,
    "gpu_number": 0
  },
  "version": "1.2.0"
}
JSON
)

status=$(curl -sS -o "$TMP_ROOT/reregister_response.json" -w '%{http_code}' -H 'Content-Type: application/json' -X POST "$API_BASE/nodes" -d "$payload")
if [[ "$status" != "200" ]]; then
  echo "[ERROR] 重注册返回非 200: $status" >&2
  cat "$TMP_ROOT/reregister_response.json" >&2
  exit 1
fi

python3 - "$TMP_ROOT/reregister_response.json" <<'PY'
import json, sys
with open(sys.argv[1]) as handle:
    node = json.load(handle)
assert node["meta_data"]["ip"] == "10.0.0.11", node["meta_data"]
assert node["meta_data"]["cpu_number"] == 8, node["meta_data"]
assert node["version"] == "1.2.0", node
PY

echo "[INFO] 重注册成功，元数据已更新"

# 未知 ID => 404
unknown_payload=$(cat <<'JSON'
{
  "id": "A999",
  "name": "dev-testuser-testinst-pod-0",
  "type": "agent",
  "meta_data": {
    "hostname": "dev-testuser-testinst-pod-0",
    "ip": "10.0.0.12",
    "env": "dev",
    "user": "testuser",
    "instance": "testinst",
    "cpu_number": 4,
    "memory_in_bytes": 2147483648,
    "gpu_number": 0
  },
  "version": "1.2.0"
}
JSON
)

status=$(curl -sS -o "$TMP_ROOT/unknown_id_response.json" -w '%{http_code}' -H 'Content-Type: application/json' -X POST "$API_BASE/nodes" -d "$unknown_payload")
if [[ "$status" != "404" ]]; then
  echo "[ERROR] 未知 ID 应返回 404，实际=$status" >&2
  cat "$TMP_ROOT/unknown_id_response.json" >&2
  exit 1
fi

echo "[INFO] 未知 ID 返回 404 验证通过"

# id 与 name 不匹配 => 500，节点保持原名
mismatch_payload=$(cat <<JSON
{
  "id": "$NODE_ID",
  "name": "dev-testuser-testinst-pod-0-mismatch",
  "type": "agent",
  "meta_data": {
    "hostname": "dev-testuser-testinst-pod-0-mismatch",
    "ip": "10.0.0.13",
    "env": "dev",
    "user": "testuser",
    "instance": "testinst",
    "cpu_number": 4,
    "memory_in_bytes": 2147483648,
    "gpu_number": 0
  },
  "version": "1.2.0"
}
JSON
)

status=$(curl -sS -o "$TMP_ROOT/mismatch_response.json" -w '%{http_code}' -H 'Content-Type: application/json' -X POST "$API_BASE/nodes" -d "$mismatch_payload")
if [[ "$status" != "500" ]]; then
  echo "[ERROR] 名称不匹配应返回 500，实际=$status" >&2
  cat "$TMP_ROOT/mismatch_response.json" >&2
  exit 1
fi

# 验证名称仍保持正确
curl -sS "$API_BASE/nodes/$NODE_ID" -o "$TMP_ROOT/post_mismatch_detail.json"
python3 - "$TMP_ROOT/post_mismatch_detail.json" <<'PY'
import json, sys
with open(sys.argv[1]) as handle:
    node = json.load(handle)
assert node["name"] == "dev-testuser-testinst-pod-0", node["name"]
PY

echo "[INFO] 名称不匹配返回 500，且原始节点未被篡改"
