Ontology tests: select, admit rules, prompt section, run pin, round prompts, draft door, CLI
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0ea07d21fa
commit
cd435e2a48
5 changed files with 575 additions and 2 deletions
|
|
@ -681,4 +681,28 @@ mod tests {
|
|||
}
|
||||
assert!(EditDraft::parse("3").is_err() && EditDraft::parse("\"x\"").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ontology_draft_refuses_unknown_fields_and_numbers_from_first_n() {
|
||||
for bad in [
|
||||
r#"{"entries": [], "extra": 1}"#,
|
||||
r#"{"entries": [{"layer": "Lexicon", "choice": "c", "language": "ja-JP"}]}"#,
|
||||
"```json\n{\"entries\": []}\n```",
|
||||
"[]",
|
||||
] {
|
||||
assert!(OntologyDraft::parse(bad).is_err(), "{bad}");
|
||||
}
|
||||
let draft = OntologyDraft::parse(
|
||||
r#"{"entries": [{"layer": "Grammar", "choice": "a"}, {"layer": "Lexicon", "choice": "b", "examples": ["x"]}]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let by = Annotator::Operator;
|
||||
let entries = draft.into_entries("ja-JP", 3, 5, &by);
|
||||
assert_eq!(
|
||||
entries.iter().map(|e| (e.language.as_str(), e.n, e.inventory_rev)).collect::<Vec<_>>(),
|
||||
[("ja-JP", 5, 3), ("ja-JP", 6, 3)]
|
||||
);
|
||||
assert!(entries.iter().all(|e| e.annotator == by));
|
||||
assert_eq!(entries[1].examples, ["x"]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1767,3 +1767,47 @@ fn panel_and_language_are_required() {
|
|||
assert!(!o.status.success());
|
||||
assert!(out(&o).contains("--language"), "{}", out(&o));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ontology_admit_and_dump() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
dao_stores(d.path());
|
||||
let draft = |name: &str, body: &str| {
|
||||
std::fs::write(d.path().join(name), body).unwrap();
|
||||
};
|
||||
draft(
|
||||
"good.json",
|
||||
r#"{"entries": [{"layer": "Discourse", "when": [{"slot": "Stance", "feature": "feature:18", "test": {"AtLeast": 600}}], "choice": "SOFTCHOICE", "examples": ["EXAMPLETEXT"]}, {"layer": "Lexicon", "choice": "plain"}]}"#,
|
||||
);
|
||||
let admit = |file: &str| {
|
||||
weksa(
|
||||
d.path(),
|
||||
&[
|
||||
"ontology", "admit", file, "--language", "ja-JP", "--inventory-rev", "1",
|
||||
"--model-family", "anthropic", "--generator", "a drafting model",
|
||||
],
|
||||
)
|
||||
};
|
||||
let o = admit("good.json");
|
||||
assert!(o.status.success(), "{}", out(&o));
|
||||
let o = weksa(d.path(), &["ontology", "dump"]);
|
||||
let text = out(&o);
|
||||
assert_eq!(text.matches("onto:ja-JP:").count(), 2, "{text}");
|
||||
for want in ["onto:ja-JP:1", "onto:ja-JP:2", "Anthropic", "a drafting model", "SOFTCHOICE", "EXAMPLETEXT", "Stance"] {
|
||||
assert!(text.contains(want), "{want}: {text}");
|
||||
}
|
||||
// A second draft numbers on from the language's latest.
|
||||
let o = admit("good.json");
|
||||
assert!(o.status.success(), "{}", out(&o));
|
||||
assert!(out(&weksa(d.path(), &["ontology", "dump"])).contains("onto:ja-JP:4"));
|
||||
// A refused draft writes nothing and echoes no choice text.
|
||||
draft(
|
||||
"bad.json",
|
||||
r#"{"entries": [{"layer": "Discourse", "when": [{"slot": "Stance", "feature": "feature:9999", "test": {"AtLeast": 600}}], "choice": "LEAKYCHOICE"}]}"#,
|
||||
);
|
||||
let before = out(&weksa(d.path(), &["ontology", "dump"]));
|
||||
let o = admit("bad.json");
|
||||
assert!(!o.status.success());
|
||||
assert!(!out(&o).contains("LEAKYCHOICE"), "{}", out(&o));
|
||||
assert_eq!(out(&weksa(d.path(), &["ontology", "dump"])), before);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -537,9 +537,10 @@ mod lowering {
|
|||
use weksa::daodejing::packets;
|
||||
use weksa::entity::{Entity, EntityOrigin, LanguageForms};
|
||||
use weksa::packet::{EntityId, Packet, proof_packet};
|
||||
use weksa::packet::Grade;
|
||||
use weksa::{
|
||||
CorpusItemId, CorpusStore, EntityForms, EntityStore, Inventory, InventoryStore,
|
||||
LowerError, LoweringInput, PacketStore,
|
||||
Annotator, Condition, CorpusItemId, CorpusStore, EntityForms, EntityStore, Inventory,
|
||||
InventoryStore, Layer, LowerError, LoweringInput, OntologyEntry, OntologyStore, PacketStore, Test, select,
|
||||
};
|
||||
|
||||
fn data(sub: &str) -> PathBuf {
|
||||
|
|
@ -746,6 +747,149 @@ mod lowering {
|
|||
assert_eq!(Prompt::lower(&view, &item, "en-US", &inv, &held, &weksa::Ontology::empty()).err(), Some(want));
|
||||
}
|
||||
}
|
||||
|
||||
fn feature(inv: &Inventory, gloss: &str) -> weksa::InvId {
|
||||
inv.entries().find(|e| e.gloss == gloss).unwrap().id
|
||||
}
|
||||
|
||||
fn entry(language: &str, n: u32, when: Vec<Condition>, choice: &str) -> OntologyEntry {
|
||||
OntologyEntry {
|
||||
language: language.into(),
|
||||
n,
|
||||
inventory_rev: 1,
|
||||
layer: Layer::Discourse,
|
||||
when,
|
||||
choice: choice.into(),
|
||||
examples: vec![],
|
||||
annotator: Annotator::Operator,
|
||||
}
|
||||
}
|
||||
|
||||
/// The ontology the store holds after admitting `entries` as one batch.
|
||||
fn ontology_of(cast: &InventoryStore, entries: Vec<OntologyEntry>) -> weksa::Ontology {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut store = OntologyStore::open(&dir.path().join("o.cc")).unwrap();
|
||||
store.admit(entries, cast).unwrap();
|
||||
store.ontology().unwrap()
|
||||
}
|
||||
|
||||
fn at_least(inv: &Inventory, gloss: &str, g: u16) -> Condition {
|
||||
Condition {
|
||||
slot: weksa::Slot::Stance,
|
||||
feature: feature(inv, gloss),
|
||||
test: Test::AtLeast(Grade::new(g).unwrap()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The proof packet's view with the given stance restraint and discourse irony.
|
||||
fn view_of(
|
||||
restraint: Option<u16>,
|
||||
irony: Option<bool>,
|
||||
cast: &InventoryStore,
|
||||
entities: &EntityStore,
|
||||
) -> LoweringInput {
|
||||
use weksa::packet::{FeatValue, FeatureValue};
|
||||
let inv = cast.inventory().unwrap();
|
||||
let mut packet = proof_packet();
|
||||
packet.stance.features = restraint
|
||||
.map(|v| FeatureValue {
|
||||
feature: feature(&inv, "restraint"),
|
||||
value: FeatValue::Grade(Grade::new(v).unwrap()),
|
||||
})
|
||||
.into_iter()
|
||||
.collect();
|
||||
packet.discourse.features = irony
|
||||
.map(|v| FeatureValue {
|
||||
feature: feature(&inv, "irony"),
|
||||
value: FeatValue::Flag(v),
|
||||
})
|
||||
.into_iter()
|
||||
.collect();
|
||||
admitted(packet, cast, "corpus/corpus.cc", entities)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn select_reads_stance_and_discourse_of_its_language() {
|
||||
use weksa::packet::FeatValue;
|
||||
let cast = InventoryStore::open(&data("interlingua/inventory.cc")).unwrap();
|
||||
let entities = EntityStore::open(&data("entity/entity.cc")).unwrap();
|
||||
let inv = cast.inventory().unwrap();
|
||||
let ontology = ontology_of(&cast, vec![
|
||||
entry("ja-JP", 1, vec![at_least(&inv, "restraint", 600)], "high"),
|
||||
entry(
|
||||
"ja-JP",
|
||||
2,
|
||||
vec![Condition {
|
||||
slot: weksa::Slot::Stance,
|
||||
feature: feature(&inv, "restraint"),
|
||||
test: Test::AtMost(Grade::new(300).unwrap()),
|
||||
}],
|
||||
"low",
|
||||
),
|
||||
entry(
|
||||
"ja-JP",
|
||||
3,
|
||||
vec![Condition {
|
||||
slot: weksa::Slot::Discourse,
|
||||
feature: feature(&inv, "irony"),
|
||||
test: Test::Is(FeatValue::Flag(true)),
|
||||
}],
|
||||
"ironic",
|
||||
),
|
||||
entry("ja-JP", 4, vec![], "always"),
|
||||
entry("en-US", 1, vec![], "other language"),
|
||||
]);
|
||||
let keys = |view: &LoweringInput, language: &str| -> Vec<String> {
|
||||
select(&ontology, language, view).iter().map(|e| e.key()).collect()
|
||||
};
|
||||
let both = view_of(Some(700), Some(true), &cast, &entities);
|
||||
assert_eq!(keys(&both, "ja-JP"), ["onto:ja-JP:1", "onto:ja-JP:3", "onto:ja-JP:4"]);
|
||||
assert_eq!(keys(&both, "en-US"), ["onto:en-US:1"]);
|
||||
let low = view_of(Some(200), Some(false), &cast, &entities);
|
||||
assert_eq!(keys(&low, "ja-JP"), ["onto:ja-JP:2", "onto:ja-JP:4"]);
|
||||
// An absent feature meets no condition, however lax.
|
||||
let bare = view_of(None, None, &cast, &entities);
|
||||
assert_eq!(keys(&bare, "ja-JP"), ["onto:ja-JP:4"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lowering_prompt_prints_only_selected_entries() {
|
||||
let cast = InventoryStore::open(&data("interlingua/inventory.cc")).unwrap();
|
||||
let cast_entities = EntityStore::open(&data("entity/entity.cc")).unwrap();
|
||||
let inv = cast.inventory().unwrap();
|
||||
let mut hit = entry("ja-JP", 1, vec![at_least(&inv, "restraint", 600)], "HITCHOICE");
|
||||
hit.examples = vec!["HITEXAMPLE".into()];
|
||||
let ontology = ontology_of(&cast, vec![
|
||||
hit,
|
||||
entry("ja-JP", 2, vec![at_least(&inv, "restraint", 900)], "MISSCHOICE"),
|
||||
entry("en-US", 1, vec![], "ENGLISHCHOICE"),
|
||||
]);
|
||||
let held = Held(vec![nibu(&[("ja-JP", &["ニブ"]), ("en-US", &["Nibu"])])]);
|
||||
let item = CorpusItemId::parse("item:nibu:1").unwrap();
|
||||
let view = view_of(Some(700), None, &cast, &cast_entities);
|
||||
let lowered = |view: &LoweringInput, o: &weksa::Ontology| {
|
||||
Prompt::lower(view, &item, "ja-JP", &inv, &held, o).unwrap()
|
||||
};
|
||||
let prompt = lowered(&view, &ontology);
|
||||
let text = prompt.text();
|
||||
assert!(text.contains("\n\nontology:\nonto:ja-JP:1 [discourse] HITCHOICE | e.g. HITEXAMPLE\n\n"), "{text}");
|
||||
for absent in ["MISSCHOICE", "ENGLISHCHOICE", "onto:ja-JP:2", "onto:en-US:1"] {
|
||||
assert!(!text.contains(absent), "{absent}");
|
||||
}
|
||||
// The same view with no entries: the section reads none.
|
||||
let none = lowered(&view, &weksa::Ontology::empty());
|
||||
assert!(none.text().contains("\n\nontology: none\n\n"));
|
||||
// Entries move the prompt and nothing else of the view does.
|
||||
assert_ne!(prompt.digest(), none.digest());
|
||||
// The group is still private; the speaker's entity appears only because
|
||||
// the packet cites it on a referent.
|
||||
let held_item = CorpusStore::open(&data("corpus/corpus.cc"))
|
||||
.unwrap()
|
||||
.get(item.as_str())
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(!text.contains(held_item.group.as_str()));
|
||||
}
|
||||
}
|
||||
|
||||
/// First real rounds: five of six encoders wrote bare integers (`"concept": 7`)
|
||||
|
|
|
|||
|
|
@ -871,6 +871,16 @@ fn dao_over(edit: impl FnOnce(&Path, &Path)) -> Dao {
|
|||
|
||||
/// `dao_over` with the run opened on `panel`.
|
||||
fn dao_panel(edit: impl FnOnce(&Path, &Path), panel: &[&str]) -> Dao {
|
||||
dao_build(edit, panel, |_, _| {})
|
||||
}
|
||||
|
||||
/// `dao_panel` with `seed_ontology` (given the ontology and inventory paths)
|
||||
/// run before the run pins the ontology.
|
||||
fn dao_build(
|
||||
edit: impl FnOnce(&Path, &Path),
|
||||
panel: &[&str],
|
||||
seed_ontology: impl FnOnce(&Path, &Path),
|
||||
) -> Dao {
|
||||
let panel: Vec<String> = panel.iter().map(|t| t.to_string()).collect();
|
||||
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../data/daodejing");
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
|
@ -885,6 +895,7 @@ fn dao_panel(edit: impl FnOnce(&Path, &Path), panel: &[&str]) -> Dao {
|
|||
);
|
||||
edit(&corpus, &entities);
|
||||
let ontology = common::ontology_in(dir.path());
|
||||
seed_ontology(&ontology, &inventory);
|
||||
let root = dir.path().join("run");
|
||||
let run = RunStores::create(
|
||||
&root,
|
||||
|
|
@ -2650,6 +2661,7 @@ fn source_tripwires() {
|
|||
assert!(!signature.contains(banned), "Prompt::lower takes {banned}");
|
||||
}
|
||||
assert!(signature.contains("&LoweringInput") && signature.contains("&CorpusItemId"));
|
||||
assert!(signature.contains("&Ontology"), "the whole ontology goes in; select chooses");
|
||||
assert_eq!(count("admit_revision("), 1, "one door call");
|
||||
assert_eq!(
|
||||
count("std::fs::copy"),
|
||||
|
|
@ -4583,3 +4595,186 @@ fn ablation_reads_each_language_apart() {
|
|||
[("en-US", Train, 1), ("en-US", Validation, 2), ("ja-JP", Train, 1), ("ja-JP", Validation, 2)]
|
||||
);
|
||||
}
|
||||
|
||||
/// An en-US Dao ontology: entry 1 for a stance with affection, entry 2 for one
|
||||
/// with almost no restraint (which no Dao packet has).
|
||||
fn dao_ontology(path: &Path, inventory: &Path) {
|
||||
use weksa::packet::Grade;
|
||||
use weksa::{Condition, Layer, OntologyEntry, OntologyStore, Slot, Test};
|
||||
let inv = InventoryStore::open(inventory).unwrap();
|
||||
let feature = |gloss: &str| {
|
||||
inv.inventory()
|
||||
.unwrap()
|
||||
.entries()
|
||||
.find(|e| e.gloss == gloss)
|
||||
.unwrap()
|
||||
.id
|
||||
};
|
||||
let entry = |n, gloss: &str, test, choice: &str| OntologyEntry {
|
||||
language: "en-US".into(),
|
||||
n,
|
||||
inventory_rev: 1,
|
||||
layer: Layer::Discourse,
|
||||
when: vec![Condition {
|
||||
slot: Slot::Stance,
|
||||
feature: feature(gloss),
|
||||
test,
|
||||
}],
|
||||
choice: choice.into(),
|
||||
examples: vec![],
|
||||
annotator: weksa::Annotator::Operator,
|
||||
};
|
||||
OntologyStore::open(path)
|
||||
.unwrap()
|
||||
.admit(
|
||||
vec![
|
||||
entry(1, "affection", Test::AtLeast(Grade::new(200).unwrap()), "warm"),
|
||||
entry(2, "restraint", Test::AtMost(Grade::new(100).unwrap()), "blunt"),
|
||||
],
|
||||
&inv_store(inventory),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn inv_store(path: &Path) -> InventoryStore {
|
||||
InventoryStore::open(path).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_run_pins_its_ontology_and_refuses_drift() {
|
||||
let dao = dao_build(|_, _| {}, &["en-US"], dao_ontology);
|
||||
let r = dao.run.record();
|
||||
assert_eq!(r.ontology, std::fs::canonicalize(&dao.ontology).unwrap().to_str().unwrap());
|
||||
assert_eq!(r.ontology_digest, hex(&dao.ontology));
|
||||
let root = dao.root.clone();
|
||||
assert_eq!(RunStores::open(&root).unwrap().record(), dao.run.record());
|
||||
// Another entry after the pin is another ontology.
|
||||
let entry = weksa::OntologyStore::open(&dao.ontology).unwrap().all().unwrap().remove(0);
|
||||
let mut store = weksa::OntologyStore::open(&dao.ontology).unwrap();
|
||||
store
|
||||
.admit(
|
||||
vec![weksa::OntologyEntry { n: 3, ..entry }],
|
||||
&inv_store(&dao.inventory),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
RunStores::open(&root).err(),
|
||||
Some(RunError::Drift(DriftOf::Ontology))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_refuses_an_entry_its_inventory_cannot_read() {
|
||||
use weksa::packet::Grade;
|
||||
use weksa::{Condition, Layer, OntologyEntry, OntologyStore, Slot, Test};
|
||||
// The entry reads feature 18 at revision 1; the run's head inventory is
|
||||
// another file where that feature is retired or absent, so the entry is
|
||||
// admitted against a richer inventory and refused against the run's.
|
||||
let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../data/daodejing");
|
||||
let make = |language: &str, dir: &Path| {
|
||||
for (from, to) in [("corpus.cc", "c.cc"), ("inventory.cc", "i.cc"), ("entity.cc", "e.cc")] {
|
||||
std::fs::copy(src.join(from), dir.join(to)).unwrap();
|
||||
}
|
||||
let ontology = dir.join("o.cc");
|
||||
// A richer inventory the entry is admitted against: the Dao one plus a feature.
|
||||
let rich = dir.join("rich.cc");
|
||||
std::fs::copy(src.join("inventory.cc"), &rich).unwrap();
|
||||
let mut store = InventoryStore::open(&rich).unwrap();
|
||||
let inv = store.inventory().unwrap();
|
||||
let head = inv.latest().unwrap().n;
|
||||
let n = head + 1;
|
||||
let next = std::num::NonZeroU32::new(
|
||||
inv.entries().filter(|e| e.id.family() == weksa::Family::Feature).map(|e| e.id.n().get()).max().unwrap() + 1,
|
||||
)
|
||||
.unwrap();
|
||||
let id = weksa::Family::Feature.id(next);
|
||||
store
|
||||
.admit_revision(
|
||||
weksa::InventoryRevision {
|
||||
n,
|
||||
parent: Some(head),
|
||||
changes: vec![weksa::Change::Add(id)],
|
||||
frozen: false,
|
||||
},
|
||||
vec![weksa::InventoryEntry {
|
||||
id,
|
||||
gloss: "pride".into(),
|
||||
value_kind: Some(weksa::ValueKind::Grade),
|
||||
applies_to: vec![Slot::Stance],
|
||||
introduced_rev: n,
|
||||
retired_rev: None,
|
||||
}],
|
||||
)
|
||||
.unwrap();
|
||||
OntologyStore::open(&ontology)
|
||||
.unwrap()
|
||||
.admit(
|
||||
vec![OntologyEntry {
|
||||
language: language.into(),
|
||||
n: 1,
|
||||
inventory_rev: n,
|
||||
layer: Layer::Lexicon,
|
||||
when: vec![Condition {
|
||||
slot: Slot::Stance,
|
||||
feature: id,
|
||||
test: Test::AtLeast(Grade::new(1).unwrap()),
|
||||
}],
|
||||
choice: "boast".into(),
|
||||
examples: vec![],
|
||||
annotator: weksa::Annotator::Operator,
|
||||
}],
|
||||
&store,
|
||||
)
|
||||
.unwrap();
|
||||
ontology
|
||||
};
|
||||
let create = |dir: &Path, ontology: &Path| {
|
||||
RunStores::create(
|
||||
&dir.join("run"),
|
||||
"x",
|
||||
RunPaths {
|
||||
corpus: &dir.join("c.cc"),
|
||||
inventory: &dir.join("i.cc"),
|
||||
entities: &dir.join("e.cc"),
|
||||
ontology,
|
||||
},
|
||||
"fake-model",
|
||||
"bag",
|
||||
weksa::run::LowererReach::ToolLessRelay,
|
||||
ModelFamily::Unrecorded,
|
||||
&["en-US".to_string()],
|
||||
)
|
||||
};
|
||||
let panel_dir = tempfile::tempdir().unwrap();
|
||||
let o = make("en-US", panel_dir.path());
|
||||
assert_eq!(create(panel_dir.path(), &o).err(), Some(RunError::Ontology));
|
||||
assert!(!panel_dir.path().join("run/run.cc").exists());
|
||||
assert!(!panel_dir.path().join("run/inventory.cc").exists());
|
||||
// The same entry under a language outside the panel does not refuse.
|
||||
let other_dir = tempfile::tempdir().unwrap();
|
||||
let o = make("ja-JP", other_dir.path());
|
||||
assert!(create(other_dir.path(), &o).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn round_lowering_prompts_carry_selected_entries() {
|
||||
let mut dao = dao_build(|_, _| {}, &["en-US"], dao_ontology);
|
||||
let mut model = fake(&good_answers());
|
||||
let r = recorded(round(&mut dao.run, &mut model, &Scorer::new(Bag)).unwrap());
|
||||
let mut lowerings = 0;
|
||||
for ((_, text), call) in model.seen.iter().zip(r.calls()) {
|
||||
if !text.starts_with(LOWER_INSTRUCTIONS) {
|
||||
continue;
|
||||
}
|
||||
lowerings += 1;
|
||||
let item = call.items[0].as_str();
|
||||
// Only the third Dao packet carries affection.
|
||||
if item == "item:daodejing:3" {
|
||||
assert!(text.contains("\nonto:en-US:1 [discourse] warm\n"), "{text}");
|
||||
} else {
|
||||
assert!(text.contains("\n\nontology: none\n\n"), "{item}: {text}");
|
||||
}
|
||||
assert!(!text.contains("onto:en-US:2") && !text.contains("blunt"));
|
||||
}
|
||||
assert_eq!(lowerings, 3);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1628,3 +1628,169 @@ fn a_public_domain_source_without_an_edition_year_is_refused() {
|
|||
.add(item_in(key, "line", "scene-1", source(1891)), &ents(&d))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
mod ontology {
|
||||
use super::*;
|
||||
use weksa::packet::{FeatValue, Grade};
|
||||
use weksa::speaker::Annotator;
|
||||
use weksa::{Condition, Layer, OntologyEntry, OntologyStore, Test};
|
||||
|
||||
fn feature(gloss: &str) -> weksa::InvId {
|
||||
seed().1.into_iter().find(|e| e.gloss == gloss).unwrap().id
|
||||
}
|
||||
|
||||
fn when(slot: Slot, gloss: &str, test: Test) -> Condition {
|
||||
Condition {
|
||||
slot,
|
||||
feature: feature(gloss),
|
||||
test,
|
||||
}
|
||||
}
|
||||
|
||||
fn entry(language: &str, n: u32) -> OntologyEntry {
|
||||
OntologyEntry {
|
||||
language: language.into(),
|
||||
n,
|
||||
inventory_rev: 1,
|
||||
layer: Layer::Lexicon,
|
||||
when: vec![when(
|
||||
Slot::Stance,
|
||||
"restraint",
|
||||
Test::AtLeast(Grade::new(600).unwrap()),
|
||||
)],
|
||||
choice: "prefer the humble form".into(),
|
||||
examples: vec!["example one".into()],
|
||||
annotator: Annotator::Operator,
|
||||
}
|
||||
}
|
||||
|
||||
fn store(d: &TempDir) -> OntologyStore {
|
||||
OntologyStore::open(&d.path().join("o.cc")).unwrap()
|
||||
}
|
||||
|
||||
fn refused(r: Result<AddOutcome, StoreError>) -> StoreRefusal {
|
||||
refusal(r)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ontology_admit_refuses_each_rule() {
|
||||
let d = dir();
|
||||
let w = world();
|
||||
let mut s = store(&d);
|
||||
let long = |n| "x".repeat(n);
|
||||
let mut cases: Vec<(&str, Vec<OntologyEntry>)> = Vec::new();
|
||||
cases.push(("non-tag language", vec![entry("not a tag", 1)]));
|
||||
cases.push(("n not latest+1", vec![entry("ja-JP", 2)]));
|
||||
cases.push((
|
||||
"65th entry",
|
||||
(1..=65).map(|n| entry("ja-JP", n)).collect(),
|
||||
));
|
||||
for (why, choice) in [("empty choice", String::new()), ("201-byte choice", long(201))] {
|
||||
cases.push((why, vec![OntologyEntry { choice, ..entry("ja-JP", 1) }]));
|
||||
}
|
||||
cases.push((
|
||||
"line break in an example",
|
||||
vec![OntologyEntry { examples: vec!["a\nb".into()], ..entry("ja-JP", 1) }],
|
||||
));
|
||||
cases.push((
|
||||
"121-byte example",
|
||||
vec![OntologyEntry { examples: vec![long(121)], ..entry("ja-JP", 1) }],
|
||||
));
|
||||
cases.push((
|
||||
"four examples",
|
||||
vec![OntologyEntry { examples: vec!["a".into(); 4], ..entry("ja-JP", 1) }],
|
||||
));
|
||||
let restraint = || when(Slot::Stance, "restraint", Test::AtLeast(Grade::new(1).unwrap()));
|
||||
cases.push((
|
||||
"five conditions",
|
||||
vec![OntologyEntry { when: (0..5).map(|_| restraint()).collect(), ..entry("ja-JP", 1) }],
|
||||
));
|
||||
for slot in [Slot::Speaker, Slot::Scene, Slot::Predication] {
|
||||
cases.push((
|
||||
"slot not Stance or Discourse",
|
||||
vec![OntologyEntry {
|
||||
when: vec![when(slot, "restraint", Test::AtLeast(Grade::new(1).unwrap()))],
|
||||
..entry("ja-JP", 1)
|
||||
}],
|
||||
));
|
||||
}
|
||||
cases.push((
|
||||
"AtLeast on an option feature",
|
||||
vec![OntologyEntry {
|
||||
when: vec![when(Slot::Discourse, "irony", Test::AtLeast(Grade::new(1).unwrap()))],
|
||||
..entry("ja-JP", 1)
|
||||
}],
|
||||
));
|
||||
cases.push((
|
||||
"Is with a grade",
|
||||
vec![OntologyEntry {
|
||||
when: vec![when(Slot::Stance, "restraint", Test::Is(FeatValue::Grade(Grade::new(1).unwrap())))],
|
||||
..entry("ja-JP", 1)
|
||||
}],
|
||||
));
|
||||
cases.push((
|
||||
"unknown feature",
|
||||
vec![OntologyEntry {
|
||||
when: vec![Condition {
|
||||
slot: Slot::Stance,
|
||||
feature: "feature:9999".parse().unwrap(),
|
||||
test: Test::AtLeast(Grade::new(1).unwrap()),
|
||||
}],
|
||||
..entry("ja-JP", 1)
|
||||
}],
|
||||
));
|
||||
cases.push((
|
||||
"unknown inventory revision",
|
||||
vec![OntologyEntry { inventory_rev: 99, ..entry("ja-JP", 1) }],
|
||||
));
|
||||
cases.push((
|
||||
"model generator that is not an attestation",
|
||||
vec![OntologyEntry {
|
||||
annotator: Annotator::Model {
|
||||
family: ModelFamily::Anthropic,
|
||||
generator: "--".into(),
|
||||
},
|
||||
..entry("ja-JP", 1)
|
||||
}],
|
||||
));
|
||||
for (why, batch) in cases {
|
||||
assert!(
|
||||
s.admit(batch, &w.inventory).is_err(),
|
||||
"{why} must be refused"
|
||||
);
|
||||
assert!(s.all().unwrap().is_empty(), "{why} wrote something");
|
||||
}
|
||||
// The refusal of a bad choice names the key and no text.
|
||||
let secret = OntologyEntry { choice: "SECRETCHOICE\n".into(), ..entry("ja-JP", 1) };
|
||||
let shown = refused(s.admit(vec![secret], &w.inventory)).to_string();
|
||||
assert!(!shown.contains("SECRETCHOICE"), "{shown}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ontology_admit_is_all_or_nothing() {
|
||||
let d = dir();
|
||||
let w = world();
|
||||
let mut s = store(&d);
|
||||
let bad = OntologyEntry { choice: String::new(), ..entry("ja-JP", 3) };
|
||||
assert!(s.admit(vec![entry("ja-JP", 1), entry("ja-JP", 2), bad], &w.inventory).is_err());
|
||||
assert!(s.all().unwrap().is_empty());
|
||||
|
||||
let batch = vec![entry("ja-JP", 1), entry("en-US", 1), entry("ja-JP", 2), entry("en-US", 2)];
|
||||
assert_eq!(s.admit(batch.clone(), &w.inventory).unwrap(), AddOutcome::Added);
|
||||
assert_eq!(s.all().unwrap().len(), 4);
|
||||
// An identical replay, from a reopened store, changes nothing.
|
||||
let mut again = store(&d);
|
||||
assert_eq!(again.admit(batch, &w.inventory).unwrap(), AddOutcome::Unchanged);
|
||||
// Each language numbers from its own latest.
|
||||
assert_eq!(
|
||||
again
|
||||
.admit(vec![entry("ja-JP", 3), entry("en-US", 3)], &w.inventory)
|
||||
.unwrap(),
|
||||
AddOutcome::Added
|
||||
);
|
||||
// An entry never changes in place.
|
||||
let changed = OntologyEntry { choice: "something else".into(), ..entry("ja-JP", 1) };
|
||||
assert!(again.admit(vec![changed], &w.inventory).is_err());
|
||||
assert_eq!(again.all().unwrap().len(), 6);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue