GitCorner

Code

ossartcraftvectorcraft🌐
2bc41aeb8602
use serde_json::{Value, json};
use vectorcraft_doc::{Kinsoku, NodeId, NodeKind};

use crate::Session;

fn session() -> Session {
    let mut s = Session::new();
    s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
    s
}

fn names(s: &Session) -> Vec<String> {
    s.doc().unwrap().doc.kinsoku_sets.iter().map(|k| k.name.clone()).collect()
}

fn steps(s: &Session) -> usize {
    s.doc().unwrap().history.undo.len()
}

fn text(s: &Session, id: u64) -> vectorcraft_doc::TextObject {
    match &s.doc().unwrap().doc.node(NodeId(id)).unwrap().kind {
        NodeKind::Text(t) => (**t).clone(),
        _ => panic!("not text"),
    }
}

/// Two paragraphs of area type, the second one selected by its range.
fn type_object(s: &mut Session) -> u64 {
    s.execute("text.create", &json!({"x": 10, "y": 10, "text": "あいう\nかきく", "area": {"width": 100, "height": 100}})).unwrap()["id"]
        .as_u64()
        .unwrap()
}

fn tight() -> Value {
    json!({"name": "Tight", "cannotStart": "」。、う", "cannotEnd": "γ€Œ", "hanging": "、。", "inseparable": ["β€”β€”"]})
}

/// Kinsoku Shori Settings' OK replaces the document's custom sets as one undo step (none when
/// nothing changes); each category keeps each character once, sorted; the sets are listed with
/// their users, saved with the document and undone with it.
#[test]
fn kinsoku_sets_are_set_as_one_undo_step_and_saved() {
    let mut s = session();
    let before = steps(&s);
    let r = s.execute("type.kinsokuSets.set", &json!({"sets": [tight(), {"name": " Loose ", "cannotStart": "ーーッ"}]})).unwrap();
    assert_eq!(r["sets"][1]["name"], "Loose");
    assert_eq!(r["sets"][1]["cannotStart"], "ッー");
    assert_eq!(names(&s), ["Tight", "Loose"]);
    assert_eq!(steps(&s), before + 1);
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight(), {"name": "Loose", "cannotStart": "ッー"}]})).unwrap();
    assert_eq!(steps(&s), before + 1, "the same sets again change nothing");
    let listed = s.execute("type.kinsokuSets.list", &json!({})).unwrap();
    assert_eq!((listed["sets"][0]["name"].clone(), listed["sets"][0]["paragraphs"].clone()), (json!("Tight"), json!(0)));
    let reopened = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
    assert_eq!(*reopened.kinsoku_sets, *s.doc().unwrap().doc.kinsoku_sets);
    s.execute("edit.undo", &json!({})).unwrap();
    assert!(names(&s).is_empty());
    // A document without sets saves none.
    let saved = vectorcraft_format::save(&s.doc().unwrap().doc, false);
    assert!(!String::from_utf8_lossy(&saved).contains("kinsoku_sets"));
}

/// The sets are checked before anything changes: a built-in name, a blank or too-long name, two
/// sets of one name in any case, an unknown key, a sequence of one character or with a space, a
/// control character, a `from` the document has no set of, a null value, and more than 256 sets
/// all fail.
#[test]
fn kinsoku_sets_refuse_what_a_set_cannot_hold() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let before = steps(&s);
    let long = "x".repeat(129);
    for bad in [
        json!([{"name": "Hard"}]),
        json!([{"name": "soft"}]),
        json!([{"name": "  "}]),
        json!([{"name": long}]),
        json!([{"name": "A"}, {"name": "a"}]),
        json!([{"name": "A", "strict": true}]),
        json!([{"name": "A", "inseparable": ["あ"]}]),
        json!([{"name": "A", "inseparable": ["あ い"]}]),
        json!([{"name": "A", "inseparable": "あい"}]),
        json!([{"name": "A", "cannotStart": "あ\u{7}"}]),
        json!([{"name": "A", "cannotStart": ["あ"]}]),
        json!([{"name": "A", "from": "Missing"}]),
        json!([{"name": "A", "from": "Tight"}, {"name": "B", "from": "tight"}]),
        json!([{"name": "Tight", "from": "Tight", "cannotStart": null}]),
        json!([{"name": "Tight", "from": null}]),
        json!([{"name": "A", "inseparable": null}]),
        json!((0..257).map(|i| json!({"name": format!("S{i}")})).collect::<Vec<_>>()),
    ] {
        assert!(s.execute("type.kinsokuSets.set", &json!({"sets": bad})).is_err(), "{bad}");
    }
    assert!(s.execute("type.kinsokuSets.set", &json!({})).is_err());
    assert_eq!((names(&s), steps(&s)), (vec!["Tight".to_string()], before), "a refused call changes nothing");
}

/// Kinsoku Set chooses a custom set by name (any case) for the paragraphs `text.setFormat` sets: each
/// holds a copy of it beside Hard, in one undo step; a name the document has no set of fails the call
/// and changes nothing; a built-in set takes the custom one's place. Paragraph styles carry it.
#[test]
fn text_set_format_chooses_a_custom_set_per_paragraph() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let id = type_object(&mut s);
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    assert!(s.execute("text.setFormat", &json!({"kinsoku": "Missing"})).is_err());
    assert!(text(&s, id).para_at(0).kinsoku_set.is_none(), "a refused call changes nothing");
    let before = steps(&s);
    let second = "あいう\n".len();
    s.execute("text.setFormat", &json!({"kinsoku": "tight", "start": second, "end": second})).unwrap();
    assert_eq!(steps(&s), before + 1);
    let t = text(&s, id);
    assert!(t.para_at(0).kinsoku_set.is_none());
    let p = t.para_at(1);
    assert_eq!((p.kinsoku_name(), p.kinsoku), ("Tight", Kinsoku::Hard));
    s.execute("text.setFormat", &json!({"kinsoku": "Tight", "start": second, "end": second})).unwrap();
    assert_eq!(steps(&s), before + 1, "the set the paragraph already has records nothing");
    // The file keeps the paragraph's copy.
    let reopened = vectorcraft_format::load(&vectorcraft_format::save(&s.doc().unwrap().doc, false)).unwrap();
    let Some(NodeKind::Text(back)) = reopened.node(NodeId(id)).map(|n| &n.kind) else { panic!("text") };
    assert_eq!(back.para_at(1).kinsoku_name(), "Tight");
    // A paragraph style captured from it carries the set; Soft replaces it.
    s.execute("paraStyle.new", &json!({"name": "Tight Body", "id": id, "start": second, "end": second})).unwrap();
    s.execute("text.setFormat", &json!({"kinsoku": "soft"})).unwrap();
    assert!(text(&s, id).para_at(1).kinsoku_set.is_none());
    assert_eq!(text(&s, id).para_at(1).kinsoku, Kinsoku::Soft);
    s.execute("paraStyle.apply", &json!({"name": "Tight Body", "id": id})).unwrap();
    assert_eq!(text(&s, id).para_at(0).kinsoku_name(), "Tight");
    assert_eq!(text(&s, id).para_at(1).kinsoku_name(), "Tight");
}

/// A paragraph style that gives only a built-in Kinsoku Set replaces a custom set on the paragraphs
/// it is applied to: the style keeps Kinsoku Set's custom set with its built-in choice. One whose
/// attributes give only a custom set (written by hand into a file) brings Hard with it.
#[test]
fn a_style_with_a_built_in_kinsoku_set_replaces_a_custom_set() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let id = type_object(&mut s);
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    for (style, kinsoku, name) in [("Soft Only", "soft", "Soft"), ("None Only", "none", "None"), ("Hard Only", "hard", "Hard")] {
        s.execute("text.setFormat", &json!({"kinsoku": "Tight"})).unwrap();
        assert_eq!(text(&s, id).para_at(0).kinsoku_name(), "Tight");
        s.execute("paraStyle.new", &json!({"name": style, "attrs": {"kinsoku": kinsoku}})).unwrap();
        s.execute("paraStyle.apply", &json!({"name": style, "id": id})).unwrap();
        let t = text(&s, id);
        assert_eq!((t.para_at(0).kinsoku_name(), t.para_at(1).kinsoku_name()), (name, name), "{style}");
    }
    s.execute("text.setFormat", &json!({"kinsoku": "soft"})).unwrap();
    let only_set = serde_json::Map::from_iter([("kinsoku_set".to_string(), tight_set(&s))]);
    let def = vectorcraft_doc::TextStyleDef { name: "Set Only".into(), attrs: only_set };
    std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).para_styles.push(def);
    s.execute("paraStyle.apply", &json!({"name": "Set Only", "id": id})).unwrap();
    assert_eq!(text(&s, id).para_at(0).kinsoku_name(), "Tight");
}

fn tight_set(s: &Session) -> Value {
    json!(s.doc().unwrap().doc.kinsoku_sets[0])
}

/// Redefining a paragraph style's Kinsoku Set changes the paragraphs that use the style's set and
/// keeps a paragraph's own custom set: the set and Hard beside it change together.
#[test]
fn redefining_a_style_keeps_a_paragraphs_own_custom_set() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight(), {"name": "Loose"}]})).unwrap();
    let id = type_object(&mut s);
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"kinsoku_set": tight_set(&s)}})).unwrap();
    s.execute("paraStyle.apply", &json!({"name": "Body", "id": id})).unwrap();
    let second = "あいう\n".len();
    s.execute("text.setFormat", &json!({"kinsoku": "Loose", "start": second, "end": second})).unwrap();
    s.execute("paraStyle.setAttrs", &json!({"name": "Body", "attrs": {"kinsoku": "soft"}})).unwrap();
    let t = text(&s, id);
    assert_eq!((t.para_at(0).kinsoku_name(), t.para_at(1).kinsoku_name()), ("Soft", "Loose"));
}

/// A paragraph style's custom set is one of the document's, by name in any case, with the
/// document's characters; a name the document has no set of fails the call.
#[test]
fn a_styles_custom_set_is_one_of_the_documents() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    assert!(s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"kinsoku_set": {"name": "Missing"}}})).is_err());
    s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"kinsoku_set": {"name": "tight", "cannotStart": "x"}}})).unwrap();
    let listed = s.execute("paraStyle.list", &json!({})).unwrap();
    let body = listed["styles"].as_array().unwrap().iter().find(|st| st["name"] == "Body").unwrap().clone();
    assert_eq!(body["attrs"]["kinsoku_set"], tight_set(&s));
    assert!(s.execute("paraStyle.setAttrs", &json!({"name": "Body", "attrs": {"kinsoku_set": {"name": "Missing"}}})).is_err());
}

/// A custom set goes with Hard and has a name a custom set can have: a paragraph style call that
/// gives one beside None or Soft, or with a blank or built-in name, fails and leaves the styles as
/// they were, and one beside Hard is kept.
#[test]
fn a_styles_custom_set_given_beside_none_or_soft_fails_the_call() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    s.execute("paraStyle.new", &json!({"name": "Body", "attrs": {"kinsoku_set": tight_set(&s)}})).unwrap();
    let styles = |s: &Session| s.doc().unwrap().doc.para_styles.clone();
    let before = styles(&s);
    for kinsoku in ["none", "soft"] {
        let attrs = json!({"kinsoku": kinsoku, "kinsoku_set": tight_set(&s)});
        assert!(s.execute("paraStyle.new", &json!({"name": "Other", "attrs": attrs})).is_err(), "new with {kinsoku}");
        assert!(s.execute("paraStyle.setAttrs", &json!({"name": "Body", "attrs": attrs})).is_err(), "setAttrs with {kinsoku}");
        assert_eq!(styles(&s), before, "{kinsoku}");
    }
    for name in ["", " ", "Hard", "soft"] {
        let attrs = json!({"kinsoku_set": {"name": name}});
        assert!(s.execute("paraStyle.new", &json!({"name": "Other", "attrs": attrs})).is_err(), "new named `{name}`");
        assert!(s.execute("paraStyle.setAttrs", &json!({"name": "Body", "attrs": attrs})).is_err(), "setAttrs named `{name}`");
        assert_eq!(styles(&s), before, "`{name}`");
    }
    let attrs = json!({"kinsoku": "hard", "kinsoku_set": tight_set(&s)});
    s.execute("paraStyle.new", &json!({"name": "Other", "attrs": attrs})).unwrap();
    let other = styles(&s).into_iter().find(|st| st.name == "Other").unwrap();
    assert_eq!(other.attrs["kinsoku_set"], tight_set(&s));
}

/// Editing a set gives every paragraph and paragraph style using it the new characters, renaming it
/// (`from`) the new name too, and deleting it sets them to Hard: each in one undo step, which undo
/// takes back with the set. Paragraphs using no set, or another set, stay as they are.
#[test]
fn editing_renaming_and_deleting_a_set_updates_its_users() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight(), {"name": "Other", "cannotStart": "γƒΌ"}]})).unwrap();
    let id = type_object(&mut s);
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    let second = "あいう\n".len();
    s.execute("text.setFormat", &json!({"kinsoku": "Tight", "start": 0, "end": 0})).unwrap();
    s.execute("text.setFormat", &json!({"kinsoku": "Other", "start": second, "end": second})).unwrap();
    s.execute("paraStyle.new", &json!({"name": "Uses Tight", "id": id, "start": 0, "end": 0})).unwrap();
    let style_set = |s: &Session| s.doc().unwrap().doc.para_styles.iter().find(|st| st.name == "Uses Tight").unwrap().attrs["kinsoku_set"].clone();
    assert_eq!(style_set(&s)["name"], "Tight");
    let listed = s.execute("type.kinsokuSets.list", &json!({})).unwrap();
    assert_eq!((listed["sets"][0]["paragraphs"].clone(), listed["sets"][0]["styles"].clone()), (json!(1), json!(1)));

    // Edit: γ€Œ may end a line now, and う may start one.
    let before = steps(&s);
    let edited = json!({"name": "Tight", "cannotStart": "」。、", "hanging": "、。", "inseparable": ["β€”β€”"]});
    s.execute("type.kinsokuSets.set", &json!({"sets": [edited, {"name": "Other", "cannotStart": "γƒΌ"}]})).unwrap();
    assert_eq!(steps(&s), before + 1);
    let p = text(&s, id).para_at(0).clone();
    assert_eq!(p.kinsoku_set.as_ref().unwrap().cannot_start, ['、', '。', '」']);
    assert!(p.kinsoku_set.as_ref().unwrap().cannot_end.is_empty());
    assert_eq!(style_set(&s)["cannotStart"], "、。」");
    assert_eq!(text(&s, id).para_at(1).kinsoku_name(), "Other", "another set's paragraph stays");

    // Rename: the users take the new name.
    let renamed = json!({"name": "Strict", "from": "Tight", "cannotStart": "」。、", "hanging": "、。", "inseparable": ["β€”β€”"]});
    s.execute("type.kinsokuSets.set", &json!({"sets": [renamed, {"name": "Other", "cannotStart": "γƒΌ"}]})).unwrap();
    assert_eq!(names(&s), ["Strict", "Other"]);
    assert_eq!(text(&s, id).para_at(0).kinsoku_name(), "Strict");
    assert_eq!(style_set(&s)["name"], "Strict");

    // Delete: its users take Hard, in one step that undo takes back.
    let before = steps(&s);
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "Other", "cannotStart": "γƒΌ"}]})).unwrap();
    assert_eq!(steps(&s), before + 1);
    let p = text(&s, id).para_at(0).clone();
    assert_eq!((p.kinsoku_set.is_none(), p.kinsoku), (true, Kinsoku::Hard));
    assert_eq!(style_set(&s), Value::Null);
    assert_eq!(text(&s, id).para_at(1).kinsoku_name(), "Other");
    s.execute("edit.undo", &json!({})).unwrap();
    assert_eq!(text(&s, id).para_at(0).kinsoku_name(), "Strict");
    assert_eq!(style_set(&s)["name"], "Strict");
    assert_eq!(names(&s), ["Strict", "Other"]);
}

/// Type pasted into another document brings its custom sets: a missing set is added, the
/// document's set of that name with the same characters is used, and one with other characters
/// keeps its own and the pasted set comes in under a free name, so editing the document's set
/// leaves the pasted paragraphs alone. A paragraph style that comes along takes the same set. A
/// document with no room for another set gives the pasted paragraphs Hard.
#[test]
fn pasted_type_brings_its_kinsoku_sets() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let id = type_object(&mut s);
    s.execute("paraStyle.new", &json!({"name": "Tight Body", "attrs": {"kinsoku_set": tight_set(&s)}})).unwrap();
    s.execute("paraStyle.apply", &json!({"name": "Tight Body", "id": id})).unwrap();
    let source = s.doc().unwrap().doc.kinsoku_sets[0].clone();
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    s.execute("edit.copy", &json!({})).unwrap();
    let pasted = |s: &mut Session| -> (vectorcraft_doc::TextObject, Value) {
        let v = s.execute("edit.paste", &json!({})).unwrap();
        (text(s, v["ids"][0].as_u64().unwrap()), v)
    };
    let style_set = |s: &Session| s.doc().unwrap().doc.para_styles.iter().find(|st| st.name == "Tight Body").unwrap().attrs["kinsoku_set"].clone();

    // Pasted back after the set was edited: the document's set as it is now, laid out anew.
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "Tight", "cannotStart": "いうかきく"}]})).unwrap();
    let (t, _) = pasted(&mut s);
    let now = s.doc().unwrap().doc.kinsoku_sets[0].clone();
    assert_eq!(t.para_at(0).kinsoku_set.as_deref(), Some(&now));
    let mut fresh = t.clone();
    crate::cmd::typecmd::refresh_bounds(&mut fresh);
    assert!(fresh == t, "the pasted type is laid out with the document's set");

    // A document without the set.
    s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
    let (t, _) = pasted(&mut s);
    assert_eq!(names(&s), ["Tight"]);
    assert_eq!(t.para_at(1).kinsoku_set.as_deref(), Some(&source));
    assert_eq!(style_set(&s), json!(source));
    let (_, again) = pasted(&mut s);
    assert_eq!((again["added"].clone(), names(&s)), (json!(0), vec!["Tight".to_string()]), "a second paste uses the set the first added");

    // A document whose set of that name has other characters.
    s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "TIGHT", "cannotStart": "γƒΌ"}]})).unwrap();
    let (t, v) = pasted(&mut s);
    assert_eq!(names(&s), ["TIGHT", "Tight 2"]);
    let p = t.para_at(0);
    assert_eq!((p.kinsoku_name(), p.kinsoku_set.as_deref().map(|k| &k.cannot_start)), ("Tight 2", Some(&source.cannot_start)));
    assert_eq!(style_set(&s)["name"], "Tight 2");
    assert!(v["renamed"].as_array().unwrap().contains(&json!({"kind": "kinsokuSet", "from": "Tight", "to": "Tight 2"})), "{v}");
    let kept = json!(s.doc().unwrap().doc.kinsoku_sets[1]);
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "TIGHT", "cannotStart": "ッ"}, kept]})).unwrap();
    let users = s.execute("type.kinsokuSets.list", &json!({})).unwrap();
    assert_eq!(users["sets"][0]["paragraphs"], 0, "editing the document's own set leaves the pasted paragraphs alone: {users}");

    // A document with no room for another set.
    s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
    let full: Vec<Value> = (0..256).map(|i| json!({"name": format!("Set {i}")})).collect();
    s.execute("type.kinsokuSets.set", &json!({"sets": full})).unwrap();
    let (t, _) = pasted(&mut s);
    assert_eq!(names(&s).len(), 256);
    let p = t.para_at(0);
    assert_eq!((p.kinsoku_set.is_none(), p.kinsoku), (true, Kinsoku::Hard));
    assert_eq!(style_set(&s), Value::Null);
}

