@@ -124,6 +124,28 @@ def wait_for_webset_completion(client, webset_id, timeout: 120, interval: 2)
124124module WebsetsCleanupHelper
125125 require "open3"
126126
127+ FAKE_API_KEYS = %w[ test_key_for_vcr test_key_for_vcr_playback ] . freeze
128+
129+ # Delete all websets on the account. Run before/after the suite to clear
130+ # debris left by previous failed or interrupted runs.
131+ def self . sweep_stale_websets ( api_key )
132+ client = Exa ::Client . new ( api_key : api_key )
133+ cursor = nil
134+ loop do
135+ collection = client . list_websets ( limit : 100 , **( { cursor : cursor } if cursor ) . to_h )
136+ collection . data . each do |ws |
137+ client . delete_webset ( ws [ "id" ] )
138+ rescue Exa ::Error
139+ nil
140+ end
141+ break unless collection . has_more
142+
143+ cursor = collection . next_cursor
144+ end
145+ rescue Exa ::Error => e
146+ warn "Webset sweep failed: #{ e . message } "
147+ end
148+
127149 def setup
128150 super
129151 @created_websets = [ ]
@@ -184,7 +206,7 @@ def cleanup_resources
184206 # Check if we should perform cleanup
185207 # Only cleanup when using a real API key (not VCR placeholder)
186208 def should_cleanup?
187- @api_key && @api_key != "test_key_for_vcr"
209+ @api_key && ! FAKE_API_KEYS . include? ( @api_key )
188210 end
189211
190212 def cleanup_enrichments
@@ -301,3 +323,19 @@ def run_cli_command(command)
301323 [ stdout , stderr , status ]
302324 end
303325end
326+
327+ # When running integration tests against the real API, sweep leftover websets
328+ # from previous failed or interrupted runs before the suite starts, and again
329+ # after it finishes to leave the account clean.
330+ if ENV [ "RUN_INTEGRATION_TESTS" ] == "true" &&
331+ ENV [ "EXA_API_KEY" ] &&
332+ !WebsetsCleanupHelper ::FAKE_API_KEYS . include? ( ENV [ "EXA_API_KEY" ] )
333+
334+ $stderr. puts "Sweeping leftover websets before suite..."
335+ WebsetsCleanupHelper . sweep_stale_websets ( ENV [ "EXA_API_KEY" ] )
336+
337+ Minitest . after_run do
338+ $stderr. puts "Sweeping websets after suite..."
339+ WebsetsCleanupHelper . sweep_stale_websets ( ENV [ "EXA_API_KEY" ] )
340+ end
341+ end
0 commit comments