From 7aa01eb5f7956eb4a749e399fa6c97d0d3f3576c Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Wed, 5 Nov 2025 10:17:25 -0500 Subject: [PATCH] refactor(tests): Phase 2 - Extract shared utilities --- tests/shared/helpers.sh | 120 ++++++++++++++++++++++++++++++++++++++ tests/shared/test-data.js | 114 ++++++++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100755 tests/shared/helpers.sh create mode 100644 tests/shared/test-data.js diff --git a/tests/shared/helpers.sh b/tests/shared/helpers.sh new file mode 100755 index 00000000..c52d3ff9 --- /dev/null +++ b/tests/shared/helpers.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +# Shared test utilities for CCS test suite +# Common functions and variables used across multiple test files + +# Test counters +PASS_COUNT=0 +FAIL_COUNT=0 +TOTAL_TESTS=0 + +# Colors +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +CYAN='\033[0;36m' +GRAY='\033[0;37m' +NC='\033[0m' # No Color + +# Test case function +# Usage: test_case "Test Name" "Expected outcome" command [args...] +test_case() { + local name="$1" + local expected="$2" + shift 2 + + ((TOTAL_TESTS++)) + echo "" + echo -e "${CYAN}[$TOTAL_TESTS] $name${NC}" + echo -e "${GRAY} Expected: $expected${NC}" + + if "$@"; then + echo -e "${GREEN} Result: PASS${NC}" + ((PASS_COUNT++)) + return 0 + else + echo -e "${RED} Result: FAIL${NC}" + ((FAIL_COUNT++)) + return 1 + fi +} + +# Print test summary +# Usage: print_test_summary +print_test_summary() { + echo "" + echo -e "${YELLOW}========================================${NC}" + echo -e "${YELLOW}TEST RESULTS SUMMARY${NC}" + echo -e "${YELLOW}========================================${NC}" + echo "" + echo -e "${CYAN}Total Tests: $TOTAL_TESTS${NC}" + echo -e "${GREEN}Passed: $PASS_COUNT${NC}" + + if [[ $FAIL_COUNT -eq 0 ]]; then + echo -e "${GREEN}Failed: $FAIL_COUNT${NC}" + else + echo -e "${RED}Failed: $FAIL_COUNT${NC}" + fi + + SUCCESS_RATE=$(awk "BEGIN {printf \"%.2f\", ($PASS_COUNT / $TOTAL_TESTS) * 100}") + + # Use awk for comparison since bc may not be installed + if awk "BEGIN {exit !($SUCCESS_RATE >= 90)}"; then + echo -e "${GREEN}Success Rate: $SUCCESS_RATE%${NC}" + elif awk "BEGIN {exit !($SUCCESS_RATE >= 70)}"; then + echo -e "${YELLOW}Success Rate: $SUCCESS_RATE%${NC}" + else + echo -e "${RED}Success Rate: $SUCCESS_RATE%${NC}" + fi + + if [[ $FAIL_COUNT -eq 0 ]]; then + return 0 + else + return 1 + fi +} + +# Print final result with exit code +# Usage: print_final_result +print_final_result() { + echo "" + + if [[ $FAIL_COUNT -eq 0 ]]; then + echo -e "${GREEN}========================================${NC}" + echo -e "${GREEN}ALL TESTS PASSED!${NC}" + echo -e "${GREEN}========================================${NC}" + echo "" + echo -e "${GREEN}CCS is ready for production use!${NC}" + exit 0 + else + echo -e "${YELLOW}========================================${NC}" + echo -e "${YELLOW}SOME TESTS FAILED${NC}" + echo -e "${YELLOW}========================================${NC}" + echo "" + echo -e "${YELLOW}Review failed tests above for details${NC}" + exit 1 + fi +} + +# Print test header +# Usage: print_test_header "Test Suite Name" +print_test_header() { + local suite_name="$1" + echo -e "${YELLOW}========================================${NC}" + echo -e "${YELLOW}$suite_name${NC}" + echo -e "${YELLOW}========================================${NC}" +} + +# Print section header +# Usage: print_section_header "Section Name" +print_section_header() { + echo "" + echo -e "${YELLOW}===== $1 =====${NC}" +} + +# Reset test counters +# Usage: reset_test_counters +reset_test_counters() { + PASS_COUNT=0 + FAIL_COUNT=0 + TOTAL_TESTS=0 +} \ No newline at end of file diff --git a/tests/shared/test-data.js b/tests/shared/test-data.js new file mode 100644 index 00000000..547cd444 --- /dev/null +++ b/tests/shared/test-data.js @@ -0,0 +1,114 @@ +/** + * Shared test data for CCS npm tests + * Common test profiles, configurations, and sample data + */ + +module.exports = { + // Valid profile names for testing + validProfiles: ['glm', 'sonnet', 'default', 'haiku', 'opus'], + + // Invalid profile names for error testing + invalidProfiles: [ + 'test@profile', // Contains @ symbol + 'profile;injection', // Contains semicolon + 'profile|pipe', // Contains pipe + 'profile>redirect', // Contains redirect + 'profile$(command)', // Contains command injection + '', // Empty string + ' ', // Space only + 'profile with spaces', // Contains spaces + 'profile-with-dashes!',// Contains exclamation + 'a'.repeat(100), // Very long name + ], + + // Sample configuration data + sampleConfig: { + profiles: { + glm: '~/.ccs/glm.settings.json', + sonnet: '~/.ccs/sonnet.settings.json', + default: '~/.ccs/default.settings.json' + } + }, + + // Sample GLM settings + sampleGlmSettings: { + env: { + ANTHROPIC_BASE_URL: "https://api.z.ai/api/anthropic", + ANTHROPIC_AUTH_TOKEN: "your_api_key_here", + ANTHROPIC_MODEL: "glm-4.6", + ANTHROPIC_DEFAULT_OPUS_MODEL: "glm-4.6", + ANTHROPIC_DEFAULT_SONNET_MODEL: "glm-4.6", + ANTHROPIC_DEFAULT_HAIKU_MODEL: "glm-4.6" + } + }, + + // Sample default settings + sampleDefaultSettings: { + env: { + ANTHROPIC_MODEL: "claude-3-5-sonnet-20241022" + } + }, + + // Test flags and arguments + testFlags: [ + '-c', + '--version', + '-v', + '--help', + '-h', + '--verbose', + '-p', + '--debug', + '--test-flag', + '-d', + '--model=gpt4', + '-1' + ], + + // Test flag combinations + testFlagCombinations: [ + ['-c'], + ['--verbose'], + ['-p', 'test prompt'], + ['-c', '--verbose'], + ['glm', '-c'], + ['glm', '-c', '--verbose'], + ['--version'], + ['--help'] + ], + + // Cross-platform path test cases + pathTestCases: [ + ['~', 'home directory expansion'], + ['~/test', 'home subdirectory'], + ['.', 'current directory'], + ['..', 'parent directory'], + ['/tmp', 'absolute path'], + ['relative/path', 'relative path'], + ['path with spaces', 'path containing spaces'] + ], + + // Expected output patterns + expectedPatterns: { + version: /\b(2\.\d+\.\d+)\b/, + help: /usage|help|options/i, + error: /error|failed|not found/i, + success: /\[OK\]|\[✓\]|success/i + }, + + // Mock file system paths for testing + mockPaths: { + homeDir: process.env.HOME || process.env.USERPROFILE || '/tmp/test-home', + ccsDir: '~/.ccs', + configFile: '~/.ccs/config.json', + glmFile: '~/.ccs/glm.settings.json', + versionFile: '~/.ccs/VERSION' + }, + + // Test environment variables + testEnvVars: { + NO_COLOR: '1', + CI: 'true', + NODE_ENV: 'test' + } +}; \ No newline at end of file