|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from commandlayer import CommandLayerClient, create_client |
| 3 | +import json |
| 4 | +from pathlib import Path |
4 | 5 |
|
| 6 | +import httpx |
| 7 | + |
| 8 | +from commandlayer import ( |
| 9 | + CommandLayerClient, |
| 10 | + canonicalize_stable_json_v1, |
| 11 | + create_client, |
| 12 | + normalize_command_response, |
| 13 | + recompute_receipt_hash_sha256, |
| 14 | + verify_receipt, |
| 15 | +) |
| 16 | + |
| 17 | +ROOT = Path(__file__).resolve().parents[2] |
| 18 | +VECTORS = ROOT / "test_vectors" |
| 19 | +EXPECTED_EXPORTS = { |
| 20 | + "CommandLayerClient": CommandLayerClient, |
| 21 | + "create_client": create_client, |
| 22 | + "verify_receipt": verify_receipt, |
| 23 | + "normalize_command_response": normalize_command_response, |
| 24 | + "canonicalize_stable_json_v1": canonicalize_stable_json_v1, |
| 25 | + "recompute_receipt_hash_sha256": recompute_receipt_hash_sha256, |
| 26 | +} |
| 27 | +EXPECTED_VERBS = [ |
| 28 | + "summarize", |
| 29 | + "analyze", |
| 30 | + "classify", |
| 31 | + "clean", |
| 32 | + "convert", |
| 33 | + "describe", |
| 34 | + "explain", |
| 35 | + "format", |
| 36 | + "parse", |
| 37 | + "fetch", |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +def load_fixture(name: str) -> dict: |
| 42 | + return json.loads((VECTORS / name).read_text(encoding="utf-8")) |
| 43 | + |
| 44 | + |
| 45 | +def load_pubkey() -> str: |
| 46 | + return f"ed25519:{(VECTORS / 'public_key_base64.txt').read_text(encoding='utf-8').strip()}" |
| 47 | + |
| 48 | + |
| 49 | +def test_expected_symbols_are_importable() -> None: |
| 50 | + for export_name, export_value in EXPECTED_EXPORTS.items(): |
| 51 | + assert export_value is not None, export_name |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +def test_create_client_accepts_basic_configuration() -> None: |
| 56 | + client = create_client( |
| 57 | + actor="api-user", |
| 58 | + runtime="https://runtime.example", |
| 59 | + timeout_ms=12_345, |
| 60 | + headers={"X-Test": "1"}, |
| 61 | + verify_receipts=False, |
| 62 | + ) |
5 | 63 |
|
6 | | -def test_create_client_factory() -> None: |
7 | | - client = create_client(actor="api-user") |
8 | 64 | assert isinstance(client, CommandLayerClient) |
9 | 65 | assert client.actor == "api-user" |
| 66 | + assert client.runtime == "https://runtime.example" |
| 67 | + assert client.timeout_ms == 12_345 |
| 68 | + assert client.default_headers["X-Test"] == "1" |
10 | 69 | client.close() |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +def test_public_client_verbs_exist_and_are_callable() -> None: |
| 74 | + client = create_client(actor="verb-check") |
| 75 | + try: |
| 76 | + for verb in EXPECTED_VERBS: |
| 77 | + method = getattr(client, verb) |
| 78 | + assert callable(method), verb |
| 79 | + finally: |
| 80 | + client.close() |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +def test_mocked_client_response_matches_public_envelope_shape() -> None: |
| 85 | + def handler(_: httpx.Request) -> httpx.Response: |
| 86 | + return httpx.Response( |
| 87 | + 200, |
| 88 | + json={ |
| 89 | + "receipt": { |
| 90 | + "status": "success", |
| 91 | + "x402": {"verb": "summarize", "version": "1.1.0"}, |
| 92 | + "result": {"summary": "done"}, |
| 93 | + "metadata": { |
| 94 | + "proof": { |
| 95 | + "alg": "ed25519-sha256", |
| 96 | + "canonical": "cl-stable-json-v1", |
| 97 | + } |
| 98 | + }, |
| 99 | + }, |
| 100 | + "runtime_metadata": {"duration_ms": 7, "provider": "mock-runtime"}, |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + client = create_client( |
| 105 | + actor="shape-check", |
| 106 | + http_client=httpx.Client(transport=httpx.MockTransport(handler)), |
| 107 | + ) |
| 108 | + |
| 109 | + try: |
| 110 | + response = client.summarize(content="Hello", style="bullet_points") |
| 111 | + finally: |
| 112 | + client.close() |
| 113 | + |
| 114 | + assert set(response.keys()) == {"receipt", "runtime_metadata"} |
| 115 | + assert response["receipt"]["x402"]["verb"] == "summarize" |
| 116 | + assert response["receipt"]["result"]["summary"] == "done" |
| 117 | + assert response["runtime_metadata"]["duration_ms"] == 7 |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +def test_verify_receipt_is_importable_callable_and_matches_vector_contract() -> None: |
| 122 | + receipt = load_fixture("receipt_valid.json") |
| 123 | + |
| 124 | + result = verify_receipt(receipt, public_key=load_pubkey()) |
| 125 | + |
| 126 | + assert callable(verify_receipt) |
| 127 | + assert result["ok"] is True |
| 128 | + assert result["values"]["recomputed_hash"] == recompute_receipt_hash_sha256(receipt)["hash_sha256"] |
| 129 | + assert result["values"]["signer_id"] == "runtime.commandlayer.eth" |
| 130 | + assert result["errors"]["verify_error"] is None |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +def test_mocked_end_to_end_flow_uses_vector_shaped_response() -> None: |
| 135 | + receipt = load_fixture("receipt_valid.json") |
| 136 | + |
| 137 | + def handler(_: httpx.Request) -> httpx.Response: |
| 138 | + return httpx.Response( |
| 139 | + 200, |
| 140 | + json={ |
| 141 | + "receipt": receipt, |
| 142 | + "runtime_metadata": {"duration_ms": 11, "provider": "mock-runtime"}, |
| 143 | + }, |
| 144 | + ) |
| 145 | + |
| 146 | + client = create_client( |
| 147 | + actor="vector-flow", |
| 148 | + http_client=httpx.Client(transport=httpx.MockTransport(handler)), |
| 149 | + ) |
| 150 | + |
| 151 | + try: |
| 152 | + response = client.analyze(content="vector-backed", goal="parity") |
| 153 | + finally: |
| 154 | + client.close() |
| 155 | + |
| 156 | + assert response["receipt"] == receipt |
| 157 | + assert response["runtime_metadata"]["provider"] == "mock-runtime" |
| 158 | + verification = verify_receipt(response["receipt"], public_key=load_pubkey()) |
| 159 | + assert verification["ok"] is True |
0 commit comments