#!/usr/bin/env bash
TEST_CATEGORY=platform

level=$1
if [ -z "$level" ]; then
  level=1
else
  shift
fi
echo "starting: ${BASH_SOURCE[@]##*/} <LOG_LEVEL=$level>"

source this
source test.suite

log.level $level

# platform.shared.oosh.invariant — post-state-31 invariant for ~/oosh.
#
# Sibling of platform.shared.config.invariant. Catches the macstudio
# regression where root's ~/oosh stays a real directory (init/oosh's
# initial clone) instead of being symlinked to the shared worktree at
# $sharedOoshBase/$OOSH_BRANCH. When that happens, the lazy
#   : ${OOSH_DIR:="$(cd "$HOME/oosh" 2>/dev/null && pwd -P || …)"}
# in sharedConfig/user.env (sourced by the bashrc) resolves OOSH_DIR to
# the user's private clone — oo.mode.base.get can't find the worktree
# base, `oo mode <TAB>` shows nothing.
#
# Run inside any user shell of `os platform.test <platform> terminal`
# after install completes:
#
#   ./test.suite run platform.shared.oosh.invariant 1
#
# Asserts:
#   1. ~/oosh is a symlink resolving INTO the shared sharedOosh tree.
#   2. Symlink target is a real directory.
#   3. oo.mode.base.get returns a worktree base (rc=0, non-empty path).
#   4. The base contains at least one OTHER worktree sibling (so
#      `oo mode <branch>` has actual targets to switch to).
#   5. oo.mode.completion.branch returns at least one branch.

ooshLink="$HOME/oosh"

# (1) symlink existence
test.case $level "platform.shared.oosh: ~/oosh is a symlink" test -L "$ooshLink"
if [ ! -L "$ooshLink" ]; then
  expect.fail "$ooshLink is not a symlink — state 31 didn't link ~/oosh for $(whoami)"
  test.suite.save.results
  return 0 2>/dev/null || exit 0
else
  expect.pass "$ooshLink is a symlink"
fi

# Resolve target via OOSH primitive (handles GNU readlink + BSD fallback).
resolved=$(private.this.path.canonical "$ooshLink")
test.case $level "platform.shared.oosh: symlink resolves to a real dir" test -d "$resolved"
if [ ! -d "$resolved" ]; then
  expect.fail "symlink target $resolved is not a directory"
  test.suite.save.results
  return 0 2>/dev/null || exit 0
else
  expect.pass "resolved to: $resolved"
fi

# (2) target lives under /home/shared (Linux) or /Users/Shared (macOS)
# — the canonical shared tree location. We check via path-prefix rather
# than a hard-coded path to keep platform-agnostic.
test.case $level "platform.shared.oosh: target is under shared base" true
case "$resolved" in
  */shared/EAMD.ucp/Components/com/ceruleanCircle/EAM/1_infrastructure/Once.sh/*)
    expect.pass "target under sharedOosh tree: $resolved"
    ;;
  *)
    expect.fail "target NOT under shared tree (got: $resolved) — root may have a private clone instead of the shared link"
    ;;
esac

# (3) oo.mode.base.get succeeds (worktree base detected)
source "$OOSH_DIR/oo" 2>/dev/null
base=$(oo.mode.base.get 2>/dev/null)
rc=$?
test.case $level "platform.shared.oosh: oo.mode.base.get returns a base" true
if [ "$rc" -eq 0 ] && [ -n "$base" ] && [ -d "$base" ]; then
  expect.pass "base=$base"
else
  expect.fail "oo.mode.base.get rc=$rc base=$base — completion will return empty"
fi

# (4) base contains at least one OTHER worktree sibling (so `oo mode <X>`
# has somewhere to switch to). Need ≥ 2 because the current dir counts as one.
siblingCount=0
if [ -n "$base" ] && [ -d "$base" ]; then
  for d in "$base"/*/; do
    [ -L "${d%/}" ] && continue
    [ -d "$d/.git" ] || [ -f "$d/.git" ] || continue
    siblingCount=$((siblingCount + 1))
  done
fi
test.case $level "platform.shared.oosh: base has at least 2 worktree siblings" true
if [ "$siblingCount" -ge 2 ]; then
  expect.pass "$siblingCount worktree sibling dirs found under $base"
else
  expect.fail "only $siblingCount sibling(s) under $base — `oo mode <branch>` has no real switch targets"
fi

# (5) oo.mode.completion.branch returns at least one branch (and ideally more).
completionOut=$(oo.mode.completion.branch 2>/dev/null)
branchCount=$(echo "$completionOut" | grep -cE '^[A-Za-z0-9._/-]+$')
test.case $level "platform.shared.oosh: oo.mode.completion.branch is non-empty" true
if [ "$branchCount" -ge 1 ]; then
  expect.pass "completion returned $branchCount branch(es): $(echo "$completionOut" | tr '\n' ' ')"
else
  expect.fail "completion returned empty — this is the user-visible macstudio bug"
fi

test.suite.save.results
