Three changes that together replace "deterministic hash decides everything"
with "the physical world genuinely participates".
PHYSICAL ENTROPY (app/entropy.py, lib/entropy.ts)
Contact was a database lookup: signature_from_anomalies() hashed the
anomaly pattern, so identical conditions always produced an identical
spirit. Now the client harvests real thermal/acoustic/RF noise from the
microphone and receiver noise floors — Von Neumann debiased, SHA-256
conditioned — and contributes it to every summon.
The client is untrusted by construction. A contribution is never a seed:
every draw is HMAC-SHA256(fresh server secret, client bytes || context).
Because fresh CSPRNG server bytes are always present, the output is
unpredictable and uniform no matter what the client sends — all-zeros, a
replayed value, or one chosen adversarially. The room can only ever ADD
unpredictability, never steer the result. Tests assert this directly:
400 replays of one contribution stay uniformly distributed.
A signature now identifies a *channel*, not a spirit. Whether the familiar
presence answers or something else picks up is a real draw
(RETURN_CHANCE). The Codex stays collectable; it is just no longer
guaranteed. test_same_signature_recontacts_same_entity became two tests —
one pinning the probability to prove re-contact works, one pinning it to
zero to prove something else can answer — because at 0.72 the original
would have passed ~72% of the time, which is worse than failing.
REAL ASTRONOMY (app/celestial.py)
Moon phase from the standard mean-synodic approximation, and true solar
midnight from the seeker's own longitude — the real witching hour for
where they are standing, not clock 3am. Computed, never fetched: an API
that can fail would mean the veil silently changes behaviour during
someone else's outage. Validated against published ephemeris dates (2024
full moons, 2025 new moons) rather than against its own output. A thinner
veil erodes the familiar presence's claim on a channel, so a full moon at
solar midnight makes strangers likelier. Only longitude is kept, never a
full coordinate; a denied location degrades to moon-only, silently.
GENERATION FROM NOTHING (SpiritService.manifest)
Not chat_stream with an empty question. The prompt contains no seeker
input at all — only measured room state, rendered as measurements
("deviation above the floor: 31.4") rather than interpretations
("terrifying spike"), so the horror comes from the entity instead of from
us. And the Ollama `seed` is derived from the physical entropy harvested
in that room, which fixes the token-sampling path: the room genuinely
selects the words. Change the noise, get different speech. Two rooms
cannot produce the same utterance.
Rendered as an intrusion rather than a reply — violet edge, full opacity
against the faded ambient murmurs, brief blur-in. The unsettling part is
that it is perfectly clear and completely unbidden.
Also fixes a hang I introduced: the two new summon tests consumed the
shared module-level per-IP budget, so test_summon_rate_limited_* blocked
forever on an entity frame that had been rate-limited away. They now scope
their own limiters.
264 backend + 355 frontend tests pass; i18n parity gate passes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
177 lines
6.4 KiB
Python
177 lines
6.4 KiB
Python
"""Tests for the real-astronomy layer.
|
|
|
|
These check the maths against genuinely known astronomical events rather
|
|
than against the implementation's own output — a test that only asserts
|
|
"the function returns what the function returns" would happily pass on
|
|
completely wrong orbital mechanics.
|
|
"""
|
|
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
|
|
from app.celestial import (
|
|
PHASE_NAMES,
|
|
SYNODIC_MONTH_DAYS,
|
|
moon_illumination,
|
|
moon_phase,
|
|
moon_phase_name,
|
|
solar_midnight,
|
|
veil_thinness,
|
|
witching_proximity,
|
|
)
|
|
|
|
|
|
def _utc(y, m, d, hh=0, mm=0):
|
|
return datetime(y, m, d, hh, mm, tzinfo=timezone.utc)
|
|
|
|
|
|
class TestMoonPhase:
|
|
def test_epoch_new_moon_reads_as_new(self):
|
|
# The reference epoch itself must land on ~0.
|
|
assert moon_phase(_utc(2000, 1, 6, 18, 14)) == pytest.approx(0.0, abs=0.01)
|
|
|
|
def test_known_full_moons_read_as_full(self):
|
|
# Real full moons, from published ephemerides. Tolerance is ~half a
|
|
# day in phase terms, which is what the mean-synodic approximation
|
|
# honestly supports.
|
|
for when in (
|
|
_utc(2024, 1, 25, 17, 54),
|
|
_utc(2024, 8, 19, 18, 26),
|
|
_utc(2025, 3, 14, 6, 55),
|
|
):
|
|
assert moon_phase(when) == pytest.approx(0.5, abs=0.04), when
|
|
|
|
def test_known_new_moons_read_as_new(self):
|
|
for when in (
|
|
_utc(2024, 2, 9, 22, 59),
|
|
_utc(2024, 9, 3, 1, 56),
|
|
_utc(2025, 3, 29, 10, 58),
|
|
):
|
|
phase = moon_phase(when)
|
|
# New moon sits at the 0/1 wraparound, so accept either end.
|
|
assert min(phase, 1 - phase) == pytest.approx(0.0, abs=0.04), when
|
|
|
|
def test_phase_always_in_unit_range(self):
|
|
when = _utc(2024, 1, 1)
|
|
for i in range(400):
|
|
p = moon_phase(when + timedelta(days=i))
|
|
assert 0.0 <= p < 1.0
|
|
|
|
def test_advances_a_full_cycle_over_one_synodic_month(self):
|
|
start = _utc(2024, 6, 1)
|
|
later = start + timedelta(days=SYNODIC_MONTH_DAYS)
|
|
assert moon_phase(start) == pytest.approx(moon_phase(later), abs=0.001)
|
|
|
|
def test_naive_datetime_is_treated_as_utc_rather_than_raising(self):
|
|
# A summon must never fail because a caller forgot a tzinfo.
|
|
naive = datetime(2024, 1, 25, 17, 54)
|
|
assert moon_phase(naive) == pytest.approx(0.5, abs=0.04)
|
|
|
|
|
|
class TestPhaseNames:
|
|
def test_new_and_full_map_to_the_right_names(self):
|
|
assert moon_phase_name(0.0) == "new moon"
|
|
assert moon_phase_name(0.5) == "full moon"
|
|
|
|
def test_quarters_map_to_the_right_names(self):
|
|
assert moon_phase_name(0.25) == "first quarter"
|
|
assert moon_phase_name(0.75) == "last quarter"
|
|
|
|
def test_every_phase_yields_a_known_name(self):
|
|
for i in range(100):
|
|
assert moon_phase_name(i / 100) in PHASE_NAMES
|
|
|
|
def test_names_progress_in_order_across_a_cycle(self):
|
|
seen = []
|
|
for i in range(64):
|
|
name = moon_phase_name(i / 64)
|
|
if not seen or seen[-1] != name:
|
|
seen.append(name)
|
|
# Starts and ends on "new moon" (the cycle wraps), covering all 8.
|
|
assert set(seen) == set(PHASE_NAMES)
|
|
|
|
|
|
class TestIllumination:
|
|
def test_new_moon_is_dark_and_full_moon_is_lit(self):
|
|
assert moon_illumination(0.0) == pytest.approx(0.0, abs=1e-9)
|
|
assert moon_illumination(0.5) == pytest.approx(1.0, abs=1e-9)
|
|
|
|
def test_quarters_are_half_lit(self):
|
|
assert moon_illumination(0.25) == pytest.approx(0.5, abs=1e-9)
|
|
assert moon_illumination(0.75) == pytest.approx(0.5, abs=1e-9)
|
|
|
|
def test_always_in_unit_range(self):
|
|
for i in range(200):
|
|
v = moon_illumination(i / 200)
|
|
assert 0.0 <= v <= 1.0
|
|
|
|
|
|
class TestSolarMidnight:
|
|
def test_greenwich_midnight_is_utc_midnight(self):
|
|
got = solar_midnight(0.0, _utc(2024, 6, 15, 23, 0))
|
|
assert got.hour == 0 and got.minute == 0
|
|
|
|
def test_longitude_shifts_midnight_by_an_hour_per_15_degrees(self):
|
|
at_zero = solar_midnight(0.0, _utc(2024, 6, 15, 12, 0))
|
|
at_fifteen_east = solar_midnight(15.0, _utc(2024, 6, 15, 12, 0))
|
|
delta_hours = (at_zero - at_fifteen_east).total_seconds() / 3600
|
|
assert delta_hours == pytest.approx(1.0, abs=0.01)
|
|
|
|
def test_returns_the_nearest_midnight_not_a_stale_one(self):
|
|
# Just before local midnight the answer must be the one ahead,
|
|
# never the one ~24h behind.
|
|
when = _utc(2024, 6, 15, 23, 50)
|
|
assert abs((solar_midnight(0.0, when) - when).total_seconds()) < 3600
|
|
|
|
|
|
class TestWitchingProximity:
|
|
def test_peaks_at_solar_midnight(self):
|
|
when = _utc(2024, 6, 15, 0, 0)
|
|
assert witching_proximity(0.0, when) == pytest.approx(1.0, abs=0.01)
|
|
|
|
def test_bottoms_out_at_solar_noon(self):
|
|
when = _utc(2024, 6, 15, 12, 0)
|
|
assert witching_proximity(0.0, when) == pytest.approx(0.0, abs=0.01)
|
|
|
|
def test_always_in_unit_range_around_the_clock(self):
|
|
base = _utc(2024, 6, 15)
|
|
for hour in range(48):
|
|
v = witching_proximity(0.0, base + timedelta(hours=hour))
|
|
assert 0.0 <= v <= 1.0
|
|
|
|
def test_accounts_for_the_seekers_longitude(self):
|
|
# 03:00 UTC is the dead of night at Greenwich but not in Tokyo.
|
|
when = _utc(2024, 6, 15, 3, 0)
|
|
assert witching_proximity(0.0, when) > witching_proximity(139.7, when)
|
|
|
|
|
|
class TestVeilThinness:
|
|
def test_reports_all_components(self):
|
|
r = veil_thinness(0.0, _utc(2024, 1, 25, 0, 0))
|
|
assert set(r) == {
|
|
"moon_phase",
|
|
"moon_name",
|
|
"moon_illumination",
|
|
"witching_proximity",
|
|
"thinness",
|
|
}
|
|
|
|
def test_moon_only_when_location_is_unknown(self):
|
|
r = veil_thinness(None, _utc(2024, 1, 25, 17, 54))
|
|
assert r["witching_proximity"] is None
|
|
# A full moon with no location should still read as thin.
|
|
assert r["thinness"] > 0.9
|
|
|
|
def test_full_moon_at_solar_midnight_is_thinner_than_new_moon_at_noon(self):
|
|
best = veil_thinness(0.0, _utc(2024, 1, 25, 0, 0))
|
|
worst = veil_thinness(0.0, _utc(2024, 2, 9, 12, 0))
|
|
assert best["thinness"] > worst["thinness"]
|
|
|
|
def test_thinness_always_in_unit_range(self):
|
|
base = _utc(2024, 1, 1)
|
|
for i in range(0, 400, 7):
|
|
for lon in (-180.0, -75.0, 0.0, 139.7, 180.0):
|
|
v = veil_thinness(lon, base + timedelta(days=i, hours=i % 24))
|
|
assert 0.0 <= v["thinness"] <= 1.0
|