Files
improvise/crates/improvise-wasm-client/tests/browser_round_trip.rs
T
Edward Langley 7e6cac0885 fix(protocol): a key can ask its host to save (improvise-jz6w)
`ClientFrame::Save` landed with improvise-368h and `Host::handle` has answered
it ever since, but `KeyOutcome` was `Local | Upstream | Ignored`, so no
resolver could say "send this frame". `save` therefore sat on
`the_command_names_that_still_need_the_host_are_these`, resolved to `Upstream`,
and `Host::handle` answers `Upstream` with an empty vec — gotcha 47's black
hole. An implemented server feature that no key could reach.

`KeyOutcome::Request(Request)` closes it, with `Request::Save` the one member
and `Request::frame` the one mapping to an envelope. The variant carries a
narrow enum rather than a whole `ClientFrame` on purpose: a resolver handing
out arbitrary frames could compose a `Viewport` (the page's fact, not the
keymap's) or a `Commands` batch, which would be indistinguishable on the wire
from a `Local` while skipping the local fold `Local`'s contract requires.
`requested` is checked first in `step`, so no name is in two lists, and a
request cannot travel inside a `Binding::Sequence` — a sequence is one frame.

`save` leaves the host-needed list in the same commit, as gotcha 47 requires.

The guard is end to end, in `browser_round_trip.rs`: Ctrl-S on a real `Client`
yields `ClientFrame::Save`, and a real `Host` over a real `App` writes the
document to storage. Two half-tests that never meet is exactly what left this
gap open. Reasoning in `context/code/clients.md`; evidence and the injected
rivals in `context/test-evidence/clients.md`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FC2jVn8Au7E9JPkw6bwXXH
2026-09-22 13:52:36 -07:00

563 lines
20 KiB
Rust

//! improvise-1ey's stated scope, asserted end to end without a browser: a real
//! `Host` and a real `Client`. See `context/test-evidence/clients.md`.
use improvise_command::ui::app::App;
use improvise_core::model::cell::{CellKey, CellValue};
use improvise_core::view::Axis;
use improvise_core::workbook::Workbook;
use improvise_protocol::ClientFrame;
use improvise_wasm_client::render::RenderRow;
use improvise_wasm_client::{Client, RenderModel};
use improvise_ws_server::host::{Addressed, Host};
/// The document the server opens: two regions by two measures, one cell
/// already filled so the client has something to read before it writes.
fn host() -> Host {
let mut wb = Workbook::new("Test");
wb.add_category("Region").unwrap();
wb.add_item("Region", "North").unwrap();
wb.add_item("Region", "South").unwrap();
wb.add_item("_Measure", "Revenue").unwrap();
wb.add_item("_Measure", "Cost").unwrap();
wb.active_view_mut().set_axis("Region", Axis::Row);
wb.active_view_mut().set_axis("_Measure", Axis::Column);
wb.model.set_cell(
CellKey::new(vec![
("Region".into(), "North".into()),
("_Measure".into(), "Revenue".into()),
]),
CellValue::Number(1200.0),
);
let mut app = App::new(wb, None);
app.rebuild_layout();
Host::new(app)
}
/// The page's own loop, minus the DOM: every frame the host addressed to
/// this session, in order, through the one call that folds them.
fn deliver(client: &mut Client, frames: &[Addressed]) {
for Addressed { msg, .. } in frames {
client
.on_frame(&serde_json::to_string(msg).expect("a server frame serializes"))
.expect("the client must fold every frame the host sends");
}
}
/// The one frame the page sends that is not a command: the handshake.
fn send(
host: &mut Host,
id: improvise_command::ui::project::SessionId,
json: &str,
) -> Vec<Addressed> {
let msg: ClientFrame =
serde_json::from_str(json).expect("the server must be able to read what the page sends");
host.handle(id, msg)
}
/// Where the first settable cell is, in **both** coordinate systems — which
/// are two numbers because they differ. See `context/test-evidence/clients.md`.
struct FirstEditable {
/// Indexes `RenderModel::rows`.
visual_row: usize,
/// Indexes the grid the server projects — what a command names.
data_row: usize,
col: usize,
}
/// Every settable cell the model offers, first per row, in both systems.
fn editable_cells(model: &RenderModel) -> Vec<FirstEditable> {
let mut data_row = 0usize;
let mut found = Vec::new();
for (visual_row, row) in model.rows.iter().enumerate() {
let RenderRow::Data { cells, .. } = row else {
continue;
};
if let Some(col) = cells.iter().position(|cell| cell.editable) {
found.push(FirstEditable {
visual_row,
data_row,
col,
});
}
data_row += 1;
}
found
}
fn first_editable(model: &RenderModel) -> FirstEditable {
editable_cells(model)
.into_iter()
.next()
.expect("a pivot of raw data must offer at least one settable cell")
}
#[test]
fn a_browser_client_seeds_types_a_number_and_sees_it_come_back() {
let mut host = host();
let mut client = Client::new();
// The handshake. The extent comes first because the snapshot seeds the
// *visible* cells: one built at extent zero is an empty grid.
let handshake: ClientFrame = serde_json::from_str(&client.set_viewport(10, 10))
.expect("the viewport frame must be one the server reads");
let ClientFrame::Viewport { rows, cols } = handshake else {
panic!("set_viewport must produce a Viewport frame");
};
let (id, snapshot) = host.connect(rows, cols);
deliver(&mut client, &[snapshot]);
// ── what the page draws ─────────────────────────────────────────────
let model = client.model();
assert_eq!(
model
.rows
.iter()
.flat_map(|r| r.labels())
.collect::<Vec<_>>(),
vec!["North", "South"],
"the seed must carry the row labels framing the cells"
);
assert_eq!(model.col_levels[0], vec!["Revenue", "Cost"]);
assert_eq!(
model.rows[0].cells()[0].text,
"1,200.00",
"the value arrives *formatted* — the separator is the server's view \
format, and a client that re-derived the text would not have it"
);
assert!(model.rows[0].cells()[0].numeric);
// ── typing a number ─────────────────────────────────────────────────
let at = first_editable(&model);
assert_eq!(
(at.data_row, at.visual_row, at.col),
(0, 0, 0),
"with no category on the None axis every cell is a stored cell, and \
this fixture has no group headers, so the two row coordinates \
coincide here — see a_grouped_row_axis_does_not_shift_the_write"
);
let frame = client
.typed_cell(at.data_row, at.col, "7500")
.expect("serialization must not fail")
.expect("an editable cell must compose a write");
let frames = send(&mut host, id, &frame);
// ── the server value updated ────────────────────────────────────────
assert_eq!(
host.app()
.model_state
.workbook
.model
.get_cell(&CellKey::new(vec![
("Region".into(), "North".into()),
("_Measure".into(), "Revenue".into()),
])),
Some(&CellValue::Number(7500.0)),
"the authoritative document is the thing that must have moved"
);
// ── and the projection arrives with the new value ───────────────────
assert!(
!frames.is_empty(),
"a cell write owes this client a grid projection"
);
let before = client.revision();
deliver(&mut client, &frames);
assert!(
client.revision() > before,
"folding the fan-out must move the client"
);
assert_eq!(
client.model().rows[at.visual_row].cells()[at.col].text,
"7,500.00",
"the page redraws from the server's formatting, never from the \
\"7500\" the user typed"
);
}
/// A group header is a row of the render model and **not** a row of the grid,
/// and confusing them writes a different record. Evidence file.
#[test]
fn a_grouped_row_axis_does_not_shift_the_write() {
let mut host = {
let mut wb = Workbook::new("Grouped");
wb.add_category("Region").unwrap();
// West first and ungrouped, so the `Coastal` header falls *between* the
// two rows — a leading header is dropped by `AxisFrame::body_from`.
wb.add_item("Region", "West").unwrap();
wb.add_item_in_group("Region", "East", "Coastal").unwrap();
wb.add_item("_Measure", "Revenue").unwrap();
wb.active_view_mut().set_axis("Region", Axis::Row);
wb.active_view_mut().set_axis("_Measure", Axis::Column);
for region in ["East", "West"] {
wb.model.set_cell(
CellKey::new(vec![
("Region".into(), region.into()),
("_Measure".into(), "Revenue".into()),
]),
CellValue::Number(1.0),
);
}
let mut app = App::new(wb, None);
app.rebuild_layout();
Host::new(app)
};
let mut client = Client::new();
let (id, snapshot) = host.connect(10, 10);
deliver(&mut client, &[snapshot]);
let model = client.model();
let at = editable_cells(&model)
.pop()
.expect("both rows are stored cells");
assert_eq!(
(at.visual_row, at.data_row),
(2, 1),
"precondition: the Coastal header sits between the two rows, so the \
last row is visual 2 and grid 1 — without this divergence the two \
coordinates coincide and nothing below is being tested"
);
let frame = client
.typed_cell(at.data_row, at.col, "4200")
.expect("serialization must not fail")
.expect("an editable cell must compose a write");
send(&mut host, id, &frame);
assert_eq!(
host.app()
.model_state
.workbook
.model
.get_cell(&CellKey::new(vec![
("Region".into(), "East".into()),
("_Measure".into(), "Revenue".into()),
])),
Some(&CellValue::Number(4200.0)),
"the write must land on grid row 1, East"
);
assert_eq!(
host.app()
.model_state
.workbook
.model
.get_cell(&CellKey::new(vec![
("Region".into(), "West".into()),
("_Measure".into(), "Revenue".into()),
])),
Some(&CellValue::Number(1.0)),
"and must not have shifted: passing the VISUAL index writes nothing \
at all here (row 2 is off the grid), and in a document with one \
more row it would silently write the wrong one"
);
}
/// The other half of ADR 002, end to end: an aggregate renders non-editable,
/// so the page never offers an editor `SetCell` could not be composed for.
#[test]
fn an_aggregated_cell_reaches_the_page_as_not_editable() {
let mut host = {
let mut wb = Workbook::new("Test");
wb.add_category("Region").unwrap();
wb.add_item("Region", "North").unwrap();
// A regular category left on the None axis is what makes a cell an
// aggregate — the same question `EditOrDrill` asks in the TUI.
wb.add_category("Product").unwrap();
wb.add_item("Product", "Shirts").unwrap();
wb.add_item("Product", "Hats").unwrap();
wb.add_item("_Measure", "Revenue").unwrap();
wb.active_view_mut().set_axis("Region", Axis::Row);
wb.active_view_mut().set_axis("_Measure", Axis::Column);
wb.active_view_mut().set_axis("Product", Axis::None);
let mut app = App::new(wb, None);
app.rebuild_layout();
Host::new(app)
};
let mut client = Client::new();
let (_id, snapshot) = host.connect(10, 10);
deliver(&mut client, &[snapshot]);
let model = client.model();
assert!(
model
.rows
.iter()
.flat_map(|r| r.cells())
.all(|c| !c.editable),
"every cell here aggregates over Product, so none may be written"
);
assert_eq!(
client.typed_cell(0, 0, "42").unwrap(),
None,
"and typing into one composes nothing, because the id is of the wrong kind"
);
}
/// A resize is a fan-out with nothing dirty, and it is the page's job to send
/// one: no command carries the extent.
#[test]
fn a_resize_reprojects_the_grid_at_the_new_extent() {
let mut host = host();
let mut client = Client::new();
let (id, snapshot) = host.connect(1, 1);
deliver(&mut client, &[snapshot]);
assert_eq!(
client.model().rows.len(),
1,
"a one-row viewport is seeded with one row"
);
let frames = send(&mut host, id, &client.set_viewport(10, 10));
deliver(&mut client, &frames);
assert_eq!(
client.model().rows.len(),
2,
"the resize must bring the rest of the grid"
);
assert_eq!(client.model().col_levels[0], vec!["Revenue", "Cost"]);
}
/// improvise-955n: **the one real producer of a multi-command frame must still
/// be accepted** — and must land. See `context/test-evidence/clients.md`.
#[test]
fn a_commit_keypress_is_one_mutation_among_view_commands_and_is_accepted() {
use improvise_protocol::ServerFrame;
let mut host = host();
let mut client = Client::new();
let handshake: ClientFrame = serde_json::from_str(&client.set_viewport(10, 10))
.expect("the viewport frame must be one the server reads");
let ClientFrame::Viewport { rows, cols } = handshake else {
panic!("set_viewport must produce a Viewport frame");
};
let (id, snapshot) = host.connect(rows, cols);
deliver(&mut client, &[snapshot]);
// Type into the cell under the cursor the way the page does: enter edit
// mode, type, commit.
for key in ["i", "8", "8", "8"] {
if let Some(frame) = client
.on_key_event(key, false, false, false, false)
.unwrap()
{
send(&mut host, id, &frame);
}
}
let commit = client
.on_key_event("Enter", false, false, false, false)
.expect("serialization must not fail")
.expect("a commit must compose a frame");
// The shape under test: several commands, exactly one a mutation —
// asserted, or a `commit` that stopped batching would pass vacuously.
let parsed: ClientFrame = serde_json::from_str(&commit).expect("the commit frame parses");
let ClientFrame::Commands(cmds) = &parsed else {
panic!("a commit must be a Commands frame, got {parsed:?}");
};
assert!(cmds.len() > 1, "the shape under test is a batch: {cmds:?}");
// A characterization of what `keys::commit` emits, **not** a protocol
// constraint: a frame may carry more than one mutation.
assert_eq!(
cmds.iter()
.filter(|c| matches!(c, improvise_protocol::Command::Model { .. }))
.count(),
1,
"keys::commit is expected to emit exactly one mutation: {cmds:?}"
);
let answer = send(&mut host, id, &commit);
assert!(
!answer
.iter()
.any(|a| matches!(a.msg, ServerFrame::Rejected(_))),
"the batch a real keypress produces must not be refused: {answer:?}"
);
assert_eq!(
host.app()
.model_state
.workbook
.model
.get_cell(&CellKey::new(vec![
("Region".into(), "North".into()),
("_Measure".into(), "Revenue".into()),
])),
// `i` seeds the edit buffer, so typing 888 appends. Asserting the
// *appended* value is what makes this more than a coincidence.
Some(&CellValue::Number(1_200_888.0)),
"and it must actually land — 'not refused' alone would pass against \
a host that dropped the frame"
);
}
// ── the tile-bar route into records mode (improvise-bd9o) ───────────────
/// Drive one key all the way round: the client resolves it, the host answers
/// whatever it composed, and every frame comes back. Evidence file.
fn press(
host: &mut Host,
id: improvise_command::ui::project::SessionId,
client: &mut Client,
key: &str,
) {
let Some(frame) = client.key(&improvise_wasm_client::BrowserKey::new(key)) else {
return;
};
let json = serde_json::to_string(&frame).expect("a client frame serializes");
let frames = send(host, id, &json);
deliver(client, &frames);
}
/// Walk the tile cursor to a named category and put it on `axis`'s key. The
/// tile bar's own order is the host's, so it is read rather than assumed.
fn tile_to(
host: &mut Host,
id: improvise_command::ui::project::SessionId,
client: &mut Client,
category: &str,
key: &str,
) {
let at = client
.state()
.tiles
.tiles
.iter()
.position(|t| t.category == category)
.unwrap_or_else(|| panic!("the tile bar must carry {category}"));
while client.state().view.tile_cat_idx < at {
press(host, id, client, "l");
}
while client.state().view.tile_cat_idx > at {
press(host, id, client, "h");
}
press(host, id, client, key);
}
/// `toggle-records-mode` is declined and the tile bar reaches records mode
/// anyway (improvise-bd9o). Evidence file.
#[test]
fn the_tile_bar_route_into_records_mode_does_not_strand_the_user() {
let mut host = host();
let mut client = Client::new();
let handshake: ClientFrame = serde_json::from_str(&client.set_viewport(10, 10))
.expect("the viewport frame must be one the server reads");
let ClientFrame::Viewport { rows, cols } = handshake else {
panic!("set_viewport must produce a Viewport frame");
};
let (id, snapshot) = host.connect(rows, cols);
deliver(&mut client, &[snapshot]);
press(&mut host, id, &mut client, "T");
tile_to(&mut host, id, &mut client, "_Index", "r");
tile_to(&mut host, id, &mut client, "_Dim", "c");
press(&mut host, id, &mut client, "Escape");
assert!(
client.state().grid.records,
"the tile-bar route must actually reach records mode, or this test is \
not standing where the ticket stood"
);
// Every column of the row, because the silence had TWO sources and a
// fixture on one of them cannot see the other. Evidence file.
let cols = client.state().grid.extent.cols;
assert!(
cols >= 3,
"the fixture must reach past the coordinate column"
);
for col in 0..cols {
while client.state().view.cursor().selected.1 < col {
press(&mut host, id, &mut client, "l");
}
let kind = format!(
"{:?}",
client.state().grid.cell(0, col).and_then(|c| c.id.clone())
);
// A sentinel rather than a key: clearing the line must not itself be
// a keypress this client might decline.
client.state_mut().view.status_msg = "\u{0}unsaid".to_string();
press(&mut host, id, &mut client, "i");
assert_ne!(
client.state().view.status_msg,
"\u{0}unsaid",
"column {col} ({kind}): a key whose commit this client cannot \
compose must say so"
);
assert!(
!client.state().view.mode.is_editing(),
"column {col} ({kind}): the editor must not open on a cell this \
client cannot commit — an Enter that is a black hole"
);
}
}
// ── Ctrl-S, all the way to storage (improvise-jz6w) ─────────────────────
/// The same document, over `MemoryFs` and knowing where it lives, so a save
/// has somewhere to land and the test can read it back.
fn host_with_a_path() -> (Host, std::path::PathBuf) {
let path = std::path::PathBuf::from("doc.improv");
let mut host = host();
host.app_mut().model_state.file_path = Some(path.clone());
host.app_mut().storage = Box::new(improvise_command::storage::MemoryFs::new());
(host, path)
}
/// Ctrl-S, as a browser reports it.
fn ctrl_s() -> improvise_wasm_client::BrowserKey {
improvise_wasm_client::BrowserKey {
key: "s".to_string(),
ctrl: true,
alt: false,
shift: false,
meta: false,
}
}
/// improvise-jz6w: `ClientFrame::Save` existed and `Host::handle` answered it,
/// but no `KeyOutcome` could name it. See `context/test-evidence/clients.md`.
#[test]
fn ctrl_s_in_the_browser_writes_the_hosts_document() {
let (mut host, path) = host_with_a_path();
let mut client = Client::new();
let handshake: ClientFrame =
serde_json::from_str(&client.set_viewport(10, 10)).expect("the viewport frame parses");
let ClientFrame::Viewport { rows, cols } = handshake else {
panic!("set_viewport must produce a Viewport frame");
};
let (id, snapshot) = host.connect(rows, cols);
deliver(&mut client, &[snapshot]);
assert!(
host.app().storage.read(&path).is_err(),
"nothing may be on storage before the key is pressed"
);
let frame = client
.key(&ctrl_s())
.expect("Ctrl-S must produce a frame to send");
assert_eq!(
frame,
ClientFrame::Save,
"the frame must be the save REQUEST — a Key frame is the black hole \
this ticket is about, and a Commands batch is the atomicity Save is \
a frame to escape"
);
let answer = send(&mut host, id, &serde_json::to_string(&frame).unwrap());
let written = String::from_utf8(
host.app()
.storage
.read(&path)
.expect("the keypress must have reached this host's storage"),
)
.expect("the document is text");
assert!(
written.contains("North"),
"and it must be this document, got:\n{written}"
);
assert!(
!answer
.iter()
.any(|a| matches!(a.msg, improvise_protocol::ServerFrame::Rejected(_))),
"a save that landed is not a rejection: {answer:?}"
);
}