diff --git a/.github/workflows/modrinth.yml b/.github/workflows/modrinth.yml index ec51e18..5c1d153 100644 --- a/.github/workflows/modrinth.yml +++ b/.github/workflows/modrinth.yml @@ -1,6 +1,8 @@ name: Publish Mod -on: [ pull_request, workflow_dispatch ] +on: + pull_request: + types: [ closed ] env: MOD_VERSION: 2.3.1 @@ -10,17 +12,19 @@ permissions: jobs: build: + if: github.event.pull_request.merged == true runs-on: ubuntu-latest permissions: contents: write steps: - name: Checkout Repository - uses: actions/checkout@v4 + uses: actions/checkout@v6 with: + ref: ${{ github.event.pull_request.merge_commit_sha }} submodules: true - name: Setup Java - uses: actions/setup-java@v4 + uses: actions/setup-java@v5 with: distribution: temurin java-version: | @@ -31,9 +35,9 @@ jobs: run: bash ./gradlew buildAndCollect - name: Create GitHub Release - uses: softprops/action-gh-release@v2 + uses: softprops/action-gh-release@v3 with: - token: ${{ secrets.G_TOKEN }} + token: ${{ secrets.GITHUB_TOKEN }} tag_name: ${{ env.MOD_VERSION }} name: ${{ env.MOD_VERSION }} files: build/libs/${{ env.MOD_VERSION }}/*.jar diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..04e7c29 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,33 @@ +name: Test + +on: [ pull_request ] + +jobs: + test: + runs-on: ubuntu-latest + # A Minecraft client that fails to start can hang instead of exiting; don't burn the 6h default. + timeout-minutes: 60 + steps: + - name: Checkout Repository + uses: actions/checkout@v6 + with: + submodules: true + + - name: Setup Java + uses: actions/setup-java@v5 + with: + distribution: temurin + java-version: | + 21 + 25 + + # MC 26.3 (SDL) can't get an OpenGL context under Xvfb and falls back to Vulkan; this provides + # Mesa's software Vulkan driver (lavapipe). Without it the 26.3 client hangs on startup. + - name: Install Software Vulkan Driver + run: sudo apt-get update && sudo apt-get install -y mesa-vulkan-drivers + + # Loom runs each client under xvfb-run on CI, so no display setup is needed here. + # Capped workers: each client is memory/GPU heavy and too many in parallel can hang. + # --continue so one failing version doesn't hide the results of the others. + - name: Run Client Game Tests + run: bash ./gradlew runTestClient --max-workers=2 --continue diff --git a/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/RepeatLastTests.java b/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/RepeatLastTests.java index 0109538..00ba411 100644 --- a/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/RepeatLastTests.java +++ b/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/RepeatLastTests.java @@ -20,6 +20,13 @@ public class RepeatLastTests extends OneClickTests { public static int REPEAT_KEY_CODE = InputConstants.KEY_R; + /** Upper bound for holding the repeat key until all crafts are done (slow CI machines need far more + * ticks than a desktop, so the hold waits on the result instead of a fixed tick count). */ + private static final int REPEAT_TIMEOUT_TICKS = 400; + /** Keep holding briefly after the last craft: releasing mid-attempt can leave the client inventory + * out of sync with the server until the next repeat attempt resyncs it. */ + private static final int REPEAT_SETTLE_TICKS = 20; + public RepeatLastTests(ClientGameTestContext context, TestSingleplayerContext world) { super(context, world); } @@ -75,11 +82,8 @@ public void repeatLastStack() { wait(2); input.holdRepeatKey(); - //? if <1.21.8 { - /*wait(200); - *///? } else { - wait(65); - //? } + waitUntil(mc -> inventoryCount(mc, ctx.outputItem) >= ctx.outputCount * 64, REPEAT_TIMEOUT_TICKS); + wait(REPEAT_SETTLE_TICKS); input.releaseRepeatKey(); ctx.close(); @@ -101,11 +105,8 @@ public void repeatLastStacksFullInventory() { input.holdShift(); input.holdRepeatKey(); - //? if <1.21.8 { - /*wait(35); - *///? } else { - wait(24); - //? } + waitUntil(mc -> inventoryCount(mc, ctx.outputItem) >= ctx.outputCount * 9 * 64, REPEAT_TIMEOUT_TICKS); + wait(REPEAT_SETTLE_TICKS); input.releaseShift(); input.releaseRepeatKey(); @@ -130,11 +131,8 @@ public void repeatLastDropKeyDropsManyItems() { wait(2); input.holdRepeatKey(); - //? if <1.21.8 { - /*wait(200); - *///? } else { - wait(66); - //? } + waitUntil(mc -> groundCount(mc, ctx.outputItem) >= ctx.outputCount * 64, REPEAT_TIMEOUT_TICKS); + wait(REPEAT_SETTLE_TICKS); input.releaseRepeatKey(); input.releaseDrop(); diff --git a/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/TestSuite.java b/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/TestSuite.java index 3a07b13..8d05327 100644 --- a/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/TestSuite.java +++ b/src/test/java/com/github/breadmoirai/oneclickcrafting/testmod/suite/TestSuite.java @@ -8,6 +8,7 @@ import com.mojang.blaze3d.platform.InputConstants; import com.github.breadmoirai.oneclickcrafting.mixin.KeyMappingAccessor; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.InventoryScreen; import net.minecraft.core.BlockPos; @@ -24,6 +25,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Predicate; @SuppressWarnings("UnstableApiUsage") public abstract class TestSuite { @@ -144,6 +146,19 @@ protected void wait(int ticks) { context.waitTicks(ticks); } + /** + * Waits until {@code condition} holds on the client, or {@code timeoutTicks} pass. A timeout is not a + * failure: use this to bound a held key that should be released once the work is done (so the test + * doesn't depend on how many ticks the machine needs), then assert on the result. + */ + protected void waitUntil(Predicate condition, int timeoutTicks) { + try { + context.waitFor(condition, timeoutTicks); + } catch (AssertionError timeout) { + // The assertion that follows reports what actually happened. + } + } + // ------------------------------------------------------------------------- // Inventory assertions // ------------------------------------------------------------------------- @@ -186,16 +201,7 @@ protected void assertInventoryExact(Map items) { } protected void assertInventoryCount(String itemId, int expectedCount) { - int actual = context.computeOnClient(mc -> { - if (mc.player == null) return 0; - var inv = mc.player.getInventory(); - int total = 0; - for (int i = 0; i < inv.getContainerSize(); i++) { - ItemStack stack = inv.getItem(i); - if (BuiltInRegistries.ITEM.getKey(stack.getItem()).toString().equals(itemId)) total += stack.getCount(); - } - return total; - }); + int actual = context.computeOnClient(mc -> inventoryCount(mc, itemId)); if (actual != expectedCount) { throw new AssertionError( "Expected exactly %d of %s in inventory, found %d" @@ -205,27 +211,9 @@ protected void assertInventoryCount(String itemId, int expectedCount) { protected void assertInventoryAtLeast(String itemId, int minCount) { try { - context.waitFor(mc -> { - if (mc.player == null) return false; - var inv = mc.player.getInventory(); - int total = 0; - for (int i = 0; i < inv.getContainerSize(); i++) { - ItemStack stack = inv.getItem(i); - if (BuiltInRegistries.ITEM.getKey(stack.getItem()).toString().equals(itemId)) total += stack.getCount(); - } - return total >= minCount; - }, 20); + context.waitFor(mc -> inventoryCount(mc, itemId) >= minCount, 20); } catch (AssertionError timeout) { - int actual = context.computeOnClient(mc -> { - if (mc.player == null) return 0; - var inv = mc.player.getInventory(); - int total = 0; - for (int i = 0; i < inv.getContainerSize(); i++) { - ItemStack stack = inv.getItem(i); - if (BuiltInRegistries.ITEM.getKey(stack.getItem()).toString().equals(itemId)) total += stack.getCount(); - } - return total; - }); + int actual = context.computeOnClient(mc -> inventoryCount(mc, itemId)); throw new AssertionError( "Expected >= %d of %s in inventory, found %d" .formatted(minCount, itemId, actual)); @@ -274,6 +262,27 @@ protected int stacks(int stacks) { return stacks * 64; } + /** Total count of {@code itemId} in the player's inventory (0 if there is no player). */ + protected static int inventoryCount(Minecraft mc, String itemId) { + if (mc.player == null) return 0; + var inv = mc.player.getInventory(); + int total = 0; + for (int i = 0; i < inv.getContainerSize(); i++) { + ItemStack stack = inv.getItem(i); + if (BuiltInRegistries.ITEM.getKey(stack.getItem()).toString().equals(itemId)) total += stack.getCount(); + } + return total; + } + + /** Total count of {@code itemId} in item entities within 16 blocks of the player. */ + protected static int groundCount(Minecraft mc, String itemId) { + if (mc.player == null || mc.level == null) return 0; + AABB box = mc.player.getBoundingBox().inflate(16.0); + return mc.level.getEntitiesOfClass(ItemEntity.class, box, + e -> BuiltInRegistries.ITEM.getKey(e.getItem().getItem()).toString().equals(itemId)) + .stream().mapToInt(e -> e.getItem().getCount()).sum(); + } + // ------------------------------------------------------------------------- // Private helpers // -------------------------------------------------------------------------