/// A pasted set whose name, at the longest a name can be, the document's set with other
/// characters already has comes in under a free name that fits: the number takes the place of the
/// name's last characters, and a space left at the end of them is dropped. A second paste uses
/// the set the first added.
#[test]
fn a_pasted_set_with_the_longest_name_comes_in_under_a_name_that_fits() {
    for (long, fitted) in
        [("あ".repeat(128), format!("{} 2", "あ".repeat(126))), (format!("{} いい", "あ".repeat(125)), format!("{} 2", "あ".repeat(125)))]
    {
        let mut s = session();
        s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": long, "cannotStart": "」"}]})).unwrap();
        let id = type_object(&mut s);
        s.execute("text.setFormat", &json!({"ids": [id], "kinsoku": long})).unwrap();
        s.execute("select.set", &json!({"ids": [id]})).unwrap();
        s.execute("edit.copy", &json!({})).unwrap();
        s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
        s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": long, "cannotStart": "γƒΌ"}]})).unwrap();
        let v = s.execute("edit.paste", &json!({})).unwrap();
        assert_eq!(names(&s), [long.clone(), fitted.clone()]);
        let t = text(&s, v["ids"][0].as_u64().unwrap());
        let p = t.para_at(0);
        assert_eq!((p.kinsoku_name(), p.kinsoku_set.as_deref().map(|k| k.cannot_start.clone())), (fitted.as_str(), Some(vec!['」'])));
        let again = s.execute("edit.paste", &json!({})).unwrap();
        assert_eq!(
            (again["added"].clone(), names(&s)),
            (json!(0), vec![long.clone(), fitted.clone()]),
            "a second paste uses the set the first added"
        );
    }
}

/// The Kinsoku Sets a graph keeps for its labels (value axis, categories, legend), and those its
/// labels are drawn with now.
fn graph_label_sets(s: &Session, id: u64) -> (Vec<String>, Vec<String>) {
    let n = s.doc().unwrap().doc.node(NodeId(id)).unwrap().clone();
    let l = n.extras().graph.as_deref().unwrap().label_styles.clone();
    let kept = [l.values_para, l.categories_para, l.legend_para].into_iter().flatten().map(|p| p.kinsoku_name().to_string()).collect();
    let mut drawn = vec![];
    n.walk(&mut |m| {
        if let NodeKind::Text(t) = &m.kind {
            drawn.push(t.para_at(0).kinsoku_name().to_string());
        }
    });
    (kept, drawn)
}

