#!/usr/bin/env bash

# Test mise github token command

# Clear any pre-existing token env vars
unset GITHUB_TOKEN GITHUB_API_TOKEN MISE_GITHUB_TOKEN MISE_GITHUB_ENTERPRISE_TOKEN

# Test 1: No token configured — should return (none)
assert_contains "mise github token" "(none)"

# Test 2: GITHUB_TOKEN env var
assert_contains "GITHUB_TOKEN=env-test-token mise github token --unmask" "env-test-token"
assert_contains "GITHUB_TOKEN=env-test-token mise github token --unmask" "GITHUB_TOKEN"

# Test 3: MISE_GITHUB_TOKEN takes precedence over GITHUB_TOKEN
assert_contains "MISE_GITHUB_TOKEN=mise-token GITHUB_TOKEN=other mise github token --unmask" "mise-token"
assert_contains "MISE_GITHUB_TOKEN=mise-token GITHUB_TOKEN=other mise github token --unmask" "MISE_GITHUB_TOKEN"

# Test 4: github_tokens.toml
mkdir -p "$MISE_CONFIG_DIR"
cat >"$MISE_CONFIG_DIR/github_tokens.toml" <<'EOF'
[tokens."github.com"]
token = "toml-test-token"

[tokens."ghe.example.com"]
token = "ghe-toml-token"
EOF
assert_contains "mise github token --unmask" "toml-test-token"
assert_contains "mise github token --unmask" "github_tokens.toml"
assert_contains "mise github token ghe.example.com --unmask" "ghe-toml-token"

# Test 5: Env var beats toml file
assert_contains "GITHUB_TOKEN=env-wins mise github token --unmask" "env-wins"
assert_not_contains "GITHUB_TOKEN=env-wins mise github token --unmask" "toml-test-token"

# Test 6: gh CLI hosts.yml
rm "$MISE_CONFIG_DIR/github_tokens.toml"
mkdir -p "$HOME/.config/gh"
cat >"$HOME/.config/gh/hosts.yml" <<'EOF'
github.com:
  oauth_token: gh-cli-token
  user: test
EOF
assert_contains "mise github token --unmask" "gh-cli-token"
assert_contains "mise github token --unmask" "gh CLI"

# Test 7: Token masking (default, no --unmask)
assert_not_contains "GITHUB_TOKEN=ghp_abcdefghijklmnop mise github token" "ghp_abcdefghijklmnop"
assert_contains "GITHUB_TOKEN=ghp_abcdefghijklmnop mise github token" "ghp_"

# Test 8: credential_command (with gh CLI hosts.yml still present)
# credential_command should beat gh CLI token
assert_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' mise github token --unmask" "cred-cmd-token"
assert_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' mise github token --unmask" "credential_command"
rm "$HOME/.config/gh/hosts.yml"

# Test 8b: credential_command beats github_tokens.toml
mkdir -p "$MISE_CONFIG_DIR"
cat >"$MISE_CONFIG_DIR/github_tokens.toml" <<'EOF'
[tokens."github.com"]
token = "toml-test-token"
EOF
assert_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' mise github token --unmask" "cred-cmd-token"
assert_not_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' mise github token --unmask" "toml-test-token"
rm "$MISE_CONFIG_DIR/github_tokens.toml"

# Test 8c: credential_command takes priority over git credential fill
assert_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' MISE_GITHUB_USE_GIT_CREDENTIALS=true mise github token --unmask" "cred-cmd-token"

# Test 8d: env var still beats credential_command
assert_contains "GITHUB_TOKEN=env-wins MISE_GITHUB_CREDENTIAL_COMMAND='echo cred-cmd-token' mise github token --unmask" "env-wins"

# Test 8e: credential_command receives host as $1
assert_contains "MISE_GITHUB_CREDENTIAL_COMMAND='echo token-for-\$1' mise github token ghe.example.com --unmask" "token-for-ghe.example.com"

# Test 9: git credential fill

# Create a standalone credential helper script
cat >"$HOME/git-cred-helper" <<'SCRIPT'
#!/bin/sh
echo "username=token"
echo "password=git-cred-token"
SCRIPT
chmod +x "$HOME/git-cred-helper"

# Write credential helper config to $HOME/.gitconfig so git finds it naturally
git config -f "$HOME/.gitconfig" credential.helper "$HOME/git-cred-helper"

GIT_CRED_ENV="GIT_CONFIG_NOSYSTEM=1 MISE_GITHUB_USE_GIT_CREDENTIALS=true"
assert_contains "$GIT_CRED_ENV mise github token --unmask" "git-cred-token"
assert_contains "$GIT_CRED_ENV mise github token --unmask" "git credential fill"

# Test 10: git credential disabled via setting
assert_contains "GIT_CONFIG_NOSYSTEM=1 MISE_GITHUB_USE_GIT_CREDENTIALS=false mise github token" "(none)"
