Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Prechecks
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <utility>
#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<PrecheckResult> run_prechecks(
Logger& logger,
const PrecheckList& checks,
SkippablePrechecks skippable,
const std::function<PrecheckOutcome(const Precheck&)>& run_check
){
std::vector<PrecheckResult> results;
for (const std::unique_ptr<const Precheck>& check : checks){
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(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::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);
}
results.emplace_back(PrecheckResult{check->name(), std::move(outcome)});
}
return results;
}

std::vector<PrecheckResult> run_prechecks(
SingleSwitchProgramEnvironment& env,
ProControllerContext& context,
const PrecheckList& checks,
SkippablePrechecks skippable
){
return run_prechecks(
env.console.logger(), checks, skippable,
[&](const Precheck& check){
return check.run(env, context);
}
);
}


} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* 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 <functional>
#include <memory>
#include <string>
#include <vector>
#include "Common/Cpp/Logging/AbstractLogger.h"
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"

namespace PokemonAutomation{
namespace NintendoSwitch{


// How one precheck ended.
enum class PrecheckStatus{
PASSED,
FAILED,
SKIPPED,
};

// Whether a precheck can be turned off.
enum class PrecheckPolicy{
ALWAYS_RUNS,
SKIPPABLE, // Left out when the program turns its skippable checks off.
};

// Whether run_prechecks() runs the SKIPPABLE checks.
enum class SkippablePrechecks{
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; }

// 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:
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<std::unique_ptr<const Precheck>>;


// 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<PrecheckResult> run_prechecks(
Logger& logger,
const PrecheckList& checks,
SkippablePrechecks skippable,
const std::function<PrecheckOutcome(const Precheck&)>& run_check
);

// Runs the checks on the console. Call first in program().
std::vector<PrecheckResult> run_prechecks(
SingleSwitchProgramEnvironment& env,
ProControllerContext& context,
const PrecheckList& checks,
SkippablePrechecks skippable
);


} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/* Prechecks - Tests
*
* From: https://github.com/PokemonAutomation/
*
* run_prechecks() with fake checks: no console, no GUI.
*/

#include <format>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#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 RunPrechecksInput{
std::vector<FakeCheck> checks;
SkippablePrechecks skippable;
};

struct RunPrechecksExpectation{
// Names of the checks run_prechecks() ran, in order.
std::vector<std::string> ran;
// Set when run_prechecks() throws UserSetupError: its message.
std::optional<std::string> error;
// Statuses run_prechecks() returned, in order, when it did not throw.
std::vector<PrecheckStatus> statuses;
};

struct RunPrechecksCase{
std::string name;
RunPrechecksInput input;
RunPrechecksExpectation expectation;
};


// Runs the case's fake checks; returns what run_prechecks() did.
RunPrechecksExpectation run_fake_prechecks(Logger& logger, const RunPrechecksInput& input){
PrecheckList checks;
std::map<std::string, PrecheckOutcome> outcomes;
for (const FakeCheck& check : input.checks){
checks.emplace_back(std::make_unique<FakePrecheck>(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(std::format("NintendoSwitch::Prechecks::RunPrechecks - {}", test_case.name))
, m_case(std::move(test_case))
{}

virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{
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 (actual.error != expected.error){
return std::format("run_prechecks() threw the wrong error: \"{}\"", actual.error.value_or("(none)"));
}
if (actual.statuses != expected.statuses){
return "run_prechecks() returned the wrong statuses.";
}
return true;
}

private:
RunPrechecksCase m_case;
};


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<RunPrechecksCase> 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 (const RunPrechecksCase& test_case : RUN_PRECHECKS_CASES){
database.add<Test_RunPrechecks>(test_case);
}
}


} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* 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{


// Registers the precheck unit tests.
void add_tests_Prechecks(UnitTestDatabase& database);


} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading