#!/bin/bash # kugetsu-helpers test suite # Tests the shell helper functions for Hermes/Chat Agent integration # # Run with: bash skills/kugetsu-helpers/tests/test-kugetsu-helpers.sh set -euo pipefail KUGETSU_HELPERS="./skills/kugetsu-helpers/scripts/kugetsu-helpers" TEST_PM_SESSION_ID="ses_test_pm_789" TEST_ISSUE_REF="github.com/shoko/kugetsu#14" PASS=0 FAIL=0 cleanup() { rm -rf ~/.kugetsu/sessions/* ~/.kugetsu/index.json 2>/dev/null || true } setup_mock_index_uninitialized() { cleanup } setup_mock_index_no_pm_agent() { mkdir -p ~/.kugetsu/sessions cat > ~/.kugetsu/index.json << EOF { "base": "ses_base_123", "pm_agent": null, "issues": {} } EOF } setup_mock_index_with_pm_agent() { mkdir -p ~/.kugetsu/sessions cat > ~/.kugetsu/index.json << EOF { "base": "ses_base_123", "pm_agent": "$TEST_PM_SESSION_ID", "issues": {} } EOF cat > ~/.kugetsu/sessions/pm-agent.json << EOF {"type": "pm_agent", "opencode_session_id": "$TEST_PM_SESSION_ID", "created_at": "2026-03-30T18:00:00+02:00", "state": "idle"} EOF } pass() { echo "PASS: $1" PASS=$((PASS + 1)) } fail() { echo "FAIL: $1" FAIL=$((FAIL + 1)) } cleanup echo "=== kugetsu-helpers Test Suite ===" echo "" # Test 1: check-status when not initialized echo "--- Test: check-status (not initialized) ---" setup_mock_index_uninitialized OUTPUT=$($KUGETSU_HELPERS check-status 2>&1 || true) if [ "$OUTPUT" = "kugetsu_not_initialized" ]; then pass "check-status returns kugetsu_not_initialized when no index.json" else fail "check-status not initialized: got '$OUTPUT', expected 'kugetsu_not_initialized'" fi echo "" # Test 2: check-status when pm_agent field is missing echo "--- Test: check-status (missing pm_agent field) ---" setup_mock_index_no_pm_agent OUTPUT=$($KUGETSU_HELPERS check-status 2>&1 || true) if [ "$OUTPUT" = "pm_agent_missing" ]; then pass "check-status returns pm_agent_missing when field is null" else fail "check-status missing pm_agent: got '$OUTPUT', expected 'pm_agent_missing'" fi echo "" # Test 3: check-status when PM agent exists echo "--- Test: check-status (PM agent exists) ---" setup_mock_index_with_pm_agent OUTPUT=$($KUGETSU_HELPERS check-status 2>&1 || true) if [ "$OUTPUT" = "ok" ]; then pass "check-status returns ok when PM agent exists" else fail "check-status ok: got '$OUTPUT', expected 'ok'" fi echo "" # Test 4: get-pm-session returns session ID when exists echo "--- Test: get-pm-session (exists) ---" setup_mock_index_with_pm_agent OUTPUT=$($KUGETSU_HELPERS get-pm-session 2>&1 || true) if [ "$OUTPUT" = "$TEST_PM_SESSION_ID" ]; then pass "get-pm-session returns correct session ID" else fail "get-pm-session: got '$OUTPUT', expected '$TEST_PM_SESSION_ID'" fi echo "" # Test 5: get-pm-session returns empty when not initialized echo "--- Test: get-pm-session (not initialized) ---" setup_mock_index_uninitialized OUTPUT=$($KUGETSU_HELPERS get-pm-session 2>&1 || true) if [ -z "$OUTPUT" ]; then pass "get-pm-session returns empty when not initialized" else fail "get-pm-session not initialized: got '$OUTPUT', expected ''" fi echo "" # Test 6: delegate-to-pm fails without task argument echo "--- Test: delegate-to-pm (no task) ---" setup_mock_index_with_pm_agent OUTPUT=$($KUGETSU_HELPERS delegate-to-pm 2>&1 || true) if echo "$OUTPUT" | grep -q "Error: task is required"; then pass "delegate-to-pm fails without task" else fail "delegate-to-pm no task: got '$OUTPUT', expected error about task required" fi echo "" # Test 7: delegate-to-pm fails when PM agent missing echo "--- Test: delegate-to-pm (PM agent missing) ---" setup_mock_index_uninitialized OUTPUT=$($KUGETSU_HELPERS delegate-to-pm "test task" 2>&1 || true) if echo "$OUTPUT" | grep -q "Error: PM agent session not found"; then pass "delegate-to-pm fails when PM agent not found" else fail "delegate-to-pm missing PM: got '$OUTPUT', expected error" fi echo "" # Test 8: list-sessions when kugetsu not installed echo "--- Test: list-sessions (kugetsu not installed) ---" cleanup TMPDIR=$(mktemp -d) KUGETSU_BAK="" if command -v kugetsu &> /dev/null; then KUGETSU_PATH=$(command -v kugetsu) KUGETSU_BAK="${TMPDIR}/kugetsu.bak" mv "$KUGETSU_PATH" "$KUGETSU_BAK" fi OUTPUT=$($KUGETSU_HELPERS list-sessions 2>&1 || true) if [ -n "$KUGETSU_BAK" ]; then mv "$KUGETSU_BAK" "$KUGETSU_PATH" fi rmdir "$TMPDIR" 2>/dev/null || true if echo "$OUTPUT" | grep -q "kugetsu command not found"; then pass "list-sessions fails gracefully when kugetsu not found" else fail "list-sessions no kugetsu: got '$OUTPUT', expected error" fi echo "" # Test 9: help command works echo "--- Test: help command ---" OUTPUT=$($KUGETSU_HELPERS help 2>&1 || true) if echo "$OUTPUT" | grep -q "delegate-to-pm"; then pass "help shows delegate-to-pm command" else fail "help: output missing delegate-to-pm" fi echo "" # Test 10: unknown command fails gracefully echo "--- Test: unknown command ---" OUTPUT=$($KUGETSU_HELPERS unknown-cmd 2>&1 || true) if echo "$OUTPUT" | grep -q "Error: Unknown command"; then pass "unknown command fails gracefully" else fail "unknown command: got '$OUTPUT', expected error" fi echo "" # Test 11: check-status handles malformed JSON gracefully echo "--- Test: check-status (malformed JSON) ---" mkdir -p ~/.kugetsu/sessions echo "not valid json" > ~/.kugetsu/index.json OUTPUT=$($KUGETSU_HELPERS check-status 2>&1 || true) if [ "$OUTPUT" = "pm_agent_missing" ]; then pass "check-status handles malformed JSON gracefully" else fail "check-status malformed JSON: got '$OUTPUT', expected 'pm_agent_missing'" fi echo "" # Cleanup cleanup echo "" echo "=== Test Summary ===" echo "Passed: $PASS" echo "Failed: $FAIL" echo "" if [ $FAIL -eq 0 ]; then echo "All tests passed!" exit 0 else echo "Some tests failed." exit 1 fi