Сократить lifecycle overhead ранних ботов

This commit is contained in:
2026-08-18 03:19:52 +03:00
parent 70106e29d8
commit 6790a84d86
2 changed files with 45 additions and 18 deletions
@@ -17,30 +17,27 @@ import static org.junit.Assert.assertTrue;
public final class EarlyLevelPlaythroughTest {
@Test
public void levelOneProvesMovementAndJumpWithoutDeaths() {
for (int pass = 1; pass <= 3; pass++) {
LevelPlaytestBot.Result result = run(EarlyLevelScenarios.levelOne(), 20_000L);
runThreeConsecutive(EarlyLevelScenarios.levelOne(), 20_000L, (pass, result) -> {
assertTrue(passDiagnostic(pass, result), result.completed());
assertEquals("Pass " + pass + ": level 1 must not require a volunteer",
0, result.deaths());
}
});
}
@Test
public void levelTwoBuildsTheIntendedThreeCorpseBridge() {
for (int pass = 1; pass <= 3; pass++) {
LevelPlaytestBot.Result result = run(EarlyLevelScenarios.levelTwo(), 30_000L);
runThreeConsecutive(EarlyLevelScenarios.levelTwo(), 30_000L, (pass, result) -> {
assertTrue(passDiagnostic(pass, result), result.completed());
assertEquals("Pass " + pass + ": the intended bridge uses three volunteers",
3, result.deaths());
assertEquals("Pass " + pass + ": all three bridge bodies must remain",
3, result.corpseCount());
}
});
}
@Test
public void levelThreeLeavesASawCorpseOnThePlateAndOpensTheDoor() {
for (int pass = 1; pass <= 3; pass++) {
LevelPlaytestBot.Result result = run(EarlyLevelScenarios.levelThree(), 25_000L);
runThreeConsecutive(EarlyLevelScenarios.levelThree(), 25_000L, (pass, result) -> {
assertTrue(passDiagnostic(pass, result), result.completed());
assertEquals("Pass " + pass + ": the saw solution requires one volunteer",
1, result.deaths());
@@ -48,35 +45,41 @@ public final class EarlyLevelPlaythroughTest {
1, result.corpseCount());
assertTrue("Pass " + pass + ": the corpse must keep the pressure door open",
result.doorOpened());
}
});
}
@Test
public void levelFourTogglesAndRidesTheLiftToTheUpperGallery() {
for (int pass = 1; pass <= 3; pass++) {
LevelPlaytestBot.Result result = run(EarlyLevelScenarios.levelFour(), 30_000L);
runThreeConsecutive(EarlyLevelScenarios.levelFour(), 30_000L, (pass, result) -> {
assertTrue(passDiagnostic(pass, result), result.completed());
assertEquals("Pass " + pass + ": the lift route must not require a volunteer",
0, result.deaths());
}
});
}
private static String passDiagnostic(int pass, LevelPlaytestBot.Result result) {
return "Consecutive pass " + pass + ": " + result.diagnostic();
}
private static LevelPlaytestBot.Result run(LevelPlaytestBot.Scenario scenario, long timeoutMillis) {
private static void runThreeConsecutive(LevelPlaytestBot.Scenario scenario, long timeoutMillis,
ResultAssertion assertion) {
try (ActivityScenario<MainActivity> activity = ActivityScenario.launch(MainActivity.class)) {
final GameView[] view = new GameView[1];
activity.onActivity(mainActivity -> view[0] = mainActivity.gameViewForAutomation());
return new LevelPlaytestBot().run(
new AndroidDriver(InstrumentationRegistry.getInstrumentation(), view[0]),
scenario,
timeoutMillis
);
AndroidDriver driver = new AndroidDriver(
InstrumentationRegistry.getInstrumentation(), view[0]);
LevelPlaytestBot bot = new LevelPlaytestBot();
for (int pass = 1; pass <= 3; pass++) {
assertion.verify(pass, bot.run(driver, scenario, timeoutMillis));
}
}
}
@FunctionalInterface
private interface ResultAssertion {
void verify(int pass, LevelPlaytestBot.Result result);
}
private static final class AndroidDriver implements LevelPlaytestBot.Driver {
private final Instrumentation instrumentation;
private final GameView gameView;