use serde_json::{json, Value};usesuper::*;usecrate::definitions::{builtin_table_definitions, DefinitionError};/// A user table of the columns given, with an optional page list.fn table(id: &str, plural: &str, columns: Value, extra: Value) -> TableDefinition {let title = columns[0]["key"].clone();letmut value = json!({"id": id, "name": id, "plural": plural, "picture": null, "builtin": false, "engine": null,"columns": columns, "title": title, "date": null, "project": null, "order": null,"slip": { "title": title, "subtitle": null, "trailing": null, "photo": null, "toggle": null },"tile": { "face": "slip-list" },"page": [{ "heading": null, "columns": [title] }],"instructions": "A fixture." });for (key, held) in extra.as_object().unwrap() { value[key] = held.clone(); }let table: TableDefinition = serde_json::from_value(value).expect("parses"); table.validate().unwrap_or_else(|why| panic!("{id}: {why}")); table}fn student(extra: Value) -> TableDefinition { table("student", "Students", json!([{ "key": "name", "type": "text" }]), extra)}fn lesson(student_link: Value) -> TableDefinition {letmut link = json!({ "key": "student", "type": "link", "link_to": "student" });for (key, held) in student_link.as_object().unwrap() { link[key] = held.clone(); } table("lesson","Lessons", json!([{ "key": "topic", "type": "text" }, link, { "key": "room", "type": "link", "link_to": "room" }]), json!({}), )}fn found(tables: &[TableDefinition], host: &str) -> Vec<(String, String, String)> { contributors(tables, host) .into_iter() .map(|c| (c.table, c.column.unwrap_or_default(), c.label)) .collect()}/// RULE: Any table with a link column to a table contributes a list to that table's record page, found from the definitions alone under the contributing table's plural, so a tool installed later appears without an edit to the host.#[test]fn a_table_that_links_to_a_host_contributes_a_list_to_its_page() {let tables = [student(json!({})), lesson(json!({}))]; assert_eq!( found(&tables, "student"), [("lesson".to_string(), "student".to_string(), "Lessons".to_string())] ); assert!(found(&tables, "lesson").is_empty(), "nothing links to a lesson"); assert!(found(&tables, "room").is_empty(), "a table that is not here has no page");}/// RULE: A link column that says contributes false contributes nothing and one that names a label is headed by it; a host that says contributions false gathers nothing; a list the host's own page names is not found a second time.#[test]fn a_link_and_a_host_each_have_their_say_and_a_named_list_is_not_found_twice() {let quiet = [student(json!({})), lesson(json!({ "contributes": false }))]; assert!(found(&quiet, "student").is_empty());let labelled = [student(json!({})), lesson(json!({ "contributes": { "label": "Lessons taken" } }))]; assert_eq!(found(&labelled, "student")[0].2, "Lessons taken");let closed = [student(json!({ "contributions": false })), lesson(json!({}))]; assert!(found(&closed, "student").is_empty());let page = json!([ { "heading": null, "columns": ["name"] }, { "heading": "Lessons", "list": { "table": "lesson", "link": "student" } } ]);let named = [student(json!({ "page": page })), lesson(json!({}))]; assert!(found(&named, "student").is_empty());}/// RULE: The contributes key belongs to a link column and its label says something, and both keys round-trip byte for byte, so a contribution is never headed by nothing and never dropped on a write.#[test]fn contributes_sits_on_a_link_and_round_trips() {let lesson = lesson(json!({ "contributes": { "label": "Lessons taken" } }));let json = lesson.to_json(); assert_eq!(TableDefinition::from_json(&json).unwrap().to_json(), json);let student = student(json!({ "contributions": false }));let json = student.to_json(); assert!(json.contains(r#""contributions":false"#)); assert_eq!(TableDefinition::from_json(&json).unwrap().to_json(), json);letmut value: Value = serde_json::from_str(&lesson.to_json()).unwrap(); value["columns"][1]["contributes"] = json!({ "label": " " });let blank: TableDefinition = serde_json::from_value(value.clone()).unwrap(); assert!(matches!(blank.validate(), Err(DefinitionError::EmptyAt(ref slot)) if slot == "columns.student.contributes.label")); value["columns"][1].as_object_mut().unwrap().remove("contributes"); value["columns"][0]["contributes"] = json!(false);let on_text: TableDefinition = serde_json::from_value(value.clone()).unwrap(); assert!(matches!(on_text.validate(), Err(DefinitionError::WrongColumnType { ref slot, .. }) if slot == "columns.topic.contributes")); value["columns"][0]["contributes"] = json!({ "label": "x", "icon": "y" }); assert!(TableDefinition::authored(&value).is_err());}/// RULE: No shipped built-in's record page is offered two found lists under the same words, so the row of links on a page never reads "Transactions · Transactions"; a second link to the same host names its own label or contributes false.#[test]fn no_built_in_page_is_offered_two_lists_under_the_same_words() {let tables = builtin_table_definitions();for host in tables {letmut labels: Vec<String> = contributors(&tables, &host.id).into_iter().map(|c| c.label).collect(); labels.sort();let before = labels.len(); labels.dedup(); assert_eq!(before, labels.len(), "{}'s page is offered the same words twice: {labels:?}", host.id); }}/// RULE: A join table's link that says contributes through its other link contributes the table that link points at, under that table's plural or the label beside it, and the join is named so the list can be read through it.#[test]fn a_join_table_contributes_the_table_it_joins_to() {let enrolment = |said: Value| { table("enrolment","Enrolments", json!([ { "key": "note", "type": "text" }, { "key": "student", "type": "link", "link_to": "student", "contributes": said }, { "key": "course", "type": "link", "link_to": "course" } ]), json!({}), ) };let course = table("course", "Courses", json!([{ "key": "name", "type": "text" }]), json!({}));let tables = [student(json!({})), course.clone(), enrolment(json!({ "through": "course" }))];let all = contributors(&tables, "student"); assert_eq!(all.len(), 1); assert_eq!((all[0].table.as_str(), all[0].label.as_str(), &all[0].column), ("course", "Courses", &None)); assert_eq!( serde_json::to_value(&all[0]).unwrap(), json!({ "table": "course", "label": "Courses", "through": { "table": "enrolment", "from": "student", "to": "course" } }) );let labelled = [student(json!({})), course, enrolment(json!({ "through": "course", "label": "Studying" }))]; assert_eq!(contributors(&labelled, "student")[0].label, "Studying");// The joined table is not here: nothing to contribute.let alone = [student(json!({})), enrolment(json!({ "through": "course" }))]; assert!(contributors(&alone, "student").is_empty());// `through` names another single link of the same table; an empty say is refused.for bad in [json!({ "through": "student" }), json!({ "through": "note" }), json!({ "through": "gone" }), json!({})] {letmut value: Value = serde_json::from_str(&enrolment(json!(false)).to_json()).unwrap(); value["columns"][1]["contributes"] = bad.clone();let parsed: TableDefinition = serde_json::from_value(value).unwrap(); assert!(parsed.validate().is_err(), "{bad} was accepted"); }}