#!/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 invite adds user to access list" {
  "$PARTY_BIN" host invtest
  other=$(pick_other_user); [ -n "$other" ] || skip "no second user"
  "$PARTY_BIN" invite "$other"
  rec=$(party_rec invtest)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  tmux -S "$sock" server-access -l | grep -q "^$other"
}

@test "party invite -r adds user as read-only" {
  "$PARTY_BIN" host invro
  other=$(pick_other_user); [ -n "$other" ] || skip
  "$PARTY_BIN" invite "$other" -r
  rec=$(party_rec invro)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  tmux -S "$sock" server-access -l | grep "^$other" | grep -qiE 'read|\(R\)'
}

@test "party voice / party mute toggle write access" {
  "$PARTY_BIN" host vmtest
  other=$(pick_other_user); [ -n "$other" ] || skip
  "$PARTY_BIN" invite "$other" -r
  "$PARTY_BIN" voice "$other"
  "$PARTY_BIN" mute "$other"
}

@test "party kick removes from access list and kills guest session" {
  "$PARTY_BIN" host ktest
  other=$(pick_other_user); [ -n "$other" ] || skip
  "$PARTY_BIN" invite "$other"
  rec=$(party_rec ktest)
  sock=$(awk -F= '$1=="SOCKET"{print $2}' "$rec")
  # Inject a fake guest session as if the user had joined.
  tmux -S "$sock" new-session -d -t ktest -s "__party_guest_$other"
  "$PARTY_BIN" kick "$other"
  ! tmux -S "$sock" server-access -l | grep -q "^$other"
  ! tmux -S "$sock" has-session -t "__party_guest_$other" 2>/dev/null
}

