[PR #7153] [MERGED] Native mod API: Host methods default to a panic, so a test double implements only what it calls #7378

Closed
opened 2026-11-03 06:38:38 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7153
Author: @arismoko
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: v2/ses_01M6DYMGWF02H5RBZ0RFVQ78M9-test-host


📝 Commits (10+)

  • 7906dcd Give every mod API Host method a default body that panics with its name
  • 57693fb Test a trait mod's hook with a partial Host double
  • dc14ce3 Move the trait-mod testing section before Adding a hook
  • 9db36ed docs: list the native-mod values a test outside horizon-client can't build
  • c16e5e3 docs: correct the native-mod values a test outside horizon-client can't build
  • 1ec8980 Document testing a trait mod through dispatch functions
  • 08a0f3e Clarify testing unconstructible mod API values
  • 7c7f781 Fix test guidance sentence
  • 0c9e0a3 Merge branch 'main' of https://github.com/horizon-rust/horizon into v2/ses_01M6DYMGWF02H5RBZ0RFVQ78M9-test-host
  • 4dcbdd2 Merge main into #7153

📊 Changes

4 files changed (+703 additions, -114 deletions)

View changed files

📝 crates/horizon-client/src/mod_api.rs (+98 -0)
📝 crates/horizon-client/src/mod_api/generated_host.rs (+572 -111)
📝 docs/native-mods.md (+24 -1)
📝 xtask/src/mod_api.rs (+9 -2)

📄 Description

🤖 Opened by Aria's agent swarm, Dyna, not by Aria.

Part of #6452 ("A test Host").

What it does

It is hard to unit-test a trait mod's hook today. Host has 111 required methods and no default bodies, so a test double has to write all of them. This PR has the mod-API generator (cargo run -p xtask -- mod-api) give every Host method a default body that panics with the method's name (not implemented: Host::tell). A test double now implements only the methods its hook calls. A call it didn't plan for fails at once, naming the method, so it can't answer silently.

  • The game is unaffected. impl Host for Game is still generated from the same helpers list in the same run and overrides every method, and generated_output_is_current keeps the two in step. mod_api_host_generated.rs, dispatch and the Hooks trait don't change.
  • This matches the generated Hooks trait, whose methods already have default bodies.
  • docs/native-mods.md gains a short "Testing a trait mod" paragraph with an example. It says to implement only the Host methods the hook calls and to drive the hook through its dispatch_<hook> function, which builds the event as the game does (events stay #[non_exhaustive]). It also notes which game values a test outside horizon-client can't build.

Deliberately not here: a no-op TestHost that answers false or None (that would hide mistakes), constructors for events or NativeServerPlayer, and changes to the level hosts, which have 2 to 4 methods each.

Most of the diff is the regenerated generated_host.rs. The hand-written change is the declaration line in host_method, one doc line, two tests and the doc paragraph.

NeoForge counterpart

NeoForge has no mock level for unit tests. Its nearest counterpart is the GameTest framework (GameTestHelper, https://docs.neoforged.net/docs/misc/gametest/), which runs tests in a real game. This PR covers plain cargo test of a hook. The in-game route below stays the end-to-end check.

Tests

  • mod_api::tests::a_test_host_implements_only_what_its_hook_calls: a double that implements only block, acting_player and action_bar drives use_item_on through dispatch_use_item_on. The hook claims the use and sends its line for the matching block, and leaves other blocks alone.
  • mod_api::tests::an_unexpected_host_call_names_the_method: a call the double doesn't implement panics with Host::tell.

Tested in game

Used the recommended horizon-machine-sample end-to-end route: built the trait-api-example driver at this PR's exact head and ran it on the integrated server thread. The server logged MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld. Sneak-using a stick on the sample machine logged MACHINE_SAMPLE server read #1 and displayed the server read in the action bar. There was no not implemented: Host:: panic.

In the sample's machine-hooks, a test drove use_item_on through dispatch with a double implementing only block and acting_player. It passed at this head; the same test failed to compile with E0046 at the earlier base cabe8b5de7.

pr_ready

Run at head 0c9e0a35635bfe54655652f2c0d9a7dc9085e2e2:

pr_ready (tools/pr_ready.py --quick --base 9cf477cc53bb9029366e2e3d01be9074382150c8 on 92a79a19501e, against 9cf477cc53 at 9cf477cc5, 12 min 49 s)

Check Result
Scope ⚠️ 4 files, +703 −114; 792 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ✅ 612 passed (46 s)
rustfmt ✅ (18 s)
clippy ✅ (2 min 3 s)
tests: horizon-client-lib, xtask ✅ 1440 passed (8 min 44 s)
doc tests ✅ 0 passed (29 s)
content root tests ➖ content root untouched
mod API up to date ✅ (24 s)
tokens.css up to date ✅ (1 s)
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 374 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, attribute_health, attribute_map_queries, attribute, attribute_updates and 362 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The scope warning counts the regenerated generated_host.rs (+683 −114 of the total): every Host method gains its two-line default body. The hand-written change is 11 lines in xtask/src/mod_api.rs, the two tests and the doc paragraph.

No dedicated-server run: the change touches no server code, only the trait default bodies the game overrides.

🤖 Generated with Dyna.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/7153 **Author:** [@arismoko](https://github.com/arismoko) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `v2/ses_01M6DYMGWF02H5RBZ0RFVQ78M9-test-host` --- ### 📝 Commits (10+) - [`7906dcd`](https://github.com/horizon-rust/horizon/commit/7906dcd4f459a8de5a66b17fe599e7460ead03be) Give every mod API Host method a default body that panics with its name - [`57693fb`](https://github.com/horizon-rust/horizon/commit/57693fbf63cb1c799802c8c35ef2c04b43a08cad) Test a trait mod's hook with a partial Host double - [`dc14ce3`](https://github.com/horizon-rust/horizon/commit/dc14ce3c6e0759cecd9f58269d8d51829a86b4fe) Move the trait-mod testing section before Adding a hook - [`9db36ed`](https://github.com/horizon-rust/horizon/commit/9db36edb93d89062e5555a6f7c1ca19c37914a64) docs: list the native-mod values a test outside horizon-client can't build - [`c16e5e3`](https://github.com/horizon-rust/horizon/commit/c16e5e3c3311e290c972800723d8c7305e477f6a) docs: correct the native-mod values a test outside horizon-client can't build - [`1ec8980`](https://github.com/horizon-rust/horizon/commit/1ec8980e6a9d3ad5f8faf0579c98f5269705eeb8) Document testing a trait mod through dispatch functions - [`08a0f3e`](https://github.com/horizon-rust/horizon/commit/08a0f3e3b12d7b8a9cd407a668c71042d5749efc) Clarify testing unconstructible mod API values - [`7c7f781`](https://github.com/horizon-rust/horizon/commit/7c7f781095bcf4171b0644d57c9f6fab6d9f2ca0) Fix test guidance sentence - [`0c9e0a3`](https://github.com/horizon-rust/horizon/commit/0c9e0a35635bfe54655652f2c0d9a7dc9085e2e2) Merge branch 'main' of https://github.com/horizon-rust/horizon into v2/ses_01M6DYMGWF02H5RBZ0RFVQ78M9-test-host - [`4dcbdd2`](https://github.com/horizon-rust/horizon/commit/4dcbdd27fc8b7297d479eaf76b098f4df8248eb3) Merge main into #7153 ### 📊 Changes **4 files changed** (+703 additions, -114 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/mod_api.rs` (+98 -0) 📝 `crates/horizon-client/src/mod_api/generated_host.rs` (+572 -111) 📝 `docs/native-mods.md` (+24 -1) 📝 `xtask/src/mod_api.rs` (+9 -2) </details> ### 📄 Description > 🤖 Opened by Aria's agent swarm, Dyna, not by Aria. Part of #6452 ("A test `Host`"). ## What it does It is hard to unit-test a trait mod's hook today. `Host` has 111 required methods and no default bodies, so a test double has to write all of them. This PR has the mod-API generator (`cargo run -p xtask -- mod-api`) give every `Host` method a default body that panics with the method's name (`not implemented: Host::tell`). A test double now implements only the methods its hook calls. A call it didn't plan for fails at once, naming the method, so it can't answer silently. - The game is unaffected. `impl Host for Game` is still generated from the same `helpers` list in the same run and overrides every method, and `generated_output_is_current` keeps the two in step. `mod_api_host_generated.rs`, dispatch and the `Hooks` trait don't change. - This matches the generated `Hooks` trait, whose methods already have default bodies. - docs/native-mods.md gains a short "Testing a trait mod" paragraph with an example. It says to implement only the `Host` methods the hook calls and to drive the hook through its `dispatch_<hook>` function, which builds the event as the game does (events stay `#[non_exhaustive]`). It also notes which game values a test outside horizon-client can't build. Deliberately not here: a no-op `TestHost` that answers false or None (that would hide mistakes), constructors for events or `NativeServerPlayer`, and changes to the level hosts, which have 2 to 4 methods each. Most of the diff is the regenerated `generated_host.rs`. The hand-written change is the declaration line in `host_method`, one doc line, two tests and the doc paragraph. ## NeoForge counterpart NeoForge has no mock level for unit tests. Its nearest counterpart is the GameTest framework (`GameTestHelper`, https://docs.neoforged.net/docs/misc/gametest/), which runs tests in a real game. This PR covers plain `cargo test` of a hook. The in-game route below stays the end-to-end check. ## Tests - `mod_api::tests::a_test_host_implements_only_what_its_hook_calls`: a double that implements only `block`, `acting_player` and `action_bar` drives `use_item_on` through `dispatch_use_item_on`. The hook claims the use and sends its line for the matching block, and leaves other blocks alone. - `mod_api::tests::an_unexpected_host_call_names_the_method`: a call the double doesn't implement panics with `Host::tell`. ## Tested in game Used the recommended [horizon-machine-sample](https://github.com/arismoko/horizon-machine-sample) end-to-end route: built the `trait-api-example` driver at this PR's exact head and ran it on the integrated server thread. The server logged `MACHINE_SAMPLE server clock: 200 ticks in minecraft:overworld`. Sneak-using a stick on the sample machine logged `MACHINE_SAMPLE server read #1` and displayed the server read in the action bar. There was no `not implemented: Host::` panic. In the sample's `machine-hooks`, a test drove `use_item_on` through dispatch with a double implementing only `block` and `acting_player`. It passed at this head; the same test failed to compile with E0046 at the earlier base `cabe8b5de7`. ## pr_ready Run at head `0c9e0a35635bfe54655652f2c0d9a7dc9085e2e2`: **pr_ready** (`tools/pr_ready.py --quick --base 9cf477cc53bb9029366e2e3d01be9074382150c8` on 92a79a19501e, against 9cf477cc53bb9029366e2e3d01be9074382150c8 at 9cf477cc5, 12 min 49 s) | Check | Result | | --- | --- | | Scope | ⚠️ 4 files, +703 −114; 792 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ✅ 612 passed (46 s) | | rustfmt | ✅ (18 s) | | clippy | ✅ (2 min 3 s) | | tests: horizon-client-lib, xtask | ✅ 1440 passed (8 min 44 s) | | doc tests | ✅ 0 passed (29 s) | | content root tests | ➖ content root untouched | | mod API up to date | ✅ (24 s) | | tokens.css up to date | ✅ (1 s) | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 374 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, armor_stand_frost, armor_stand_turtle_egg, arrow, arrow_wind_charge, attribute_health, attribute_map_queries, attribute, attribute_updates and 362 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The scope warning counts the regenerated `generated_host.rs` (+683 −114 of the total): every `Host` method gains its two-line default body. The hand-written change is 11 lines in `xtask/src/mod_api.rs`, the two tests and the doc paragraph. No dedicated-server run: the change touches no server code, only the trait default bodies the game overrides. 🤖 Generated with Dyna. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:38:38 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#7378
No description provided.