Files
CyberGril/tests/r3_baseline_probe.py
T

51 lines
2.4 KiB
Python

"""Run unchanged against c285a0f and current source, using synthetic isolated data."""
import json
import sys
import time
from pathlib import Path
from fastapi.testclient import TestClient
from test_api import CREATE, events, turn
from test_r2 import ack, add_life, app_for
import cybergril.api.app as api
from cybergril.life.persona import fingerprint
from cybergril.life.runtime import LifeRuntime
# Both revisions run the normal enabled background loop, outside a life window.
# Lifecycle, persona and growth are enabled; only the clock is deterministic.
api.LifeRuntime = lambda *args: LifeRuntime(*args, clock=lambda: 1792792800.0)
time.time = lambda: 1792792800.0
app, provider = app_for(Path(sys.argv[1]))
with TestClient(app) as c:
app.state.persona.clock = time.time
app.state.sharing.clock = time.time
assert c.post('/api/companion', json=CREATE).status_code == 201
public = add_life(app.state.store, '我给旋律换了个喜欢的结尾', told=1)
add_life(app.state.store, '未公开的孤立生活记录')
final = events(turn(c, turn_id='12345678-1234-4234-8234-123456789abc'))[-1]
receipt = final['receipt']
assert ack(c, receipt).status_code == 200
c.portal.call(app.state.sharing.judge, receipt['message_id'])
c.portal.call(app.state.persona.grow, 'life', public)
before = fingerprint(app.state.store)
protected = ('bazi_core', 'companion', 'life_event', 'telling_log', 'sharing_attempt',
'persona_interp', 'persona_seed', 'persona_job', 'growth_event', 'growth_job')
def snapshot():
return {t: [tuple(r) for r in app.state.store.query(f'SELECT * FROM {t} ORDER BY rowid')]
for t in protected}
saved = snapshot()
responses_before = {path: c.get(path).content.hex() for path in ('/api/life', '/api/messages', '/api/persona')}
if hasattr(app.state, 'memories'):
for _ in range(2):
c.portal.call(app.state.memories.tick)
app.state.memories.context('你好', 'next-turn', [], 600)
app.state.memories.decay()
assert snapshot() == saved
assert fingerprint(app.state.store) == before
responses = {path: c.get(path).content.hex() for path in responses_before}
assert responses == responses_before
# Prove growth was exercised, rather than disabling it to make equality pass.
assert c.get('/api/persona').json()['growth']
print(json.dumps({'responses': responses, 'fingerprint': before}, sort_keys=True))