#!/usr/bin/env bash
# Tests for this — OOSH kernel
# Tests: method dispatch, parameter parsing, constructor, single-word dispatch

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

source this
source test.suite

log.level $level

# ============================================================================
# T1: this.start dispatches dotted method correctly
# ============================================================================
test.case $level "this.start dispatches dotted method (config.list)" \
  config list

if [ "$RETURN_VALUE" -eq 0 ]; then
  expect.pass "config list dispatched successfully"
else
  expect.fail "config list should dispatch to config.list()"
fi

# ============================================================================
# T2: this.absolutePath resolves relative paths
# ============================================================================
test.case $level "this.absolutePath resolves relative paths" \
  this.absolutePath "."

if [ -n "$RESULT" ] && [[ "$RESULT" == /* ]]; then
  expect.pass "this.absolutePath returned absolute path: $RESULT"
else
  expect.fail "this.absolutePath should return absolute path, got: $RESULT"
fi

# ============================================================================
# T3: this.isNumber validates numbers
# ============================================================================
test.case $level "this.isNumber validates numbers" \
  this.isNumber 42

if [ "$?" -eq 0 ]; then
  expect.pass "this.isNumber recognizes 42 as a number"
else
  expect.fail "this.isNumber should recognize 42 as a number"
fi

# ============================================================================
# T4: this.isNumber rejects non-numbers
# ============================================================================
test.case $level "this.isNumber rejects non-numbers" \
  this.isNumber "abc"

# Note: test.case sets RETURN_VALUE, not $?
if [ "$RETURN_VALUE" -ne 0 ]; then
  expect.pass "this.isNumber rejects 'abc'"
else
  # BUG: this.isNumber may accept non-numbers (rc=0 for 'abc')
  expect.fail "this.isNumber should reject 'abc' (RETURN_VALUE=$RETURN_VALUE)"
fi

# ============================================================================
# T5: this.help exists and runs
# ============================================================================
test.case $level "this.help function exists" \
  type -t this.help

if type -t this.help &>/dev/null; then
  expect.pass "this.help function exists"
else
  expect.fail "this.help should be a function"
fi

# ============================================================================
# BUG REPRODUCTION: Single-word method dispatch
# Known issue: single-word methods (e.g., hiveMind dashboard) may conflict
# with OOSH dispatch when scripts have sub-methods starting with the same word
# ============================================================================
test.case $level "BUG: single-word dispatch (config list vs config.list)" \
  echo "testing dispatch"

# config list should dispatch to config.list(), not try to run "list" as a command
CONFIG_OUTPUT=$(./config list 2>&1)
CONFIG_RC=$?
# config.list outputs to LOG_DEVICE, so check exit code only
if [ $CONFIG_RC -eq 0 ]; then
  expect.pass "single-word 'config list' dispatches correctly (rc=0)"
else
  expect.fail "single-word 'config list' dispatch failed (rc=$CONFIG_RC)"
fi

# ============================================================================
# FIX VERIFICATION: Dashed parameter names
# this.start should reject dashes with error, not hang or crash
# (dashes are not valid OOSH convention — use dots: peer.compact)
# ============================================================================
test.case $level "dashed names rejected gracefully by this.start" \
  this.start peer-compact

# this.start should return non-zero for dashed names
if [ "$RETURN_VALUE" -ne 0 ]; then
  expect.pass "dashed name 'peer-compact' rejected gracefully (rc=$RETURN_VALUE)"
else
  expect.fail "dashed name 'peer-compact' should be rejected (RETURN_VALUE=$RETURN_VALUE)"
fi

# ============================================================================
# Test Summary
# ============================================================================

test.suite.save.results
