Files
galaxy-game/game/internal/controller/ship_group_upgrade_test.go
T
Ilia Denisov b4abf90ec5
Tests · Go / test (push) Successful in 1m58s
Tests · Integration / integration (pull_request) Successful in 1m50s
Tests · Go / test (pull_request) Successful in 2m5s
fix(game): fight before departure and reorder the turn sequence
Per the documented turn order (game/rules.txt "Последовательность
действий"), no ship should dodge the pre-departure battle by slipping
into hyperspace. MakeTurn now runs merge -> battle -> load+launch routed
groups -> fly -> merge -> battle, so:

- ships ordered to depart (Launched) and ships being upgraded now take
  part in the pre-departure battle at their planet (CollectPlanetGroups /
  FilterBattleGroups); only survivors then enter hyperspace;
- routed transports are loaded and launched AFTER that battle, so they
  fight empty and cannot escape it.

A just-launched group has no stored hyperspace position, so moveShipGroup
starts its first leg from the origin planet; the previous code read the
nil launch coordinate and would panic.

Because upgrading groups can now lose ships in the battle, the pending
upgrade cost is recomputed from the group's current ship count instead of
the value stored when the order was validated.

Rules: reordered "Последовательность действий" and rewrote the combat
note that ordered/routed ships skip the battle.

Tests: launched-group move from origin, launched/upgrade groups taking
part in battle, upgrade cost tracking ship losses.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 00:25:46 +02:00

194 lines
7.4 KiB
Go

package controller_test
import (
"testing"
e "galaxy/error"
"galaxy/game/internal/controller"
"galaxy/game/internal/model/game"
g "galaxy/game/internal/model/game"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestGroupUpgradeCost(t *testing.T) {
sg := &g.ShipGroup{
Tech: map[g.Tech]g.Float{
g.TechDrive: 1.0,
g.TechWeapons: 1.0,
g.TechShields: 1.0,
g.TechCargo: 1.0,
},
Number: 1,
}
assert.Equal(t, 225.0, controller.GroupUpgradeCost(sg, Cruiser, 2.0, 2.0, 2.0, 2.0).UpgradeCost(1))
}
func TestUpgradeMaxShips(t *testing.T) {
sg := &g.ShipGroup{
Tech: map[g.Tech]g.Float{
g.TechDrive: 1.0,
g.TechWeapons: 1.0,
g.TechShields: 1.0,
g.TechCargo: 1.0,
},
Number: 10,
}
uc := controller.GroupUpgradeCost(sg, Cruiser, 2.0, 2.0, 2.0, 2.0)
assert.Equal(t, uint(4), uc.UpgradeMaxShips(1000))
}
func TestCurrentUpgradingLevel(t *testing.T) {
sg := &g.ShipGroup{
StateUpgrade: nil,
}
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechDrive))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechCargo))
sg.StateUpgrade = &g.InUpgrade{
UpgradeTech: []g.UpgradePreference{
{Tech: g.TechDrive, Level: 1.5, Cost: 100.1},
},
}
assert.Equal(t, 1.5, controller.CurrentUpgradingLevel(sg, g.TechDrive))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechCargo))
sg.StateUpgrade.UpgradeTech = append(sg.StateUpgrade.UpgradeTech, g.UpgradePreference{Tech: g.TechCargo, Level: 2.2, Cost: 200.2})
assert.Equal(t, 1.5, controller.CurrentUpgradingLevel(sg, g.TechDrive))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechWeapons))
assert.Equal(t, 0.0, controller.CurrentUpgradingLevel(sg, g.TechShields))
assert.Equal(t, 2.2, controller.CurrentUpgradingLevel(sg, g.TechCargo))
}
func TestFutureUpgradeLevel(t *testing.T) {
assert.Equal(t, 0.0, controller.FutureUpgradeLevel(2.0, 2.0, 2.0))
assert.Equal(t, 0.0, controller.FutureUpgradeLevel(2.0, 2.0, 3.0))
assert.Equal(t, 1.5, controller.FutureUpgradeLevel(1.5, 2.0, 3.0))
assert.Equal(t, 2.0, controller.FutureUpgradeLevel(2.5, 1.0, 2.0))
assert.Equal(t, 2.5, controller.FutureUpgradeLevel(2.5, 1.0, 0.0))
}
func TestUpgradeGroupPreference(t *testing.T) {
sg := g.ShipGroup{
Number: 4,
Tech: g.TechSet{
g.TechDrive: 1.0,
g.TechWeapons: 1.0,
g.TechShields: 1.0,
g.TechCargo: 1.0,
},
}
assert.Nil(t, sg.StateUpgrade)
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechDrive, 0)
assert.Nil(t, sg.StateUpgrade)
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechDrive, 2.0)
assert.NotNil(t, sg.StateUpgrade)
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechDrive))
assert.Equal(t, 300., sg.StateUpgrade.Cost())
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechWeapons, 2.0)
assert.NotNil(t, sg.StateUpgrade)
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechWeapons))
assert.Equal(t, 600., sg.StateUpgrade.Cost())
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechShields, 2.0)
assert.NotNil(t, sg.StateUpgrade)
assert.Equal(t, 300., sg.StateUpgrade.TechCost(g.TechShields))
assert.Equal(t, 900., sg.StateUpgrade.Cost())
sg = controller.UpgradeGroupPreference(sg, Cruiser, g.TechCargo, 2.0)
assert.NotNil(t, sg.StateUpgrade)
assert.Equal(t, 0., sg.StateUpgrade.TechCost(g.TechCargo))
assert.Equal(t, 900., sg.StateUpgrade.Cost())
}
func TestShipGroupUpgrade(t *testing.T) {
c, g := newCache()
// group #1 - in_orbit, free to upgrade
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 10))
// group #2 - in_space
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
c.ShipGroup(1).StateInSpace = &InSpace
// group #3 - in_orbit, foreign planet
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
c.ShipGroup(2).Destination = R1_Planet_1_num
assert.ErrorContains(t,
g.ShipGroupUpgrade(UnknownRace, c.ShipGroup(0).ID, "DRIVE", 0),
e.GenericErrorText(e.ErrInputUnknownRace))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_Extinct.Name, c.ShipGroup(0).ID, "DRIVE", 0),
e.GenericErrorText(e.ErrRaceExtinct))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, uuid.New(), "DRIVE", 0),
e.GenericErrorText(e.ErrInputEntityNotExists))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(1).ID, "DRIVE", 0),
e.GenericErrorText(e.ErrShipsBusy))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(2).ID, "DRIVE", 0),
e.GenericErrorText(e.ErrInputEntityNotOwned))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "GUN", 0),
e.GenericErrorText(e.ErrInputTechUnknown))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "CARGO", 0),
e.GenericErrorText(e.ErrInputUpgradeShipTechNotUsed))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "ALL", 2.0),
e.GenericErrorText(e.ErrInputUpgradeParameterNotAllowed))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "DRIVE", 2.0),
e.GenericErrorText(e.ErrInputUpgradeTechLevelInsufficient))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "DRIVE", 1.1),
e.GenericErrorText(e.ErrInputUpgradeShipsAlreadyUpToDate))
c.RaceTechLevel(Race_0_idx, game.TechDrive, 10.0)
assert.Equal(t, 10.0, c.Race(Race_0_idx).TechLevel(game.TechDrive))
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "DRIVE", 10.0),
e.GenericErrorText(e.ErrUpgradeInsufficientResources))
assert.NoError(t, g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(0).ID, "DRIVE", 1.3))
assert.Equal(t, game.StateInOrbit, c.ShipGroup(0).State())
assert.Equal(t, uint(6), c.ShipGroup(0).Number)
assert.Equal(t, game.StateUpgrade, c.ShipGroup(3).State())
assert.Equal(t, uint(4), c.ShipGroup(3).Number)
assert.NotNil(t, c.ShipGroup(3).StateUpgrade)
assert.Equal(t, 1.3, c.ShipGroup(3).StateUpgrade.UpgradeTech[0].Level.F())
assert.Equal(t, "DRIVE", c.ShipGroup(3).StateUpgrade.UpgradeTech[0].Tech.String())
assert.ErrorContains(t,
g.ShipGroupUpgrade(Race_0.Name, c.ShipGroup(3).ID, "DRIVE", 1.3),
e.GenericErrorText(e.ErrShipsBusy))
}
// TestUpgradeCostTracksShipLosses checks that the production reserved for a
// pending upgrade follows the group's CURRENT ship count. Upgrading groups now
// take part in the pre-departure battle and may lose ships, which would leave
// the cost stored at order time stale; the cost is recomputed instead.
func TestUpgradeCostTracksShipLosses(t *testing.T) {
c, _ := newCache()
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 4))
// Pending drive upgrade with a deliberately stale stored cost.
c.ShipGroup(0).StateUpgrade = &game.InUpgrade{
UpgradeTech: []game.UpgradePreference{{Tech: game.TechDrive, Level: 2.0, Cost: 9999}},
}
before := c.PlanetProductionCapacity(R0_Planet_0_num)
// Two ships lost, as in the pre-departure battle.
c.ShipGroupDestroyItem(0)
c.ShipGroupDestroyItem(0)
after := c.PlanetProductionCapacity(R0_Planet_0_num)
// Fewer ships reserve less production. With the stale stored cost this
// would be unchanged.
assert.Greater(t, after, before)
}