#!/usr/bin/env bash
# test/fn.sh — individual function test blocks
# Sourced by test.command.sh — do not execute directly.
# Requires run_cmd / run_cmd_fails from integration.sh to be sourced first.

function cmd::test::run_function() {
  local fn="$1"
  case "$fn" in
    cmd::block::run)       cmd::test::fn_block        ;;
    cmd::unblock::run)     cmd::test::fn_unblock      ;;
    cmd::remove::run)      cmd::test::fn_remove       ;;
    cmd::rename::run)      cmd::test::fn_rename       ;;
    cmd::rule::assign)     cmd::test::fn_rule_assign  ;;
    *)
      log::error "No function test defined for: ${fn}"
      return 1
      ;;
  esac
}

function cmd::test::fn_block() {
  test::section "cmd::block::run"

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" add --name testunit --type phone > /dev/null 2>&1

  cmd::test::run_cmd "block peer"            "blocked"  block --name phone-testunit
  cmd::test::run_cmd "block already blocked" "already"  block --name phone-testunit
  "$WGCTL_BINARY" unblock --name phone-testunit > /dev/null 2>&1 || true
  cmd::test::run_cmd "block with --type"     "blocked"  block --name testunit --type phone
  cmd::test::run_cmd_fails "block nonexistent"          block --name truly-nonexistent-xyz

  "$WGCTL_BINARY" unblock --name phone-testunit > /dev/null 2>&1 || true
  "$WGCTL_BINARY" remove  --name phone-testunit --force > /dev/null 2>&1 || true
}

function cmd::test::fn_unblock() {
  test::section "cmd::unblock::run"

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" add    --name testunit --type phone > /dev/null 2>&1
  "$WGCTL_BINARY" block  --name phone-testunit > /dev/null 2>&1

  cmd::test::run_cmd "unblock peer"          "unblocked"   unblock --name phone-testunit
  cmd::test::run_cmd "unblock not blocked"   "not blocked" unblock --name phone-testunit
  cmd::test::run_cmd_fails "unblock nonexistent"            unblock --name nonexistent-peer
  cmd::test::run_cmd_fails "unblock missing --name"         unblock

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
}

function cmd::test::fn_remove() {
  test::section "cmd::remove::run"

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" add    --name testunit --type phone > /dev/null 2>&1

  cmd::test::run_cmd "remove with --force"    "removed"  remove --name phone-testunit --force
  cmd::test::run_cmd_fails "remove nonexistent"          remove --name nonexistent-peer --force
  cmd::test::run_cmd_fails "remove missing --name"       remove --force
}

function cmd::test::fn_rename() {
  test::section "cmd::rename::run"

  "$WGCTL_BINARY" remove --name phone-testunit  --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" remove --name phone-testunit2 --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" add    --name testunit --type phone > /dev/null 2>&1

  cmd::test::run_cmd "rename peer"            "renamed"  rename --name phone-testunit  --new-name phone-testunit2
  cmd::test::run_cmd_fails "rename to existing"          rename --name phone-testunit2 --new-name phone-nuno
  cmd::test::run_cmd_fails "rename nonexistent"          rename --name phone-nonexistent --new-name phone-testunit
  cmd::test::run_cmd_fails "rename missing --new-name"   rename --name phone-testunit2

  "$WGCTL_BINARY" remove --name phone-testunit2 --force > /dev/null 2>&1 || true
}

function cmd::test::fn_rule_assign() {
  test::section "cmd::rule::assign"

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
  "$WGCTL_BINARY" add    --name testunit --type phone > /dev/null 2>&1

  cmd::test::run_cmd "rule assign" "Assigned" \
    rule assign --name admin --peer phone-testunit

  "$WGCTL_BINARY" remove --name phone-testunit --force > /dev/null 2>&1 || true
}