Skip to content

Adds a test guard that fails when a change of the drawing has no undo step - #3553

Merged
HakanSeven12 merged 4 commits into
HakanSeven12:mainfrom
travisjhughes:undo-record-guard
Nov 24, 2026
Merged

HakanSeven12 merged 4 commits into
HakanSeven12:mainfrom
travisjhughes:undo-record-guard

Conversation

@travisjhughes

@travisjhughes travisjhughes commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Change summary

In a test build, each handler now runs under a guard. The guard compares each open drawing before and after the handler. When the drawing changed and no undo step has the change, the test fails and names the message. This makes each of the app's 6,839 tests a test of the rule "a change of the drawing is in an undo step". Nothing changes in a release build.

This is a proposal: the guard is small, but it adds a list file that contributors must know. If you do not want it, close the pull request.

This branch contains the two commits of #3555 (an undo step for the current layer, colour, lineweight and linetype of the palette and the ribbon), because #3555 removes two names from the guard's list. Merge them in either order: after #3555, this pull request shows only the guard; if this one merges first, #3555 has nothing left. #3555 does not need the guard.

What specifically changed

  • src/app/update/undo_record_guard.rs (new, test builds only). Before a handler it keeps a copy of each tab's document (the entities are shared pointers, so the copy is cheap). After the handler it compares. A change is reported when no undo record was open before or after the handler, the undo and redo stacks have the same length, and the tab's edit revision is the same.
  • Three hooks, all #[cfg(all(test, not(target_arch = "wasm32")))]: in update (around update_inner and finish_all_pending_history), in automation_op and in control_request. The bodies of the last two moved, unchanged, to automation_op_unguarded and control_request_unguarded, so that the guard has one place after all their returns.
  • src/app/update/undo_record_guard_known.txt (new). The 60 names that change a drawing with no undo step with Records an undo step for a change of the current layer, colour, lineweight or linetype #3555, in groups with a reason: 17 by design (a drawing is made, opened or saved; a live edit that the end of the command completes; a cancelled command; a view correction), 10 with no undo step and known before (a layout becomes current; a typed command or the linetype manager sets the current layer, colour or linetype), 33 not examined (style managers, drawing variables, view and plot settings).
  • Names. A name is the message variant with the variant of its first field (Linetype(SetCurrent)), or automation:<op> or control:<op>. The command is a part of the name, because one message does different work in different commands: CommandEscape during BACTION, ScriptLine TILEMODE, automation:run TILEMODE.
  • Nested handlers. An automation request sends messages, and each one has its own guard. The innermost handler answers for a change; the handlers around it do not report it again.
  • OCS_UNDO_GUARD_LIST=<file>. With this variable the guard fails no test and writes each name to the file. A name of the list that is not in the file is no longer necessary.

Why

