Files
hermes-agent/tests/gateway/test_api_idempotency_namespace.py
Teknium 1bb193c327 test: share re-imported fixtures through conftest instead of imports
Test files received fixtures by importing them from a sibling test module
(`from tests.x.test_y import daemon, owner  # noqa: F401`). Every test that
then takes the fixture as a parameter is a ruff F811 redefinition, which
main's code-health ratchet blocks (it ignores noqa), and it couples test
modules to each other.

Move each shared fixture to where pytest resolves it by name, scopes
unchanged (all function-scoped):
- model_peer -> tests/_fixtures/model_peer.py, registered in tests/conftest.py
  (used by gateway, acp_adapter and hermes_cli tests)
- daemon, server_spec -> tests/acp_adapter/conftest.py
- owner, api -> tests/gateway/conftest.py
- worker -> new tests/hermes_state/conftest.py
- test_session_hosted_rpc's `owner` (a different object: hosted-room
  authority on its own loop) -> tests/gateway/conftest.py as `hosted_owner`,
  so it does not collide with the API `owner`; its three user files rename
  the parameter.
Conftest fixtures import lazily so the gateway conftest still installs its
telegram/discord mocks before any gateway module loads. Plain helpers
(viewer, editor, control, policy) stay imported from their modules.

Also drop an unused HostedRoomAttachmentStore local in
test_hosted_attachment_bridge (F841) while that file is touched.

Same tests collected and passing in every touched file.

(cherry picked from commit 463102382f8cd9bbc9e0c605be7e1345165efda4)
2026-10-07 10:57:32 -07:00

44 lines
1.8 KiB
Python

"""Canonical OpenAI-compat idempotency is scoped to the authenticated key namespace."""
import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer
OLD, NEW = 'probe-old-key-long-enough', 'probe-new-key-long-enough'
def _execute(owner, calls):
async def handle(event):
from gateway.session_results import execution_result
calls.append(event.text)
text = f'execution-{len(calls)}'
execution_result.get()['result'] = {'final_response': text, 'messages': []}
return text
owner.runner._handle_message = handle
async def _rotated(api, client, path, body, key):
outputs = []
for api_key in (OLD, NEW):
api._api_key = api_key
resp = await client.post(path, json=body, headers={'Authorization': 'Bearer ' + api_key, 'Idempotency-Key': key})
assert resp.status == 200, await resp.text()
outputs.append(await resp.json())
return outputs
@pytest.mark.asyncio
async def test_rotated_api_key_is_a_different_principal_for_chat_and_responses(api, owner):
calls = []
_execute(owner, calls)
app = web.Application()
app.router.add_post('/v1/chat/completions', api._handle_chat_completions)
app.router.add_post('/v1/responses', api._handle_responses)
async with TestClient(TestServer(app)) as client:
chat = await _rotated(api, client, '/v1/chat/completions',
{'messages': [{'role': 'user', 'content': 'same request'}]}, 'rotate-chat')
assert [c['choices'][0]['message']['content'] for c in chat] == ['execution-1', 'execution-2']
responses = await _rotated(api, client, '/v1/responses', {'input': 'fresh rotation'}, 'rotate-responses')
assert [r['output'][-1]['content'][0]['text'] for r in responses] == ['execution-3', 'execution-4']
assert len(calls) == 4