Files
hx/tests/fake_anthropic.py
T
johannesandClaude Sonnet 5.5 a94632ea3d chore: delete code comments, add ratchet lint and ADRs
Classify every comment as tautology, stale, excuse or decision record.
Tautology/stale/excuse comments are deleted (excuses filed as issues
#196-#236), decisions moved to docs/adr/0001-0034.

- scripts/lint-comments.sh + .comment-baseline: per-file comment count
  may only go down; wired into tests/all.sh
- .claude/skills/delete-comment: repeatable audit workflow
- cargo fmt applied to touched crates

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 13:37:32 +01:00

71 lines
3.2 KiB
Python

"""Scripted fake of the Anthropic SSE API for end-to-end tests of `hx -p`.
Turn 1: tool_use write hello.txt. Turn 2: tool_use bash sed. Turn 3: final text.
Usage: python3 tests/fake_anthropic.py PORT [TURNS.json]
"""
import http.server, json, sys
TURNS = [
[("tool_use", {"id": "t1", "name": "Write", "input": {"path": "hello.txt", "content": "hi from hx\n"}})],
[("tool_use", {"id": "t2", "name": "Bash", "input": {"command": "sed -n p hello.txt"}})],
[("text", "Done: file says hi from hx.")],
]
if len(sys.argv) > 2:
TURNS = [[tuple(b) for b in t] for t in json.load(open(sys.argv[2]))]
LOG = []
def sse(ev, d):
return f"event: {ev}\ndata: {json.dumps(d)}\n\n".encode()
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["content-length"])))
LOG.append({"path": self.path, "auth": self.headers.get("authorization"), "beta": self.headers.get("anthropic-beta"), "body": body})
if self.path.endswith("/responses"):
self.send_response(400)
self.end_headers()
self.wfile.write(b'{"detail": "fake codex"}')
return
turn = TURNS[min(len(LOG) - 1, len(TURNS) - 1)]
self.send_response(200)
self.send_header("anthropic-ratelimit-unified-5h-utilization", "0.42")
self.send_header("anthropic-ratelimit-unified-5h-reset", str(int(__import__("time").time()) + 7980))
self.send_header("anthropic-ratelimit-unified-7d-utilization", "0.13")
self.send_header("content-type", "text/event-stream")
self.end_headers()
w = self.wfile.write
w(sse("message_start", {"message": {"usage": {"input_tokens": 5, "output_tokens": 1}}}))
stop = "end_turn"
for i, (kind, v) in enumerate(turn):
if kind == "text":
w(sse("content_block_start", {"index": i, "content_block": {"type": "text", "text": ""}}))
w(sse("content_block_delta", {"index": i, "delta": {"type": "text_delta", "text": v}}))
else:
stop = "tool_use"
w(sse("content_block_start", {"index": i, "content_block": {"type": "tool_use", "id": v["id"], "name": v["name"], "input": {}}}))
w(sse("content_block_delta", {"index": i, "delta": {"type": "input_json_delta", "partial_json": json.dumps(v["input"])}}))
w(sse("content_block_stop", {"index": i}))
w(sse("message_delta", {"delta": {"stop_reason": stop}, "usage": {"output_tokens": 3}}))
w(sse("message_stop", {}))
def do_GET(self):
self.send_response(200)
self.end_headers()
if self.path.startswith("/v1/models"):
self.wfile.write(json.dumps({"data": [
{"id": "claude-opus-5-5", "max_input_tokens": 1000000},
{"id": "claude-sonnet-5-5", "max_input_tokens": 1000000},
{"id": "claude-sonnet-5-6-preview", "max_input_tokens": 2000000},
{"id": "claude-haiku-4-5", "max_input_tokens": 200000},
]}).encode())
return
self.wfile.write(json.dumps(LOG).encode())
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()