@@ -9,12 +9,39 @@ setup() {
99 # Source the lib scripts
1010 source " $BATS_TEST_DIRNAME /../ralph-wiggum/lib/response_analyzer.sh"
1111 source " $BATS_TEST_DIRNAME /../ralph-wiggum/lib/circuit_breaker.sh"
12+ source " $BATS_TEST_DIRNAME /../ralph-wiggum/lib/api_limit_handler.sh"
1213}
1314
1415teardown () {
1516 rm -rf " $RALPH_STATE_DIR "
1617}
1718
19+ # Cross-Platform Hash Function Tests
20+
21+ @test " hash: _hash function exists and is callable" {
22+ run bash -c ' echo "test" | _hash'
23+ [[ " $status " -eq 0 ]]
24+ [[ -n " $output " ]]
25+ }
26+
27+ @test " hash: _hash produces consistent output" {
28+ hash1=$( echo " hello world" | _hash)
29+ hash2=$( echo " hello world" | _hash)
30+ [[ " $hash1 " == " $hash2 " ]]
31+ }
32+
33+ @test " hash: _hash produces different output for different input" {
34+ hash1=$( echo " hello" | _hash)
35+ hash2=$( echo " world" | _hash)
36+ [[ " $hash1 " != " $hash2 " ]]
37+ }
38+
39+ @test " hash: calculate_output_hash works" {
40+ result=$( calculate_output_hash " test string" )
41+ [[ -n " $result " ]]
42+ [[ " $result " != " no_output" ]]
43+ }
44+
1845# Response Analyzer Tests
1946
2047@test " response_analyzer: init creates state file" {
@@ -79,3 +106,38 @@ teardown() {
79106 state=$( get_circuit_state)
80107 [[ " $state " == " CLOSED" ]]
81108}
109+
110+ # API Limit Handler Tests
111+
112+ @test " api_limit_handler: init creates state file" {
113+ init_limit_handler
114+ [[ -f " $RALPH_STATE_DIR /ralph-limits.json" ]]
115+ }
116+
117+ @test " api_limit_handler: detect rate limit identifies 429" {
118+ result=$( detect_rate_limit " Error: 429 Too Many Requests" )
119+ [[ " $result " == " true" ]]
120+ }
121+
122+ @test " api_limit_handler: detect rate limit identifies rate limit text" {
123+ result=$( detect_rate_limit " You have hit the rate limit" )
124+ [[ " $result " == " true" ]]
125+ }
126+
127+ @test " api_limit_handler: detect rate limit returns false for normal text" {
128+ result=$( detect_rate_limit " Everything is working fine" )
129+ [[ " $result " == " false" ]]
130+ }
131+
132+ @test " api_limit_handler: determine limit type identifies 5-hour limit" {
133+ result=$( determine_limit_type " You have exceeded your 5-hour usage limit" )
134+ [[ " $result " == " 5_hour_limit" ]]
135+ }
136+
137+ @test " api_limit_handler: calculate wait time returns correct seconds" {
138+ result=$( calculate_wait_time " 5_hour_limit" )
139+ [[ " $result " == " 3600" ]]
140+
141+ result=$( calculate_wait_time " rate_limit" )
142+ [[ " $result " == " 60" ]]
143+ }
0 commit comments