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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
"""
eCFR API Test Script
Tests all API endpoints used by the MCP server to ensure they're returning results.
"""

import asyncio
import httpx
import json
from typing import Dict, Any, Optional
from datetime import datetime
from pathlib import Path
import re

# Constants
BASE_URL = "https://www.ecfr.gov/api"
TIMEOUT = 30.0

# Create an output directory for test artifacts
LOG_DIR = Path("../test_outputs/api_tests")
# Ensure the output directory exists
LOG_DIR.mkdir(parents=True, exist_ok=True)
# Create a timestamp for this run
RUN_TS = datetime.now().strftime("%Y%m%d_%H%M%S")
# Create a run-specific directory
RUN_DIR = LOG_DIR / RUN_TS
# Ensure the run directory exists
RUN_DIR.mkdir(parents=True, exist_ok=True)


# Test configuration
TEST_TITLE = 1
TEST_PART = 1
TEST_SECTION = 1.1
TEST_DATE = "2017-10-20"


class Color:
    """ANSI color codes for terminal output."""
    GREEN = "\033[92m"
    RED = "\033[91m"
    YELLOW = "\033[93m"
    BLUE = "\033[94m"
    BOLD = "\033[1m"
    END = "\033[0m"


async def make_api_request(endpoint: str, params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
    """Make an HTTP request to the eCFR API.

    The eCFR API requires file extensions (.json or .xml) on endpoints for content negotiation.
    """

    url = f"{BASE_URL}/{endpoint}"

    # Use a broad Accept header to allow the API to return the correct format based on the endpoint extension
    headers = {
        "Accept": "application/json, application/xml, application/octet-stream, */*"
    }

    # Log the request details for debugging
    async with httpx.AsyncClient(timeout=TIMEOUT, follow_redirects=True) as client:
        # Add a small delay for agentic use later
        await asyncio.sleep(0.5)

        try:
            response = await client.get(url, params=params, headers=headers)
            response.raise_for_status()

            # Build a filesystem-safe name for this call
            safe_endpoint = re.sub(r"[^a-zA-Z0-9._-]+", "_", endpoint)
            safe_params = re.sub(r"[^a-zA-Z0-9._-]+", "_", json.dumps(params or {}, sort_keys=True))
            base_name = f"{RUN_TS}__{safe_endpoint}__{safe_params}"

            # Choose file extension for the raw body
            if endpoint.endswith(".xml"):
                body_path = RUN_DIR / f"{base_name}.xml"
                meta_path = RUN_DIR / f"{base_name}.meta.txt"
            else:
                body_path = RUN_DIR / f"{base_name}.json"
                meta_path = RUN_DIR / f"{base_name}.meta.txt"

            # Write metadata describing the response
            meta_text = "\n".join([
                f"url={url}",
                f"params={json.dumps(params or {}, ensure_ascii=False)}",
                f"status_code={response.status_code}",
                f"content_type={response.headers.get('content-type', '')}",
                f"bytes={len(response.content)}",
            ])
            meta_path.write_text(meta_text, encoding="utf-8")

            # Write the full response body
            if endpoint.endswith(".xml"):
                body_path.write_text(response.text, encoding="utf-8")
            else:
                # Try to pretty-print JSON for readability; fall back to raw text
                try:
                    parsed = json.loads(response.content.decode("utf-8"))
                    body_path.write_text(json.dumps(parsed, indent=2, ensure_ascii=False), encoding="utf-8")
                except Exception:
                    body_path.write_text(response.content.decode("utf-8", errors="replace"), encoding="utf-8")

            # Existing parse/return behavior stays the same
            if endpoint.endswith(".xml"):
                return {"raw_xml": response.text}
            else:
                return json.loads(response.content.decode("utf-8"))

        except httpx.HTTPStatusError as e:
            if e.response.status_code == 406:
                raise RuntimeError(
                    f"406 Not Acceptable - ensure endpoint has .json or .xml extension."
                    f"URL: {url}"
                ) from e
            else:
                raise


def print_test_header(test_name: str):
    """Print a formatted test header."""
    print(f"\n{Color.BLUE}{Color.BOLD}{'='*70}{Color.END}")
    print(f"{Color.BLUE}{Color.BOLD}TEST: {test_name}{Color.END}")
    print(f"{Color.BLUE}{Color.BOLD}{'='*70}{Color.END}")


def print_success(message: str):
    """Print a success message."""
    print(f"{Color.GREEN} {message}{Color.END}")


def print_error(message: str):
    """Print an error message."""
    print(f"{Color.RED} {message}{Color.END}")


def print_info(message: str):
    """Print an info message."""
    print(f"{Color.YELLOW} {message}{Color.END}")


async def test_list_titles():
    """Test: List all CFR titles."""
    print_test_header("List All CFR Titles")

    try:
        # Call endpoint with explicit extension to avoid 406 ambiguity
        data = await make_api_request("versioner/v1/titles.json")

        # Validate response structure
        if "titles" not in data:
            print_error("Validation failed: response missing 'titles' field")
            return False

        titles = data["titles"]

        # Validate response types
        if not isinstance(titles, list):
            print_error("Validation failed: 'titles' is not a list")
            return False

        if len(titles) == 0:
            print_error("Validation failed: no titles returned")
            return False

        # Validate expected fields on the first item
        first_title = titles[0]
        expected_fields = ["number", "name", "latest_amended_on", "latest_issue_date"]

        for field in expected_fields:
            if field not in first_title:
                print_error(f"Validation failed: title missing expected field '{field}'")
                return False

        # Print consistent success output
        print_success(f"Retrieved {len(titles)} CFR titles")
        print_info(f"Sample title: {first_title['number']} {first_title['name']}")
        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_list_titles: {str(e)}")
        return False


async def test_get_title_versions():
    """Test: Get version history for a title."""
    print_test_header(f"Get Version History for Title {TEST_TITLE}")

    try:
        # Call endpoint with explicit extension to avoid 406 ambiguity
        data = await make_api_request(f"versioner/v1/versions/title-{TEST_TITLE}.json")

        # Validate response structure
        if "content_versions" not in data:
            print_error("Validation failed: response missing 'content_versions' field")
            return False

        versions = data["content_versions"]

        # Validate response types
        if not isinstance(versions, list):
            print_error("Validation failed: 'content_versions' is not a list")
            return False

        if len(versions) == 0:
            print_error("Validation failed: no versions returned")
            return False

        # Validate expected fields on the first item
        first_version = versions[0]
        expected_fields = ["date", "identifier"]

        for field in expected_fields:
            if field not in first_version:
                print_error(f"Validation failed: version missing expected field '{field}'")
                return False

        # Print consistent success output
        print_success(f"Retrieved {len(versions)} versions for Title {TEST_TITLE}")
        print_info(f"First listed version: {first_version['date']} ({first_version['identifier']})")
        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_get_title_versions: {str(e)}")
        return False


async def test_get_title_summary():
    """Test: Get summary of titles."""
    print_test_header("Get Summary of Titles")

    try:
        # Call endpoint with explicit extension to avoid 406 ambiguity
        data = await make_api_request("versioner/v1/titles.json")

        # Validate response structure
        if "titles" not in data:
            print_error("Validation failed: response missing 'titles' field")
            return False

        titles = data["titles"]

        # Validate response types
        if not isinstance(titles, list):
            print_error("Validation failed: 'titles' is not a list")
            return False

        if len(titles) == 0:
            print_error("Validation failed: no titles returned")
            return False

        # Validate expected fields on the first item
        first_element = titles[0]
        expected_fields = ["number", "name"]

        for field in expected_fields:
            if field not in first_element:
                print_error(f"Validation failed: title missing expected field '{field}'")
                return False

        # Print consistent success output
        print_success(f"Retrieved {len(titles)} titles in summary")
        print_info(f"First title: {first_element['number']} {first_element['name']}")
        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_get_title_summary: {str(e)}")
        return False


async def test_get_title_structure_by_date():
    """Test: Get structure of a title for a specific date."""
    print_test_header(f"Get Structure for Title {TEST_TITLE} on {TEST_DATE}")

    try:
        # Call endpoint with explicit extension to avoid 406 ambiguity
        data = await make_api_request(f"versioner/v1/structure/{TEST_DATE}/title-{TEST_TITLE}.json")

        # Validate response structure
        if "identifier" not in data:
            print_error("Validation failed: response missing 'identifier' field")
            print_info(f"Response preview: {json.dumps(data, indent=2)[:500]}...")
            return False

        children = data.get("children", [])

        # Print consistent success output
        print_success(f"Retrieved structure for {TEST_DATE}")
        print_info(f"Root identifier: {data['identifier']}")
        print_info(f"Top-level children: {len(children)}")

        # Print a small sample consistently
        if children:
            print_info("Sample children (first 3):")
            for i, child in enumerate(children[:3]):
                print_info(f"  {i+1}. {child.get('identifier', 'unknown')}: {child.get('label', 'no label')[:50]}")

        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_get_title_structure_by_date: {str(e)}")
        import traceback
        print_error(traceback.format_exc())
        return False


async def test_get_section_by_date():
    """Test: Get a specific section for a historical date."""
    print_test_header(f"Get Section {TEST_TITLE} CFR {TEST_PART}.{TEST_SECTION} on {TEST_DATE}")

    try:
        # Request a specific section via query params
        section_params = {"section": TEST_SECTION}

        # Call XML endpoint with explicit extension
        data = await make_api_request(
            f"versioner/v1/full/{TEST_DATE}/title-{TEST_TITLE}.xml",
            params=section_params
        )

        # Validate response structure for XML pathway
        if "raw_xml" not in data:
            print_error("Validation failed: XML endpoint did not return 'raw_xml'")
            print_info(f"Response preview: {str(data)[:500]}...")
            return False

        xml_content = data["raw_xml"]

        # Print consistent success output
        print_success(f"Retrieved section content for {TEST_DATE}")
        print_info(f"XML length: {len(xml_content)} characters")
        print_info("XML preview (first 300 chars):")
        print_info(f"{xml_content[:300]}...")

        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_get_section_by_date: {str(e)}")
        import traceback
        print_error(traceback.format_exc())
        return False


async def test_search():
    """Test: Search the CFR."""
    print_test_header("Search CFR for 'informed consent'")

    try:
        # Call endpoint with explicit extension to avoid 406 ambiguity
        params = {"query": "informed consent", "page": 1, "per_page": 20}
        data = await make_api_request("search/v1/results.json", params)

        # Validate response structure
        results = data.get("results")
        if results is None:
            print_error("Validation failed: response missing 'results' field")
            return False

        if not isinstance(results, list):
            print_error("Validation failed: 'results' is not a list")
            return False

        # Print consistent success output
        print_success(f"Search returned {len(results)} results")
        if results:
            first_result = results[0]
            print_info(f"First result citation: {first_result.get('citation', 'N/A')}")
        else:
            print_info("No results returned for query")

        return True

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_search: {str(e)}")
        return False


async def test_search_pagination(max_pages: int = 5, keyword: str = "research"):
    """Test: Search with pagination - fetches up to 5 pages."""
    print_test_header("Search with Pagination (multiple pages)")

    try:
        all_results = []
        max_pages = max_pages or 5  # Default to 5 if not provided
        total_pages = None
        
        for page_num in range(1, max_pages + 1):
            # Call endpoint with explicit extension to avoid 406 error
            params = {"query": keyword, "page": page_num, "paginate_by": "results"}
            data = await make_api_request("search/v1/results.json", params)

            # Extract metadata
            meta = data.get("meta", {})
            total_pages = meta.get("total_pages")
            current_page = meta.get("current_page")
            
            # Validate response structure
            results = data.get("results")
            if results is None:
                print_error(f"Validation failed on page {page_num}: response missing 'results' field")
                break

            if not isinstance(results, list):
                print_error(f"Validation failed on page {page_num}: 'results' is not a list")
                break

            # If no results on this page, we've reached the end
            if not results:
                print_info(f"No more results found at page {page_num}")
                break
            
            all_results.extend(results)
            print_info(f"Page {current_page}/{total_pages}: fetched {len(results)} results")
            
            # Stop if we've reached the last available page
            if total_pages and current_page >= total_pages:
                print_info(f"Reached last available page ({total_pages})")
                break

        # Summary
        if all_results:
            pages_fetched = min(page_num, max_pages) if total_pages and page_num <= total_pages else page_num
            print_success(f"Total results fetched across {pages_fetched} page(s): {len(all_results)}")
            print_info(f"First result hierarchy: {all_results[0].get('hierarchy', {}).get('section', 'N/A')}")
            if total_pages:
                print_info(f"API has {total_pages} total pages available")
            return True
        else:
            print_error("No results were fetched")
            return False

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_search_pagination: {str(e)}")
        return False

    except httpx.HTTPStatusError:
        # make_api_request already printed consistent error context
        return False
    except Exception as e:
        print_error(f"Unexpected error in test_search_pagination: {str(e)}")
        return False


async def test_api_performance():
    """Test: Measure API response time."""
    print_test_header("API Performance Test")

    try:
        # Measure around the request call for a consistent endpoint
        start_time = datetime.now()
        await make_api_request("versioner/v1/titles.json")
        end_time = datetime.now()

        duration = (end_time - start_time).total_seconds()

        # Print consistent success output
        print_success(f"API response time: {duration:.2f} seconds")

        # Print consistent interpretation output
        if duration < 2.0:
            print_info("Response time bucket: excellent (< 2s)")
        elif duration < 5.0:
            print_info("Response time bucket: good (< 5s)")
        else:
            print_info("Response time bucket: slow (> 5s)")

        return True

    except Exception as e:
        print_error(f"Performance test failed: {str(e)}")
        return False

async def run_all_tests():
    """Run all API tests."""
    print(f"\n{Color.BOLD}{'='*70}")
    print("eCFR API Test Suite")
    print(f"Testing against: {BASE_URL}")
    print(f"Test Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"{'='*70}{Color.END}\n")

    tests = [
        ("List Titles", test_list_titles),
        ("Get Version History", test_get_title_versions),
        ("Get Title Summary", test_get_title_summary),
        ("Get Historical Structure", test_get_title_structure_by_date),
        ("Get Historical Section", test_get_section_by_date),
        ("Search", test_search),
        ("Search Pagination", test_search_pagination),
        ("API Performance", test_api_performance),
    ]

    results = []

    for test_name, test_func in tests:
        try:
            result = await test_func()
            results.append((test_name, result))
        except Exception as e:
            print_error(f"Test crashed: {str(e)}")
            results.append((test_name, False))

    # Print summary
    print(f"\n{Color.BOLD}{'='*70}")
    print("TEST SUMMARY")
    print(f"{'='*70}{Color.END}\n")

    passed = sum(1 for _, result in results if result)
    total = len(results)

    for test_name, result in results:
        status = f"{Color.GREEN}PASS{Color.END}" if result else f"{Color.RED}FAIL{Color.END}"
        print(f"{test_name:.<50} {status}")

    print(f"\n{Color.BOLD}Total: {passed}/{total} tests passed{Color.END}")

    if passed == total:
        print(f"\n{Color.GREEN}{Color.BOLD}All tests passed! The eCFR API is working correctly.{Color.END}")
        return 0

    print(f"\n{Color.RED}{Color.BOLD}Some tests failed. Please check the API or network connection.{Color.END}")
    return 1


def main():
    """Main entry point."""
    try:
        exit_code = asyncio.run(run_all_tests())
        return exit_code
    except KeyboardInterrupt:
        print(f"\n{Color.YELLOW}Tests interrupted by user{Color.END}")
        return 1
    except Exception as e:
        print(f"\n{Color.RED}Unexpected error: {str(e)}{Color.END}")
        return 1

if __name__ == "__main__":
    import sys
    sys.exit(main())