personal memory agent
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

link: add test_paths for solstone

+122
+122
tests/link/test_paths.py
··· 1 + # SPDX-License-Identifier: AGPL-3.0-only 2 + # Copyright (c) 2026 sol pbc 3 + 4 + from __future__ import annotations 5 + 6 + import json 7 + from pathlib import Path 8 + 9 + import pytest 10 + 11 + from think.link.paths import ( 12 + DEFAULT_RELAY_URL, 13 + LinkState, 14 + account_token_path, 15 + load_account_token, 16 + relay_url, 17 + save_account_token, 18 + state_path, 19 + ) 20 + 21 + 22 + def _set_journal(monkeypatch: pytest.MonkeyPatch, journal: Path) -> None: 23 + monkeypatch.setenv("_SOLSTONE_JOURNAL_OVERRIDE", str(journal)) 24 + 25 + 26 + def test_link_state_load_or_create_creates_state( 27 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 28 + ) -> None: 29 + _set_journal(monkeypatch, tmp_path) 30 + 31 + state = LinkState.load_or_create() 32 + 33 + assert isinstance(state.instance_id, str) 34 + assert state.instance_id 35 + assert state.home_label == "solstone" 36 + assert state_path().exists() 37 + 38 + 39 + def test_link_state_load_or_create_idempotent( 40 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 41 + ) -> None: 42 + _set_journal(monkeypatch, tmp_path) 43 + 44 + first = LinkState.load_or_create() 45 + first_payload = state_path().read_text("utf-8") 46 + second = LinkState.load_or_create() 47 + second_payload = state_path().read_text("utf-8") 48 + 49 + assert second.instance_id == first.instance_id 50 + assert second.home_label == first.home_label 51 + assert second_payload == first_payload 52 + 53 + 54 + def test_link_state_load_or_create_custom_label( 55 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 56 + ) -> None: 57 + _set_journal(monkeypatch, tmp_path) 58 + 59 + created = LinkState.load_or_create(default_label="laptop") 60 + loaded = LinkState.load_or_create() 61 + 62 + assert created.home_label == "laptop" 63 + assert loaded.instance_id == created.instance_id 64 + assert loaded.home_label == "laptop" 65 + 66 + 67 + def test_relay_url_env_wins(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: 68 + _set_journal(monkeypatch, tmp_path) 69 + monkeypatch.setenv("SOL_LINK_RELAY_URL", "https://example.test/") 70 + 71 + assert relay_url() == "https://example.test" 72 + 73 + 74 + def test_relay_url_from_config(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: 75 + _set_journal(monkeypatch, tmp_path) 76 + config_path = tmp_path / "config" / "journal.json" 77 + config_path.parent.mkdir(parents=True, exist_ok=True) 78 + config_path.write_text( 79 + json.dumps({"link": {"relay_url": "https://cfg.test"}}), 80 + encoding="utf-8", 81 + ) 82 + 83 + assert relay_url() == "https://cfg.test" 84 + 85 + 86 + def test_relay_url_default(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: 87 + _set_journal(monkeypatch, tmp_path) 88 + monkeypatch.delenv("SOL_LINK_RELAY_URL", raising=False) 89 + 90 + assert relay_url() == DEFAULT_RELAY_URL 91 + 92 + 93 + def test_load_account_token_missing( 94 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 95 + ) -> None: 96 + _set_journal(monkeypatch, tmp_path) 97 + 98 + assert load_account_token() is None 99 + 100 + 101 + def test_save_and_load_account_token_roundtrip( 102 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 103 + ) -> None: 104 + _set_journal(monkeypatch, tmp_path) 105 + 106 + save_account_token("tok.123") 107 + 108 + token_path = account_token_path() 109 + assert load_account_token() == "tok.123" 110 + assert token_path.stat().st_mode & 0o777 == 0o600 111 + 112 + 113 + def test_save_account_token_is_atomic( 114 + tmp_path: Path, monkeypatch: pytest.MonkeyPatch 115 + ) -> None: 116 + _set_journal(monkeypatch, tmp_path) 117 + 118 + save_account_token("tok.123") 119 + 120 + token_path = account_token_path() 121 + assert token_path.exists() 122 + assert not any(path.name.endswith(".tmp") for path in token_path.parent.iterdir())