The undo sweep (#3453) found changes with no undo step by a list of commands that somebody wrote: a layout switch, a property change with nothing selected. The guard finds the same class with no list of commands: each handler that a test runs is checked. A new handler that changes the drawing and forgets its undo record fails the first test that runs it, with a message that says what to do.

A measurement before this pull request showed that the class is small today: 62 of the 323 names that change a drawing in the tests made no undo step in their own message on main 32e30c8b3, 17 of them by design and 12 in two known groups. #3555 repairs 2 of those 12. The guard keeps it small.

Testing

  • The guard's own tests (undo_record_guard_tests.rs, 20 tests): the name rules; the decision rule; a command with an undo step passes; a change in an undo record and a change with a record that is still open are not reported; an added entity, a modified and a removed entity, and a changed header with no record are reported, with the text of the report; the guard fails the test for such a change; a name of the list is not reported; the handler around a listed handler does not report the same change, and a second handler after it still reports its own; a handler that ended leaves no depth and no answer behind; list mode writes the name.

  • The list equals what main gives: a run with OCS_UNDO_GUARD_LIST gave 60 names on this branch, the same 60 as the file.

  • Fault injection, each fault built and run alone or shown separately:

    Fault Result
    push_undo_snapshot(i, "LAYER NEW") removed (layer_manager.rs) 2 tests fail; the guard reports LayerNew (changed: header, layers)
    commit_layer_undo removed from LayerToggleVisible (dispatch.rs) 2 tests fail with the guard's report of LayerToggleVisible
    LayoutSwitch removed from the list 51 tests fail with the guard's report of LayoutSwitch
  • cargo test --workspace --locked --no-fail-fast on main 32e30c8b3: 7,882 passed on main, 7,902 with this branch (the 20 new tests), 0 failed. cargo check, the wasm32 check and clippy give the same warnings as main; the changed and new files have no new rustfmt drift.

  • Mutation tests (cargo mutants --in-diff, 105 mutants): 87 caught, 12 not viable, 6 missed. Three tests were added for four of the missed ones (a modified entity, the guard's panic, the depth after a handler); each of the four, applied by hand, now fails one test. The other two are in list_file (see Not covered).

  • Time: no difference that I can measure. On 81f499f79 the app tests took 22.3 s without the guard and 21.7 s to 23.6 s with it (several runs, macOS, Apple Silicon).

Not covered

  • A handler that no test runs.
  • An undo record that a later message completes from a kept image. Such a handler changes the drawing with no record in its own message, so its name is in the list ("not examined"); the guard cannot tell it from a fault.
  • A record that opens after a part of the changes of one message.
  • The 33 names of the group "not examined": nobody read these handlers. None of them changes an entity; they change tables, drawing variables, view state or plot settings.
  • list_file, which reads OCS_UNDO_GUARD_LIST, has no test: a test cannot set the variable for the process and leave the other tests as they are. The logic behind it (check with a list file) has one.
  • State that is not in the document: the selection, the visibility states, the parametric constraints of the scene.
  • A change together with a plot: src/app/update/file/plot.rs also changes the edit revision, and the guard reads that as a history step.
  • The integration tests in tests/: they use the library without cfg(test), so the guard is not in them.
  • A name of the list that no test gives any more is not found by a test; the OCS_UNDO_GUARD_LIST run finds it.

Impact to Contributors

  • A test can now fail with "<name> changed a drawing and made no undo step". The repair is an undo record in the handler (push_undo_snapshot before the change). If the change is not an undo step by design, add the name to undo_record_guard_known.txt with the reason.
  • When you give a listed handler its undo step, remove its name from the list.
  • In a test build, an entity that a handler changes is copied first (the guard's copy shares it). The result is the same; only the allocation differs.

Co-Authored-By: Claude

…eight or linetype

With nothing selected, the ribbon and the Properties palette set the
current value of the drawing and added no undo step. UNDO then kept the
new value and took back the step before it.

The four functions now call push_undo_snapshot before the change. The
lineweight message of the palette calls on_ribbon_lineweight_changed, as
the other three palette messages call their functions.

Co-Authored-By: Claude
8 cases in the undo sweep: the current layer, colour, lineweight and
linetype, from the Properties palette and from the ribbon, with nothing
selected.

Co-Authored-By: Claude
… step

In a test build, update, automation_op and control_request compare each
drawing before and after the handler. A drawing that changed with no undo
record fails the test, with the name of the message. The 60 names that
do this on main are in undo_record_guard_known.txt, with reasons. A
release build has no change.

Co-Authored-By: Claude
The name rules, the decision rule, a change with and without an undo
record, a name of the list, nested handlers and list mode.

Co-Authored-By: Claude
@HakanSeven12
HakanSeven12 merged commit bd755aa into HakanSeven12:main Nov 24, 2026
8 checks passed
HakanSeven12 pushed a commit that referenced this pull request Nov 24, 2026
…edit

#3553 and #3565 together: the save at close or quit writes the drawing's
header, viewports and objects the way any save does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013Caxnfk2dNQgRSYSgRP1Nt
@travisjhughes
travisjhughes deleted the undo-record-guard branch November 24, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants