diff --git a/conformance/knowledge/filters.jsonl b/conformance/knowledge/filters.jsonl index 40279bf..698d9cb 100644 --- a/conformance/knowledge/filters.jsonl +++ b/conformance/knowledge/filters.jsonl @@ -8,7 +8,7 @@ {"expect":{"id":3,"result":{"puzzles":"$array","sources":"$array","seed":20406}}} {"send":{"jsonrpc":"2.0","id":4,"method":"knowledge/puzzles","params":{"daily":false,"limit":1}}} {"expect":{"id":4,"result":{"puzzles":"$array","sources":"$array"}}} -{"comment":"Facets list the openings in the same shape as the themes."} +{"comment":"Facets list the openings in the same shape as the themes, an empty list when there are none."} {"send":{"jsonrpc":"2.0","id":5,"method":"knowledge/puzzleFacets"}} {"expect":{"id":5,"result":{"bands":"$array","total":"$array","themes":"$array","openings":"$array","sources":"$array"}}} {"comment":"Refused: an opening that isn't one."} diff --git a/crates/cnp/src/knowledge.rs b/crates/cnp/src/knowledge.rs index f7e0aa9..66ba7e0 100644 --- a/crates/cnp/src/knowledge.rs +++ b/crates/cnp/src/knowledge.rs @@ -727,10 +727,15 @@ pub struct FacetsResult { /// When a [`Quality`] filter was asked for: the thresholds it used. #[serde(default, skip_serializing_if = "Option::is_none")] pub quality: Option, - /// Each opening family that has at least one puzzle, with its counts. Only from a node - /// that offers [`Capabilities::puzzle_filters`]. - #[serde(default, skip_serializing_if = "Vec::is_empty")] - pub openings: Vec, + /// Each opening family that has at least one puzzle, with its counts. `Some` from a node + /// that offers [`Capabilities::puzzle_filters`], and `None` (the field absent) from one + /// that doesn't. + /// + /// Pitfall: an `Option`, not a `Vec` left out when empty, because the spec ties the field + /// to the capability, not to the count: a node with the capability and no puzzles answers + /// `"openings": []`, and a client tells "no openings yet" from "this node counts none". + #[serde(default, skip_serializing_if = "Option::is_none")] + pub openings: Option>, } /// How many puzzles each theme has in each rating band, from complete imports. diff --git a/crates/cnp/tests/knowledge.rs b/crates/cnp/tests/knowledge.rs index 2492de0..79d0ef8 100644 --- a/crates/cnp/tests/knowledge.rs +++ b/crates/cnp/tests/knowledge.rs @@ -514,7 +514,7 @@ fn an_opening_and_a_daily_filter_are_written_only_when_asked_for() { fn facets_list_openings_only_when_the_node_has_them() { let none: knowledge::FacetsResult = serde_json::from_value(json!({"bands":[],"total":[],"themes":[],"sources":[]})).unwrap(); - assert!(none.openings.is_empty()); + assert_eq!(none.openings, None); assert_eq!( serde_json::to_value(&none).unwrap(), json!({"bands":[],"total":[],"themes":[],"sources":[]}) @@ -524,8 +524,23 @@ fn facets_list_openings_only_when_the_node_has_them() { "openings":[{"opening":"Italian_Game","counts":[4]}] })) .unwrap(); - assert_eq!(some.openings[0].opening, "Italian_Game"); - assert_eq!(some.openings[0].counts, [4]); + let openings = some.openings.expect("openings"); + assert_eq!(openings[0].opening, "Italian_Game"); + assert_eq!(openings[0].counts, [4]); +} + +#[test] +fn facets_of_a_node_with_filters_and_no_puzzles_still_answer_openings() { + // spec/knowledge.md, `knowledge/puzzleFacets`: the field goes with `puzzleFilters`, not + // with the count, and conformance/knowledge/filters.jsonl expects it on an empty library. + let empty = knowledge::FacetsResult { + openings: Some(Vec::new()), + ..Default::default() + }; + let wire = serde_json::to_value(&empty).unwrap(); + assert_eq!(wire["openings"], json!([])); + let back: knowledge::FacetsResult = serde_json::from_value(wire).unwrap(); + assert_eq!(back.openings, Some(Vec::new())); } #[test] diff --git a/spec/knowledge.md b/spec/knowledge.md index 2827b73..0350f70 100644 --- a/spec/knowledge.md +++ b/spec/knowledge.md @@ -342,7 +342,9 @@ illustrative counts): - A node that offers `puzzleFilters` also answers with `openings`, in the same shape as `themes`: `[{"opening": "Italian_Game", "counts": [...]}]`, one count for each band, for each opening family that has a puzzle (a puzzle with no opening tag is in none, so the counts - don't add up to `total`). Without `puzzleFilters` the field is absent. + don't add up to `total`). The field goes with the capability, not the count: a node with + `puzzleFilters` and no puzzles answers `"openings": []`. Without `puzzleFilters` the field is + absent. - Counts are of the puzzles of **complete imports** and are the same as asking `knowledge/puzzles` for that theme, band and quality would reach, which a node can keep as it imports, not work out at each request. A client doesn't treat a count as current after a new @@ -431,6 +433,11 @@ searching puzzles by a position, and Lumbra's OTB games. ## Changes +- **draft 1, 2026-11-16**, clarified: a node that offers `puzzleFilters` answers + `knowledge/puzzleFacets`' `openings` even when it is empty, as the conformance session + already expected; `cnp`'s `FacetsResult::openings` is now an `Option`, so a node built on it + can say so. The wire is unchanged for every client. + - **draft 1, 2026-11-17**, added to the draft: **space** (capability `space`, `knowledge/usage` and `knowledge/remove`), so a client that downloads puzzles a part at a time can keep the knowledge within a disk limit the player sets, and remove a whole database