From b595411a07a9bb02786a429511b4d8c58c274959 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 00:24:03 +0000 Subject: [PATCH] test: add test suite for create_session function Tests run sequentially to avoid memory exhaustion with too many opencode sessions: - JSON session list parsing - Session ID format validation - create_session returns valid session ID - create_session creates NEW session (different from base) - create_session creates different sessions on multiple calls - create_session accepts optional base session parameter - Created session visible in opencode session list --- skills/kugetsu/tests/test-create-session.sh | 181 ++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 skills/kugetsu/tests/test-create-session.sh diff --git a/skills/kugetsu/tests/test-create-session.sh b/skills/kugetsu/tests/test-create-session.sh new file mode 100644 index 0000000..cb18aa4 --- /dev/null +++ b/skills/kugetsu/tests/test-create-session.sh @@ -0,0 +1,181 @@ +#!/bin/bash +# Tests for create_session function +# +# Run with: bash skills/kugetsu/tests/test-create-session.sh +# +# NOTE: These tests MUST be run sequentially (not in parallel) +# to avoid exhausting memory with too many opencode sessions. + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$SCRIPT_DIR/../scripts/kugetsu-config.sh" +source "$SCRIPT_DIR/../scripts/kugetsu-index.sh" +source "$SCRIPT_DIR/../scripts/kugetsu-session.sh" + +PASS=0 +FAIL=0 +RUN=0 + +pass() { + echo "PASS: $1" + PASS=$((PASS + 1)) +} + +fail() { + echo "FAIL: $1" + echo " Expected: $2" + echo " Got: $3" + FAIL=$((FAIL + 1)) +} + +run_test() { + local name="$1" + local test_func="$2" + RUN=$((RUN + 1)) + echo "" + echo "=== Test $RUN: $name ===" + echo "--- $name ---" + $test_func +} + +echo "=== create_session Test Suite ===" +echo "NOTE: Running sequentially to avoid memory exhaustion" +echo "" + +# Test 1: create_session requires base session +test_create_session_requires_base() { + local base_id=$(get_base_session_id) + if [ -z "$base_id" ] || [ "$base_id" = "null" ]; then + skip "Base session not initialized - run 'kugetsu init' first" + return + fi + + local result=$(create_session "$base_id") + if [ -n "$result" ] && [[ "$result" =~ ^ses_ ]]; then + pass "create_session returns valid session ID" + else + fail "create_session returns valid session ID" "ses_xxx" "$result" + fi +} + +# Test 2: create_session creates a NEW session (different from base) +test_create_session_is_new() { + local base_id=$(get_base_session_id) + if [ -z "$base_id" ] || [ "$base_id" = "null" ]; then + skip "Base session not initialized - run 'kugetsu init' first" + return + fi + + local new_id=$(create_session "$base_id") + if [ "$new_id" != "$base_id" ]; then + pass "create_session returns NEW session (not same as base)" + else + fail "create_session returns NEW session" "different from base_id" "$new_id" + fi +} + +# Test 3: create_session can be called multiple times (creates different sessions) +test_create_session_multiple_calls() { + local base_id=$(get_base_session_id) + if [ -z "$base_id" ] || [ "$base_id" = "null" ]; then + skip "Base session not initialized - run 'kugetsu init' first" + return + fi + + local id1=$(create_session "$base_id") + sleep 1 + local id2=$(create_session "$base_id") + + if [ "$id1" != "$id2" ]; then + pass "create_session creates different sessions on each call" + else + fail "create_session creates different sessions" "$id1 != $id2" "both equal: $id1" + fi +} + +# Test 4: JSON session list parsing +test_session_json_parsing() { + local json='[{"id": "ses_abc123", "title": "test"}, {"id": "ses_def456", "title": "test2"}]' + local ids=$(echo "$json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print(' '.join(s['id'] for s in sessions))" 2>/dev/null) + + if [ "$ids" = "ses_abc123 ses_def456" ]; then + pass "JSON session list parsing extracts IDs correctly" + else + fail "JSON session list parsing" "ses_abc123 ses_def456" "$ids" + fi +} + +# Test 5: Session ID format validation +test_session_id_format() { + local json='[{"id": "ses_2b4814406ffe3AxcpbrP7FknDr", "title": "test"}]' + local ids=$(echo "$json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print(' '.join(s['id'] for s in sessions))" 2>/dev/null) + + if [[ "$ids" =~ ^ses_ ]]; then + pass "Session ID format is valid (starts with ses_)" + else + fail "Session ID format" "ses_xxx" "$ids" + fi +} + +# Test 6: create_session accepts optional base session parameter +test_create_session_with_param() { + local base_id=$(get_base_session_id) + if [ -z "$base_id" ] || [ "$base_id" = "null" ]; then + skip "Base session not initialized - run 'kugetsu init' first" + return + fi + + local result=$(create_session "$base_id") + if [ -n "$result" ] && [[ "$result" =~ ^ses_ ]]; then + pass "create_session accepts base session parameter" + else + fail "create_session accepts base session parameter" "ses_xxx" "$result" + fi +} + +# Test 7: Verify session appears in opencode session list after creation +test_session_visible_in_list() { + local base_id=$(get_base_session_id) + if [ -z "$base_id" ] || [ "$base_id" = "null" ]; then + skip "Base session not initialized - run 'kugetsu init' first" + return + fi + + local new_id=$(create_session "$base_id") + sleep 1 + + local all_sessions=$(opencode session list --format=json 2>/dev/null) + if echo "$all_sessions" | grep -q "$new_id"; then + pass "Created session appears in opencode session list" + else + fail "Created session appears in session list" "should contain $new_id" "$all_sessions" + fi +} + +skip() { + echo "SKIP: $1" +} + +# Run tests sequentially +run_test "create_session requires base session" test_create_session_requires_base +run_test "create_session creates NEW session" test_create_session_is_new +run_test "create_session creates different sessions on multiple calls" test_create_session_multiple_calls +run_test "JSON session list parsing" test_session_json_parsing +run_test "Session ID format validation" test_session_id_format +run_test "create_session accepts optional base session parameter" test_create_session_with_param +run_test "Created session visible in opencode session list" test_session_visible_in_list + +echo "" +echo "=== Test Results ===" +echo "Passed: $PASS" +echo "Failed: $FAIL" +echo "Total: $RUN" + +if [ $FAIL -eq 0 ]; then + echo "All tests passed!" + exit 0 +else + echo "Some tests failed!" + exit 1 +fi \ No newline at end of file