Named libraries: switch between several, each syncing on its own

The app can keep any number of named libraries and switch between them
from the top bar or Info → Libraries. Each can sync to the account's other
devices or stay on this one (a scratch library to try things in).

The sync server now keeps several libraries per user under
/api/libraries; a migration makes each existing library the user's default
"Main", which /api/library keeps serving for older app builds. Every sync
lines the device's libraries up with the account's: new ones elsewhere are
downloaded, new ones here created on the server, ones deleted elsewhere
flagged rather than thrown away. The existing library keeps its files and
merge base, so upgrading asks nothing.
This commit is contained in:
2026-10-16 21:44:55 -05:00
parent 6072d44ebe
commit 81b1df8c9f
21 changed files with 2451 additions and 202 deletions

View File

@@ -4,7 +4,7 @@
use std::path::{Path, PathBuf};
use mtgc_core::sync::account::{redeem_code, server_info, start_url, Pkce};
use mtgc_core::sync::remote::{sync_cycle, HttpRemote, SyncBase, UNAUTHORIZED};
use mtgc_core::sync::remote::{sync_cycle, HttpRemote, SyncBase, NOT_FOUND, UNAUTHORIZED};
use mtgc_core::sync::{apply_doc, merge_into, printing_key, read_doc, CardEntry};
use mtgc_server::auth::local::hash_password;
use mtgc_server::{db, router, AppState, Config};
@@ -64,6 +64,16 @@ impl Device {
}
}
/// A second working copy on the same device, for one of the account's
/// named libraries.
fn for_library(dir: &Path, name: &str, server: &str, token: &str, library_id: &str) -> Self {
Self {
db: dir.join(format!("{name}.db")),
base: SyncBase::default(),
remote: HttpRemote::for_library(server, token, library_id),
}
}
fn sync(&mut self) -> mtgc_core::Result<mtgc_core::sync::remote::CycleReport> {
let path = self.db.clone();
sync_cycle(&self.remote, &mut self.base, |base, theirs| {
@@ -197,3 +207,53 @@ fn an_address_that_is_not_an_mtgc_server_is_rejected() {
let err = server_info(&format!("http://{addr}")).unwrap_err();
assert!(err.to_string().contains("doesn't look like an mtgc sync server"), "{err}");
}
#[test]
fn a_named_library_syncs_without_touching_the_default_one() {
let (server, state) = spawn_server();
let hash = hash_password("correct horse battery").unwrap();
db::create_local_user(&state.db(), "carol", "Carol", &hash).unwrap();
let dir = std::env::temp_dir().join(format!("mtgc-e2e-named-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
assert!(server_info(&server).unwrap().libraries);
let phone_login = sign_in(&server, "carol", "correct horse battery");
let desktop_login = sign_in(&server, "carol", "correct horse battery");
let mut phone_main = Device::new(&dir, "phone-main", &server, &phone_login.token);
phone_main.set("a", 1);
phone_main.sync().unwrap();
// The phone starts a scratch library; the desktop finds it in the list.
let scratch = phone_main.remote.create_library("Scratch").unwrap();
let mut phone_scratch = Device::for_library(&dir, "phone-scratch", &server, &phone_login.token, &scratch.id);
phone_scratch.set("x", 5);
assert!(phone_scratch.sync().unwrap().published);
let desktop_account = HttpRemote::new(&server, &desktop_login.token);
let listed = desktop_account.list_libraries().unwrap();
assert_eq!(listed.iter().map(|l| (l.name.as_str(), l.default)).collect::<Vec<_>>(), [("Main", true), ("Scratch", false)]);
let mut desktop_scratch = Device::for_library(&dir, "desktop-scratch", &server, &desktop_login.token, &listed[1].id);
desktop_scratch.sync().unwrap();
assert_eq!(desktop_scratch.qty("x"), Some(5));
assert_eq!(desktop_scratch.qty("a"), None, "nothing from Main");
desktop_scratch.set("x", 6);
desktop_scratch.sync().unwrap();
phone_scratch.sync().unwrap();
assert_eq!(phone_scratch.qty("x"), Some(6));
let mut desktop_main = Device::new(&dir, "desktop-main", &server, &desktop_login.token);
desktop_main.sync().unwrap();
assert_eq!(desktop_main.qty("a"), Some(1));
assert_eq!(desktop_main.qty("x"), None, "nothing from Scratch");
// Deleted from one device: the other's next sync says so distinctly.
desktop_account.delete_library(&scratch.id).unwrap();
let err = phone_scratch.sync().unwrap_err();
assert_eq!(err.to_string(), NOT_FOUND);
assert_eq!(phone_scratch.qty("x"), Some(6), "the local copy is kept");
std::fs::remove_dir_all(&dir).unwrap();
}