Files
komorebi/komorebi/tests/schemas.rs
T
LGUG2Z 21017b573f feat(wm): initial window placement rules
A window that opens goes after the focused container. A workspace can now say
another place for its windows: the primary or the secondary tile, before or
after the focused container, the end of the list, or a place by number, counting
from 1. The place is for every window, or for the windows that match a rule on
their program, app id, title or path; the rules are the conditions the ignore
and manage rules are made of, and the first that matches in key order decides. A
place that does not exist, such as the secondary tile of a workspace with one
container, is the default place.

A place is read from a name, a number or text that is a number. The
documentation of the setting shows a number and a map's keys can only be text,
and komorebi for Windows (83c4e11d) reads only text, so the form its own page
shows is refused there.

A place that was reserved with preselect-direction is used before the rules are
asked, and a locked container keeps its index. A workspace whose
window_container_behaviour is Append puts a window in the focused container, and
the rules are not used.

A workspace takes the rules from the key initial_window_placement_rules of its
entry in komorebi.json. The key is in schema.json and so in the typed config
option of the module, and the page docs/initial-window-placement-rules.md says
how to write it. vm-placement opens windows in a headless KWin and checks where
they go, by program, app id and title.

The schema that schemars derives for a place is an object with a Placement or an
Index key, which is not what is read or written, so a check against it refuses a
file that the daemon reads. The schema of PlacementTarget is written by hand
instead: a name, a number, or text that is a number. A file with a place of each
form is checked against schema.json, and the module's config option takes them.
2026-10-28 07:20:57 -07:00

116 lines
4.4 KiB
Rust

//! The JSON Schemas the daemon answers with: `StaticConfigSchema`,
//! `ApplicationSpecificConfigurationSchema`, `NotificationSchema` and `SocketSchema`, against an
//! in-memory desktop. `komorebic` prints the same schemas without the daemon; the daemon answers
//! on its socket for a client that has none. The schema of a place for a new window, which is
//! written by hand, is checked here too.
use komorebi::backend::fake::FakeBackend;
use komorebi::backend::with_backend;
use komorebi::core::PlacementTarget;
use komorebi::core::SocketMessage;
use komorebi::window_manager::WindowManager;
use serde_json::json;
use std::sync::Arc;
fn answer(message: SocketMessage) -> serde_json::Value {
let fake = Arc::new(FakeBackend::with_one_monitor());
let (_sender, receiver) = crossbeam_channel::bounded(1);
let socket = std::path::PathBuf::from(format!("/tmp/kms-{}.sock", std::process::id()));
let mut reply = Vec::new();
with_backend(fake, || {
let mut wm = WindowManager::new(receiver, Some(socket.clone())).unwrap();
wm.init().unwrap();
wm.process_command(message, &mut reply)
})
.unwrap();
let _ = std::fs::remove_file(&socket);
serde_json::from_slice(&reply).expect("the answer is JSON")
}
#[test]
fn the_daemon_answers_with_the_schema_of_komorebi_json() {
let schema = answer(SocketMessage::StaticConfigSchema);
assert_eq!(schema["title"], "StaticConfig");
assert!(schema["properties"]["monitors"].is_object());
assert!(schema["properties"]["app_specific_configuration_path"].is_object());
}
#[test]
fn the_daemon_answers_with_the_schema_of_a_list_of_applications_files() {
let schema = answer(SocketMessage::ApplicationSpecificConfigurationSchema);
assert_eq!(schema["type"], "array");
assert!(schema["$defs"]["AscApplicationRules"].is_object());
}
#[test]
fn the_daemon_answers_with_the_schemas_of_what_it_sends_and_of_what_it_takes() {
assert!(answer(SocketMessage::NotificationSchema)["properties"]["state"].is_object());
assert!(answer(SocketMessage::SocketSchema)["oneOf"].is_array());
}
#[test]
fn the_schema_of_komorebi_json_has_the_places_a_workspace_can_give_a_new_window() {
let schema = answer(SocketMessage::StaticConfigSchema);
let key = &schema["$defs"]["WorkspaceConfig"]["properties"]["initial_window_placement_rules"];
assert!(key.is_object(), "a workspace has the key in the schema");
assert!(schema["$defs"]["InitialWindowPlacementRules"]["anyOf"].is_array());
assert!(schema["$defs"]["PlacementTarget"].is_object());
}
/// The schema derived for the enum is an object with a `Placement` or an `Index` key, which is not
/// what is read: a name, a number, or text that is a number.
#[test]
fn a_place_is_described_as_a_name_a_number_or_text_that_is_a_number() {
let schema = serde_json::to_value(schemars::schema_for!(PlacementTarget)).unwrap();
let forms = schema["anyOf"]
.as_array()
.expect("a place has several forms");
assert_eq!(forms.len(), 3);
// a name: every strategy of `WindowPlacement`, and each of them is read
assert_eq!(forms[0]["$ref"], "#/$defs/WindowPlacement");
let names: Vec<&str> = schema["$defs"]["WindowPlacement"]["oneOf"]
.as_array()
.unwrap()
.iter()
.map(|name| name["const"].as_str().unwrap())
.collect();
assert_eq!(names.len(), 5);
for name in &names {
assert!(
matches!(
serde_json::from_value(json!(name)),
Ok(PlacementTarget::Placement(_))
),
"{name} is not read as a strategy"
);
}
// a number
assert_eq!(forms[1]["type"], "integer");
assert_eq!(
serde_json::from_value::<PlacementTarget>(json!(3)).unwrap(),
PlacementTarget::Index(3)
);
// text that is a number: what the pattern accepts is what is read as a number, and a name is
// not
assert_eq!(forms[2]["type"], "string");
let pattern = regex::Regex::new(forms[2]["pattern"].as_str().unwrap()).unwrap();
assert!(pattern.is_match("3"));
assert!(!pattern.is_match("Primary"));
assert_eq!(
serde_json::from_value::<PlacementTarget>(json!("3")).unwrap(),
PlacementTarget::Index(3)
);
// and a place that is none of them is refused
assert!(serde_json::from_value::<PlacementTarget>(json!("Nowhere")).is_err());
assert!(!pattern.is_match("Nowhere"));
}