Lutra/crates/engine/tests/layout.rs
danny 02922eb89d A1: layout.get/layout.set keep the editor tab layout per project; editor.tabs.preview setting
The tab layout of the open project lives in <data_dir>/layout/<project-id>.json
(written atomically, 256 KiB cap both ways, a broken file reads as null),
never in webview storage. New bool setting editor.tabs.preview (default on).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 12:22:42 +01:00

142 lines
4.1 KiB
Rust

//! `layout.get` / `layout.set` (C10 A1): the editor tab layout per project in
//! Lutra's own state file.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use std::path::PathBuf;
use lutra_engine::{Engine, EngineConfig};
use serde_json::{Value, json};
use tempfile::TempDir;
/// Two projects and the engine directories.
struct Setup {
/// Holds everything.
_tmp: TempDir,
/// First project.
a: PathBuf,
/// Second project.
b: PathBuf,
/// Engine directories.
config: EngineConfig,
}
fn setup() -> Setup {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a");
let b = tmp.path().join("b");
std::fs::create_dir(&a).unwrap();
std::fs::create_dir(&b).unwrap();
let config = EngineConfig {
config_dir: tmp.path().join("config"),
data_dir: tmp.path().join("data"),
};
Setup {
_tmp: tmp,
a,
b,
config,
}
}
fn run(engine: &mut Engine, command: &str, params: Value) -> Value {
engine
.execute(command, params)
.unwrap_or_else(|e| panic!("{command}: {e}"))
.value
}
#[test]
fn the_layout_survives_a_restart_and_is_kept_per_project() {
let s = setup();
let layout = json!({"version": 1, "groups": [{"tabs": [{"path": "x.php", "pinned": true, "preview": false}], "active": "x.php"}]});
{
let mut engine = Engine::with_config(s.config.clone()).unwrap();
run(&mut engine, "project.open", json!({"path": s.a}));
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": null})
);
assert_eq!(
run(&mut engine, "layout.set", json!({"layout": layout})),
json!({"stored": true})
);
run(&mut engine, "project.open", json!({"path": s.b}));
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": null})
);
}
let mut engine = Engine::with_config(s.config.clone()).unwrap();
run(&mut engine, "project.open", json!({"path": s.a}));
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": layout})
);
let files: Vec<_> = std::fs::read_dir(s.config.data_dir.join("layout"))
.unwrap()
.collect();
assert_eq!(files.len(), 1);
}
#[test]
fn bad_params_and_broken_files_are_refused_without_harm() {
let s = setup();
let mut engine = Engine::with_config(s.config.clone()).unwrap();
assert!(engine.execute("layout.get", json!({})).is_err());
run(&mut engine, "project.open", json!({"path": s.a}));
for params in [
json!({}),
json!({"layout": [1]}),
json!({"layout": {}, "x": 1}),
] {
let err = engine.execute("layout.set", params).unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
let big = "x".repeat(lutra_engine_max());
let err = engine
.execute("layout.set", json!({"layout": {"pad": big}}))
.unwrap_err();
assert_eq!(err.code(), "invalid_params");
run(&mut engine, "layout.set", json!({"layout": {"version": 1}}));
let dir = s.config.data_dir.join("layout");
let file = std::fs::read_dir(&dir)
.unwrap()
.next()
.unwrap()
.unwrap()
.path();
for content in [&b"{not json"[..], b"[1, 2]", b""] {
std::fs::write(&file, content).unwrap();
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": null})
);
}
std::fs::write(&file, vec![b' '; lutra_engine_max() + 1]).unwrap();
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": null})
);
}
#[test]
fn an_engine_without_files_keeps_nothing() {
let s = setup();
let mut engine = Engine::new().unwrap();
run(&mut engine, "project.open", json!({"path": s.a}));
assert_eq!(
run(&mut engine, "layout.set", json!({"layout": {"version": 1}})),
json!({"stored": false})
);
assert_eq!(
run(&mut engine, "layout.get", json!({})),
json!({"layout": null})
);
}
/// The size cap of a layout file (256 KiB).
fn lutra_engine_max() -> usize {
256 * 1024
}