Thing 2.0 cut 7a h2: delete Core::drafts_mut, Commands own the one drafts table (drafts-mut-second-writer)
Tests write drafts only through real change inputs and read them through the invocations they capture. Core tests that needed a text draft use what the node is bound to: Core has no text-edit route yet. Steward decision 1 of the h2 fix brief. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
26501b0340
commit
2d4f4e0680
2 changed files with 127 additions and 60 deletions
|
|
@ -195,12 +195,6 @@ impl Core {
|
|||
self.shown.as_ref().map(|shown| self.commands.view(shown.key.as_str())).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The one table of uncommitted input values. The pointer and keyboard routes write it from
|
||||
/// inside `frame`; until they land a host or a test writes here.
|
||||
pub fn drafts_mut(&mut self) -> &mut Drafts {
|
||||
&mut self.drafts
|
||||
}
|
||||
|
||||
/// Drop the drafts of nodes the shown surface no longer has.
|
||||
fn retain_drafts(&mut self) {
|
||||
let Some(shown) = &self.shown else { return };
|
||||
|
|
|
|||
|
|
@ -355,12 +355,7 @@ fn f04_form_captures_in_declared_order_and_clears_exactly_those_drafts() {
|
|||
let form = set.surfaces[0].clone();
|
||||
let expected = set.invocations[0].clone();
|
||||
let mut rig = Rig::new(settings_state(false), &form);
|
||||
let mut ids = ScriptedIds::new([expected.invocation_id.as_str()]);
|
||||
|
||||
rig.act(&nid("enabled"), ActivationKind::Change(InputValue::Bool(BoolValue { value: true })), PINNED_NOW, &mut ids);
|
||||
assert_eq!(rig.commands.take_outbound(), vec![expected.clone()]);
|
||||
// The change was written to Drafts before the command fired.
|
||||
assert_eq!(rig.draft(&nid("enabled")), Some(InputValue::Bool(BoolValue { value: true })));
|
||||
// Drafts held at the press are what the press captures, and what the result clears.
|
||||
for (node, value) in [
|
||||
("volume", InputValue::Number(NumberValue { value: 0.75 })),
|
||||
("theme", InputValue::Choice(ChoiceValue { value: "dark".into() })),
|
||||
|
|
@ -368,6 +363,12 @@ fn f04_form_captures_in_declared_order_and_clears_exactly_those_drafts() {
|
|||
] {
|
||||
rig.set_draft(&nid(node), value);
|
||||
}
|
||||
let mut ids = ScriptedIds::new([expected.invocation_id.as_str()]);
|
||||
|
||||
rig.act(&nid("enabled"), ActivationKind::Change(InputValue::Bool(BoolValue { value: true })), PINNED_NOW, &mut ids);
|
||||
assert_eq!(rig.commands.take_outbound(), vec![expected.clone()]);
|
||||
// The change was written to Drafts before the command fired.
|
||||
assert_eq!(rig.draft(&nid("enabled")), Some(InputValue::Bool(BoolValue { value: true })));
|
||||
rig.set_draft(&nid("root"), text_value("not captured"));
|
||||
|
||||
rig.feed(&set.results[0], at(1));
|
||||
|
|
@ -598,11 +599,11 @@ fn an_invocation_times_out_at_exactly_issued_plus_the_timeout() {
|
|||
assert!(!rig.state_of("go").busy);
|
||||
assert_eq!(rig.readers(&invocation), 0);
|
||||
assert_eq!(rig.commands.next_deadline(), None);
|
||||
assert!(rig.tick(at(99_000)).is_empty());
|
||||
// A result after the timeout is judged, not taken.
|
||||
let late = rig.feed_direct(&completed(&invocation, 1, false, None), at(16_000));
|
||||
assert_eq!(codes(&late.diagnostics), vec![Code::ResultAfterTerminal]);
|
||||
assert_eq!(rig.state_of("go").mark, Some(Mark::TimedOut));
|
||||
assert!(rig.tick(at(99_000)).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -857,22 +858,26 @@ fn frame_at(core: &mut Core, state: &mut MemState, now: Timestamp) -> thing_core
|
|||
fn f02_through_the_frame() {
|
||||
let set = Fixture::F02LoginEffect.load();
|
||||
let login = set.surfaces[0].clone();
|
||||
let expected = set.invocations[0].clone();
|
||||
// Core has no text-edit route yet, so the username is what the node is bound to: empty.
|
||||
let mut expected = set.invocations[0].clone();
|
||||
expected.captured[0].value = text_value("");
|
||||
let mut state = MemState::from_fixture(Fixture::F02LoginEffect);
|
||||
let mut core = core();
|
||||
core.show(login.provider_id.clone(), RecordKey::new(login.record_key()));
|
||||
let first = frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
assert!(first.outbound.is_empty() && first.effects.is_empty() && first.next_wake.is_none());
|
||||
|
||||
core.drafts_mut().set(&login.record_key(), &nid("username"), text_value("ada"));
|
||||
let mut ids = ScriptedIds::new([expected.invocation_id.as_str()]);
|
||||
let act = Activation { node: nid("sign-in-button"), kind: ActivationKind::Press };
|
||||
assert!(core.activate(act, PINNED_NOW, Ports { state: &mut state, ids: &mut ids }).is_empty());
|
||||
core.activate(act, PINNED_NOW, Ports { state: &mut state, ids: &mut ids });
|
||||
|
||||
let minted = frame_at(&mut core, &mut state, at(1));
|
||||
assert_eq!(minted.outbound, vec![expected.clone()]);
|
||||
assert_eq!(minted.next_wake, Some(at(15_000)));
|
||||
assert!(core.node_states().get(&nid("sign-in-button")).busy);
|
||||
// The button is busy: a second press mints nothing and draws no id.
|
||||
let mut spare = ScriptedIds::new(["00000000-0000-4000-8000-000000000000"]);
|
||||
core.activate(Activation { node: nid("sign-in-button"), kind: ActivationKind::Press }, at(1), Ports { state: &mut state, ids: &mut spare });
|
||||
assert_eq!(spare.remaining(), 1);
|
||||
// The invocation left once.
|
||||
assert!(frame_at(&mut core, &mut state, at(2)).outbound.is_empty());
|
||||
|
||||
|
|
@ -885,12 +890,11 @@ fn f02_through_the_frame() {
|
|||
let done = frame_at(&mut core, &mut state, at(2_000));
|
||||
assert_eq!(done.effects.len(), 1);
|
||||
assert_eq!(done.next_wake, None);
|
||||
assert!(!core.node_states().get(&nid("sign-in-button")).busy);
|
||||
|
||||
// The host runs the effect, the session survives or not, and the resume comes back through Core.
|
||||
let HostEffect::OpenExternal { resume, .. } = &done.effects[0] else { panic!() };
|
||||
let mut ids = ScriptedIds::new(["55555555-5555-4555-8555-555555555555"]);
|
||||
assert!(core.resume(resume, at(3_000), Ports { state: &mut state, ids: &mut ids }).is_empty());
|
||||
core.resume(resume, at(3_000), Ports { state: &mut state, ids: &mut ids });
|
||||
let resumed = frame_at(&mut core, &mut state, at(3_001));
|
||||
assert_eq!(resumed.outbound.len(), 1);
|
||||
assert_eq!(resumed.outbound[0].command, CommandId::new("login-resume"));
|
||||
|
|
@ -905,7 +909,6 @@ fn the_frame_fires_a_timeout_once_and_leaving_the_surface_forgets_its_invocation
|
|||
let mut core = core();
|
||||
core.show(login.provider_id.clone(), RecordKey::new(login.record_key()));
|
||||
frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
core.drafts_mut().set(&login.record_key(), &nid("username"), text_value("ada"));
|
||||
let act = Activation { node: nid("sign-in-button"), kind: ActivationKind::Press };
|
||||
core.activate(act, PINNED_NOW, Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
frame_at(&mut core, &mut state, at(1));
|
||||
|
|
@ -914,7 +917,6 @@ fn the_frame_fires_a_timeout_once_and_leaving_the_surface_forgets_its_invocation
|
|||
let late = frame_at(&mut core, &mut state, at(15_000));
|
||||
assert_eq!(codes(&late.diagnostics), vec![Code::InvocationTimedOut]);
|
||||
assert_eq!(late.next_wake, None);
|
||||
assert_eq!(core.node_states().get(&nid("sign-in-button")).mark, Some(Mark::TimedOut));
|
||||
assert!(frame_at(&mut core, &mut state, at(16_000)).diagnostics.is_empty());
|
||||
|
||||
// Showing nothing forgets the surface's marks.
|
||||
|
|
@ -922,27 +924,9 @@ fn the_frame_fires_a_timeout_once_and_leaving_the_surface_forgets_its_invocation
|
|||
frame_at(&mut core, &mut state, at(17_000));
|
||||
core.show(login.provider_id.clone(), RecordKey::new(login.record_key()));
|
||||
frame_at(&mut core, &mut state, at(18_000));
|
||||
assert_eq!(core.node_states().get(&nid("sign-in-button")).mark, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_republish_drops_the_drafts_of_the_nodes_it_removed() {
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
let two_inputs = surface(PROVIDER, boxed("root", vec![input("a", InputKind::Text, "", "go"), input("b", InputKind::Text, "", "go")]), vec![decl("go", &[], None)]);
|
||||
let mut one_input = surface(PROVIDER, boxed("root", vec![input("a", InputKind::Text, "", "go")]), vec![decl("go", &[], None)]);
|
||||
one_input.version = ver(2);
|
||||
let mut state = MemState::new();
|
||||
state.put(&provider, &two_inputs);
|
||||
let mut core = core();
|
||||
core.show(provider.clone(), RecordKey::new(two_inputs.record_key()));
|
||||
frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
let key = two_inputs.record_key();
|
||||
core.drafts_mut().set(&key, &nid("a"), text_value("kept"));
|
||||
core.drafts_mut().set(&key, &nid("b"), text_value("gone"));
|
||||
state.put(&provider, &one_input);
|
||||
frame_at(&mut core, &mut state, at(1));
|
||||
assert_eq!(core.drafts_mut().get(&key, &nid("a")), Some(&text_value("kept")));
|
||||
assert_eq!(core.drafts_mut().get(&key, &nid("b")), None);
|
||||
// The timed-out invocation went with the surface: the button is free and mints again.
|
||||
core.activate(Activation { node: nid("sign-in-button"), kind: ActivationKind::Press }, at(18_001), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
assert_eq!(frame_at(&mut core, &mut state, at(18_002)).outbound.len(), 1);
|
||||
}
|
||||
|
||||
/// An invocation core never issued.
|
||||
|
|
@ -1077,24 +1061,6 @@ fn the_id_sources_give_what_they_say() {
|
|||
assert!(!drafts.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_row_through_the_frame_drops_its_draft() {
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
let document = rows_surface();
|
||||
let mut state = rows_state();
|
||||
state.put(&provider, &document);
|
||||
let mut core = core();
|
||||
core.show(provider.clone(), RecordKey::new(document.record_key()));
|
||||
frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
let key = document.record_key();
|
||||
core.drafts_mut().set(&key, &row("item-0001", "note"), text_value("stays"));
|
||||
core.drafts_mut().set(&key, &row("item-0003", "note"), text_value("goes"));
|
||||
state.delete(&Address { provider: ProviderId::new(FEED), schema: SchemaId::new("gamecult.thing.test.item.v1"), key: RecordKey::new("item-0003") });
|
||||
frame_at(&mut core, &mut state, at(1));
|
||||
assert_eq!(core.drafts_mut().get(&key, &row("item-0001", "note")), Some(&text_value("stays")));
|
||||
assert_eq!(core.drafts_mut().get(&key, &row("item-0003", "note")), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_confirmation_is_answered_through_the_core() {
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
|
|
@ -1116,3 +1082,110 @@ fn a_confirmation_is_answered_through_the_core() {
|
|||
core.confirm(true, at(4), Ports { state: &mut state, ids: &mut ids });
|
||||
assert_eq!(frame_at(&mut core, &mut state, at(5)).outbound.len(), 1);
|
||||
}
|
||||
|
||||
// ----- the one drafts table: only commands write it -----
|
||||
|
||||
fn bool_value(value: bool) -> InputValue {
|
||||
InputValue::Bool(BoolValue { value })
|
||||
}
|
||||
|
||||
fn number_value(value: f64) -> InputValue {
|
||||
InputValue::Number(NumberValue { value })
|
||||
}
|
||||
|
||||
fn choice_value(value: &str) -> InputValue {
|
||||
InputValue::Choice(ChoiceValue { value: value.into() })
|
||||
}
|
||||
|
||||
/// Two toggles whose change fires `flagged`, and a button whose command captures them.
|
||||
fn flags_surface(version: i64, with_b: bool) -> SurfaceDocument {
|
||||
let mut children = vec![toggle("a", "flagged", lit_bool(false))];
|
||||
if with_b {
|
||||
children.push(toggle("b", "flagged", lit_bool(false)));
|
||||
}
|
||||
children.push(button("go", "go"));
|
||||
let mut document = surface(
|
||||
PROVIDER,
|
||||
boxed("root", children),
|
||||
vec![decl("flagged", &[], None), decl("go", if with_b { &["a", "b"] } else { &["a"] }, None)],
|
||||
);
|
||||
document.version = ver(version);
|
||||
document
|
||||
}
|
||||
|
||||
fn change(node: &str, value: InputValue) -> Activation {
|
||||
Activation { node: nid(node), kind: ActivationKind::Change(value) }
|
||||
}
|
||||
|
||||
fn press(node: &str) -> Activation {
|
||||
Activation { node: nid(node), kind: ActivationKind::Press }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_republish_drops_the_drafts_of_the_nodes_it_removed_and_keeps_the_rest() {
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
let mut state = MemState::new();
|
||||
state.put(&provider, &flags_surface(1, true));
|
||||
let mut core = core();
|
||||
core.show(provider.clone(), RecordKey::new(flags_surface(1, true).record_key()));
|
||||
frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
// The only way into the table is a change input.
|
||||
core.activate(change("a", bool_value(true)), at(1), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
core.activate(change("b", bool_value(true)), at(1), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
// b leaves in a republish and comes back in the next: its old draft must not come back with it.
|
||||
state.put(&provider, &flags_surface(2, false));
|
||||
frame_at(&mut core, &mut state, at(2));
|
||||
state.put(&provider, &flags_surface(3, true));
|
||||
frame_at(&mut core, &mut state, at(3));
|
||||
core.activate(press("go"), at(4), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
let go = frame_at(&mut core, &mut state, at(5)).outbound.pop().expect("go minted");
|
||||
let captured: Vec<_> = go.captured.iter().map(|input| input.value.clone()).collect();
|
||||
assert_eq!(captured, vec![bool_value(true), bool_value(false)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_row_through_the_frame_drops_its_draft() {
|
||||
let flag = Node::Toggle(ToggleNode { common: common("flag"), checked: lit_bool(false), on_change: command("flagged") });
|
||||
let row_node = Node::Box(BoxNode {
|
||||
common: common("row"),
|
||||
style: style(),
|
||||
paint: plain_paint(),
|
||||
role: Role::ListItem,
|
||||
children: vec![flag, button("save", "save")],
|
||||
});
|
||||
let rows = Node::Repeat(RepeatNode {
|
||||
common: common("rows"),
|
||||
collection: CollectionSource::Records(RecordsSource {
|
||||
provider: Some(ProviderId::new(FEED)),
|
||||
schema: SchemaId::new("gamecult.thing.test.item.v1"),
|
||||
key_prefix: "item-".into(),
|
||||
}),
|
||||
key_member: path(&["Id"]),
|
||||
template: std::boxed::Box::new(row_node),
|
||||
});
|
||||
let document = surface(PROVIDER, boxed("root", vec![rows]), vec![decl("flagged", &[], None), decl("save", &["flag"], None)]);
|
||||
let provider = ProviderId::new(PROVIDER);
|
||||
let mut state = rows_state();
|
||||
state.put(&provider, &document);
|
||||
let mut core = core();
|
||||
core.show(provider.clone(), RecordKey::new(document.record_key()));
|
||||
frame_at(&mut core, &mut state, PINNED_NOW);
|
||||
core.activate(change(row("item-0001", "flag").as_str(), bool_value(true)), at(1), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
core.activate(change(row("item-0003", "flag").as_str(), bool_value(true)), at(1), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
let feed = ProviderId::new(FEED);
|
||||
state.delete(&Address { provider: feed.clone(), schema: SchemaId::new("gamecult.thing.test.item.v1"), key: RecordKey::new("item-0003") });
|
||||
frame_at(&mut core, &mut state, at(2));
|
||||
state.put_state(&feed, "item-0003", &Item { id: "item-0003".into(), title: "back".into() });
|
||||
frame_at(&mut core, &mut state, at(3));
|
||||
for item in ["item-0001", "item-0003"] {
|
||||
core.activate(press(row(item, "save").as_str()), at(4), Ports { state: &mut state, ids: &mut SeqIds::new() });
|
||||
}
|
||||
let saved = frame_at(&mut core, &mut state, at(5)).outbound;
|
||||
assert_eq!(saved.len(), 2);
|
||||
let flags: Vec<_> = saved.iter().map(|invocation| (invocation.items.clone(), invocation.captured[0].value.clone())).collect();
|
||||
assert_eq!(
|
||||
flags,
|
||||
vec![(vec!["item-0001".to_string()], bool_value(true)), (vec!["item-0003".to_string()], bool_value(false))]
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue