# SPDX-License-Identifier: AGPL-3.0-only # Copyright (c) 2026 sol pbc import json import re from pathlib import Path from types import SimpleNamespace import pytest from solstone.apps.observer.routes import ACTIVE_THRESHOLD_MS, STALE_THRESHOLD_MS from solstone.apps.observer.utils import save_observer from solstone.apps.thinking.copy import CONFIDENTIAL_LANE_DETAIL, LANES from solstone.convey import create_app from solstone.think.utils import get_journal, now_ms from tests.helpers.journal_config import seed_journal_config def _read_config(journal_dir): return json.loads((journal_dir / "config" / "journal.json").read_text()) def _read_init_state(client): response = client.get("/init/api/state") assert response.status_code == 200 return response.get_json() def _make_empty_client(tmp_path, monkeypatch, *, timezone="America/Denver"): journal = tmp_path / "journal" journal.mkdir() monkeypatch.setenv("SOLSTONE_JOURNAL", str(journal)) monkeypatch.setattr( "solstone.think.journal_config._resolve_os_identity", lambda: ("OS User", "osuser"), ) monkeypatch.setattr( "solstone.think.journal_config._resolve_os_timezone", lambda: timezone ) app = create_app(str(journal)) app.config["TESTING"] = True return app.test_client(), journal def _commit_journal_identity() -> None: from solstone.think.link.ca import load_or_generate_ca from solstone.think.link.paths import ca_dir load_or_generate_ca(ca_dir()) def _clear_setup(journal_dir): config = _read_config(journal_dir) config.pop("setup", None) seed_journal_config(config, journal_dir) def _save_test_observer( key_prefix: str, name: str, *, created_at: int, last_seen: int | None, revoked: bool = False, ): key = key_prefix + ("f" * 56) assert save_observer( { "key": key, "name": name, "created_at": created_at, "last_seen": last_seen, "last_segment": None, "enabled": True, "revoked": revoked, "revoked_at": created_at + 1 if revoked else None, "stats": {}, } ) return key @pytest.fixture def fresh_client(journal_copy): _clear_setup(journal_copy) app = create_app(str(journal_copy)) app.config["TESTING"] = True return app.test_client() @pytest.fixture def configured_client(journal_copy): app = create_app(str(journal_copy)) app.config["TESTING"] = True return app.test_client() class TestInitDetection: def test_redirects_to_init_when_setup_incomplete(self, fresh_client): resp = fresh_client.get("/") assert resp.status_code == 302 assert "/init" in resp.headers["Location"] def test_setup_complete_serves_root(self, configured_client): resp = configured_client.get("/") assert resp.status_code == 302 assert resp.headers["Location"].endswith("/app/home/") def test_init_page_renders(self, fresh_client): resp = fresh_client.get("/init") assert resp.status_code == 200 assert b"create your journal" in resp.data state = _read_init_state(fresh_client) assert state["identity_name"] == "Test User" assert state["identity_preferred"] == "Tester" assert b'id="section-password"' not in resp.data assert b'id="password"' not in resp.data def test_init_title_is_welcome_setup(self, fresh_client): resp = fresh_client.get("/init") assert "create your journal โ€” solstone".encode() in resp.data def test_init_renders_version(self, fresh_client): try: from importlib.metadata import version as _v expected = _v("solstone") except Exception: expected = "dev" assert _read_init_state(fresh_client)["version"] in {expected, "dev"} def test_init_state_carries_thinking_lanes(self, fresh_client): state = _read_init_state(fresh_client) assert state["lanes"] == [dict(lane) for lane in LANES] assert state["confidential"]["lane_detail"] == dict(CONFIDENTIAL_LANE_DETAIL) assert set(state["confidential"]["lane_detail"]) == { "heading", "sub", "mechanism", "egress", "claims", "attestation", "early_access", } def test_init_renders_journal_path_in_welcome(self, fresh_client): journal_path = str(Path(get_journal())) resp = fresh_client.get("/init") assert _read_init_state(fresh_client)["journal_path"] == journal_path assert b"solstone is three things working together" not in resp.data assert b"solstone is two things working together" not in resp.data assert b"your journal lives on this computer." in resp.data assert ( b"sol experiences your day with you and keeps it all here, in your journal" in resp.data ) def test_init_sol_agent_section_renders(self, fresh_client): resp = fresh_client.get("/init") assert b">how should sol think?<" in resp.data assert b'id="provider-lanes"' in resp.data assert b'id="provider-skip"' in resp.data def test_init_sol_agent_paragraphs(self, fresh_client): resp = fresh_client.get("/init") assert ( b"sol reasons about your journal and answers you. it thinks the way " b"you choose here \xe2\x80\x94 and you can change it anytime in " b"thinking." ) in resp.data assert ( b"when you finish, thinking opens to the lane you picked. " b"the fork lives in thinking from here on \xe2\x80\x94 switch anytime." ) in resp.data def test_init_no_legacy_trust_note(self, fresh_client): resp = fresh_client.get("/init") assert b"your key is stored locally" not in resp.data def test_init_lane_cards_are_dynamic(self, fresh_client): resp = fresh_client.get("/init") assert b'id="provider-lane-local"' not in resp.data assert b'id="provider-lane-confidential"' not in resp.data assert b'id="provider-lane-byo"' not in resp.data assert b"LANE_GLYPHS" in resp.data def test_machine_card_present_and_verbatim(self, fresh_client): resp = fresh_client.get("/init") assert b"your journal lives on this computer." in resp.data assert ( b"sol experiences your day with you and keeps it all here, " b"in your journal. nothing else is required to start." ) in resp.data assert b"sol pbc offers a few optional services" not in resp.data assert ( b"turn them on if they help. turn them off whenever you want. or never." not in resp.data ) assert ( b"observers \xe2\x80\x94 experience your day along with you" not in resp.data ) assert b"where memories are stored and curated by sol" not in resp.data def test_machine_card_section_id(self, fresh_client): resp = fresh_client.get("/init") assert b'id="section-machine-card"' in resp.data assert b'id="section-trinity"' not in resp.data def test_identity_hint_refresh(self, fresh_client): resp = fresh_client.get("/init") assert ( b"optional \xe2\x80\x94 just helps sol know what to call you." in resp.data ) assert ( b"optional \xe2\x80\x94 helps sol address you correctly." not in resp.data ) def test_footer_note_refresh(self, fresh_client): resp = fresh_client.get("/init") assert ( b"your journal stays on this computer " b"\xe2\x80\x94 nothing leaves unless you send it." ) in resp.data assert b"your data stays on your machine" not in resp.data def test_portal_unreachable_stub_inert_on_default_path(self, fresh_client): resp = fresh_client.get("/init") assert b"portal-unreachable" not in resp.data assert b"can't reach sol pbc right now." not in resp.data assert b"L11-stub: portal-unreachable" not in resp.data def test_init_retention_radios_present(self, fresh_client): resp = fresh_client.get("/init") assert resp.data.count(b'") assert form_start < button < form_end def test_init_redirects_when_configured(self, configured_client): resp = configured_client.get("/init") assert resp.status_code == 302 assert resp.headers["Location"] == "/" def test_init_empty_journal_materializes_config(self, tmp_path, monkeypatch): client, journal = _make_empty_client(tmp_path, monkeypatch) resp = client.get("/init") assert resp.status_code == 200 config = _read_config(journal) assert config["identity"]["name"] == "OS User" assert config["identity"]["preferred"] == "osuser" assert config["identity"]["timezone"] == "America/Denver" assert "convey" not in config state = _read_init_state(client) assert state["identity_name"] == "OS User" assert state["identity_preferred"] == "osuser" def test_init_escapes_identity_values(self, journal_copy): config = _read_config(journal_copy) config.pop("setup", None) config["identity"]["name"] = "" seed_journal_config(config, journal_copy) app = create_app(str(journal_copy)) app.config["TESTING"] = True resp = app.test_client().get("/init") assert b"" not in resp.data state = app.test_client().get("/init/api/state").get_json() assert state["identity_name"] == "" def test_init_does_not_overwrite_existing_identity(self, journal_copy): config = _read_config(journal_copy) config.pop("setup", None) config["identity"]["name"] = "Existing User" config["identity"]["preferred"] = "Existing" config["identity"]["timezone"] = "UTC" seed_journal_config(config, journal_copy) before = _read_config(journal_copy) app = create_app(str(journal_copy)) app.config["TESTING"] = True resp = app.test_client().get("/init") after = _read_config(journal_copy) assert resp.status_code == 200 assert after == before def test_init_get_does_not_overwrite_corrupt_config(self, journal_copy): app = create_app(str(journal_copy)) app.config["TESTING"] = True client = app.test_client() config_path = journal_copy / "config" / "journal.json" config_path.write_bytes(b"{ invalid json }") before = config_path.read_bytes() resp = client.get("/init") assert resp.status_code == 500 assert resp.get_json()["reason_code"] == "corrupt_config" assert config_path.read_bytes() == before class TestInitMark: def test_mark_section_scaffold(self, fresh_client): resp = fresh_client.get("/init") assert resp.status_code == 200 assert b'id="section-journal-mark"' in resp.data assert b"this is your journal" in resp.data assert b'id="journal-mark-display"' in resp.data def test_mark_routes_referenced(self, fresh_client): resp = fresh_client.get("/init") assert b"/init/mark/regenerate" in resp.data assert b"/init/mark/lock" in resp.data assert b"/init/mark" in resp.data def test_mark_svg_wrapper_present(self, fresh_client): resp = fresh_client.get("/init") assert b'viewBox="0 0 24 24"' in resp.data assert b'stroke-width="2"' in resp.data def test_mark_error_branches_present(self, fresh_client): resp = fresh_client.get("/init") assert b'id="journal-mark-error"' in resp.data assert b'id="journal-mark-retry"' in resp.data assert resp.data.count(b"showMarkError(") >= 4 def test_mark_copy_verbatim(self, fresh_client): resp = fresh_client.get("/init") for text in ( "this is your journal", "every journal has its own mark โ€” two symbols and two words, unique to yours. you'll recognize it whenever you connect a device.", "try another", "lock it in", "lock it in and this mark is your journal's, for good โ€” it can't be changed later. try as many as you like first.", "locked in ยท this is your journal's mark, for good.", ): assert text.encode() in resp.data def test_no_jid_token(self, fresh_client): resp = fresh_client.get("/init") assert re.search(r"\bjid\b", resp.data.decode()) is None def test_finalize_blocked_when_unlocked(self, fresh_client): resp = fresh_client.post( "/init/finalize", json={"name": "X"}, content_type="application/json", ) assert resp.status_code == 400 assert resp.get_json()["reason_code"] == "identity_not_locked" def test_finalize_catch_routes_server_error(self, fresh_client): resp = fresh_client.get("/init") assert b"data.warnings" in resp.data assert b"err.serverMessage" in resp.data assert b"showFinalizeError" in resp.data def test_mark_locked_on_commit(self, fresh_client): _commit_journal_identity() resp = fresh_client.get("/init/mark") assert resp.get_json()["locked"] is True def test_regenerate_blocked_when_locked(self, fresh_client): _commit_journal_identity() resp = fresh_client.post("/init/mark/regenerate") assert resp.status_code == 400 assert resp.get_json()["reason_code"] == "invalid_operation_for_state" def test_linked_assets_load(self, fresh_client): resp = fresh_client.get("/init") assert resp.status_code == 200 text = resp.data.decode() urls = re.findall(r'