From 019fa241be4e7c7d8f1d55be5ccf4f734e3637fe Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:28:54 -0800 Subject: [PATCH 1/2] Fix shared story engine review nits --- .../ScreenEvents/ScreenEvents_Tests.cpp | 6 +- .../AutoStory/Data/Pokemon_StoryFiles.cpp | 28 +- .../Data/Pokemon_StoryFiles_Tests.cpp | 8 + .../Engine/Pokemon_StoryEngineRuns_Tests.cpp | 296 +----------- .../Engine/Pokemon_StoryEngineRuns_Tests.h | 3 +- .../Pokemon_StoryEngineSaveRetry_Tests.cpp | 442 ++++++++++++++++++ .../Pokemon_StoryEngineSaveRetry_Tests.h | 23 + .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_StoryFailureClasses.cpp | 12 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 10 files changed, 514 insertions(+), 308 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineSaveRetry_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineSaveRetry_Tests.h diff --git a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index 04720d2aa6..7b61ab9227 100644 --- a/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp +++ b/SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp @@ -186,17 +186,17 @@ namespace{ // A stream of frames into a ScreenEventLog, and its describe() text. struct ScreenEventLogCase{ struct Frame{ - int ms; + int ms = 0; std::vector events; }; struct Input{ - size_t capacity; + size_t capacity = 0; std::vector frames; }; struct Expectation{ // describe() of the log, times from the first frame. std::string text; - size_t dropped; + size_t dropped = 0; }; std::string name; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp index b7b108c311..3c958c9feb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles.cpp @@ -63,16 +63,32 @@ void check_in_story_order( namespace{ +// Which row sequence the reload check applies to. +enum class RunKind{ + DEVELOPER, + USER, +}; + +// Text naming `run` in a reload error. +std::string_view run_suffix(RunKind run){ + switch (run){ + case RunKind::DEVELOPER: + return ""; + case RunKind::USER: + return " in a user's run"; + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Unknown story run kind."); +} + // Throws ParseException naming `source` unless `previous` ends on the -// game's autosave in `rows`; `run` says in whose run ("" or " in a -// user's run"). +// game's autosave in `rows`, for `run`. void check_reload_after( - std::optional previous, const StoryRows& rows, std::string_view run, std::string_view source + std::optional previous, const StoryRows& rows, RunKind run, std::string_view source ){ if (!previous || !rows.autosaves.contains(*previous)){ throw ParseException(std::format( "{}: reload_when_chained after a checkpoint that does not end on the game's autosave{}: the reload would load an older save", - source, run + source, run_suffix(run) )); } } @@ -87,9 +103,9 @@ void add_story_row( if (start == RowSave::WITHOUT_SAVE){ throw ParseException(std::format("{}: reload_when_chained on a checkpoint with no save to reload", source)); } - check_reload_after(previous.in_dev_run, rows, "", source); + check_reload_after(previous.in_dev_run, rows, RunKind::DEVELOPER, source); if (!execution.dev_only && previous.in_user_run != previous.in_dev_run){ - check_reload_after(previous.in_user_run, rows, " in a user's run", source); + check_reload_after(previous.in_user_run, rows, RunKind::USER, source); } rows.reload_when_chained.emplace(id); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp index 8b1d06bb5a..54d4b9b78b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryFiles_Tests.cpp @@ -184,6 +184,14 @@ const LoadCase LOAD_CASES[] = { .input = {.builtin = {{"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, ROW_1}}, .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, }, + { + .name = "a dev-only reload after a user row that does not autosave fails", + .input = {.builtin = { + {"test-01.00.plan", file_text("01.00", R"({"save": "menu"})")}, + {"test-01.01.plan", file_text("01.01", R"({"dev_only": true, "reload_when_chained": true})")}, + }}, + .expectation = {.error = "test-01.01.plan: reload_when_chained after a checkpoint that does not end on the game's autosave"}, + }, { .name = "reload_when_chained after a dev-only autosave row, the user's row before it not autosaving, fails", .input = {.builtin = { diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index ecfc8a691b..9f4a51a3d8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -5,12 +5,11 @@ * What a game needs from the engine to drop its own workarounds (the PLZA * move, #335): a scripted backend for any pose type, the move stick and * the heading reading, the choice and interact events, a screen machine - * over observations, the step in an action's call, the checkpoint in the - * save, the segment and verify loops, console-only actions, declared poses - * of the game's kind only, and tile actions only for tile games. + * over observations, the step in an action's call, declared poses of the + * game's kind only, and tile actions only for tile games. * Then what PLZA found on the engine: readers opt in to the postcondition * check, inputs logged by button name, waiting states may stay still, and - * the stick's release. + * the stick's release. It also checks the engine's breakpoints. * */ @@ -60,15 +59,6 @@ bool has_line(const FakeStoryBackend& backend, std::string_view line){ return false; } -template -size_t count_lines(const FakeStoryBackend& backend, std::string_view line){ - size_t count = 0; - for (const std::string& each : backend.lines()){ - count += each == line ? 1 : 0; - } - return count; -} - template std::string expect_inputs(const FakeStoryBackend& backend, std::string_view expected){ const std::string got = joined(backend.inputs()); @@ -98,16 +88,6 @@ StoryCheckpoint checkpoint_of(std::string id, std::vector steps){ return checkpoint; } -// `checkpoint` in `phase`, verified with its steps as they are now. -template -StoryCheckpoint in_phase(StoryCheckpoint checkpoint, StoryPhase phase, const StoryEngine& engine){ - checkpoint.metadata.phase = phase; - checkpoint.metadata.verified = StoryVerification{ - .steps_hash = engine.steps_hash(checkpoint), .build = "abc", .date = "2026-11-10", .runs = "3/3", - }; - return checkpoint; -} - // A GAME action of no args. template StoryAction game_action(std::string name, StoryActionHandler handler){ @@ -517,265 +497,6 @@ std::string handler_sees_step(CancellableScope& scope){ return *seen == "opening / the long intro" ? "" : std::format("expected the step, got \"{}\"", *seen); } -StoryStep save_step(std::string name){ - StoryStep step; - step.kind = StoryStepKind::SAVE; - step.name = std::move(name); - return step; -} - -// Gap 6: the save is told its checkpoint, at the end and at a SAVE step. -std::string save_gets_checkpoint(CancellableScope& scope){ - StoryGame game = flaky_game(0); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const StoryCheckpoint checkpoint = in_phase( - checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here")}), StoryPhase::REPRODUCTION, engine - ); - RunState state; - const StoryEngineReport report = engine.run_checkpoint(checkpoint, SavePoint::CHECKPOINT_END, state); - if (!report.saved){ - return "expected the checkpoint to save at its end"; - } - const size_t saves = count_lines(backend, "[Test] save 02.01"); - return saves == 2 ? "" : std::format("expected 2 saves of 02.01, got {}: {}", saves, joined(backend.lines())); -} - -// A SAVE step part-way through a checkpoint moves the save: a reload -// after a later failure would not return to the checkpoint's start, so -// the segment stops there instead of retrying from step 1. -std::string no_retry_after_a_save_step(CancellableScope& scope){ - StoryGame game = flaky_game(1); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{in_phase( - checkpoint_of("02.01", {game_step("walk", "fine"), save_step("save_here"), game_step("talk", "flaky")}), - StoryPhase::REPRODUCTION, engine - )}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - if (report.completed || report.failed_event != "talk" || report.failure.failure != "flaky_failed"){ - return std::format("expected the segment to stop at talk, got {}: {}", to_string(report.failure), joined(backend.lines())); - } - if (count_lines(backend, "[Test] reload") != 0){ - return std::format("expected no reload: {}", joined(backend.lines())); - } - return has_line(backend, "[Checkpoint] 02.01: a save step saved part-way, so a reload would not return to its start: no retry") - ? "" - : std::format("expected the no-retry line: {}", joined(backend.lines())); -} - -// Gap 8: a console_only action is refused, before any input, on a backend -// that is no console; it runs on one. -StoryGame console_game(){ - StoryGame game = flaky_game(0); - StoryAction menu = game_action("open_menu", [](StoryBackend& backend, const StoryActionCall&, RunState&){ - backend.press(NintendoSwitch::BUTTON_X, 80ms, 120ms); - return outcome_ok(); - }); - menu.spec.console_only = true; - game.actions.push_back(std::move(menu)); - return game; -} - -std::string console_only_refused(CancellableScope& scope){ - const StoryGame game = console_game(); - const StoryActionRegistry registry(game); - RunState state; - FakeStoryBackend script({}, scope); - const Outcome refused = registry.dispatch(script, game_step("menu", "open_menu"), state); - if (refused.failure != CONSOLE_ONLY || refused.detail != "open_menu runs only on a console, not on a script backend"){ - return std::format("expected console_only, got {}", to_string(refused)); - } - const std::string none = expect_inputs(script, ""); - if (!none.empty()){ - return none; - } - FakeStoryBackend console({.kind = StoryBackendKind::CONSOLE}, scope); - if (!registry.dispatch(console, game_step("menu", "open_menu"), state).succeeded()){ - return "expected the action to run on a console"; - } - return expect_inputs(console, "press X"); -} - -// Gap 7 and 8: no reload for a console_only failure, which every attempt -// would repeat. -std::string segment_no_reload_for_console_only(CancellableScope& scope){ - const StoryGame game = console_game(); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{checkpoint_of("01", {game_step("menu", "open_menu")})}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - if (report.completed || report.failure.failure != CONSOLE_ONLY || report.failed_event != "menu"){ - return std::format("expected the segment to stop on console_only at menu, got {}", to_string(report.failure)); - } - return count_lines(backend, "[Test] reload") == 0 ? "" : "expected no reload"; -} - -// Gap 7: the first checkpoint is retried after a reload; one after it -// only when the one before saved at its end (#329). -struct SegmentCase{ - struct Input{ - StoryPhase phase; - // Which checkpoint fails once: 0 or 1. - size_t flaky = 0; - }; - - struct Expectation{ - bool completed = false; - size_t reloads = 0; - size_t saves = 0; - }; - - std::string_view name; - Input input; - Expectation expectation; -}; - -const SegmentCase SEGMENT_CASES[] = { - {.name = "first retries while testing", .input = {.phase = StoryPhase::TESTING, .flaky = 0}, - .expectation = {.completed = true, .reloads = 1, .saves = 0}}, - {.name = "second does not retry while testing", .input = {.phase = StoryPhase::TESTING, .flaky = 1}, - .expectation = {.completed = false, .reloads = 0, .saves = 0}}, - {.name = "second does not retry while verifying", .input = {.phase = StoryPhase::VERIFICATION, .flaky = 1}, - .expectation = {.completed = false, .reloads = 0, .saves = 0}}, - {.name = "second retries after a reproduction save", .input = {.phase = StoryPhase::REPRODUCTION, .flaky = 1}, - .expectation = {.completed = true, .reloads = 1, .saves = 2}}, -}; - -std::string check_segment(const SegmentCase& test, CancellableScope& scope){ - const StoryGame game = flaky_game(1); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - std::vector checkpoints; - for (size_t c = 0; c < 2; c++){ - const std::string action = c == test.input.flaky ? "flaky" : "fine"; - checkpoints.push_back(in_phase(checkpoint_of(std::format("0{}", c + 1), {game_step("walk", action)}), test.input.phase, engine)); - } - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, 3, state); - const size_t reloads = count_lines(backend, "[Test] reload"); - const size_t saves = count_lines(backend, "[Test] save 01") + count_lines(backend, "[Test] save 02"); - if (report.completed != test.expectation.completed || reloads != test.expectation.reloads || saves != test.expectation.saves){ - return std::format( - "expected completed {}, {} reloads, {} saves; got {}, {}, {}: {}", - test.expectation.completed, test.expectation.reloads, test.expectation.saves, - report.completed, reloads, saves, joined(backend.lines()) - ); - } - return report.reloads == reloads ? "" : "expected the report to count each reload"; -} - -class StorySegmentTest : public UnitTest{ -public: - explicit StorySegmentTest(SegmentCase test) - : UnitTest(std::format("Pokemon::StoryEngine segment - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ - const std::string error = check_segment(m_case, scope); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - SegmentCase m_case; -}; - -// The engine's retries run through run_checkpoint_attempts(): the same -// reload and pass lines and attempt counts as its own loop had. -struct RetryCase{ - struct Input{ - // Calls of the checkpoint's action that fail before one passes. - size_t failures = 0; - size_t attempts = 0; - }; - - struct Expectation{ - bool passed = false; - size_t attempts = 0; - // Lines the run writes once each. - std::vector lines; - }; - - std::string_view name; - Input input; - Expectation expectation; -}; - -const RetryCase RETRY_CASES[] = { - {.name = "pass after a reload", .input = {.failures = 1, .attempts = 3}, - .expectation = {.passed = true, .attempts = 2, .lines = { - "[Checkpoint] 01: failed, resetting for attempt 2/3", - "[Test] reload", - "[Checkpoint] 01: passed on attempt 2 (1 reset)", - }}}, - {.name = "stop after the last attempt", .input = {.failures = 5, .attempts = 2}, - .expectation = {.passed = false, .attempts = 2, .lines = { - "[Checkpoint] 01: failed, resetting for attempt 2/2", - "[Test] reload", - }}}, -}; - -std::string check_retry(const RetryCase& test, CancellableScope& scope){ - const StoryGame game = flaky_game(test.input.failures); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const std::vector checkpoints{checkpoint_of("01", {game_step("walk", "flaky")})}; - RunState state; - const SegmentReport report = engine.run_segment("seg", checkpoints, test.input.attempts, state); - const CheckpointRun& run = report.runs.at(0); - if (run.passed != test.expectation.passed || run.attempts != test.expectation.attempts){ - return std::format( - "expected passed {} after {} attempts, got {} after {}: {}", - test.expectation.passed, test.expectation.attempts, run.passed, run.attempts, joined(backend.lines()) - ); - } - for (std::string_view line : test.expectation.lines){ - if (count_lines(backend, line) != 1){ - return std::format("expected \"{}\" once: {}", line, joined(backend.lines())); - } - } - return ""; -} - -class StoryRetryTest : public UnitTest{ -public: - explicit StoryRetryTest(RetryCase test) - : UnitTest(std::format("Pokemon::StoryEngine retry - {}", test.name)) - , m_case(std::move(test)) - {} - virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ - const std::string error = check_retry(m_case, scope); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); - } -private: - RetryCase m_case; -}; - -// Gap 7: verify runs a checkpoint 3 times from one save, saving only at -// the end of the last, and only when every run passed. -std::string verify_saves_after_all_passed(CancellableScope& scope){ - for (size_t failures = 0; failures < 2; failures++){ - const StoryGame game = flaky_game(failures); - FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); - StoryEngine engine(game, backend); - const StoryCheckpoint checkpoint = in_phase(checkpoint_of("00.15b", {game_step("battle", "flaky")}), StoryPhase::VERIFICATION, engine); - RunState state; - const VerifyReport report = engine.verify_checkpoint(checkpoint, VerifyPolicy{.runs = 3, .passes_needed = 2}, state); - const size_t saves = count_lines(backend, "[Test] save 00.15b"); - const size_t reloads = count_lines(backend, "[Test] reload"); - const size_t passes = 3 - failures; - if (!report.verified || report.passes != passes || reloads != 2 || saves != (failures == 0 ? 1 : 0) || report.saved != (failures == 0)){ - return std::format( - "{} failures: expected verified, {} passes, 2 reloads, {} saves; got {}, {}, {}, {}", - failures, passes, failures == 0 ? 1 : 0, report.verified, report.passes, reloads, saves - ); - } - } - return ""; -} - - // A game whose "mark" action records "run " and whose pause hook // records "pause