Python SDK for shellbound Django applications. Provides ShellApp, ShardContext, ShellboundMiddleware. Emits Chronicle events to stdout in dev mode. Includes fix for IndexError in apps.py when DJANGO_SETTINGS_MODULE has no dots (e.g. instance_settings). Shard name now falls back safely without eager default argument parsing. Implements SHELLBOUND-APP-0001 §4 (dev mode). Wired into entropyopposition as of 2026-03-18.
52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
"""Tests for Chronicle event emission."""
|
|
|
|
import json
|
|
from io import StringIO
|
|
from unittest.mock import patch
|
|
|
|
from substrate_sdk.chronicle import LAYER_APPLICATION, ShardEmitter, emit_event
|
|
from substrate_sdk.context import ShardContext
|
|
|
|
|
|
class TestEmitEvent:
|
|
|
|
def test_writes_to_stdout(self, capsys):
|
|
emit_event("TEST_EVENT", 3, {"key": "val"})
|
|
out = capsys.readouterr().out.strip()
|
|
data = json.loads(out)
|
|
assert data["substrate_event"] is True
|
|
|
|
def test_kind_in_output(self, capsys):
|
|
emit_event("APP_SHARD_STARTED", 3, {})
|
|
data = json.loads(capsys.readouterr().out.strip())
|
|
assert data["kind"] == "APP_SHARD_STARTED"
|
|
|
|
def test_layer_in_output(self, capsys):
|
|
emit_event("X", 2, {})
|
|
data = json.loads(capsys.readouterr().out.strip())
|
|
assert data["layer"] == 2
|
|
|
|
def test_non_fatal_on_error(self):
|
|
with patch("builtins.print", side_effect=Exception("boom")):
|
|
result = emit_event("TEST", 3, {})
|
|
assert result is False
|
|
|
|
|
|
class TestShardEmitter:
|
|
|
|
def test_carries_context(self, capsys):
|
|
ctx = ShardContext(shard_name="test", shard_id="abc-123", accord_hash="xyz-456")
|
|
emitter = ShardEmitter(ctx)
|
|
emitter.emit("TEST", 3, {})
|
|
data = json.loads(capsys.readouterr().out.strip())
|
|
assert data["accord_hash"] == "xyz-456"
|
|
assert data["shard_id"] == "abc-123"
|
|
|
|
def test_request_helper(self, capsys):
|
|
ctx = ShardContext(shard_name="test", shard_id="s1")
|
|
emitter = ShardEmitter(ctx)
|
|
emitter.request("GET", "/api/", 200, 42.0)
|
|
data = json.loads(capsys.readouterr().out.strip())
|
|
assert data["kind"] == "APP_DJANGO_REQUEST"
|
|
assert data["payload"]["method"] == "GET"
|
|
assert data["payload"]["status"] == 200
|