From 439cedd294255c47b402a4997df9586695a561f3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:35:48 -0800 Subject: [PATCH 1/3] Prechecks: a program's start checks as a named list; FRLG RNG first A Precheck has a stable name, a one-line "needs" and a policy (REQUIRED or OPTIONAL). run_prechecks() runs a list in order, leaves out the OPTIONAL ones when asked, returns one result per check (name, passed/failed/skipped, message) and throws UserSetupError with the check's message at the first failure. FRLG RNG's run_precheck() now runs rng_prechecks(n): black_border (REQUIRED), then free_party_slots(n) (OPTIONAL, only when n > 0). The "Run Start Checks" box in the FRLG Game Settings skips the OPTIONAL checks, as it skipped the party check before. Messages are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../Programs/NintendoSwitch_Prechecks.cpp | 63 +++++++ .../Programs/NintendoSwitch_Prechecks.h | 104 ++++++++++++ .../NintendoSwitch_Prechecks_Tests.cpp | 155 +++++++++++++++++ .../Programs/NintendoSwitch_Prechecks_Tests.h | 21 +++ .../PokemonFRLG/PokemonFRLG_Prechecks.cpp | 100 +++++++++++ .../PokemonFRLG/PokemonFRLG_Prechecks.h | 36 ++++ .../PokemonFRLG_Prechecks_Tests.cpp | 158 ++++++++++++++++++ .../PokemonFRLG/PokemonFRLG_Prechecks_Tests.h | 23 +++ .../PokemonFRLG/PokemonFRLG_Tests.cpp | 2 + .../PokemonFRLG_RngProgram.cpp | 64 ++----- .../RngManipulation/PokemonFRLG_RngProgram.h | 21 ++- SerialPrograms/cmake/SourceFiles.cmake | 8 + 13 files changed, 697 insertions(+), 60 deletions(-) create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 9b48682bc1..ade4631c3c 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -28,6 +28,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" @@ -73,6 +74,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); + NintendoSwitch::add_tests_Prechecks(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(ret); NintendoSwitch::PokemonBDSP::add_tests_MenuDetector(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp new file mode 100644 index 0000000000..f77d70ede8 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp @@ -0,0 +1,63 @@ +/* Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "NintendoSwitch_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +Precheck::Precheck(std::string name, std::string needs, PrecheckPolicy policy) + : m_name(std::move(name)) + , m_needs(std::move(needs)) + , m_policy(policy) +{} + + +std::vector run_prechecks( + Logger& logger, + const PrecheckList& checks, + OptionalPrechecks optional, + const std::function& run_check +){ + std::vector results; + for (const std::unique_ptr& check : checks){ + if (check->policy() == PrecheckPolicy::OPTIONAL && optional == OptionalPrechecks::SKIP){ + logger.log("Precheck " + check->name() + ": skipped, optional prechecks are off."); + results.emplace_back(PrecheckResult{check->name(), {PrecheckStatus::SKIPPED, "Optional prechecks are off."}}); + continue; + } + logger.log("Precheck " + check->name() + ": " + check->needs()); + PrecheckOutcome outcome = run_check(*check); + if (outcome.status == PrecheckStatus::FAILED){ + throw UserSetupError(logger, std::move(outcome.message)); + } + if (outcome.status == PrecheckStatus::SKIPPED){ + logger.log("Precheck " + check->name() + ": skipped. " + outcome.message, COLOR_RED); + } + results.emplace_back(PrecheckResult{check->name(), std::move(outcome)}); + } + return results; +} + +std::vector run_prechecks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const PrecheckList& checks, + OptionalPrechecks optional +){ + return run_prechecks( + env.console.logger(), checks, optional, + [&](const Precheck& check){ + return check.run(env, context); + } + ); +} + + +} +} diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h new file mode 100644 index 0000000000..5e11ba9a0d --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h @@ -0,0 +1,104 @@ +/* Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + * What the console and the game must look like before a program starts. + * A program lists its prechecks in order and runs them first thing, so a + * setup mistake stops the run before it resets or saves, with a message that + * says what to fix. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_Prechecks_H +#define PokemonAutomation_NintendoSwitch_Prechecks_H + +#include +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +enum class PrecheckStatus{ + PASSED, + FAILED, + SKIPPED, +}; + +// Whether a precheck can be turned off. +enum class PrecheckPolicy{ + REQUIRED, // Always runs. + OPTIONAL, // Runs unless the program turns its optional checks off. +}; + +// Whether run_prechecks() runs the OPTIONAL checks. +enum class OptionalPrechecks{ + RUN, + SKIP, +}; + +// What one precheck found. On FAILED, the message tells the user what to fix. +struct PrecheckOutcome{ + PrecheckStatus status; + std::string message; +}; + +// One precheck of a run, as run_prechecks() reports it. +struct PrecheckResult{ + std::string name; + PrecheckOutcome outcome; +}; + + +// One thing a program needs before it starts. +class Precheck{ +public: + Precheck(std::string name, std::string needs, PrecheckPolicy policy); + virtual ~Precheck() = default; + + // Stable id, e.g. "free_party_slots". + const std::string& name() const{ return m_name; } + // One line for the user: what the program needs. + const std::string& needs() const{ return m_needs; } + PrecheckPolicy policy() const{ return m_policy; } + + // Reads the console. Never resets, saves or changes options. + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const = 0; + +private: + std::string m_name; + std::string m_needs; + PrecheckPolicy m_policy; +}; + +// A program's prechecks, in the order they run. Building the list never +// touches the console. +using PrecheckList = std::vector>; + + +// Runs the checks in order, each through run_check, leaving out the OPTIONAL +// ones when optional is SKIP. Throws UserSetupError with the check's message +// at the first FAILED one. Returns one result per check. +std::vector run_prechecks( + Logger& logger, + const PrecheckList& checks, + OptionalPrechecks optional, + const std::function& run_check +); + +// Runs the checks on the console. Call first in program(). +std::vector run_prechecks( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + const PrecheckList& checks, + OptionalPrechecks optional +); + + +} +} +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp new file mode 100644 index 0000000000..4f404043d6 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp @@ -0,0 +1,155 @@ +/* Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + * run_prechecks() with fake checks: no console, no GUI. + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch_Prechecks.h" +#include "NintendoSwitch_Prechecks_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +namespace{ + + +// A check that only has a name and a policy. The tests run it through +// run_check, never through run(). +class FakePrecheck : public Precheck{ +public: + using Precheck::Precheck; + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment&, ProControllerContext&) const override{ + return {PrecheckStatus::FAILED, "A fake check must not touch the console."}; + } +}; + +struct FakeCheck{ + std::string name; + PrecheckPolicy policy; + PrecheckOutcome outcome; +}; + +struct RunPrechecksCase{ + struct Input{ + std::vector checks; + OptionalPrechecks optional; + }; + struct Expectation{ + // Names of the checks run_prechecks() ran, in order. + std::vector ran; + // Set when run_prechecks() throws UserSetupError: its message. + std::optional error; + // Statuses run_prechecks() returned, in order, when it did not throw. + std::vector statuses; + }; + std::string name; + Input input; + Expectation expectation; +}; + +const PrecheckOutcome PASS{PrecheckStatus::PASSED, ""}; + + +class Test_RunPrechecks : public UnitTest{ +public: + explicit Test_RunPrechecks(RunPrechecksCase test_case) + : UnitTest("NintendoSwitch::Prechecks::RunPrechecks - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + PrecheckList checks; + std::map outcomes; + for (const FakeCheck& check : m_case.input.checks){ + checks.emplace_back(std::make_unique(check.name, "Fake needs.", check.policy)); + outcomes.emplace(check.name, check.outcome); + } + std::vector ran; + auto run_check = [&](const Precheck& check){ + ran.emplace_back(check.name()); + return outcomes.at(check.name()); + }; + + const RunPrechecksCase::Expectation& expected = m_case.expectation; + std::optional error; + std::vector statuses; + try{ + for (const PrecheckResult& result : run_prechecks(logger, checks, m_case.input.optional, run_check)){ + statuses.emplace_back(result.outcome.status); + } + }catch (UserSetupError& e){ + error = e.message(); + } + + if (ran != expected.ran){ + return "run_prechecks() ran the wrong checks or in the wrong order."; + } + if (error != expected.error){ + return "run_prechecks() threw the wrong error: \"" + error.value_or("(none)") + "\""; + } + if (statuses != expected.statuses){ + return "run_prechecks() returned the wrong statuses."; + } + return true; + } + +private: + RunPrechecksCase m_case; +}; + + +std::vector run_prechecks_cases(){ + using enum PrecheckPolicy; + using enum PrecheckStatus; + const PrecheckOutcome no_room{FAILED, "Your party is full."}; + const PrecheckOutcome no_menu{SKIPPED, "Unable to open the party menu."}; + return { + { + "AllPassInOrder", + {{{"first", REQUIRED, PASS}, {"second", OPTIONAL, PASS}}, OptionalPrechecks::RUN}, + {{"first", "second"}, std::nullopt, {PASSED, PASSED}}, + }, + { + "SkipLeavesOutOptionalChecks", + {{{"first", OPTIONAL, PASS}, {"second", REQUIRED, PASS}}, OptionalPrechecks::SKIP}, + {{"second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + "FirstFailureStopsWithItsMessage", + {{{"first", REQUIRED, no_room}, {"second", REQUIRED, PASS}}, OptionalPrechecks::RUN}, + {{"first"}, "Your party is full.", {}}, + }, + { + "SkippedCheckDoesNotStopTheRun", + {{{"first", OPTIONAL, no_menu}, {"second", REQUIRED, PASS}}, OptionalPrechecks::RUN}, + {{"first", "second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + "EmptyListRunsNothing", + {{}, OptionalPrechecks::RUN}, + {{}, std::nullopt, {}}, + }, + }; +} + + +} // namespace + + +void add_tests_Prechecks(UnitTestDatabase& database){ + for (RunPrechecksCase& test_case : run_prechecks_cases()){ + database.add(std::move(test_case)); + } +} + + +} +} diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h new file mode 100644 index 0000000000..ea7d53be29 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h @@ -0,0 +1,21 @@ +/* Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_Prechecks_Tests_H +#define PokemonAutomation_NintendoSwitch_Prechecks_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +void add_tests_Prechecks(UnitTestDatabase& database); + + +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp new file mode 100644 index 0000000000..eb820e08b6 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp @@ -0,0 +1,100 @@ +/* FRLG Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "Pokemon/Pokemon_Strings.h" +#include "PokemonFRLG_Navigation.h" +#include "PokemonFRLG_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +constexpr size_t MAX_PARTY_SIZE = 6; + + +std::string not_enough_free_slots_message(size_t party_size, size_t free_slots_needed){ + if (free_slots_needed == 1){ + return std::format( + "Your party is full. Leave a party slot free so the program can check the {} it catches or receives.", + Pokemon::STRING_POKEMON + ); + } + return std::format( + "Your party has {} {}. Leave at least {} party slots free for the {} the program catches or receives.", + party_size, Pokemon::STRING_POKEMON, free_slots_needed, Pokemon::STRING_POKEMON + ); +} + + +class BlackBorderPrecheck : public Precheck{ +public: + BlackBorderPrecheck() + : Precheck( + "black_border", + "Screen size at 100% with no black border, and the controller connected.", + PrecheckPolicy::REQUIRED + ) + {} + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const override{ + home_black_border_check(env.console, context); + return {PrecheckStatus::PASSED, ""}; + } +}; + + +class FreePartySlotsPrecheck : public Precheck{ +public: + explicit FreePartySlotsPrecheck(size_t free_slots_needed) + : Precheck( + "free_party_slots", + std::format("At least {} free party slot(s), on the overworld where the game was saved.", free_slots_needed), + PrecheckPolicy::OPTIONAL + ) + , m_free_slots_needed(free_slots_needed) + {} + virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const override{ + try{ + open_party_menu_from_overworld(env.console, context); + }catch (OperationFailedException&){ + return {PrecheckStatus::SKIPPED, "Unable to open the party menu."}; + } + context.wait_for_all_requests(); + const size_t party_size = static_cast(detect_last_occupied_party_slot(env.console)) + 1; + return free_party_slots_outcome(party_size, m_free_slots_needed); + } + +private: + size_t m_free_slots_needed; +}; + + +} // namespace + + +std::unique_ptr make_black_border_precheck(){ + return std::make_unique(); +} + +std::unique_ptr make_free_party_slots_precheck(size_t free_slots_needed){ + return std::make_unique(free_slots_needed); +} + +PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_needed){ + if (party_size + free_slots_needed > MAX_PARTY_SIZE){ + return {PrecheckStatus::FAILED, not_enough_free_slots_message(party_size, free_slots_needed)}; + } + return {PrecheckStatus::PASSED, "Party has enough free slots."}; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h new file mode 100644 index 0000000000..e3790cad0c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h @@ -0,0 +1,36 @@ +/* FRLG Prechecks + * + * From: https://github.com/PokemonAutomation/ + * + * The prechecks FRLG programs share. See NintendoSwitch_Prechecks.h. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_Prechecks_H +#define PokemonAutomation_PokemonFRLG_Prechecks_H + +#include +#include +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// "black_border": goes Home to check the screen size and the controller, +// then resumes the game. Always runs. +std::unique_ptr make_black_border_precheck(); + +// "free_party_slots": opens the party menu from the overworld and checks +// that at least free_slots_needed slots are free. Optional. +std::unique_ptr make_free_party_slots_precheck(size_t free_slots_needed); + +// The free_party_slots verdict for a party of party_size Pokemon. +PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_needed); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp new file mode 100644 index 0000000000..c2bb6ca756 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp @@ -0,0 +1,158 @@ +/* FRLG Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The FRLG precheck lists and verdicts: no console, no GUI. + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Programs/RngManipulation/PokemonFRLG_RngProgram.h" +#include "PokemonFRLG_Prechecks.h" +#include "PokemonFRLG_Prechecks_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + + +struct ListedPrecheck{ + std::string name; + PrecheckPolicy policy; + + bool operator==(const ListedPrecheck&) const = default; +}; + +struct RngPrechecksCase{ + struct Input{ + size_t free_party_slots_needed; + }; + struct Expectation{ + std::vector checks; + }; + std::string name; + Input input; + Expectation expectation; +}; + +class Test_RngPrechecks : public UnitTest{ +public: + explicit Test_RngPrechecks(RngPrechecksCase test_case) + : UnitTest("PokemonFRLG::Prechecks::RngPrechecks - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector listed; + for (const std::unique_ptr& check : rng_prechecks(m_case.input.free_party_slots_needed)){ + if (check->needs().empty()){ + return "Precheck " + check->name() + " must say what it needs."; + } + listed.emplace_back(ListedPrecheck{check->name(), check->policy()}); + } + if (listed != m_case.expectation.checks){ + return "The RNG precheck list has the wrong checks, order or policies."; + } + return true; + } + +private: + RngPrechecksCase m_case; +}; + + +struct FreePartySlotsCase{ + struct Input{ + size_t party_size; + size_t free_slots_needed; + }; + struct Expectation{ + PrecheckStatus status; + // Checked only on FAILED: the message the user sees. + std::string message; + }; + std::string name; + Input input; + Expectation expectation; +}; + +class Test_FreePartySlotsOutcome : public UnitTest{ +public: + explicit Test_FreePartySlotsOutcome(FreePartySlotsCase test_case) + : UnitTest("PokemonFRLG::Prechecks::FreePartySlotsOutcome - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const PrecheckOutcome outcome = free_party_slots_outcome( + m_case.input.party_size, m_case.input.free_slots_needed + ); + if (outcome.status != m_case.expectation.status){ + return "Wrong verdict: \"" + outcome.message + "\""; + } + if (outcome.status == PrecheckStatus::FAILED && outcome.message != m_case.expectation.message){ + return "Wrong message: \"" + outcome.message + "\""; + } + return true; + } + +private: + FreePartySlotsCase m_case; +}; + + +std::vector rng_prechecks_cases(){ + const ListedPrecheck black_border{"black_border", PrecheckPolicy::REQUIRED}; + const ListedPrecheck free_party_slots{"free_party_slots", PrecheckPolicy::OPTIONAL}; + return { + {"NeedsNoSlots", {0}, {{black_border}}}, + {"NeedsOneSlot", {1}, {{black_border, free_party_slots}}}, + {"NeedsTwoSlots", {2}, {{black_border, free_party_slots}}}, + }; +} + +std::vector free_party_slots_cases(){ + using enum PrecheckStatus; + return { + {"FiveOfSixNeedsOne", {5, 1}, {PASSED, ""}}, + { + "FullPartyNeedsOne", {6, 1}, + { + FAILED, + "Your party is full. Leave a party slot free so the program can check the " + "Pokémon it catches or receives.", + }, + }, + {"FourOfSixNeedsTwo", {4, 2}, {PASSED, ""}}, + { + "FiveOfSixNeedsTwo", {5, 2}, + { + FAILED, + "Your party has 5 Pokémon. Leave at least 2 party slots free for the " + "Pokémon the program catches or receives.", + }, + }, + }; +} + + +} // namespace + + +void add_tests_Prechecks(UnitTestDatabase& database){ + for (RngPrechecksCase& test_case : rng_prechecks_cases()){ + database.add(std::move(test_case)); + } + for (FreePartySlotsCase& test_case : free_party_slots_cases()){ + database.add(std::move(test_case)); + } +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h new file mode 100644 index 0000000000..b0f0af3f26 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h @@ -0,0 +1,23 @@ +/* FRLG Prechecks - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_Prechecks_Tests_H +#define PokemonAutomation_PokemonFRLG_Prechecks_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +void add_tests_Prechecks(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 94dc41dd85..0dd809965a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -30,6 +30,7 @@ #include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h" #include "Tests/TestUtils.h" +#include "PokemonFRLG_Prechecks_Tests.h" #include "PokemonFRLG_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -54,6 +55,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_PartyHeldItemDetector(database); add_tests_PartyLevelUpReader(database); add_tests_PartyMenuDetector(database); + add_tests_Prechecks(database); add_tests_PrizeSelectDetector(database); add_tests_PartySlotDetector(database); add_tests_StatsReader(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp index d4257d7f37..a32c0e7c78 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp @@ -4,13 +4,7 @@ * */ -#include -#include -#include "Common/Cpp/Exceptions.h" -#include "CommonFramework/Exceptions/OperationFailedException.h" -#include "Pokemon/Pokemon_Strings.h" -#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" -#include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "PokemonFRLG_RngProgram.h" @@ -19,59 +13,27 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -namespace{ - -std::string not_enough_free_slots_message(size_t party_size, size_t free_slots_needed){ - if (free_slots_needed == 1){ - return std::format( - "Your party is full. Leave a party slot free so the program can check the {} it catches or receives.", - Pokemon::STRING_POKEMON - ); +PrecheckList rng_prechecks(size_t free_party_slots_needed){ + PrecheckList checks; + checks.emplace_back(make_black_border_precheck()); + if (free_party_slots_needed > 0){ + checks.emplace_back(make_free_party_slots_precheck(free_party_slots_needed)); } - return std::format( - "Your party has {} {}. Leave at least {} party slots free for the {} the program catches or receives.", - party_size, Pokemon::STRING_POKEMON, free_slots_needed, Pokemon::STRING_POKEMON - ); + return checks; } -} // namespace - - -void RngProgramInstance::check_free_party_slots( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context -){ - const size_t free_slots_needed = free_party_slots_needed(); - if (free_slots_needed == 0){ - return; - } - Logger& logger = env.console.logger(); - logger.log("Checking for " + std::to_string(free_slots_needed) + " free party slot(s)..."); - try{ - open_party_menu_from_overworld(env.console, context); - }catch (OperationFailedException&){ - logger.log("Unable to open the party menu. Skipping the free party slot check.", COLOR_RED); - return; - } - context.wait_for_all_requests(); - constexpr size_t MAX_PARTY_SIZE = 6; - const size_t party_size = static_cast(detect_last_occupied_party_slot(env.console)) + 1; - if (MAX_PARTY_SIZE - party_size < free_slots_needed){ - throw UserSetupError(logger, not_enough_free_slots_message(party_size, free_slots_needed)); - } - logger.log("Party has enough free slots."); +PrecheckList RngProgramInstance::prechecks() const{ + return rng_prechecks(free_party_slots_needed()); } void RngProgramInstance::run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context ){ - home_black_border_check(env.console, context); - if (!GameSettings::instance().RUN_RNG_START_CHECKS){ - env.console.log("Start checks are off in the FRLG Game Settings. Skipping them."); - return; - } - check_free_party_slots(env, context); + const OptionalPrechecks optional = GameSettings::instance().RUN_RNG_START_CHECKS + ? OptionalPrechecks::RUN + : OptionalPrechecks::SKIP; + run_prechecks(env, context, prechecks(), optional); } } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h index 727ba9b099..6d0a0e4154 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h @@ -14,12 +14,18 @@ #include #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "NintendoSwitch/Programs/NintendoSwitch_Prechecks.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonFRLG{ +// The prechecks of an RNG program that needs free_party_slots_needed free +// party slots: black_border, then free_party_slots when it needs any. +PrecheckList rng_prechecks(size_t free_party_slots_needed); + + // Base class of the RNG programs. class RngProgramInstance : public SingleSwitchProgramInstance{ public: @@ -30,21 +36,18 @@ class RngProgramInstance : public SingleSwitchProgramInstance{ // party that Pokemon would go to the PC instead. virtual size_t free_party_slots_needed() const = 0; + // What the program needs before it starts, in the order run_precheck() + // checks it. Does not touch the console. + PrecheckList prechecks() const; + protected: // Call first in program(), with the game on the overworld where the - // player saved: checks the party has the free slots - // free_party_slots_needed() asks for, then goes Home for the black - // border check. Throws UserSetupError if a check fails. + // player saved: runs prechecks(). "Run Start Checks" off in the FRLG Game + // Settings skips the optional ones. Throws UserSetupError if a check fails. void run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context ); - -private: - void check_free_party_slots( - SingleSwitchProgramEnvironment& env, - ProControllerContext& context - ); }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5bde7d3e75..e85cd7987f 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1417,6 +1417,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/NintendoSwitch_GameEntry.h Source/NintendoSwitch/Programs/NintendoSwitch_MenuStabilityTester.cpp Source/NintendoSwitch/Programs/NintendoSwitch_MenuStabilityTester.h + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp + Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h Source/NintendoSwitch/Programs/NintendoSwitch_PreventSleep.cpp Source/NintendoSwitch/Programs/NintendoSwitch_PreventSleep.h Source/NintendoSwitch/Programs/NintendoSwitch_PushJoySticks.cpp @@ -1767,6 +1771,10 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Panels.cpp Source/VideoGames/PokemonFRLG/PokemonFRLG_Panels.h + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp + Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Settings.cpp Source/VideoGames/PokemonFRLG/PokemonFRLG_Settings.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp From 25763445fae4a1584e01a23cb950e902bfd9b140 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 13:37:52 -0800 Subject: [PATCH 2/3] Prechecks: table-driven tests, SKIPPABLE/ALWAYS_RUNS policy names, code health OPTIONAL is a macro in the Windows headers, so the policy values are ALWAYS_RUNS and SKIPPABLE. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/NintendoSwitch_Prechecks.cpp | 21 +-- .../Programs/NintendoSwitch_Prechecks.h | 21 +-- .../NintendoSwitch_Prechecks_Tests.cpp | 165 +++++++++--------- .../Programs/NintendoSwitch_Prechecks_Tests.h | 5 +- .../PokemonFRLG/PokemonFRLG_Prechecks.cpp | 10 +- .../PokemonFRLG/PokemonFRLG_Prechecks.h | 10 +- .../PokemonFRLG_Prechecks_Tests.cpp | 127 +++++++------- .../PokemonFRLG/PokemonFRLG_Prechecks_Tests.h | 7 +- .../PokemonFRLG_RngProgram.cpp | 8 +- .../RngManipulation/PokemonFRLG_RngProgram.h | 2 +- 10 files changed, 195 insertions(+), 181 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp index f77d70ede8..42f0ceaa8d 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Exceptions.h" #include "NintendoSwitch_Prechecks.h" @@ -21,23 +22,23 @@ Precheck::Precheck(std::string name, std::string needs, PrecheckPolicy policy) std::vector run_prechecks( Logger& logger, const PrecheckList& checks, - OptionalPrechecks optional, + SkippablePrechecks skippable, const std::function& run_check ){ std::vector results; for (const std::unique_ptr& check : checks){ - if (check->policy() == PrecheckPolicy::OPTIONAL && optional == OptionalPrechecks::SKIP){ - logger.log("Precheck " + check->name() + ": skipped, optional prechecks are off."); - results.emplace_back(PrecheckResult{check->name(), {PrecheckStatus::SKIPPED, "Optional prechecks are off."}}); + if (check->policy() == PrecheckPolicy::SKIPPABLE && skippable == SkippablePrechecks::SKIP){ + logger.log(std::format("Precheck {}: skipped, skippable prechecks are off.", check->name())); + results.emplace_back(PrecheckResult{check->name(), {PrecheckStatus::SKIPPED, "Skippable prechecks are off."}}); continue; } - logger.log("Precheck " + check->name() + ": " + check->needs()); + logger.log(std::format("Precheck {}: {}", check->name(), check->needs())); PrecheckOutcome outcome = run_check(*check); if (outcome.status == PrecheckStatus::FAILED){ throw UserSetupError(logger, std::move(outcome.message)); } if (outcome.status == PrecheckStatus::SKIPPED){ - logger.log("Precheck " + check->name() + ": skipped. " + outcome.message, COLOR_RED); + logger.log(std::format("Precheck {}: skipped. {}", check->name(), outcome.message), COLOR_RED); } results.emplace_back(PrecheckResult{check->name(), std::move(outcome)}); } @@ -48,10 +49,10 @@ std::vector run_prechecks( SingleSwitchProgramEnvironment& env, ProControllerContext& context, const PrecheckList& checks, - OptionalPrechecks optional + SkippablePrechecks skippable ){ return run_prechecks( - env.console.logger(), checks, optional, + env.console.logger(), checks, skippable, [&](const Precheck& check){ return check.run(env, context); } @@ -59,5 +60,5 @@ std::vector run_prechecks( } -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h index 5e11ba9a0d..992c6b5683 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h @@ -23,6 +23,7 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ +// How one precheck ended. enum class PrecheckStatus{ PASSED, FAILED, @@ -31,12 +32,12 @@ enum class PrecheckStatus{ // Whether a precheck can be turned off. enum class PrecheckPolicy{ - REQUIRED, // Always runs. - OPTIONAL, // Runs unless the program turns its optional checks off. + ALWAYS_RUNS, + SKIPPABLE, // Left out when the program turns its skippable checks off. }; -// Whether run_prechecks() runs the OPTIONAL checks. -enum class OptionalPrechecks{ +// Whether run_prechecks() runs the SKIPPABLE checks. +enum class SkippablePrechecks{ RUN, SKIP, }; @@ -80,13 +81,13 @@ class Precheck{ using PrecheckList = std::vector>; -// Runs the checks in order, each through run_check, leaving out the OPTIONAL -// ones when optional is SKIP. Throws UserSetupError with the check's message +// Runs the checks in order, each through run_check, leaving out the SKIPPABLE +// ones when skippable is SKIP. Throws UserSetupError with the check's message // at the first FAILED one. Returns one result per check. std::vector run_prechecks( Logger& logger, const PrecheckList& checks, - OptionalPrechecks optional, + SkippablePrechecks skippable, const std::function& run_check ); @@ -95,10 +96,10 @@ std::vector run_prechecks( SingleSwitchProgramEnvironment& env, ProControllerContext& context, const PrecheckList& checks, - OptionalPrechecks optional + SkippablePrechecks skippable ); -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp index 4f404043d6..89315f6f41 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp @@ -5,6 +5,7 @@ * run_prechecks() with fake checks: no console, no GUI. */ +#include #include #include #include @@ -37,65 +38,68 @@ struct FakeCheck{ PrecheckOutcome outcome; }; +struct RunPrechecksInput{ + std::vector checks; + SkippablePrechecks skippable; +}; + +struct RunPrechecksExpectation{ + // Names of the checks run_prechecks() ran, in order. + std::vector ran; + // Set when run_prechecks() throws UserSetupError: its message. + std::optional error; + // Statuses run_prechecks() returned, in order, when it did not throw. + std::vector statuses; +}; + struct RunPrechecksCase{ - struct Input{ - std::vector checks; - OptionalPrechecks optional; - }; - struct Expectation{ - // Names of the checks run_prechecks() ran, in order. - std::vector ran; - // Set when run_prechecks() throws UserSetupError: its message. - std::optional error; - // Statuses run_prechecks() returned, in order, when it did not throw. - std::vector statuses; - }; std::string name; - Input input; - Expectation expectation; + RunPrechecksInput input; + RunPrechecksExpectation expectation; }; -const PrecheckOutcome PASS{PrecheckStatus::PASSED, ""}; + +// Runs the case's fake checks; returns what run_prechecks() did. +RunPrechecksExpectation run_fake_prechecks(Logger& logger, const RunPrechecksInput& input){ + PrecheckList checks; + std::map outcomes; + for (const FakeCheck& check : input.checks){ + checks.emplace_back(std::make_unique(check.name, "Fake needs.", check.policy)); + outcomes.emplace(check.name, check.outcome); + } + RunPrechecksExpectation actual; + auto run_check = [&](const Precheck& check){ + actual.ran.emplace_back(check.name()); + return outcomes.at(check.name()); + }; + try{ + for (const PrecheckResult& result : run_prechecks(logger, checks, input.skippable, run_check)){ + actual.statuses.emplace_back(result.outcome.status); + } + }catch (UserSetupError& e){ + actual.error = e.message(); + } + return actual; +} class Test_RunPrechecks : public UnitTest{ public: explicit Test_RunPrechecks(RunPrechecksCase test_case) - : UnitTest("NintendoSwitch::Prechecks::RunPrechecks - " + test_case.name) + : UnitTest(std::format("NintendoSwitch::Prechecks::RunPrechecks - {}", test_case.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - PrecheckList checks; - std::map outcomes; - for (const FakeCheck& check : m_case.input.checks){ - checks.emplace_back(std::make_unique(check.name, "Fake needs.", check.policy)); - outcomes.emplace(check.name, check.outcome); - } - std::vector ran; - auto run_check = [&](const Precheck& check){ - ran.emplace_back(check.name()); - return outcomes.at(check.name()); - }; - - const RunPrechecksCase::Expectation& expected = m_case.expectation; - std::optional error; - std::vector statuses; - try{ - for (const PrecheckResult& result : run_prechecks(logger, checks, m_case.input.optional, run_check)){ - statuses.emplace_back(result.outcome.status); - } - }catch (UserSetupError& e){ - error = e.message(); - } - - if (ran != expected.ran){ + const RunPrechecksExpectation actual = run_fake_prechecks(logger, m_case.input); + const RunPrechecksExpectation& expected = m_case.expectation; + if (actual.ran != expected.ran){ return "run_prechecks() ran the wrong checks or in the wrong order."; } - if (error != expected.error){ - return "run_prechecks() threw the wrong error: \"" + error.value_or("(none)") + "\""; + if (actual.error != expected.error){ + return std::format("run_prechecks() threw the wrong error: \"{}\"", actual.error.value_or("(none)")); } - if (statuses != expected.statuses){ + if (actual.statuses != expected.statuses){ return "run_prechecks() returned the wrong statuses."; } return true; @@ -106,50 +110,51 @@ class Test_RunPrechecks : public UnitTest{ }; -std::vector run_prechecks_cases(){ - using enum PrecheckPolicy; - using enum PrecheckStatus; - const PrecheckOutcome no_room{FAILED, "Your party is full."}; - const PrecheckOutcome no_menu{SKIPPED, "Unable to open the party menu."}; - return { - { - "AllPassInOrder", - {{{"first", REQUIRED, PASS}, {"second", OPTIONAL, PASS}}, OptionalPrechecks::RUN}, - {{"first", "second"}, std::nullopt, {PASSED, PASSED}}, - }, - { - "SkipLeavesOutOptionalChecks", - {{{"first", OPTIONAL, PASS}, {"second", REQUIRED, PASS}}, OptionalPrechecks::SKIP}, - {{"second"}, std::nullopt, {SKIPPED, PASSED}}, - }, - { - "FirstFailureStopsWithItsMessage", - {{{"first", REQUIRED, no_room}, {"second", REQUIRED, PASS}}, OptionalPrechecks::RUN}, - {{"first"}, "Your party is full.", {}}, - }, - { - "SkippedCheckDoesNotStopTheRun", - {{{"first", OPTIONAL, no_menu}, {"second", REQUIRED, PASS}}, OptionalPrechecks::RUN}, - {{"first", "second"}, std::nullopt, {SKIPPED, PASSED}}, - }, - { - "EmptyListRunsNothing", - {{}, OptionalPrechecks::RUN}, - {{}, std::nullopt, {}}, - }, - }; -} +using enum PrecheckPolicy; +using enum PrecheckStatus; + +const PrecheckOutcome PASS{PASSED, ""}; +const PrecheckOutcome PARTY_FULL{FAILED, "Your party is full."}; +const PrecheckOutcome NO_PARTY_MENU{SKIPPED, "Unable to open the party menu."}; + +const std::vector RUN_PRECHECKS_CASES{ + { + .name = "AllPassInOrder", + .input = {{{"first", ALWAYS_RUNS, PASS}, {"second", SKIPPABLE, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first", "second"}, std::nullopt, {PASSED, PASSED}}, + }, + { + .name = "SkipLeavesOutSkippableChecks", + .input = {{{"first", SKIPPABLE, PASS}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::SKIP}, + .expectation = {{"second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + .name = "FirstFailureStopsWithItsMessage", + .input = {{{"first", ALWAYS_RUNS, PARTY_FULL}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first"}, "Your party is full.", {}}, + }, + { + .name = "SkippedCheckDoesNotStopTheRun", + .input = {{{"first", SKIPPABLE, NO_PARTY_MENU}, {"second", ALWAYS_RUNS, PASS}}, SkippablePrechecks::RUN}, + .expectation = {{"first", "second"}, std::nullopt, {SKIPPED, PASSED}}, + }, + { + .name = "EmptyListRunsNothing", + .input = {{}, SkippablePrechecks::RUN}, + .expectation = {{}, std::nullopt, {}}, + }, +}; } // namespace void add_tests_Prechecks(UnitTestDatabase& database){ - for (RunPrechecksCase& test_case : run_prechecks_cases()){ - database.add(std::move(test_case)); + for (const RunPrechecksCase& test_case : RUN_PRECHECKS_CASES){ + database.add(test_case); } } -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h index ea7d53be29..d9bd4a511c 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.h @@ -13,9 +13,10 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ +// Registers the precheck unit tests. void add_tests_Prechecks(UnitTestDatabase& database); -} -} +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp index eb820e08b6..a4cde21233 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp @@ -40,7 +40,7 @@ class BlackBorderPrecheck : public Precheck{ : Precheck( "black_border", "Screen size at 100% with no black border, and the controller connected.", - PrecheckPolicy::REQUIRED + PrecheckPolicy::ALWAYS_RUNS ) {} virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const override{ @@ -56,7 +56,7 @@ class FreePartySlotsPrecheck : public Precheck{ : Precheck( "free_party_slots", std::format("At least {} free party slot(s), on the overworld where the game was saved.", free_slots_needed), - PrecheckPolicy::OPTIONAL + PrecheckPolicy::SKIPPABLE ) , m_free_slots_needed(free_slots_needed) {} @@ -95,6 +95,6 @@ PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_ne } -} -} -} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h index e3790cad0c..6e8d30cd4d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.h @@ -19,18 +19,18 @@ namespace PokemonFRLG{ // "black_border": goes Home to check the screen size and the controller, -// then resumes the game. Always runs. +// then resumes the game. ALWAYS_RUNS. std::unique_ptr make_black_border_precheck(); // "free_party_slots": opens the party menu from the overworld and checks -// that at least free_slots_needed slots are free. Optional. +// that at least free_slots_needed slots are free. SKIPPABLE. std::unique_ptr make_free_party_slots_precheck(size_t free_slots_needed); // The free_party_slots verdict for a party of party_size Pokemon. PrecheckOutcome free_party_slots_outcome(size_t party_size, size_t free_slots_needed); -} -} -} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp index c2bb6ca756..44b8b888c0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.cpp @@ -5,6 +5,7 @@ * The FRLG precheck lists and verdicts: no console, no GUI. */ +#include #include #include #include "Common/Cpp/TestRunners/UnitTest.h" @@ -27,22 +28,24 @@ struct ListedPrecheck{ bool operator==(const ListedPrecheck&) const = default; }; +struct RngPrechecksInput{ + size_t free_party_slots_needed; +}; + +struct RngPrechecksExpectation{ + std::vector checks; +}; + struct RngPrechecksCase{ - struct Input{ - size_t free_party_slots_needed; - }; - struct Expectation{ - std::vector checks; - }; std::string name; - Input input; - Expectation expectation; + RngPrechecksInput input; + RngPrechecksExpectation expectation; }; class Test_RngPrechecks : public UnitTest{ public: explicit Test_RngPrechecks(RngPrechecksCase test_case) - : UnitTest("PokemonFRLG::Prechecks::RngPrechecks - " + test_case.name) + : UnitTest(std::format("PokemonFRLG::Prechecks::RngPrechecks - {}", test_case.name)) , m_case(std::move(test_case)) {} @@ -50,7 +53,7 @@ class Test_RngPrechecks : public UnitTest{ std::vector listed; for (const std::unique_ptr& check : rng_prechecks(m_case.input.free_party_slots_needed)){ if (check->needs().empty()){ - return "Precheck " + check->name() + " must say what it needs."; + return std::format("Precheck {} must say what it needs.", check->name()); } listed.emplace_back(ListedPrecheck{check->name(), check->policy()}); } @@ -65,25 +68,27 @@ class Test_RngPrechecks : public UnitTest{ }; +struct FreePartySlotsInput{ + size_t party_size; + size_t free_slots_needed; +}; + +struct FreePartySlotsExpectation{ + PrecheckStatus status; + // Checked only on FAILED: the message the user sees. + std::string message; +}; + struct FreePartySlotsCase{ - struct Input{ - size_t party_size; - size_t free_slots_needed; - }; - struct Expectation{ - PrecheckStatus status; - // Checked only on FAILED: the message the user sees. - std::string message; - }; std::string name; - Input input; - Expectation expectation; + FreePartySlotsInput input; + FreePartySlotsExpectation expectation; }; class Test_FreePartySlotsOutcome : public UnitTest{ public: explicit Test_FreePartySlotsOutcome(FreePartySlotsCase test_case) - : UnitTest("PokemonFRLG::Prechecks::FreePartySlotsOutcome - " + test_case.name) + : UnitTest(std::format("PokemonFRLG::Prechecks::FreePartySlotsOutcome - {}", test_case.name)) , m_case(std::move(test_case)) {} @@ -92,10 +97,10 @@ class Test_FreePartySlotsOutcome : public UnitTest{ m_case.input.party_size, m_case.input.free_slots_needed ); if (outcome.status != m_case.expectation.status){ - return "Wrong verdict: \"" + outcome.message + "\""; + return std::format("Wrong verdict: \"{}\"", outcome.message); } if (outcome.status == PrecheckStatus::FAILED && outcome.message != m_case.expectation.message){ - return "Wrong message: \"" + outcome.message + "\""; + return std::format("Wrong message: \"{}\"", outcome.message); } return true; } @@ -105,54 +110,54 @@ class Test_FreePartySlotsOutcome : public UnitTest{ }; -std::vector rng_prechecks_cases(){ - const ListedPrecheck black_border{"black_border", PrecheckPolicy::REQUIRED}; - const ListedPrecheck free_party_slots{"free_party_slots", PrecheckPolicy::OPTIONAL}; - return { - {"NeedsNoSlots", {0}, {{black_border}}}, - {"NeedsOneSlot", {1}, {{black_border, free_party_slots}}}, - {"NeedsTwoSlots", {2}, {{black_border, free_party_slots}}}, - }; -} +const ListedPrecheck BLACK_BORDER{"black_border", PrecheckPolicy::ALWAYS_RUNS}; +const ListedPrecheck FREE_PARTY_SLOTS{"free_party_slots", PrecheckPolicy::SKIPPABLE}; + +const std::vector RNG_PRECHECKS_CASES{ + {.name = "NeedsNoSlots", .input = {0}, .expectation = {{BLACK_BORDER}}}, + {.name = "NeedsOneSlot", .input = {1}, .expectation = {{BLACK_BORDER, FREE_PARTY_SLOTS}}}, + {.name = "NeedsTwoSlots", .input = {2}, .expectation = {{BLACK_BORDER, FREE_PARTY_SLOTS}}}, +}; -std::vector free_party_slots_cases(){ - using enum PrecheckStatus; - return { - {"FiveOfSixNeedsOne", {5, 1}, {PASSED, ""}}, - { - "FullPartyNeedsOne", {6, 1}, - { - FAILED, - "Your party is full. Leave a party slot free so the program can check the " - "Pokémon it catches or receives.", - }, +// The messages are the ones the party check showed before it moved onto the +// precheck list. +const std::vector FREE_PARTY_SLOTS_CASES{ + {.name = "FiveOfSixNeedsOne", .input = {5, 1}, .expectation = {PrecheckStatus::PASSED, ""}}, + { + .name = "FullPartyNeedsOne", + .input = {6, 1}, + .expectation = { + PrecheckStatus::FAILED, + "Your party is full. Leave a party slot free so the program can check the " + "Pokémon it catches or receives.", }, - {"FourOfSixNeedsTwo", {4, 2}, {PASSED, ""}}, - { - "FiveOfSixNeedsTwo", {5, 2}, - { - FAILED, - "Your party has 5 Pokémon. Leave at least 2 party slots free for the " - "Pokémon the program catches or receives.", - }, + }, + {.name = "FourOfSixNeedsTwo", .input = {4, 2}, .expectation = {PrecheckStatus::PASSED, ""}}, + { + .name = "FiveOfSixNeedsTwo", + .input = {5, 2}, + .expectation = { + PrecheckStatus::FAILED, + "Your party has 5 Pokémon. Leave at least 2 party slots free for the " + "Pokémon the program catches or receives.", }, - }; -} + }, +}; } // namespace void add_tests_Prechecks(UnitTestDatabase& database){ - for (RngPrechecksCase& test_case : rng_prechecks_cases()){ - database.add(std::move(test_case)); + for (const RngPrechecksCase& test_case : RNG_PRECHECKS_CASES){ + database.add(test_case); } - for (FreePartySlotsCase& test_case : free_party_slots_cases()){ - database.add(std::move(test_case)); + for (const FreePartySlotsCase& test_case : FREE_PARTY_SLOTS_CASES){ + database.add(test_case); } } -} -} -} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h index b0f0af3f26..29d6a11284 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks_Tests.h @@ -14,10 +14,11 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ +// Registers the precheck unit tests. void add_tests_Prechecks(UnitTestDatabase& database); -} -} -} +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation #endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp index a32c0e7c78..3b95ab5762 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.cpp @@ -30,10 +30,10 @@ void RngProgramInstance::run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context ){ - const OptionalPrechecks optional = GameSettings::instance().RUN_RNG_START_CHECKS - ? OptionalPrechecks::RUN - : OptionalPrechecks::SKIP; - run_prechecks(env, context, prechecks(), optional); + const SkippablePrechecks skippable = GameSettings::instance().RUN_RNG_START_CHECKS + ? SkippablePrechecks::RUN + : SkippablePrechecks::SKIP; + run_prechecks(env, context, prechecks(), skippable); } } // namespace PokemonFRLG diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h index 6d0a0e4154..8e8cc64055 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/RngManipulation/PokemonFRLG_RngProgram.h @@ -43,7 +43,7 @@ class RngProgramInstance : public SingleSwitchProgramInstance{ protected: // Call first in program(), with the game on the overworld where the // player saved: runs prechecks(). "Run Start Checks" off in the FRLG Game - // Settings skips the optional ones. Throws UserSetupError if a check fails. + // Settings skips the SKIPPABLE ones. Throws UserSetupError if a check fails. void run_precheck( SingleSwitchProgramEnvironment& env, ProControllerContext& context From 9236b46c13f7b0da3a6ea51640151a75b7381620 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 15:54:41 -0800 Subject: [PATCH 3/3] Prechecks: log passed outcomes, explicit includes, accurate run() contract --- .../NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp | 4 ++++ .../Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h | 3 ++- .../Programs/NintendoSwitch_Prechecks_Tests.cpp | 1 + .../Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp | 2 ++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp index 42f0ceaa8d..dc4d5986a5 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "Common/Cpp/Exceptions.h" #include "NintendoSwitch_Prechecks.h" @@ -37,6 +38,9 @@ std::vector run_prechecks( if (outcome.status == PrecheckStatus::FAILED){ throw UserSetupError(logger, std::move(outcome.message)); } + if (outcome.status == PrecheckStatus::PASSED && !outcome.message.empty()){ + logger.log(std::format("Precheck {}: passed. {}", check->name(), outcome.message)); + } if (outcome.status == PrecheckStatus::SKIPPED){ logger.log(std::format("Precheck {}: skipped. {}", check->name(), outcome.message), COLOR_RED); } diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h index 992c6b5683..4c360fa34c 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks.h @@ -67,7 +67,8 @@ class Precheck{ const std::string& needs() const{ return m_needs; } PrecheckPolicy policy() const{ return m_policy; } - // Reads the console. Never resets, saves or changes options. + // May navigate (open a menu, go Home) but must return to the screen it started on. + // Never resets, saves or changes options. virtual PrecheckOutcome run(SingleSwitchProgramEnvironment& env, ProControllerContext& context) const = 0; private: diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp index 89315f6f41..fcf4ba3bd7 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/NintendoSwitch_Prechecks_Tests.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp index a4cde21233..10f830f46f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Prechecks.cpp @@ -5,10 +5,12 @@ */ #include +#include #include #include "CommonFramework/Exceptions/OperationFailedException.h" #include "Pokemon/Pokemon_Strings.h" #include "PokemonFRLG_Navigation.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_PartySlot.h" #include "PokemonFRLG_Prechecks.h" namespace PokemonAutomation{