Files
code_of_conquest_dnd/api/tests/test_npc.py
Phillip Tarrant a668cf98eb refactor(contract)!: class_id -> calling_id, and the log gains race_id
class_id is the rulebook's word; the world has callings — that was the whole §17
reconcile. The contract the Narrator reads should say what the world says. There
are no saves (M9) and no deployed clients, so this is as cheap as it will ever be
and strictly more expensive every milestone after.

race_id reaches the log because the AI must be able to describe the player:
api/app/prompts.py rendered 'a sellsword' and now renders 'a beastfolk cutpurse'.
It humanizes the id (the model must never read 'hedge_mage') and picks the right
article ('an elf', not 'a elf').

LogPlayer's hardcoded CLASSES const is deleted — the roster is Callings.IDS. A
parity test reads the JSON Schema from the client and asserts its enum equals the
code, so the two sides of the HTTP boundary cannot drift.

The deserter allowed three callings that no longer exist. He now allows all seven;
a thematic gate is a content decision for the Greywater authoring.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QYa9u7Kdxv5gX4AnwWexy8
2026-07-12 20:27:56 -05:00

56 lines
2.0 KiB
Python

import pytest
import app.npc as npc
from app.ollama_client import ModelError
LOG = {
"player": {"name": "Aldric", "race_id": "human", "calling_id": "sellsword", "luck_descriptor": "kind"},
"location": {"id": "greywater_docks", "name": "Greywater Docks"},
"party": [], "recent_events": [], "established_facts": [],
"active_quests": [], "humiliations": [],
}
FENN = {"id": "fenn", "name": "Fenn", "persona": "A clerk.",
"knowledge": ["The ledger is gone."], "capabilities": {}}
def test_run_returns_prose_and_logs(monkeypatch):
seen = {}
def fake_chat(model, messages, options, *, think=None):
seen.update(messages=messages, think=think)
return 'Aye. [MOVE: reveal(varrell_twins)] [FACT: the ledger is gone]'
logs = []
monkeypatch.setattr(npc, "load_npc", lambda i: FENN)
monkeypatch.setattr(npc.ollama_client, "chat", fake_chat)
monkeypatch.setattr(npc.call_log, "record", lambda **kw: logs.append(kw))
out = npc.run(LOG, "fenn", -10, ["reveal(varrell_twins)", "refuse"], "What happened?")
assert out.startswith("Aye.")
# persona + knowledge reached the model; utterance is present
user_msg = seen["messages"][1]["content"]
assert "A clerk." in user_msg and "The ledger is gone." in user_msg
assert "What happened?" in user_msg
assert seen["think"] is False
assert logs and logs[0]["ok"] is True and logs[0]["role"] == "npc"
def test_run_unknown_npc_raises(monkeypatch):
monkeypatch.setattr(npc, "load_npc", lambda i: None)
with pytest.raises(npc.UnknownNpc):
npc.run(LOG, "nobody", 0, [], "hi")
def test_run_logs_failure_and_reraises(monkeypatch):
monkeypatch.setattr(npc, "load_npc", lambda i: FENN)
def boom(*a, **k):
raise ModelError("down")
logs = []
monkeypatch.setattr(npc.ollama_client, "chat", boom)
monkeypatch.setattr(npc.call_log, "record", lambda **kw: logs.append(kw))
with pytest.raises(ModelError):
npc.run(LOG, "fenn", 0, [], "hi")
assert logs and logs[0]["ok"] is False