#!/usr/bin/env bats

load 'helpers'

setup() {
  setup_party_sandbox
  require_party_group
}
teardown() { teardown_party_sandbox; }

party_rec() { printf '%s/party-%s:%s.d/roster\n' "$PARTY_SOCKET_DIR" "$USER" "$1"; }

@test "party who lists invited and attached users" {
  "$PARTY_BIN" host whotest
  run "$PARTY_BIN" who --party whotest
  [ "$status" -eq 0 ]
  [[ "$output" == *"Party 'whotest'"* ]]
  [[ "$output" == *"Invited:"* ]]
  [[ "$output" == *"$USER"* ]]
  [[ "$output" == *"Attached:"* ]]
}

@test "party who --short emits a single line for status-right" {
  "$PARTY_BIN" host shorttest
  rec=$(party_rec shorttest)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  out=$("$PARTY_BIN" who --short --socket "$sock")
  case "$out" in
    party:*) ;;
    *) echo "got: $out"; false ;;
  esac
}

@test "party who --socket prints Party line (long form)" {
  "$PARTY_BIN" host wstest
  rec=$(party_rec wstest)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  run "$PARTY_BIN" who --socket "$sock"
  [ "$status" -eq 0 ]
  [[ "$output" == *"Party 'wstest'"* ]]
  [[ "$output" == *"host: $USER"* ]]
}

@test "party who --socket survives a missing roster (set -u regression)" {
  "$PARTY_BIN" host wsroster
  rec=$(party_rec wsroster)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  rm -f "$rec"
  run "$PARTY_BIN" who --socket "$sock"
  [ "$status" -eq 0 ]
  [[ "$output" == *"Party 'wsroster'"* ]]
  [[ "$output" == *"host: $USER"* ]]
}

@test "party who --socket rejects a path that is not a party socket" {
  run "$PARTY_BIN" who --socket /tmp/not-a-party-socket
  [ "$status" -eq 2 ]
  [[ "$output" == *"is not a party socket"* ]]
}

@test "party status reports hosting" {
  "$PARTY_BIN" host stat
  run "$PARTY_BIN" status
  [ "$status" -eq 0 ]
  [[ "$output" == *"hosting"* ]]
  [[ "$output" == *"stat"* ]]
}

@test "party status reports nothing when not in or hosting any party" {
  run "$PARTY_BIN" status
  [ "$status" -eq 0 ]
  [[ "$output" == *"not in any party"* ]]
}
