refactor!: one way to serve books: the stdio server is gone (D-44)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
The owner (2026-10-23): "We should probably just delete the stdio path altogether. THere's no point in that. And it sitting there unused will just lead to bitrot. Nuke it?" Books are served only by the books server (`wanderbooks server`), reached by agents through the bridge (`wanderbooks mcp --book ID`), which stays. Removed: `wanderbooks dev-stdio`, the `ledger-mcp` binary, and `ledger_mcp::run` (stdio and `--http` over a bare data dir), with all that only that mode had: - files the books make coming back inline: `HandedFile.content_base64` is gone, and `download` and `expires_at` are always there; - books with no access log or backups: `books.access_log`, `books.backup` and `books.back_up_now` are always offered, by scope; - the settings file read and reloaded (`legacy_files`), the vault key named by a file or `--vault-key-file`, and the books dir's key-inside-the-books and git checks (with `paths.rs`); - `wire.rs` (the bridge frames an agent's lines itself); - `books.settings`' file fields and `books.start`'s `settings_source`. `BooksServer` is built one way, from `Served` (a book as the books server serves it), in production and in the unit tests. An unknown tool is now a protocol error (mcp-tools.md). Inline file input is unchanged. Then the owner's three decisions (2026-10-23, the question tool): (a) `books.import_settings` is removed entirely (118 tools): old books come in whole by `wanderbooks books adopt`, which reads their settings file once; ledger-config keeps its settings-file parsing only as far as adoption uses it (`load` and the path-reading `effective_receipt_policy` are gone), and `books.chart_template` gives its settings as `books.set_setting` changes. (b) The viewer's loose-books-folder mode is removed (`--data-dir`, `--books-toml`, `--vault-key-file`, the settings file and config.json followed): it opens only books of the data root, reading settings from the journal and the vault from the book's own `vault/` and `keys/`. (c) `wanderbooks dev-stdio` has no stub: it is an unknown command like any other word. Also: an upload's expiry past the calendar is an error, never an empty string. Tests: every suite that drove the stdio server moved to crates/ledger-cli/tests/ and drives a real `wanderbooks server` on a data root of its own (tests/common), handed files by their download; mcp_stdio.rs is mcp_journeys.rs. Tests of what is gone are deleted and listed in docs/testing.md. Docs: plan D-44 (D-18 superseded), STATUS, testing, rules, mcp-interface, the handbook, README; justfile recipes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
This commit is contained in:
87 files changed
+3477
-6476
No files matched your search
@@ -1,91 +1,56 @@
|
||||
//! The server's settings: the books' own (journaled, ledger_core::
|
||||
//! settings), or, for books that hold none yet, the settings files
|
||||
//! (`wanderbooks.toml`, and the receipt policy in `books-data/config.json`)
|
||||
//! reloaded whenever they change.
|
||||
//! settings), the defaults until the first is set.
|
||||
//!
|
||||
//! ```text
|
||||
//! GOAL: every request sees the settings in force, one version for the
|
||||
//! whole request; books that keep their own settings never read a
|
||||
//! file; a broken file edit never takes the server down.
|
||||
//! whole request, and they are only ever the books' own.
|
||||
//! SOURCE: plan/served.md I-3 (the journal holds all books state; the
|
||||
//! server never reads a settings file for books that hold their
|
||||
//! settings), 2.1 ("dev-stdio keeps legacy_files mode until a book
|
||||
//! is adopted"); DECISION (owner, 2026-10-15): file settings are
|
||||
//! always auto-reloaded.
|
||||
//! server never reads a settings file), 3.4 (a book in the data
|
||||
//! root keeps its own settings), D-44 (one way to serve books: no
|
||||
//! settings file is read by any of them).
|
||||
//! INVARIANTS:
|
||||
//! 1. Books whose journal holds settings (any settings.set) use those,
|
||||
//! and from then on no file is read: sync takes them whenever the
|
||||
//! journal's last settings.set changes.
|
||||
//! 2. Otherwise every request (tool call, resource read) sees the files'
|
||||
//! contents as of its start: a request after an edit sees the edit
|
||||
//! (compared by content, not by timestamp, so two edits in one second
|
||||
//! are both seen).
|
||||
//! 3. A file that fails to load leaves the last good settings in force,
|
||||
//! and the failure is reported (books.settings, stderr) until a later
|
||||
//! edit loads.
|
||||
//! 4. A settings file that doesn't exist and wasn't asked for (the
|
||||
//! default wanderbooks.toml beside the books) means the defaults; one
|
||||
//! named with --books-toml that goes missing is a failure (3).
|
||||
//! 5. Only the settings change while serving: the data dir, the books'
|
||||
//! base currency, and a vault key file given on the command line are
|
||||
//! fixed when the server starts (the key file given wins over any
|
||||
//! file's [vault] key_file).
|
||||
//! 6. A book in the data root (plan 3.4) has no settings file at all:
|
||||
//! its settings are its own, the defaults until the first is set,
|
||||
//! and no file is ever read for it.
|
||||
//! 1. The settings in force are the books' own: the defaults until
|
||||
//! their journal holds any settings.set, then those; sync takes them
|
||||
//! whenever the journal's last settings.set changes, and only then.
|
||||
//! 2. No settings file is ever read.
|
||||
//! ```
|
||||
|
||||
use crate::policy::ReceiptPolicy;
|
||||
use ledger_config::Company;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// `[books]` account roles for the cash flow statement, used when a call
|
||||
/// doesn't pass its own: cash, financing, and investing account codes.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct CashRoles {
|
||||
/// `[books] cash_accounts`.
|
||||
/// `books.cash_accounts`.
|
||||
pub cash: Vec<String>,
|
||||
/// `[books] financing_accounts`.
|
||||
/// `books.financing_accounts`.
|
||||
pub financing: Vec<String>,
|
||||
/// `[books] investing_accounts`.
|
||||
/// `books.investing_accounts`.
|
||||
pub investing: Vec<String>,
|
||||
}
|
||||
|
||||
/// What the settings files say, as last loaded.
|
||||
/// The settings in force, as the server uses them.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Settings {
|
||||
/// Which entries need a receipt.
|
||||
pub policy: ReceiptPolicy,
|
||||
/// `[company]`.
|
||||
/// The company (`company.*`).
|
||||
pub company: Company,
|
||||
/// `[books]` cash flow roles.
|
||||
/// The cash flow roles.
|
||||
pub roles: CashRoles,
|
||||
/// The prefix of generated asset tags (`[books] asset_tag_prefix`, or
|
||||
/// The prefix of generated asset tags (`books.asset_tag_prefix`, or
|
||||
/// the company's initials).
|
||||
pub asset_tag_prefix: String,
|
||||
/// `[vault] key_file`: the TIN vault's key file (unset: the vault's
|
||||
/// default, `ledger_vault::default_key_file`).
|
||||
pub vault_key_file: Option<PathBuf>,
|
||||
/// `[books] parties`: suggest, require, or never mention each entry's
|
||||
/// `policy.parties`: suggest, require, or never mention each entry's
|
||||
/// counterparty.
|
||||
pub parties: ledger_config::PartyPolicy,
|
||||
/// The company's time zone (an IANA name), if set: what "today" means.
|
||||
pub timezone: Option<String>,
|
||||
/// Whether these are the books' own settings (journaled) rather than a
|
||||
/// file's.
|
||||
pub journaled: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
/// The vault key file in force: `[vault] key_file`, else the default
|
||||
/// outside the books (`~/.config/wanderbooks/vault.key`).
|
||||
pub fn key_file(&self) -> PathBuf {
|
||||
self.vault_key_file
|
||||
.clone()
|
||||
.unwrap_or_else(ledger_vault::default_key_file)
|
||||
}
|
||||
|
||||
/// The settings the books keep (`ledger_core::settings`), as the
|
||||
/// server uses them.
|
||||
pub fn from_books(books: &ledger_core::settings::Settings, currency: &str) -> Self {
|
||||
@@ -93,7 +58,6 @@ impl Settings {
|
||||
Settings {
|
||||
policy: ledger_config::codec::receipt_policy(books),
|
||||
asset_tag_prefix: b.asset_tag_prefix(),
|
||||
vault_key_file: None,
|
||||
parties: b.parties,
|
||||
timezone: b.timezone.clone(),
|
||||
roles: CashRoles {
|
||||
@@ -102,7 +66,6 @@ impl Settings {
|
||||
investing: b.investing_accounts,
|
||||
},
|
||||
company: b.company,
|
||||
journaled: true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,179 +79,24 @@ impl Settings {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the settings come from.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Source {
|
||||
/// The books' wanderbooks.toml.
|
||||
pub books_toml: PathBuf,
|
||||
/// Whether it must exist (named with --books-toml) or may be absent
|
||||
/// (the default beside the books: then the defaults apply).
|
||||
pub required: bool,
|
||||
/// `config.json`, for its receipt policy (optional).
|
||||
pub config_json: PathBuf,
|
||||
}
|
||||
|
||||
/// Both files' bytes (`None`: absent), to tell when either changed.
|
||||
type Seen = (Option<Vec<u8>>, Option<Vec<u8>>);
|
||||
|
||||
impl Source {
|
||||
fn read(&self) -> Seen {
|
||||
(
|
||||
std::fs::read(&self.books_toml).ok(),
|
||||
std::fs::read(&self.config_json).ok(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Load the settings now.
|
||||
pub fn load(&self) -> Result<Settings, String> {
|
||||
let books = if self.required || self.books_toml.exists() {
|
||||
ledger_config::load(&self.books_toml)?
|
||||
} else {
|
||||
ledger_config::BooksSettings::default()
|
||||
};
|
||||
// config.json sits in the data dir, which no message names.
|
||||
let policy = ledger_config::effective_receipt_policy(&self.config_json, &books)
|
||||
.map_err(|e| e.replace(&self.config_json.display().to_string(), "config.json"))?;
|
||||
Ok(Settings {
|
||||
asset_tag_prefix: books.asset_tag_prefix(),
|
||||
vault_key_file: books.vault_key_file.clone(),
|
||||
parties: books.parties,
|
||||
timezone: books.timezone.clone(),
|
||||
journaled: false,
|
||||
policy,
|
||||
roles: CashRoles {
|
||||
cash: books.cash_accounts,
|
||||
financing: books.financing_accounts,
|
||||
investing: books.investing_accounts,
|
||||
},
|
||||
company: books.company,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// The settings in force, and how they got there.
|
||||
/// The settings in force, and the books' settings.set they came from.
|
||||
#[derive(Debug)]
|
||||
pub struct Live {
|
||||
current: Arc<Settings>,
|
||||
source: Option<Source>,
|
||||
seen: Seen,
|
||||
error: Option<String>,
|
||||
loads: u64,
|
||||
/// The seq of the books' last settings.set taken (INVARIANT 1).
|
||||
journal_seq: Option<u64>,
|
||||
/// A vault key file given on the command line (INVARIANT 5).
|
||||
vault_key_file: Option<PathBuf>,
|
||||
/// Whether the settings files may be read: false once the books keep
|
||||
/// their own (INVARIANT 1).
|
||||
read_files: bool,
|
||||
/// A book in the data root: its settings are only ever its own
|
||||
/// (INVARIANT 6).
|
||||
own: bool,
|
||||
}
|
||||
|
||||
/// What books.settings reports about the files.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Status {
|
||||
/// The wanderbooks.toml read, if the settings come from files.
|
||||
pub books_toml: Option<PathBuf>,
|
||||
/// Whether that file exists now.
|
||||
pub books_toml_exists: bool,
|
||||
/// Whether a config.json is read (it sits in the data dir, which is
|
||||
/// never named).
|
||||
pub config_json_present: bool,
|
||||
/// Why the last change failed to load (the last good settings stay in
|
||||
/// force), or `None`.
|
||||
pub error: Option<String>,
|
||||
/// How many times the settings have loaded, the first included.
|
||||
pub loads: u64,
|
||||
/// Whether the settings are the books' own (journaled): then no file
|
||||
/// is read.
|
||||
pub journaled: bool,
|
||||
}
|
||||
|
||||
impl Live {
|
||||
/// Settings that never reload (tests, and servers built in code).
|
||||
pub fn fixed(settings: Settings) -> Self {
|
||||
Self {
|
||||
current: Arc::new(settings),
|
||||
source: None,
|
||||
seen: (None, None),
|
||||
error: None,
|
||||
loads: 1,
|
||||
journal_seq: None,
|
||||
vault_key_file: None,
|
||||
read_files: false,
|
||||
own: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// For a book in the data root (INVARIANT 6): no settings file, now
|
||||
/// or later; `sync` brings the books' settings in once they hold any.
|
||||
/// For a book (INVARIANT 1): the defaults; `sync` brings the books'
|
||||
/// settings in once they hold any.
|
||||
pub fn own() -> Self {
|
||||
Self {
|
||||
current: Arc::new(Settings {
|
||||
journaled: true,
|
||||
..Settings::default()
|
||||
}),
|
||||
source: None,
|
||||
seen: (None, None),
|
||||
error: None,
|
||||
loads: 0,
|
||||
current: Arc::new(Settings::default()),
|
||||
journal_seq: None,
|
||||
vault_key_file: None,
|
||||
read_files: false,
|
||||
own: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Load from `source`, and reload from it whenever it changes. Fails
|
||||
/// only on this first load: a server doesn't start on broken settings.
|
||||
pub fn watching(source: Source) -> Result<Self, String> {
|
||||
let seen = source.read();
|
||||
let settings = source.load()?;
|
||||
Ok(Self {
|
||||
current: Arc::new(settings),
|
||||
source: Some(source),
|
||||
seen,
|
||||
error: None,
|
||||
loads: 1,
|
||||
journal_seq: None,
|
||||
vault_key_file: None,
|
||||
read_files: true,
|
||||
own: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// For books that keep their own settings: nothing is read from
|
||||
/// `source` (INVARIANT 1), not even at start, so a broken settings
|
||||
/// file beside them can't stop the server; `sync` brings the books'
|
||||
/// settings in.
|
||||
pub fn journaled(source: Source) -> Self {
|
||||
Self {
|
||||
current: Arc::new(Settings {
|
||||
journaled: true,
|
||||
..Settings::default()
|
||||
}),
|
||||
source: Some(source),
|
||||
seen: (None, None),
|
||||
error: None,
|
||||
loads: 0,
|
||||
journal_seq: None,
|
||||
vault_key_file: None,
|
||||
read_files: false,
|
||||
own: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Use this vault key file whatever the settings say (INVARIANT 5).
|
||||
pub fn with_vault_key_file(mut self, path: Option<PathBuf>) -> Self {
|
||||
self.vault_key_file = path;
|
||||
if let Some(p) = &self.vault_key_file {
|
||||
Arc::make_mut(&mut self.current).vault_key_file = Some(p.clone());
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Take the books' own settings when they hold any and they changed
|
||||
/// since the last look (INVARIANT 1). The server calls this whenever
|
||||
/// it holds the books.
|
||||
@@ -296,230 +104,34 @@ impl Live {
|
||||
if !books.is_journaled() || books.last_seq() == self.journal_seq {
|
||||
return;
|
||||
}
|
||||
let mut s = Settings::from_books(books, currency);
|
||||
s.vault_key_file = self.vault_key_file.clone();
|
||||
self.current = Arc::new(s);
|
||||
self.current = Arc::new(Settings::from_books(books, currency));
|
||||
self.journal_seq = books.last_seq();
|
||||
self.read_files = false;
|
||||
self.error = None;
|
||||
self.loads += 1;
|
||||
}
|
||||
|
||||
/// The settings files' texts as the server reads them now
|
||||
/// (`wanderbooks.toml`, `config.json`; `None` for one absent), when the
|
||||
/// settings come from files; `None` when the books keep their own or
|
||||
/// the settings were built in code.
|
||||
pub fn file_texts(&self) -> Option<(Option<String>, Option<String>)> {
|
||||
let source = self.source.as_ref().filter(|_| self.read_files)?;
|
||||
Some((
|
||||
std::fs::read_to_string(&source.books_toml).ok(),
|
||||
std::fs::read_to_string(&source.config_json).ok(),
|
||||
))
|
||||
}
|
||||
|
||||
/// The vault key file given on the command line, if any.
|
||||
pub fn vault_key_override(&self) -> Option<PathBuf> {
|
||||
self.vault_key_file.clone()
|
||||
}
|
||||
|
||||
/// Whether the settings in force are the books' own.
|
||||
pub fn is_journaled(&self) -> bool {
|
||||
self.own || self.journal_seq.is_some() || (self.source.is_some() && !self.read_files)
|
||||
}
|
||||
|
||||
/// The settings in force.
|
||||
pub fn current(&self) -> Arc<Settings> {
|
||||
self.current.clone()
|
||||
}
|
||||
|
||||
/// Reload if a file changed since the last look: the server calls this
|
||||
/// once at the start of every request, so the whole request sees one
|
||||
/// version.
|
||||
pub fn refresh(&mut self) {
|
||||
if !self.read_files {
|
||||
// The books keep their own settings (or the settings were
|
||||
// built in code): no file is read (INVARIANT 1).
|
||||
return;
|
||||
}
|
||||
if let Some(source) = &self.source {
|
||||
let now = source.read();
|
||||
if now != self.seen {
|
||||
self.seen = now;
|
||||
match source.load() {
|
||||
Ok(mut s) => {
|
||||
if let Some(p) = &self.vault_key_file {
|
||||
s.vault_key_file = Some(p.clone());
|
||||
}
|
||||
self.current = Arc::new(s);
|
||||
self.error = None;
|
||||
self.loads += 1;
|
||||
eprintln!("ledger-mcp: reloaded {}", source.books_toml.display());
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!(
|
||||
"ledger-mcp: settings not reloaded (keeping the last good ones): {e}"
|
||||
);
|
||||
self.error = Some(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the settings in force (builders; never reloads them away
|
||||
/// unless a watched file changes).
|
||||
#[cfg(test)]
|
||||
pub fn edit(&mut self, f: impl FnOnce(&mut Settings)) {
|
||||
f(Arc::make_mut(&mut self.current));
|
||||
}
|
||||
|
||||
/// Where the settings come from and whether the last change loaded.
|
||||
pub fn status(&mut self) -> Status {
|
||||
self.refresh();
|
||||
let journaled = self.is_journaled();
|
||||
Status {
|
||||
books_toml: self
|
||||
.source
|
||||
.as_ref()
|
||||
.filter(|_| !journaled)
|
||||
.map(|s| s.books_toml.clone()),
|
||||
books_toml_exists: self.source.as_ref().is_some_and(|s| s.books_toml.exists()),
|
||||
config_json_present: self.source.as_ref().is_some_and(|s| s.config_json.exists()),
|
||||
error: self.error.clone(),
|
||||
loads: self.loads,
|
||||
journaled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A test's dir, removed when the test ends, pass or panic.
|
||||
struct TestDir(std::path::PathBuf);
|
||||
|
||||
impl std::ops::Deref for TestDir {
|
||||
type Target = std::path::Path;
|
||||
fn deref(&self) -> &std::path::Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TestDir {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn dir(tag: &str) -> TestDir {
|
||||
let d =
|
||||
std::env::temp_dir().join(format!("ledger-mcp-settings-{}-{tag}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&d);
|
||||
std::fs::create_dir_all(&d).unwrap();
|
||||
TestDir(d)
|
||||
}
|
||||
|
||||
fn source(d: &std::path::Path, required: bool) -> Source {
|
||||
Source {
|
||||
books_toml: d.join("wanderbooks.toml"),
|
||||
required,
|
||||
config_json: d.join("config.json"),
|
||||
}
|
||||
}
|
||||
|
||||
/// INVARIANT 3: an absent default means the defaults, and the file
|
||||
/// loads once it appears; an unchanged file is not reloaded.
|
||||
/// INVARIANT 1: books that hold no settings keep the defaults; their
|
||||
/// settings come in once per change, and an unchanged journal leaves
|
||||
/// the settings in force as they are.
|
||||
#[test]
|
||||
fn an_absent_default_is_the_defaults_until_it_appears() {
|
||||
let d = dir("absent");
|
||||
let mut live = Live::watching(source(&d, false)).unwrap();
|
||||
fn books_take_their_own_settings_once_per_change() {
|
||||
let mut live = Live::own();
|
||||
assert_eq!(live.current().company.name, Company::default().name);
|
||||
live.refresh();
|
||||
assert_eq!(live.status().loads, 1, "nothing changed");
|
||||
std::fs::write(
|
||||
d.join("wanderbooks.toml"),
|
||||
"[company]\nname = \"Late Co\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
live.refresh();
|
||||
assert_eq!(live.current().company.name, "Late Co");
|
||||
assert_eq!(live.status().loads, 2);
|
||||
assert!(live.status().books_toml_exists);
|
||||
}
|
||||
|
||||
/// INVARIANT 3: a named file must exist to start; INVARIANT 2: the
|
||||
/// first load fails on broken settings, a later one keeps the last.
|
||||
#[test]
|
||||
fn a_named_file_must_load_to_start() {
|
||||
let d = dir("named");
|
||||
assert!(Live::watching(source(&d, true)).is_err());
|
||||
std::fs::write(d.join("wanderbooks.toml"), "[books\n").unwrap();
|
||||
assert!(Live::watching(source(&d, true)).is_err());
|
||||
std::fs::write(d.join("wanderbooks.toml"), "[company]\nname = \"One\"\n").unwrap();
|
||||
let mut live = Live::watching(source(&d, true)).unwrap();
|
||||
std::fs::write(d.join("wanderbooks.toml"), "[company]\nname = 5\n").unwrap();
|
||||
live.refresh();
|
||||
assert_eq!(live.current().company.name, "One");
|
||||
assert!(live.status().error.is_some());
|
||||
}
|
||||
|
||||
/// config.json sits in the data dir, which no tool result names: a
|
||||
/// broken one is reported by name, never by its path.
|
||||
#[test]
|
||||
fn a_broken_config_json_never_names_the_data_dir() {
|
||||
let d = dir("json-broken");
|
||||
std::fs::write(d.join("config.json"), "{ not json").unwrap();
|
||||
let err = Live::watching(source(&d, false)).unwrap_err();
|
||||
assert!(err.contains("config.json"), "{err}");
|
||||
assert!(!err.contains(d.to_str().unwrap()), "{err}");
|
||||
let status = Live::fixed(Settings::default()).status();
|
||||
assert!(!status.config_json_present);
|
||||
}
|
||||
|
||||
/// The receipt policy in config.json is watched too.
|
||||
#[test]
|
||||
fn the_config_json_policy_reloads() {
|
||||
let d = dir("json");
|
||||
std::fs::write(
|
||||
d.join("wanderbooks.toml"),
|
||||
"[books]\nreceipt_threshold = \"25.00\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
let mut live = Live::watching(source(&d, true)).unwrap();
|
||||
let before = live.current().policy.rule("USD");
|
||||
std::fs::write(
|
||||
d.join("config.json"),
|
||||
r#"{"receipt_policy": {"require_receipts_above": "0.00"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
live.refresh();
|
||||
assert_ne!(live.current().policy.rule("USD"), before);
|
||||
assert!(live.status().error.is_none(), "{:?}", live.status().error);
|
||||
}
|
||||
|
||||
/// Settings built in code never reload.
|
||||
#[test]
|
||||
fn fixed_settings_stay() {
|
||||
let mut live = Live::fixed(Settings::default());
|
||||
live.refresh();
|
||||
let s = live.status();
|
||||
assert_eq!((s.loads, s.books_toml), (1, None));
|
||||
}
|
||||
|
||||
/// Books that keep their own settings: no file is read, even a broken
|
||||
/// one; their settings come in once per change (INVARIANT 1).
|
||||
#[test]
|
||||
fn journaled_books_take_their_own_settings_once_per_change() {
|
||||
let d = dir("journaled");
|
||||
std::fs::write(d.join("wanderbooks.toml"), "[books\n").unwrap();
|
||||
let mut live = Live::journaled(source(&d, true));
|
||||
assert!(live.current().journaled);
|
||||
assert!(live.is_journaled());
|
||||
assert!(live.file_texts().is_none());
|
||||
let mut books = ledger_core::settings::Settings::default();
|
||||
let before = live.current();
|
||||
live.sync(&books, "USD");
|
||||
assert_eq!(live.status().loads, 0, "books without settings");
|
||||
assert!(
|
||||
Arc::ptr_eq(&before, &live.current()),
|
||||
"books without settings"
|
||||
);
|
||||
let src = ledger_core::settings::Source {
|
||||
decided_by: "Owner".into(),
|
||||
decided_on: chrono::NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(),
|
||||
@@ -533,54 +145,14 @@ mod tests {
|
||||
books.apply(&[change("Kept Co")], &src, 7).unwrap();
|
||||
live.sync(&books, "USD");
|
||||
assert_eq!(live.current().company.name, "Kept Co");
|
||||
assert_eq!(live.status().loads, 1);
|
||||
let taken = live.current();
|
||||
live.sync(&books, "USD");
|
||||
assert_eq!(live.status().loads, 1, "unchanged: not taken again");
|
||||
assert!(
|
||||
Arc::ptr_eq(&taken, &live.current()),
|
||||
"unchanged: not taken again"
|
||||
);
|
||||
books.apply(&[change("Kept Co 2")], &src, 9).unwrap();
|
||||
live.sync(&books, "USD");
|
||||
assert_eq!(live.current().company.name, "Kept Co 2");
|
||||
assert_eq!(live.status().loads, 2);
|
||||
assert!(live.current().journaled && live.is_journaled());
|
||||
}
|
||||
|
||||
/// Settings built in code are the books' own once synced.
|
||||
#[test]
|
||||
fn fixed_settings_synced_are_journaled() {
|
||||
let mut live = Live::fixed(Settings::default());
|
||||
assert!(!live.is_journaled());
|
||||
let mut books = ledger_core::settings::Settings::default();
|
||||
let src = ledger_core::settings::Source {
|
||||
decided_by: "Owner".into(),
|
||||
decided_on: chrono::NaiveDate::from_ymd_opt(2026, 10, 17).unwrap(),
|
||||
citation: None,
|
||||
note: None,
|
||||
};
|
||||
books
|
||||
.apply(
|
||||
&[ledger_core::settings::Change {
|
||||
key: "company.name".into(),
|
||||
value: Some(serde_json::json!("Kept Co")),
|
||||
}],
|
||||
&src,
|
||||
3,
|
||||
)
|
||||
.unwrap();
|
||||
live.sync(&books, "USD");
|
||||
assert!(live.is_journaled());
|
||||
}
|
||||
|
||||
/// Books in the data root keep their own settings from the start
|
||||
/// (INVARIANT 6): before they hold any, the settings in force say so
|
||||
/// too, so no message sends anyone to a settings file; none is read.
|
||||
#[test]
|
||||
fn books_in_the_data_root_keep_their_own_settings_before_they_hold_any() {
|
||||
let mut live = Live::own();
|
||||
assert!(live.current().journaled);
|
||||
assert!(live.is_journaled());
|
||||
assert!(live.file_texts().is_none());
|
||||
live.sync(&ledger_core::settings::Settings::default(), "USD");
|
||||
assert!(live.current().journaled);
|
||||
let s = live.status();
|
||||
assert!(s.journaled && s.books_toml.is_none(), "{s:?}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user