From 2aa9928ad0d1d8706efd68a24739b51d812e6633 Mon Sep 17 00:00:00 2001 From: Algent Date: Thu, 27 Aug 2026 20:25:38 +0300 Subject: [PATCH] test: freeze generated trait and pass-through behavior --- JAVA_MIGRATION.md | 24 ++-- JAVA_MIGRATION_CONSUMER_AUDIT.md | 9 +- JAVA_MIGRATION_HANDOFF.md | 13 ++- .../test/ForgeEnvironmentSmokeTest.java | 105 ++++++++++++++++++ .../test/ForgeMultipartFunctionalTestMod.java | 6 + .../test/GeneratorPassThroughFixture.java | 13 +++ .../test/ExternalScalaMicroblockFixture.scala | 16 +++ 7 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 src/functionalTest/java/codechicken/multipart/test/GeneratorPassThroughFixture.java create mode 100644 src/functionalTest/scala/codechicken/multipart/test/ExternalScalaMicroblockFixture.scala diff --git a/JAVA_MIGRATION.md b/JAVA_MIGRATION.md index 62e6b89..c5f8532 100644 --- a/JAVA_MIGRATION.md +++ b/JAVA_MIGRATION.md @@ -24,14 +24,15 @@ No trustworthy tool will produce a maintainable Java port automatically. A decom ## Current status The binary and source-level consumer audits are complete. Phase 0 still lacks a pre-optimization profile. The -low-coupling Phase 3 queue is complete; before entering measured hot-path work, the immediate milestone is the -remaining generated-tile portion of the audit-derived compatibility gate below. +low-coupling Phase 3 queue and the audit-derived immediate compatibility gate are complete. The next milestone is a +focused CPU/allocation baseline before entering measured hot-path work. That audit found and this branch has now repaired one existing regression: Schematica 1.12.6 reflects the original private `MultiPartRegistry$` field `codechicken$multipart$MultiPartRegistry$$typeMap` and casts it to a Scala mutable map. The companion again exposes that exact private field as a live view of the canonical Java `HashMap`, and a test performs -Schematica's reflective lookup. The remaining source-only member guards, `TileMultipart` ordering/move lifecycle, and -compact mixed-part NBT/description packet are also frozen; generated-trait and pass-through fixtures are next. +Schematica's reflective lookup. The remaining source-only member guards, `TileMultipart` ordering/move lifecycle, +compact mixed-part NBT/description packet, generated Scala and Java traits, and a server-only pass-through interface +are also frozen. Phase 1 has not properly started: GTNHLib is present but was added at `api` scope to satisfy a test compile rather than because a migration change needed it, and no Forge mod dependency was declared. Revisit that entry only when a @@ -294,7 +295,7 @@ retained, as are both registries, the central part/tile types, scheduler/helper Exit condition: leaf code is Java and no longer generates avoidable Scala closures or collection adapters. -### Immediate compatibility gate — current next milestone +### Immediate compatibility gate — complete Do this before another source conversion: @@ -307,7 +308,7 @@ Do this before another source conversion: rebinding, add/remove/replace callback order, moving the live tile, and `onMoved`. - [x] Add compact mixed-part NBT and description-packet fixtures. The existing torch/button parts provide the needed distinct IDs, order, slots and payload without adding downstream mods or a test-only part registry. -- [ ] Freeze one generated Scala trait, one Java trait, and representative pass-through interfaces before changing +- [x] Freeze one generated Scala trait, one Java trait, and representative pass-through interfaces before changing built-in tile traits or either generator. Exit condition: every source-only constraint that can silently fail has an automated structural/behavioral guard, @@ -394,7 +395,7 @@ The work should move from low to high risk while keeping a buildable mixed-langu Extensive tests are both possible and recommended here. Their purpose is not to prove that the current Scala behavior is ideal; it is to make its observable behavior explicit before translation, so every later difference is intentional. The repository now has a Java-8-compatible JUnit Jupiter suite and a small Forge integration runner. The current -baseline is 130 plain-JVM tests and 25 Forge server tests. Minecraft 1.7.10 does not provide the modern GameTest +baseline is 130 plain-JVM tests and 28 Forge server tests. Minecraft 1.7.10 does not provide the modern GameTest framework, so game-dependent coverage continues through the existing narrow Forge harness rather than a second test framework. @@ -623,4 +624,11 @@ generated-trait compatibility work. Scala-runtime removal remains a later projec - Froze the complete logical chunk-description payload around the same torch/button pair: packet type, chunk coordinates, tile count, nibble-relative X/Z, full Y, part count, sorted runtime part IDs, part order and both meta bytes. CodeChickenLib's deferred Deflater transport step is deliberately outside this FMP fixture. -- The expanded baseline is 130 plain-JVM tests and 25 Forge server tests, all passing. +- Froze representative generated-trait behavior. The existing `TSlottedTile` fixture pins built-in Scala trait class + caching and field initialization; a ProjectRed-shaped external Scala microblock trait pins registration by name, + trait initialization and method dispatch; and `TPartialOcclusionTile` pins Java-trait rewriting and override + dispatch through the generated tile. +- Froze a server-only pass-through interface with primitive/reference overloads. The generated tile forwards to one + implementing part, rejects a second implementor, preserves and rebinds its delegate through `copyFrom`, and clears + the generated implementation field on removal. +- The expanded baseline is 130 plain-JVM tests and 28 Forge server tests, all passing. diff --git a/JAVA_MIGRATION_CONSUMER_AUDIT.md b/JAVA_MIGRATION_CONSUMER_AUDIT.md index 476cf8f..048486a 100644 --- a/JAVA_MIGRATION_CONSUMER_AUDIT.md +++ b/JAVA_MIGRATION_CONSUMER_AUDIT.md @@ -495,10 +495,17 @@ behavior: fields, before converting their owners. 3. Keep the completed `TileMultipart` list/map/slot and live-move characterization green before optimizing storage or iteration. -4. Characterize pass-through interfaces and ProjectRed's external Scala trait before changing either generator. +4. Keep the completed pass-through and ProjectRed-shaped external Scala-trait characterization green before changing + either generator. 5. Keep the completed compact mixed NBT/chunk-description fixture green; add subsystem-specific payload cases when converting microblocks or Minecraft part implementations. 6. Treat Scala runtime removal as downstream coordination work after the Java port, not as a requirement of it. +Current-branch generator verification: `ForgeEnvironmentSmokeTest` pins cached built-in `TSlottedTile` generation, +an external Scala microblock trait registered by name like ProjectRed's `LightMicroblock`, Java +`TPartialOcclusionTile` rewriting and override dispatch, and server-only pass-through forwarding, overloading, +single-implementor selection, copying/rebinding, and removal cleanup. Full downstream mixed tiles and client-side +selection remain Phase 7 integration cases. + With these gates, the source audit is actionable: the port can simplify internals freely where no listed observation changes, while each externally observed behavior has a named consumer and a focused way to prove compatibility. diff --git a/JAVA_MIGRATION_HANDOFF.md b/JAVA_MIGRATION_HANDOFF.md index d5457f3..48493a6 100644 --- a/JAVA_MIGRATION_HANDOFF.md +++ b/JAVA_MIGRATION_HANDOFF.md @@ -57,7 +57,7 @@ awk -F'"' '/ arguments = JavaConversions.asScalaBuffer(Collections.singletonList(0)).toList(); + + Microblock microblock = (Microblock) MicroblockGenerator.construct(traits, arguments); + Class trait = Class.forName("codechicken.multipart.test.ExternalScalaMicroblockFixture"); + + assertTrue(trait.isInterface()); + assertTrue(trait.isInstance(microblock)); + assertTrue(microblock.shouldRenderDynamic()); + assertEquals(41, microblock.getLightValue()); + assertEquals(41, ((Number) microblock.getClass().getMethod("fixtureState").invoke(microblock)).intValue()); + } + /** * Part registration needs FML's active mod container, so none of this is reachable from a plain JVM test. Startup * exercises both overloads downstream depends on: Content registers through the deprecated IPartFactory array form, @@ -149,4 +172,86 @@ class ForgeEnvironmentSmokeTest { assertSame(first.getClass(), second.getClass()); assertEquals(27, ((TSlottedTile) first).v_partMap().length); } + + @Test + void generatedJavaTraitOverridesPartialOcclusion() throws Exception { + PartialPart existing = new PartialPart(voxel(0)); + TileMultipart tile = MultipartHelper.createTileFromParts(Collections.singletonList(existing)); + Class trait = Class.forName("codechicken.multipart.scalatraits.TPartialOcclusionTile"); + + assertTrue(trait.isInterface(), "The registered Java mixin class is rewritten to a trait interface"); + assertTrue(trait.isInstance(tile)); + assertEquals( + boolean.class, + tile.getClass().getMethod("partialOcclusionTest", scala.collection.Seq.class).getReturnType()); + assertTrue(tile.canAddPart(new PartialPart(voxel(1)))); + assertFalse(tile.canAddPart(new PartialPart(voxel(0)))); + } + + @Test + void generatedServerPassThroughDelegatesCopiesAndAllowsOnlyOneImplementor() throws Exception { + PassThroughPart part = new PassThroughPart(); + TileMultipart tile = MultipartHelper.createTileFromParts(Collections.singletonList(part)); + + assertTrue(tile instanceof GeneratorPassThroughFixture); + GeneratorPassThroughFixture api = (GeneratorPassThroughFixture) tile; + assertEquals(12, api.transform(5)); + assertEquals("part:value", api.transform("value")); + assertSame(part, api.implementation()); + assertFalse(tile.canAddPart(new PassThroughPart())); + + TileMultipart copy = (TileMultipart) tile.getClass().getDeclaredConstructor().newInstance(); + copy.from(tile); + assertSame(part, ((GeneratorPassThroughFixture) copy).implementation()); + assertSame(copy, part.tile()); + + copy.partRemoved(part, 0); + assertTrue(copy.canAddPart(new PassThroughPart()), "Removing the delegate must clear the generated impl field"); + } + + private static Cuboid6 voxel(int x) { + return new Cuboid6(x / 8d, 0, 0, (x + 1) / 8d, 1 / 8d, 1 / 8d); + } + + private static final class PartialPart extends TMultiPart implements JPartialOcclusion { + + private final Cuboid6 box; + + private PartialPart(Cuboid6 box) { + this.box = box; + } + + @Override + public String getType() { + return "partial_fixture"; + } + + @Override + public Iterable getPartialOcclusionBoxes() { + return Collections.singletonList(box); + } + } + + private static final class PassThroughPart extends TMultiPart implements GeneratorPassThroughFixture { + + @Override + public String getType() { + return "pass_through_fixture"; + } + + @Override + public int transform(int value) { + return value + 7; + } + + @Override + public String transform(String value) { + return "part:" + value; + } + + @Override + public TMultiPart implementation() { + return this; + } + } } diff --git a/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartFunctionalTestMod.java b/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartFunctionalTestMod.java index b6c4e48..6436537 100644 --- a/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartFunctionalTestMod.java +++ b/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartFunctionalTestMod.java @@ -19,6 +19,8 @@ import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; +import codechicken.microblock.MicroblockGenerator; +import codechicken.multipart.MultipartGenerator; import cpw.mods.fml.common.Mod; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; @@ -39,6 +41,7 @@ public final class ForgeMultipartFunctionalTestMod { static boolean postInitialized; static boolean serverAboutToStart; static boolean serverStarted; + static int externalScalaMicroblockTraitId; @Mod.EventHandler public void preInit(FMLPreInitializationEvent event) { @@ -47,6 +50,9 @@ public final class ForgeMultipartFunctionalTestMod { @Mod.EventHandler public void init(FMLInitializationEvent event) { + MultipartGenerator.registerPassThroughInterface(GeneratorPassThroughFixture.class.getName(), false, true); + externalScalaMicroblockTraitId = MicroblockGenerator + .registerTrait("codechicken.multipart.test.ExternalScalaMicroblockFixture"); initialized = true; } diff --git a/src/functionalTest/java/codechicken/multipart/test/GeneratorPassThroughFixture.java b/src/functionalTest/java/codechicken/multipart/test/GeneratorPassThroughFixture.java new file mode 100644 index 0000000..10f582b --- /dev/null +++ b/src/functionalTest/java/codechicken/multipart/test/GeneratorPassThroughFixture.java @@ -0,0 +1,13 @@ +package codechicken.multipart.test; + +import codechicken.multipart.TMultiPart; + +/** Representative external tile API for generated pass-through characterization. */ +public interface GeneratorPassThroughFixture { + + int transform(int value); + + String transform(String value); + + TMultiPart implementation(); +} diff --git a/src/functionalTest/scala/codechicken/multipart/test/ExternalScalaMicroblockFixture.scala b/src/functionalTest/scala/codechicken/multipart/test/ExternalScalaMicroblockFixture.scala new file mode 100644 index 0000000..efda1e9 --- /dev/null +++ b/src/functionalTest/scala/codechicken/multipart/test/ExternalScalaMicroblockFixture.scala @@ -0,0 +1,16 @@ +package codechicken.multipart.test + +import codechicken.microblock.Microblock + +/** Minimal external Scala trait matching ProjectRed's MicroblockGenerator + * registration path. + */ +trait ExternalScalaMicroblockFixture extends Microblock { + private var state = 41 + + def fixtureState = state + + override def shouldRenderDynamic = true + + override def getLightValue = state +}