Spaces:
Running
Running
Commit History
-
Move orchestration out of the route handlers into a service layer
Claude +1: Claude Opus 5Unverified signatureThe backend policy - which backend to try, in what order, when to fall back, what counts as evidence a backend is unhealthy, when a patent is absent rather than merely unreachable - was written inline in FastAPI route handlers. It is not HTTP-specific: it would be equally true behind a CLI or a queue consumer, and putting it there meant exercising any of it cost an ASGI round-trip plus monkeypatching module globals. It now lives in services.py as SearchService and PatentService, which take their stateful collaborators (HTTP client, browser provider, circuit breaker, OPS credentials) as constructor arguments. app.py keeps the module-level instances and wires them in through `Depends`, so `app.dependency_overrides` replaces them in a test. The services raise domain errors - PatentNotFound, UpstreamUnavailable, OPSUnconfigured - rather than HTTPException. Mapping those onto status codes is the HTTP layer's job and is now three exception handlers instead of the same branching repeated ac…e44fdef -
Test the API-key middleware, and stop it 500ing on a non-ASCII key
Claude +1: Claude Opus 5Unverified signatureThe optional API-key guard was the only security control in the codebase and had no tests at all. Mutation testing showed what that cost: replacing `secrets.compare_digest` with `==` (a timing-oracle regression) or disabling the guard's condition outright both left the whole suite green. Add tests/test_auth.py covering the behaviour that was already there - open when unset, 401 on a missing/wrong/prefix key, both header forms accepted, bearer scheme matched case-insensitively, and the docs paths staying public - all confirmed green against the unchanged middleware first. Three of the four mutants above now fail the suite. The fourth cannot be caught behaviourally: `==` and `compare_digest` return the same answer for every input and differ only in timing. Since a timing oracle on the API key is worth pinning anyway, that one test asserts structurally that the comparison goes through compare_digest. Then fix a real defect those tests exposed. `compare_digest` raises TypeError on str a…d03af21