-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: preserve null gold in DonateGoldExecution so auto-donate works (BUG-03) #3944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -183,6 +183,74 @@ describe("Donate troops to a non ally", () => { | |||||||||||||||||||
| }); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| describe("Donate gold with null goldNum (auto-donate fallback)", () => { | ||||||||||||||||||||
| it("Should auto-donate 1/3 of sender gold when goldNum is null", async () => { | ||||||||||||||||||||
| const game = await setup("ocean_and_land", { | ||||||||||||||||||||
| infiniteGold: false, | ||||||||||||||||||||
| donateGold: true, | ||||||||||||||||||||
| }); | ||||||||||||||||||||
| const gameID: GameID = "game_id"; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const donorInfo = new PlayerInfo( | ||||||||||||||||||||
| "donor", | ||||||||||||||||||||
| PlayerType.Human, | ||||||||||||||||||||
| null, | ||||||||||||||||||||
| "donor_id", | ||||||||||||||||||||
| ); | ||||||||||||||||||||
| const recipientInfo = new PlayerInfo( | ||||||||||||||||||||
| "recipient", | ||||||||||||||||||||
| PlayerType.Human, | ||||||||||||||||||||
| null, | ||||||||||||||||||||
| "recipient_id", | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| game.addPlayer(donorInfo); | ||||||||||||||||||||
| game.addPlayer(recipientInfo); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| const donor = game.player(donorInfo.id); | ||||||||||||||||||||
| const recipient = game.player(recipientInfo.id); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Spawn both players | ||||||||||||||||||||
| const spawnA = game.ref(0, 10); | ||||||||||||||||||||
| const spawnB = game.ref(0, 15); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| game.addExecution( | ||||||||||||||||||||
| new SpawnExecution(gameID, donorInfo, spawnA), | ||||||||||||||||||||
| new SpawnExecution(gameID, recipientInfo, spawnB), | ||||||||||||||||||||
| ); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // donor sends alliance request to recipient | ||||||||||||||||||||
| const allianceRequest = donor.createAllianceRequest(recipient); | ||||||||||||||||||||
| expect(allianceRequest).not.toBeNull(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // recipient accepts the alliance request | ||||||||||||||||||||
| if (allianceRequest) { | ||||||||||||||||||||
| allianceRequest.accept(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| game.executeNextTick(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Give donor a known amount of gold | ||||||||||||||||||||
| donor.addGold(9000n); | ||||||||||||||||||||
| const donorGoldBefore = donor.gold(); | ||||||||||||||||||||
| const recipientGoldBefore = recipient.gold(); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Pass null as goldNum — should trigger auto-donate of sender.gold() / 3n | ||||||||||||||||||||
| game.addExecution(new DonateGoldExecution(donor, recipientInfo.id, null)); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for (let i = 0; i < 5; i++) { | ||||||||||||||||||||
| game.executeNextTick(); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| // Donor should have lost gold | ||||||||||||||||||||
| expect(donor.gold() < donorGoldBefore).toBe(true); | ||||||||||||||||||||
| // Recipient should have received gold | ||||||||||||||||||||
| expect(recipient.gold() > recipientGoldBefore).toBe(true); | ||||||||||||||||||||
| // Check donor lost roughly 1/3 of their gold | ||||||||||||||||||||
| const donorLost = donorGoldBefore - donor.gold(); | ||||||||||||||||||||
| expect(donorLost).toBeGreaterThan(0n); | ||||||||||||||||||||
|
Comment on lines
+248
to
+250
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the expected auto-donate amount, not just This regression can still pass if donation is incorrect but positive. Please assert against Suggested tightening- // Check donor lost roughly 1/3 of their gold
+ // Check null path donates one-third of sender gold
+ const expectedAutoDonate = donorGoldBefore / 3n;
const donorLost = donorGoldBefore - donor.gold();
+ const recipientGained = recipient.gold() - recipientGoldBefore;
- expect(donorLost).toBeGreaterThan(0n);
+ expect(donorLost).toBeGreaterThan(0n);
+ expect(recipientGained).toBeGreaterThanOrEqual(expectedAutoDonate);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||
| }); | ||||||||||||||||||||
| }); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| describe("Donate Gold to a non ally", () => { | ||||||||||||||||||||
| it("Gold should not be donated", async () => { | ||||||||||||||||||||
| const game = await setup("ocean_and_land", { | ||||||||||||||||||||
|
|
||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: openfrontio/OpenFrontIO
Length of output: 119
🏁 Script executed:
Repository: openfrontio/OpenFrontIO
Length of output: 4882
🏁 Script executed:
Repository: openfrontio/OpenFrontIO
Length of output: 433
🏁 Script executed:
Repository: openfrontio/OpenFrontIO
Length of output: 605
Add test for auto-donate scenario (null goldNum).
The fix correctly preserves
nullat line 33 to enable the auto-donate fallback at line 49. However, no test cases cover the scenario wheregoldNumisnull. Per coding guidelines, all changes tosrc/core/must include tests. Please add a test case that verifies: whenDonateGoldExecutionis instantiated withgoldNum = null, the donation amount defaults tosender.gold() / 3nduring initialization.🤖 Prompt for AI Agents