Fix: Station disconnect from long clusters or during cluster merges - #5096
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review. Walkthrough
ChangesCluster reassignment behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This change prevents duplicate station registration from disconnecting stations while adding focused regression coverage; no actionable merge-blocking risk remains after normal checks and review. Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…5096) > **Before opening a PR:** discuss new features on [Discord](https://discord.gg/K9zernJB5z) first, and file bugs or small improvements as [issues](https://github.com/openfrontio/OpenFrontIO/issues/new/choose). You must be assigned to an `approved` issue — unsolicited PRs will be auto-closed. **Add approved & assigned issue number here:** None - small fix, big issue. ## Description: On the main Discord many bug reports started coming in about factories not producing trains. This took me a good while to investigate because the tests did seem to test connecting. [Thread with 9 reports](https://discord.com/channels/1284581928254701718/1530715070063972433) [Report 2 months ago](https://discord.com/channels/1284581928254701718/1520149582988710110) ### Bug origin - Introduced in PR #3185, Feb 13th - Added cluster disconnection logic to [TrainStation.setCluster](src/core/game/TrainStation.ts#L137-L143) - Unconditionally called removeStation without checking if the station was already in the target cluster. ### Mechanisms and triggers - [Cluster.addStation](src/core/game/TrainStation.ts#L174-L180) registers the station and invokes [TrainStation.setCluster](src/core/game/TrainStation.ts#L137-L143). - Calling addStation on a registered station caused setCluster to delete it from stations. - Empty tradeStations prevented factories from finding valid destinations, permanently stopping train production. - Small clusters avoided the bug -- a 4-hop distance check suppressed duplicate neighbor connections. - Snapping at rail junctions triggered the bug by processing multiple rail segments. - Branching tracks or factories that closed loops longer than 4 hops triggered duplicate connection calls. - Connecting networks across player borders forced cluster merges triggering the bug - June 2026 updates expanded range to 110 tiles and track length to 155 tiles, causing long-loop connections to form more frequently. ### Over-mocking in tests - [tests/core/game/RailNetwork.test.ts](tests/core/game/RailNetwork.test.ts) and [tests/core/game/Cluster.test.ts](tests/core/game/Cluster.test.ts) replaced setCluster with dummy vi.fn(). - Tests only checked that setCluster was called, did not check cluster state. - TrainStation and Cluster instances were never executed together during connection tests. ### Fixes/prevention - [TrainStation.setCluster](src/core/game/TrainStation.ts#L137-L143) now checks that this.cluster != new cluster - Duplicate addStation calls now retain all trade stations. - [tests/core/game/Cluster.test.ts](tests/core/game/Cluster.test.ts) updated to use TrainStation instances and directly test duplicate addStation. ## Please complete the following: - [x] I have added relevant tests to the test directory ## Please put your Discord username so you can be contacted if a bug or regression is found: JB940
Add approved & assigned issue number here:
None - small fix, big issue.
Description:
On the main Discord many bug reports started coming in about factories not producing trains. This took me a good while to investigate because the tests did seem to test connecting. Thread with 9 reports Report 2 months ago
Bug origin
Mechanisms and triggers
Over-mocking in tests
Fixes/prevention
Please complete the following:
Please put your Discord username so you can be contacted if a bug or regression is found:
JB940