refactor!: one way to serve books: the stdio server is gone (D-44)
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:
swaitsandClaude Opus 5.5 committed 2026-10-23 19:45:18 -06:00
1 parent 0df7f52ac3
commit 7741e96eb3
87 files changed
+3477 -6476

No files matched your search

+13 -74
View File
@@ -16,7 +16,7 @@ use ledger_store::{Attachment, JournalLine, Reconciliation, Snapshot, Statement,
use rust_decimal::Decimal;
use serde_json::Value;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use crate::nav::Route;
@@ -597,18 +597,8 @@ impl Books {
let attachments = snap.attachments(None).map_err(|e| e.to_string())?;
let journal = snap.journal().to_vec();
// The server's own rule, so the viewer flags exactly what the
// server would refuse: the books' settings; for an older books dir
// that keeps none, its config.json, else its settings file.
let policy = if ledger.settings().is_journaled()
|| ledger_store::root::inside_a_root(snap.data_dir())
{
ledger_config::codec::receipt_policy(ledger.settings())
} else {
ledger_config::effective_receipt_policy(
&snap.data_dir().join("config.json"),
&settings,
)?
};
// server would refuse: the books' settings.
let policy = ledger_config::codec::receipt_policy(ledger.settings());
let receipt_threshold = policy.threshold();
let cash_accounts = cash_accounts(&ledger, &settings);
let members = settings.company.party_members(&ledger);
@@ -1658,15 +1648,6 @@ impl Books {
ledger_core::parties::activity_with(&self.ledger, party, span, &self.money, &self.members)
}
/// The books dir: the folder holding `books-data/` (and `vault/`).
fn books_dir(&self) -> PathBuf {
let d = self
.data_dir
.canonicalize()
.unwrap_or_else(|_| self.data_dir.clone());
d.parent().map(Path::to_path_buf).unwrap_or(d)
}
/// A party's full TIN from the encrypted vault, formatted
/// ("123-45-6789" for an SSN or ITIN, "12-3456789" for an EIN), for the
/// clipboard only: never drawn on screen.
@@ -1675,12 +1656,8 @@ impl Books {
/// INVARIANT 1: read only on demand (a click), never on load or paint.
/// INVARIANT 2: never creates a key file; a missing key is a locked
/// vault ("restore it from your password manager").
/// INVARIANT 3: the vault and key are the server's: for a book in the
/// data root, its own `vault/` and `keys/legacy.key`; for older
/// books, `vault/` beside `books-data/` and the key file the one
/// given on the command line (--vault-key-file), else the settings'
/// [vault] key_file, else ~/.config/wanderbooks/vault.key, one
/// inside the books dir refused, as the server refuses it.
/// INVARIANT 3: the vault and key are the server's: the book's own
/// `vault/` and `keys/legacy.key` (a book of the data root, D-44).
/// INVARIANT 4: an error never carries the digits (the vault's errors
/// are redacted); a party with no record is "not in the vault".
/// ```
@@ -1694,26 +1671,13 @@ impl Books {
}
/// Where the vault and its key are (INVARIANT 3 of [`Self::tin_from_vault`]).
fn vault_place(&self) -> Result<(PathBuf, PathBuf), String> {
if ledger_store::root::inside_a_root(&self.data_dir) {
return Ok((
self.data_dir.join(ledger_store::root::VAULT_DIR),
self.data_dir
.join(ledger_store::root::KEYS_DIR)
.join(ledger_store::root::LEGACY_KEY_FILE),
));
}
let key = crate::vault_key_file()
.or_else(|| self.settings.vault_key_file.clone())
.unwrap_or_else(ledger_vault::default_key_file);
let books = self.books_dir();
if resolved(&key).starts_with(resolved(&books)) {
return Err(format!(
"The vault key file {} is inside the books folder: move it out, and name its new place to the server and the viewer (--vault-key-file).",
key.display()
));
}
Ok((books.join("vault"), key))
fn vault_place(&self) -> (PathBuf, PathBuf) {
(
self.data_dir.join(ledger_store::root::VAULT_DIR),
self.data_dir
.join(ledger_store::root::KEYS_DIR)
.join(ledger_store::root::LEGACY_KEY_FILE),
)
}
/// A party's signed W-9 file from the vault, on demand (a click).
@@ -1727,7 +1691,7 @@ impl Books {
/// The vault, opened read-only, never making a key (a missing one is a
/// locked vault).
pub fn open_vault(&self) -> Result<ledger_vault::Vault, String> {
let (dir, key) = self.vault_place()?;
let (dir, key) = self.vault_place();
ledger_vault::Vault::open(&dir, &key, false).map_err(
|e| match e {
ledger_vault::Error::NoKey(k) => format!(
@@ -2287,28 +2251,3 @@ fn origin(events: &[usize], journal: &[JournalLine], closing: bool) -> &'static
fn cash_accounts(ledger: &Ledger, settings: &BooksSettings) -> Vec<AccountId> {
ledger_store::reconcilable::cash_accounts(ledger, &settings.cash_accounts)
}
/// `path` made absolute through its nearest existing ancestor (symlinks
/// resolved), so a path that doesn't exist yet can still be placed: the
/// server's rule for where the vault key may live.
fn resolved(path: &Path) -> PathBuf {
let abs = if path.is_absolute() {
path.to_path_buf()
} else {
std::env::current_dir().unwrap_or_default().join(path)
};
let mut base = abs.as_path();
let mut rest = Vec::new();
while !base.exists() {
match (base.file_name(), base.parent()) {
(Some(name), Some(up)) => {
rest.push(name.to_os_string());
base = up;
}
_ => return abs,
}
}
let mut out = base.canonicalize().unwrap_or_else(|_| base.to_path_buf());
out.extend(rest.into_iter().rev());
out
}
+15 -83
View File
@@ -16,11 +16,9 @@ use std::sync::Arc;
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, channel};
use std::time::Duration;
/// Where the books come from: their folder (a book of the data root, or
/// an older books dir's `books-data`). Their settings are their own, in
/// their journal (plan 3.6); only an older books dir whose journal keeps
/// none is read as its server reads it, from its settings file (see
/// `settings`).
/// Where the books come from: their folder, a book of the data root
/// (`wanderbooks gui ID`; D-44). Their settings are their own, in their
/// journal (plan 3.6), read as the server reads them.
#[derive(Clone, Debug)]
pub struct Source {
/// The books' folder (journal and blobs).
@@ -29,34 +27,6 @@ pub struct Source {
pub today: Option<NaiveDate>,
}
/// The settings file an older books dir's server reads when the books
/// keep no settings of their own: `wanderbooks.toml` beside `books-data`,
/// or the one named with `--books-toml`. Never for a book of the data
/// root.
pub fn legacy_toml(data_dir: &std::path::Path) -> Option<PathBuf> {
legacy_toml_path(data_dir).filter(|p| p.is_file())
}
/// Where an older books dir's settings file is, whether or not it is
/// there yet (a file made later is followed, as the server follows it).
fn legacy_toml_path(data_dir: &std::path::Path) -> Option<PathBuf> {
if ledger_store::root::inside_a_root(data_dir) {
return None;
}
if let Some(named) = LEGACY_TOML.get() {
return Some(named.clone());
}
Some(data_dir.parent()?.join("wanderbooks.toml"))
}
/// `--books-toml`, for an older books dir (once, at start).
static LEGACY_TOML: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
/// Name an older books dir's settings file (`--books-toml`).
pub fn set_legacy_toml(path: PathBuf) {
let _ = LEGACY_TOML.set(path);
}
pub enum Update {
Books(Arc<Books>),
Error(String),
@@ -109,7 +79,7 @@ pub fn load_once(source: &Source) -> Result<Books, String> {
})
})?;
let integrity = Integrity::check(&snap, &mut HashSet::new());
let settings = settings(source, &snap)?;
let settings = settings(&snap);
let today = today(source, &settings);
Books::build(&snap, settings, today, None, integrity)
}
@@ -139,26 +109,13 @@ fn today(source: &Source, settings: &BooksSettings) -> NaiveDate {
.unwrap_or_else(|| ledger_core::settings::today_in(settings.timezone.as_deref()))
}
/// The settings, as the server reads them: the books' own, from their
/// journal; for an older books dir that keeps none, its settings file
/// (else the defaults). A book of the data root reads no file.
fn settings(source: &Source, snap: &Snapshot) -> Result<BooksSettings, String> {
settings_of(&source.data_dir, snap)
}
/// `settings` for the books in `data_dir`, as `snap` holds them.
pub fn settings_of(data_dir: &std::path::Path, snap: &Snapshot) -> Result<BooksSettings, String> {
let books = snap.ledger().settings();
if !books.is_journaled()
&& let Some(toml) = legacy_toml(data_dir)
&& toml.is_file()
{
return ledger_config::load(&toml);
}
Ok(ledger_config::codec::books_settings(
books,
/// The settings: the books' own, from their journal, as the server reads
/// them (the defaults until the first is set).
pub fn settings(snap: &Snapshot) -> BooksSettings {
ledger_config::codec::books_settings(
snap.ledger().settings(),
snap.ledger().base_currency().code(),
))
)
}
fn run(
@@ -191,30 +148,14 @@ fn run(
let mut build = |snap: &Snapshot, prev: Option<u64>| -> (Update, ledger_store::DiskCheck) {
let integrity = Integrity::check(snap, &mut verified);
let disk = integrity.disk.clone();
let update = match settings(&source, snap).and_then(|s| {
let today = today(&source, &s);
Books::build(snap, s, today, prev, integrity)
}) {
let s = settings(snap);
let today = today(&source, &s);
let update = match Books::build(snap, s, today, prev, integrity) {
Ok(b) => Update::Books(Arc::new(b)),
Err(e) => Update::Error(e),
};
(update, disk)
};
// An older books dir's settings file, followed as its server follows
// it (only read while the books keep no settings of their own).
// And its config.json, whose receipt rule the server reads too.
let toml = legacy_toml_path(&source.data_dir);
let config = (!ledger_store::root::inside_a_root(&source.data_dir))
.then(|| source.data_dir.join("config.json"));
let mtime = |t: &Option<PathBuf>| {
t.as_ref()
.and_then(|p| std::fs::metadata(p).ok())
.and_then(|m| m.modified().ok())
};
let toml_mtime = |t: &Option<PathBuf>| (mtime(t), mtime(&config));
// Taken before the first build, so a file written while it is being
// built is a change, never part of the starting state.
let mut toml_seen = toml_mtime(&toml);
let (first, disk) = build(&snap, None);
let mut last_disk = Some(disk);
send(first);
@@ -267,15 +208,8 @@ fn run(
}
};
look_at_ops(&mut watcher);
let day_now = |snap: &Snapshot| {
settings(&source, snap)
.map(|s| today(&source, &s))
.unwrap_or_else(|_| today(&source, &BooksSettings::default()))
};
let day_now = |snap: &Snapshot| today(&source, &settings(snap));
let mut day = day_now(&snap);
if let (Some(w), Some(dir)) = (watcher.as_mut(), toml.as_ref().and_then(|t| t.parent())) {
let _ = w.watch(dir, RecursiveMode::NonRecursive);
}
loop {
if gone() {
return;
@@ -302,7 +236,6 @@ fn run(
}
};
look_at_ops(&mut watcher);
let toml_now = toml_mtime(&toml);
let new_day = day_now(&snap);
// The writer journals first and updates its database right after:
// a "behind" database usually catches up a moment later, so look
@@ -311,9 +244,8 @@ fn run(
&& last_disk
.as_ref()
.is_some_and(|d| *d != ledger_store::DiskCheck::Matches && snap.check_disk() != *d);
if grew || disk_changed || new_day != day || toml_now != toml_seen {
if grew || disk_changed || new_day != day {
day = new_day;
toml_seen = toml_now;
let (update, disk) = build(&snap, grew.then_some(before));
last_disk = Some(disk);
send(update);
+7 -41
View File
@@ -29,21 +29,6 @@ pub use data::Source;
use std::path::PathBuf;
/// The vault key file named on the command line (`--vault-key-file`),
/// which wins over a settings file's `[vault] key_file`, as it does for
/// the server: books that keep their own settings never name it.
static VAULT_KEY_FILE: std::sync::OnceLock<std::path::PathBuf> = std::sync::OnceLock::new();
/// Name the vault key file (once, at start).
pub fn set_vault_key_file(path: std::path::PathBuf) {
let _ = VAULT_KEY_FILE.set(path);
}
/// The vault key file named on the command line, if one was.
pub fn vault_key_file() -> Option<std::path::PathBuf> {
VAULT_KEY_FILE.get().cloned()
}
/// How [`run`] stops when it can't go on: the reason is already printed
/// on stderr, and the number is the process's exit code (2: a bad argument; 1: the demo books couldn't be built). A
/// library never ends the process itself, so the books it opened are
@@ -64,14 +49,12 @@ impl std::error::Error for Exit {}
/// `wanderbooks gui`). It opens the books read-only and returns when the
/// window closes. An unknown argument returns [`Exit`] (code 2).
///
/// `ledger-gui [--root PATH] [--book ID] [--data-dir PATH [--books-toml PATH] [--vault-key-file PATH]] [--today YYYY-MM-DD] [--demo]`:
/// a book of the data root (plan 3.6): `--root`, else the root `wanderbooks
/// setup` made, never the current directory or a settings file; `--book`
/// names one, else the root's only one (or the first, by id).
/// `--data-dir` opens an older books dir's `books-data` instead (its
/// settings file `--books-toml`, read while its journal keeps none, else
/// the `wanderbooks.toml` beside it; its vault key `--vault-key-file`);
/// `--demo` builds a demo company in a temp directory and opens that.
/// `ledger-gui [--root PATH] [--book ID] [--today YYYY-MM-DD] [--demo]`:
/// a book of the data root (plan 3.6), the only books it opens (D-44):
/// `--root`, else the root `wanderbooks setup` made, never the current
/// directory or a settings file; `--book` names one, else the root's only
/// one (or the first, by id). `--demo` builds a demo company in a temp
/// directory and opens that.
pub fn run(
prog: &str,
args: impl IntoIterator<Item = String>,
@@ -82,28 +65,11 @@ pub fn run(
let mut today = None;
let mut demo = false;
let mut args = args.into_iter();
let usage = || {
format!(
"usage: {prog} [--root PATH] [--book ID] [--data-dir PATH [--books-toml PATH] [--vault-key-file PATH]] [--today YYYY-MM-DD] [--demo]"
)
};
let usage = || format!("usage: {prog} [--root PATH] [--book ID] [--today YYYY-MM-DD] [--demo]");
while let Some(a) = args.next() {
match a.as_str() {
"--root" => root = args.next().map(PathBuf::from),
"--book" => book = args.next(),
"--data-dir" => data_dir = args.next().map(PathBuf::from),
// An older books dir's settings file, read only while its
// books keep no settings of their own (data::legacy_toml).
"--books-toml" => {
if let Some(t) = args.next() {
crate::data::set_legacy_toml(PathBuf::from(t));
}
}
"--vault-key-file" => {
if let Some(k) = args.next() {
crate::set_vault_key_file(PathBuf::from(k));
}
}
"--today" => today = args.next().and_then(|d| d.parse().ok()),
"--demo" => demo = true,
"-h" | "--help" => {
+2 -3
View File
@@ -151,9 +151,8 @@ impl Job {
let snap = ledger_store::Snapshot::load(&books.data_dir).map_err(|e| e.to_string())?;
let ledger = snap.ledger();
// The settings the rest of the viewer shows, read as the server
// reads them (data::settings: an older books dir that keeps none
// in its journal reads its settings file).
let st = &crate::data::settings_of(&books.data_dir, &snap)?;
// reads them (data::settings).
let st = &crate::data::settings(&snap);
let settings = ledger_package::Settings {
company: &st.company,
cash_accounts: &st.cash_accounts,
+3 -3
View File
@@ -21,8 +21,8 @@
//! 2. Each runs off the UI thread; what came of it is shown, in words.
//! 3. A restore is shown before it is done (the backup proven, what it
//! would do), and done only for that backup, at that head.
//! 4. Without an owner (the viewer on its own, older books dirs, the
//! demo) the menu isn't there.
//! 4. Without an owner (the viewer on its own, books outside a data
//! root: the demo) the menu isn't there.
//! 5. Books waiting for a format upgrade show a standing notice above
//! every page, and the go-ahead is asked once each time the viewer
//! opens them: the owner passphrase, typed here and handed to the
@@ -109,7 +109,7 @@ fn owner() -> Option<Arc<dyn Owner>> {
}
/// The id of books of the data root (their folder's name), or None for
/// an older books dir (INVARIANT 4).
/// books outside one, the demo's (INVARIANT 4).
pub fn book_id(books: &Books) -> Option<String> {
ledger_store::root::inside_a_root(&books.data_dir)
.then(|| books.data_dir.file_name()?.to_str().map(str::to_string))
+6 -15
View File
@@ -169,19 +169,12 @@ pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|_| {},
|ui| {
let kept = b.ledger.settings();
// Where the settings shown come from: a settings
// file only for an older books dir that keeps none
// in its journal and has one (data::settings).
let from_file =
!kept.is_journaled() && crate::data::legacy_toml(&b.data_dir).is_some();
// The currency is always known: facts are the rest.
if c.profile.iter().all(|(k, _)| k == "base_currency") {
ui.label(
RichText::new(if from_file {
"No company facts in the settings file yet: the agent records them as you give them."
} else {
"No company facts set yet: the agent records them as you give them."
})
RichText::new(
"No company facts set yet: the agent records them as you give them.",
)
.color(p.muted),
);
}
@@ -204,11 +197,9 @@ pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
kv(ui, "Time zone", z);
}
ui.label(
RichText::new(if from_file {
"Read from the settings file: the agent can move it into the books."
} else {
"Kept in the books: the agent changes settings, with who decided and when."
})
RichText::new(
"Kept in the books: the agent changes settings, with who decided and when.",
)
.font(theme::regular(12.0))
.color(p.muted),
);
+41
View File
@@ -0,0 +1,41 @@
//! The viewer's command line: an unknown argument stops the viewer with
//! status 2, and it opens only books of the data root (D-44): no flag
//! names a loose books folder, its settings file or a vault key.
use std::process::Command;
/// The viewer, run in a home of the test's own with an empty environment
/// but PATH: if an argument it should refuse were ever taken, it would
/// find no books, no display and no preferences of this account's.
fn viewer(home: &std::path::Path) -> Command {
let mut c = Command::new(env!("CARGO_BIN_EXE_ledger-gui"));
c.env_clear().env("PATH", "/usr/bin:/bin").env("HOME", home);
c
}
#[test]
fn an_unknown_argument_stops_the_viewer() {
let home = tempfile::Builder::new()
.prefix("wb-gui-args-")
.tempdir()
.unwrap();
let out = viewer(home.path()).arg("--no-such-flag").output().unwrap();
assert_eq!(out.status.code(), Some(2));
assert!(
String::from_utf8_lossy(&out.stderr).contains("unknown argument --no-such-flag"),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let help = viewer(home.path()).arg("--help").output().unwrap();
assert!(help.status.success());
let help = String::from_utf8_lossy(&help.stdout);
assert!(help.contains("--book ID"), "{help}");
for gone in ["--data-dir", "--books-toml", "--vault-key-file"] {
assert!(!help.contains(gone), "{help}");
}
// A gone flag is an unknown argument like any other.
for gone in ["--data-dir", "--books-toml", "--vault-key-file"] {
let out = viewer(home.path()).args([gone, "x"]).output().unwrap();
assert_eq!(out.status.code(), Some(2), "{gone}");
}
}
+26 -9
View File
@@ -219,9 +219,6 @@ fn a_book_in_the_root_reads_its_own_vault() {
);
let e = b.w9_file(f.member).unwrap_err();
assert!(e.contains("no W-9 file"), "{e}");
// Its key is the book's own: one named on the command line is never
// used for it (none is named here, and none is needed).
assert!(ledger_gui::vault_key_file().is_none());
}
#[test]
@@ -465,12 +462,13 @@ fn the_menus_1099_worksheet_carries_a_payees_full_tin() {
}
/// The menu reads settings as the rest of the viewer and the server do:
/// an older books dir that keeps none in its journal, from its file.
/// the books' own, from their journal; a settings file beside them is
/// never read (D-44).
#[test]
fn the_menu_reads_an_older_books_dirs_settings_file() {
fn the_menu_reads_the_books_own_settings() {
downloads();
let dir = tempfile::Builder::new()
.prefix("wb-gui-older-")
.prefix("wb-gui-kept-")
.tempdir()
.unwrap();
let data = dir.path().join("books-data");
@@ -482,14 +480,33 @@ fn the_menu_reads_an_older_books_dirs_settings_file() {
] {
s.add_account(code, name, ty, None, false).unwrap();
}
s.change_settings(
&[
ledger_core::settings::Change {
key: "company.name".into(),
value: Some(serde_json::json!("Journal Kept Co")),
},
ledger_core::settings::Change {
key: "books.cash_accounts".into(),
value: Some(serde_json::json!(["1000"])),
},
],
&ledger_core::settings::Source {
decided_by: "Tobin Harlow".into(),
decided_on: today(),
citation: None,
note: None,
},
)
.unwrap();
}
std::fs::write(
dir.path().join("wanderbooks.toml"),
"[company]\nname = \"File Kept Co\"\n[books]\ncash_accounts = [\"1000\"]\n",
"[company]\nname = \"File Beside Co\"\n",
)
.unwrap();
let b = load(&data);
assert_eq!(b.settings.company.name, "File Kept Co");
assert_eq!(b.settings.company.name, "Journal Kept Co");
let (path, _) = Job::Package.run(&b).unwrap();
let mut zip = zip::ZipArchive::new(std::fs::File::open(&path).unwrap()).unwrap();
let mut readme = String::new();
@@ -497,7 +514,7 @@ fn the_menu_reads_an_older_books_dirs_settings_file() {
.unwrap()
.read_to_string(&mut readme)
.unwrap();
assert!(readme.starts_with("File Kept Co"), "{readme}");
assert!(readme.starts_with("Journal Kept Co"), "{readme}");
}
/// The worksheet says when a reportable payee's TIN isn't in it (the
+6 -6
View File
@@ -3,8 +3,8 @@
//! and the go-ahead is asked as the viewer opens them, with the owner
//! passphrase, handed to the owner the program gives the viewer (the
//! server checks it). "Not now" changes nothing; a refusal is said in the
//! dialog, which stays; books this version made, and older books dirs,
//! are never asked; the viewer writes nothing of the books.
//! dialog, which stays; books this version made, and books outside a
//! data root (the demo's), are never asked; the viewer writes nothing of the books.
use chrono::NaiveDate;
use egui_kittest::Harness;
@@ -95,7 +95,7 @@ fn asked(f: &Fake) -> Vec<String> {
f.asked.lock().unwrap().clone()
}
/// Books `acme`: in a data root (`root`) or an older books dir, in journal
/// Books `acme`: in a data root (`root`) or outside one, in journal
/// format 1 (as an older version wrote them) or this version's (`old`).
fn books(root: bool, old: bool) -> (tempfile::TempDir, PathBuf, Books) {
let dir = tempfile::Builder::new()
@@ -320,11 +320,11 @@ fn an_upgrade_that_ends_without_an_answer_never_holds_the_window() {
assert!(h.query_by_label(ASKS).is_none(), "and closes");
}
/// Books this version made wait for nothing, and older books dirs (no
/// owner's server for them) are never asked: no notice, no dialog, no
/// Books this version made wait for nothing, and books outside a data
/// root (no owner's server for them) are never asked: no notice, no dialog, no
/// menu item.
#[test]
fn books_in_this_versions_format_and_older_books_dirs_are_never_asked() {
fn books_in_this_versions_format_and_books_outside_a_root_are_never_asked() {
let _alone = alone();
let _ = fake();
for (root, old) in [(true, false), (false, true), (false, false)] {
+30 -17
View File
@@ -1,4 +1,4 @@
//! Live updates: a change written to the books (by the MCP server, in
//! Live updates: a change written to the books (by the books server, in
//! practice) reaches the GUI's view model on its own, without a restart,
//! and the changed entry is marked fresh; a show request reaches it, and
//! while it runs it holds the books' viewer presence.
@@ -121,16 +121,21 @@ fn a_change_in_another_process_shows_up_live() {
);
}
/// An older books dir that keeps no settings in its journal and has no
/// settings file: one written while the viewer runs is followed, as its
/// server follows it.
/// A setting the server journals while the viewer runs is followed (the
/// books' own settings, the only ones the viewer reads: D-44); a settings
/// file beside the books is never read.
#[test]
fn a_settings_file_made_later_is_followed() {
let dir = std::env::temp_dir().join(format!("ledger-gui-live-toml-{}", std::process::id()));
fn a_setting_changed_while_it_runs_is_followed() {
let dir = std::env::temp_dir().join(format!("ledger-gui-live-set-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let _cleanup = TestDir(dir.clone());
let data = dir.join("books-data");
drop(Store::create(&data, Currency::USD).unwrap());
std::fs::write(
dir.join("wanderbooks.toml"),
"[company]\nname = \"Beside Co\"\n",
)
.unwrap();
let loader = Loader::spawn(
Source {
data_dir: data.clone(),
@@ -139,23 +144,31 @@ fn a_settings_file_made_later_is_followed() {
egui::Context::default(),
);
match loader.wait(Duration::from_secs(10)) {
Some(Update::Books(b)) => assert_ne!(b.settings.company.name, "Made Later Co"),
Some(Update::Books(b)) => assert_ne!(b.settings.company.name, "Beside Co"),
_ => panic!("no initial load"),
}
std::fs::write(
dir.join("wanderbooks.toml"),
"[company]\nname = \"Made Later Co\"\n",
)
.unwrap();
let mut store = Store::open(&data).unwrap();
store
.change_settings(
&[ledger_core::settings::Change {
key: "company.name".into(),
value: Some(serde_json::json!("Made Later Co")),
}],
&ledger_core::settings::Source {
decided_by: "Tobin Harlow".into(),
decided_on: NaiveDate::from_ymd_opt(2026, 10, 12).unwrap(),
citation: None,
note: None,
},
)
.unwrap();
drop(store);
let deadline = Instant::now() + Duration::from_secs(10);
loop {
assert!(
Instant::now() < deadline,
"the new settings file was never read"
);
assert!(Instant::now() < deadline, "the new setting was never read");
match loader.wait(Duration::from_millis(200)) {
Some(Update::Books(b)) if b.settings.company.name == "Made Later Co" => break,
Some(Update::Error(e)) => panic!("the settings file didn't load: {e}"),
Some(Update::Error(e)) => panic!("the books didn't load: {e}"),
_ => {}
}
}
+22 -52
View File
@@ -480,62 +480,32 @@ fn books_that_keep_their_settings_show_them() {
}
}
/// Books that keep no settings in their journal are read as their server
/// reads them (plan 3.6): an older books dir from its settings file; a
/// book of the data root never from a file, so with the defaults.
/// Books that keep no settings in their journal show the defaults, as
/// their server reads them (plan 3.6, D-44): no settings file or
/// `config.json` beside them is read, in a data root or not.
#[test]
fn books_that_keep_no_settings_read_their_file_only_outside_a_root() {
fn books_that_keep_no_settings_read_no_file_beside_them() {
let from = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../ledger-store/tests/fixtures/old-books")
.join("shares_big");
// An older books dir: its server reads wanderbooks.toml, so the viewer
// does too.
let t = temp("said-file");
copy_books(&from, &t.0.join("books-data"));
std::fs::write(t.0.join("wanderbooks.toml"), SETTINGS).unwrap();
let b = load(&t);
assert_eq!(b.settings.company.name, "Old Books Co");
assert!(!b.settings.company.members.is_empty());
// The same books in a data root: no file is read, whatever lies
// beside them; they show the defaults until their settings are in
// their journal.
let t = temp("said-defaults");
std::fs::write(t.0.join(ledger_store::root::MARKER_FILE), b"").unwrap();
copy_books(&from, &t.0.join("books-data"));
std::fs::write(t.0.join("wanderbooks.toml"), SETTINGS).unwrap();
let b = load(&t);
assert_eq!(
b.settings,
ledger_config::codec::books_settings(&ledger_core::settings::Settings::default(), "USD")
);
}
/// An older books dir with no settings file but a `config.json`: its
/// receipt rule is the server's, from that file (the regression round 2
/// of slice 6's review found).
#[test]
fn an_older_books_dirs_config_json_sets_its_receipt_rule() {
let from = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../ledger-store/tests/fixtures/old-books")
.join("shares_big");
let t = temp("said-config");
copy_books(&from, &t.0.join("books-data"));
std::fs::write(
t.0.join("books-data/config.json"),
r#"{"receipt_policy": {"require_receipts_above": "0.00"}}"#,
)
.unwrap();
assert_eq!(load(&t).receipt_threshold, rust_decimal::Decimal::ZERO);
// In a data root, no file: the books' own rule (the default).
let t = temp("said-config-root");
std::fs::write(t.0.join(ledger_store::root::MARKER_FILE), b"").unwrap();
copy_books(&from, &t.0.join("books-data"));
std::fs::write(
t.0.join("books-data/config.json"),
r#"{"receipt_policy": {"require_receipts_above": "0.00"}}"#,
)
.unwrap();
assert_ne!(load(&t).receipt_threshold, rust_decimal::Decimal::ZERO);
let defaults =
ledger_config::codec::books_settings(&ledger_core::settings::Settings::default(), "USD");
for (tag, in_root) in [("said-file", false), ("said-root", true)] {
let t = temp(tag);
if in_root {
std::fs::write(t.0.join(ledger_store::root::MARKER_FILE), b"").unwrap();
}
copy_books(&from, &t.0.join("books-data"));
std::fs::write(t.0.join("wanderbooks.toml"), SETTINGS).unwrap();
std::fs::write(
t.0.join("books-data/config.json"),
r#"{"receipt_policy": {"require_receipts_above": "0.00"}}"#,
)
.unwrap();
let b = load(&t);
assert_eq!(b.settings, defaults, "{tag}");
assert_ne!(b.receipt_threshold, rust_decimal::Decimal::ZERO, "{tag}");
}
}
/// rules.md B-37: the classes an earlier version wrote read as projects
+3 -3
View File
@@ -1,8 +1,8 @@
//! The viewer's Books menu (owner.rs, plan P2 "Viewer menu", D-32): each
//! item hands its work to the owner the program gives the viewer, off the
//! UI thread, and says what came of it; a restore is previewed first; the
//! viewer itself writes nothing of the books; without an owner, or for an
//! older books dir, the menu isn't there.
//! viewer itself writes nothing of the books; without an owner, or for
//! books outside a data root (the demo's), the menu isn't there.
use chrono::NaiveDate;
use egui_kittest::Harness;
@@ -102,7 +102,7 @@ fn today() -> NaiveDate {
}
/// Books in a data root (`root`: a folder holding the root's marker), or
/// an older books dir's.
/// outside one (as the demo's are).
fn books(root: bool) -> (tempfile::TempDir, PathBuf, Books) {
let dir = tempfile::Builder::new()
.prefix("wb-gui-owner-")
+20 -3
View File
@@ -1,7 +1,8 @@
//! P3's seams in the viewer: on the seam review's books (a year of a
//! two-member LLC, `ledger-package`'s `tests/seams/books.rs`), the
//! viewer's figures, read through its own [`Books`] from the books' folder
//! and settings file as it opens them, are the engine's (the server's
//! as it opens them (their settings journaled, as the books keep them:
//! D-44), are the engine's (the server's
//! tools make theirs with the same calls and settings): each member's
//! capital statement, party money, the cash flow statement, the fixed
//! asset register and the 1099 worksheet, on several dates.
@@ -9,7 +10,7 @@
#[path = "../../ledger-package/tests/seams/books.rs"]
mod books;
use books::{books, d};
use books::{SETTINGS_TOML, books, d};
use chrono::NaiveDate;
use ledger_gui::books::Books as Viewer;
use ledger_gui::{Source, data};
@@ -27,7 +28,23 @@ fn viewer(dir: &std::path::Path, today: NaiveDate) -> Viewer {
#[test]
fn the_viewers_figures_are_the_engines_on_every_date() {
let dir = tempfile::tempdir().unwrap();
let b = books(dir.path());
let mut b = books(dir.path());
// The settings in the books, where the viewer reads them (it reads no
// file beside them).
let import =
ledger_config::codec::import(SETTINGS_TOML, None, b.store.ledger().settings(), "USD")
.unwrap();
b.store
.change_settings(
&import.changes,
&ledger_core::settings::Source {
decided_by: "the test".into(),
decided_on: d(START),
citation: None,
note: None,
},
)
.unwrap();
let l = b.store.ledger();
let cash: Vec<_> = b
.cash
+21 -31
View File
@@ -21,7 +21,7 @@ use ledger_gui::nav::Route;
use ledger_gui::{App, Source, data};
use ledger_store::Store;
use ledger_vault::{Tin, Vault};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
const SSN: &str = "123-45-6789";
@@ -38,17 +38,19 @@ impl Drop for Fixture {
}
}
/// Books with one member whose SSN is in the vault; the key lives outside
/// the books dir, as the server requires.
/// Books with one member whose SSN is in the vault, the vault and its key
/// where a book of the data root keeps them (its own `vault/` and
/// `keys/`).
fn fixture(tag: &str) -> Fixture {
let root = std::env::temp_dir().join(format!("ledger-gui-tin-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let books = root.join("books");
let key = root.join("keys/vault.key");
std::fs::create_dir_all(&books).unwrap();
let mut s = Store::create(&books.join("books-data"), ledger_core::Currency::USD).unwrap();
let key = books
.join(ledger_store::root::KEYS_DIR)
.join(ledger_store::root::LEGACY_KEY_FILE);
let mut s = Store::create(&books, ledger_core::Currency::USD).unwrap();
let party = s.add_party("Pat Lee", &[Role::Member]).unwrap();
let v = Vault::open(&books.join("vault"), &key, true).unwrap();
let v = Vault::open(&books.join(ledger_store::root::VAULT_DIR), &key, true).unwrap();
let hash = v
.put_tin(party, &Tin::new(SSN, ledger_vault::TinType::Ssn).unwrap())
.unwrap();
@@ -70,14 +72,12 @@ fn fixture(tag: &str) -> Fixture {
}
}
fn load(f: &Fixture, key: Option<&Path>) -> Books {
let mut b = data::load_once(&Source {
data_dir: f.books.join("books-data"),
fn load(f: &Fixture) -> Books {
data::load_once(&Source {
data_dir: f.books.clone(),
today: Some(chrono::NaiveDate::from_ymd_opt(2026, 10, 16).unwrap()),
})
.unwrap();
b.settings.vault_key_file = key.map(Path::to_path_buf);
b
.unwrap()
}
fn copied(h: &Harness<'_, App>) -> Vec<String> {
@@ -102,7 +102,7 @@ fn no_digits(s: &str) {
#[test]
fn reads_the_vault_with_the_key_in_force() {
let f = fixture("read");
let b = load(&f, Some(&f.key));
let b = load(&f);
assert_eq!(b.tin_from_vault(f.party).unwrap(), SSN);
let e = b.tin_from_vault(f.party + 1).unwrap_err();
assert!(e.contains("Not in the vault"), "{e}");
@@ -112,22 +112,12 @@ fn reads_the_vault_with_the_key_in_force() {
#[test]
fn a_missing_key_is_locked_and_never_created() {
let f = fixture("locked");
let gone = f.root.join("elsewhere/vault.key");
let b = load(&f, Some(&gone));
std::fs::remove_file(&f.key).unwrap();
let b = load(&f);
let e = b.tin_from_vault(f.party).unwrap_err();
assert!(e.contains("locked"), "{e}");
no_digits(&e);
assert!(!gone.exists(), "reading must never create a key");
}
/// INVARIANT 3: a key file inside the books dir is refused.
#[test]
fn a_key_inside_the_books_is_refused() {
let f = fixture("inside");
let inside = f.books.join("vault.key");
std::fs::copy(&f.key, &inside).unwrap();
let e = load(&f, Some(&inside)).tin_from_vault(f.party).unwrap_err();
assert!(e.contains("inside the books folder"), "{e}");
assert!(!f.key.exists(), "reading must never create a key");
}
/// INVARIANT 4: a tampered record is unreadable, and says nothing of it.
@@ -139,7 +129,7 @@ fn a_bad_record_is_an_error_without_digits() {
let last = raw.len() - 1;
raw[last] ^= 1;
std::fs::write(&rec, raw).unwrap();
let e = load(&f, Some(&f.key)).tin_from_vault(f.party).unwrap_err();
let e = load(&f).tin_from_vault(f.party).unwrap_err();
no_digits(&e);
}
@@ -156,7 +146,7 @@ fn party_page(b: Books, party: PartyId) -> Harness<'static, App> {
fn clicking_the_masked_tin_copies_the_full_number() {
let f = fixture("click");
let shown = "TIN on file ••••6789 (SSN)";
let mut h = party_page(load(&f, Some(&f.key)), f.party);
let mut h = party_page(load(&f), f.party);
assert!(
h.query_by_label_contains("12345").is_none(),
"the full number is never drawn"
@@ -168,8 +158,8 @@ fn clicking_the_masked_tin_copies_the_full_number() {
assert!(h.query_by_label_contains("12345").is_none());
// Locked: nothing copied; the reason shows beside it.
let gone = f.root.join("elsewhere/vault.key");
let mut h = party_page(load(&f, Some(&gone)), f.party);
std::fs::remove_file(&f.key).unwrap();
let mut h = party_page(load(&f), f.party);
h.get_by_label(shown).click();
h.step();
assert!(copied(&h).is_empty());
-84
View File
@@ -1,84 +0,0 @@
//! The viewer's command line: `--vault-key-file` names the vault key and
//! wins over a settings file's `[vault] key_file`, as it does for the
//! server; an unknown argument stops the viewer with status 2. (Its own
//! test binary: the key file is named once per process.)
use ledger_core::{Role, TinFacts, TinType};
use ledger_gui::{Source, data};
use ledger_store::Store;
use ledger_vault::{Tin, Vault};
const SSN: &str = "123-45-6789";
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(std::path::PathBuf);
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn the_key_named_on_the_command_line_wins() {
let root = std::env::temp_dir().join(format!("ledger-gui-key-flag-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let _cleanup = TestDir(root.clone());
let books = root.join("books");
let key = root.join("keys/vault.key");
std::fs::create_dir_all(&books).unwrap();
let mut s = Store::create(&books.join("books-data"), ledger_core::Currency::USD).unwrap();
let party = s.add_party("Pat Lee", &[Role::Member]).unwrap();
let v = Vault::open(&books.join("vault"), &key, true).unwrap();
let hash = v
.put_tin(party, &Tin::new(SSN, ledger_vault::TinType::Ssn).unwrap())
.unwrap();
s.set_party_tin(
party,
Some(TinFacts {
tin_type: TinType::Ssn,
last4: "6789".into(),
vault_record: Some(hash),
purged: None,
}),
)
.unwrap();
drop(s);
// Named first, so the default key location is never consulted.
assert_eq!(ledger_gui::vault_key_file(), None);
ledger_gui::set_vault_key_file(key.clone());
assert_eq!(ledger_gui::vault_key_file(), Some(key.clone()));
// Named once: a second naming changes nothing.
ledger_gui::set_vault_key_file(root.join("elsewhere.key"));
assert_eq!(ledger_gui::vault_key_file(), Some(key.clone()));
let mut b = data::load_once(&Source {
data_dir: books.join("books-data"),
today: Some(chrono::NaiveDate::from_ymd_opt(2026, 10, 17).unwrap()),
})
.unwrap();
// The settings name a key that isn't there; the command line wins.
b.settings.vault_key_file = Some(root.join("missing/vault.key"));
assert_eq!(b.tin_from_vault(party).unwrap(), SSN);
}
#[test]
fn an_unknown_argument_stops_the_viewer() {
let out = std::process::Command::new(env!("CARGO_BIN_EXE_ledger-gui"))
.arg("--no-such-flag")
.output()
.unwrap();
assert_eq!(out.status.code(), Some(2));
assert!(
String::from_utf8_lossy(&out.stderr).contains("unknown argument --no-such-flag"),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let help = std::process::Command::new(env!("CARGO_BIN_EXE_ledger-gui"))
.arg("--help")
.output()
.unwrap();
assert!(help.status.success());
assert!(String::from_utf8_lossy(&help.stdout).contains("--vault-key-file PATH"));
}