/// A graph's labels keep a custom set as type does. Pasted into a document whose set of that name
/// has other characters, they take the copy the paste adds, also once the graph is drawn again.
/// Editing a set gives the graph's kept attributes the new characters, and deleting it gives them
/// Hard.
#[test]
fn graph_labels_keep_their_custom_set() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let csv = ",2024,2025\nQ1,3,2\nQ2,5,4";
    let id = s.execute("graph.create", &json!({"type": "column", "x": 10, "y": 10, "width": 300, "height": 200, "csv": csv})).unwrap()["id"]
        .as_u64()
        .unwrap();
    s.execute("select.set", &json!({"ids": [id]})).unwrap();
    s.execute("text.setFormat", &json!({"kinsoku": "Tight"})).unwrap();
    let (kept, drawn) = graph_label_sets(&s, id);
    assert_eq!(kept, ["Tight"; 3]);
    assert!(!drawn.is_empty() && drawn.iter().all(|k| k == "Tight"), "{drawn:?}");
    s.execute("edit.copy", &json!({})).unwrap();

    // Pasted where the set of that name has other characters: the labels take the added copy.
    s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "TIGHT", "cannotStart": "γƒΌ"}]})).unwrap();
    let pasted = s.execute("edit.paste", &json!({})).unwrap()["ids"][0].as_u64().unwrap();
    assert_eq!(names(&s), ["TIGHT", "Tight 2"]);
    s.execute("graph.setData", &json!({"id": pasted, "csv": csv})).unwrap();
    let (kept, drawn) = graph_label_sets(&s, pasted);
    assert_eq!(kept, ["Tight 2"; 3]);
    assert!(drawn.iter().all(|k| k == "Tight 2"), "the labels drawn again keep the copy: {drawn:?}");

    // A second copy becomes a symbol's art.
    let in_symbol = s.execute("edit.paste", &json!({})).unwrap()["ids"][0].clone();
    s.execute("symbol.new", &json!({"name": "Chart", "ids": [in_symbol]})).unwrap();
    let symbol_starts = |s: &Session| {
        let mut out = vec![];
        s.doc().unwrap().doc.symbols.iter().find(|x| x.name == "Chart").unwrap().art.walk(&mut |n| {
            if let Some(g) = n.extras().graph.as_deref() {
                out.push(g.label_styles.values_para.as_ref().unwrap().kinsoku_set.as_deref().unwrap().cannot_start.clone());
            }
        });
        out
    };
    assert_eq!(symbol_starts(&s).len(), 1);
    let listed = s.execute("type.kinsokuSets.list", &json!({})).unwrap();
    assert_eq!((listed["sets"][1]["name"].clone(), listed["sets"][1]["graphs"].clone()), (json!("Tight 2"), json!(2)), "{listed}");
    assert_eq!(listed["sets"][0]["graphs"], 0);

    // Edited, then deleted: the kept attributes follow the set, so the labels drawn again do too.
    let edited = json!({"name": "Tight 2", "cannotStart": "γƒΌ"});
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "TIGHT", "cannotStart": "γƒΌ"}, edited]})).unwrap();
    let l = &s.doc().unwrap().doc.node(NodeId(pasted)).unwrap().extras().graph.as_deref().unwrap().label_styles;
    assert_eq!(l.values_para.as_ref().unwrap().kinsoku_set.as_deref().unwrap().cannot_start, ['γƒΌ']);
    assert_eq!(symbol_starts(&s), [vec!['γƒΌ']], "a graph in a symbol's art too");
    s.execute("type.kinsokuSets.set", &json!({"sets": [{"name": "TIGHT", "cannotStart": "γƒΌ"}]})).unwrap();
    s.execute("graph.setData", &json!({"id": pasted, "csv": csv})).unwrap();
    let (kept, drawn) = graph_label_sets(&s, pasted);
    assert_eq!(kept, ["Hard"; 3]);
    assert!(drawn.iter().all(|k| k == "Hard"), "{drawn:?}");
}

/// New Set's starting point: Hard and Soft give their characters, None gives an empty set, and a
/// custom set (any case) gives its own; an unknown base fails.
#[test]
fn a_new_set_starts_from_a_built_in_or_custom_set() {
    let mut s = session();
    s.execute("type.kinsokuSets.set", &json!({"sets": [tight()]})).unwrap();
    let hard = s.execute("type.kinsokuSets.base", &json!({"base": "hard", "name": "New"})).unwrap();
    let soft = s.execute("type.kinsokuSets.base", &json!({"base": "soft"})).unwrap();
    let start = |v: &Value| v["set"]["cannotStart"].as_str().unwrap().to_string();
    assert!(start(&hard).contains('γƒΌ') && !start(&soft).contains('γƒΌ') && start(&soft).contains('」'));
    assert_eq!(hard["set"]["name"], "New");
    assert_eq!(hard["set"]["hanging"], "γ€γ€‚οΌŒοΌŽο½‘ο½€");
    assert_eq!(hard["set"]["inseparable"].as_array().unwrap().len(), 5);
    let none = s.execute("type.kinsokuSets.base", &json!({"base": "none"})).unwrap();
    assert_eq!(none["set"]["cannotStart"], "");
    let custom = s.execute("type.kinsokuSets.base", &json!({"base": "TIGHT"})).unwrap();
    assert_eq!(custom["set"]["cannotEnd"], "γ€Œ");
    assert!(s.execute("type.kinsokuSets.base", &json!({"base": "Missing"})).is_err());
}