Files
galaxy-game/game/internal/controller/report_test.go
T
Ilia Denisov 9e9977d5f1
Tests · Go / test (push) Successful in 2m29s
Tests · UI / test (push) Waiting to run
Tests · Go / test (pull_request) Successful in 2m17s
Tests · Integration / integration (pull_request) Successful in 1m53s
Tests · UI / test (pull_request) Successful in 3m37s
feat(game): race exit warnings in the turn report (#12)
Surface the inactivity-removal countdown the rules promise but the
engine never reported. A race within five turns of being auto-removed
for inactivity gets a personal warning in its own report; every race
within three turns is listed publicly to all participants.

- model: Report.PersonalExitWarning + RacesLeavingSoon ([]RaceExitNotice)
- fbs: RaceExitNotice table + Report.personal_exit_warning /
  races_leaving_soon (regenerated Go + TS bindings)
- transcoder: encode/decode both fields
- engine: ReportExitWarnings fills the recipient's TTL (1..5) and lists
  other non-extinct races with TTL 1..3, excluding the recipient itself
- ui: danger-styled personal banner + "races leaving soon" section
  (hidden when empty), wired into the report view, EN/RU i18n
- docs: rules.txt report-section list, FUNCTIONAL.md 6.4 + RU mirror

Voluntary quit and idle timeout share the TTL countdown and are not
distinguished, per the agreed scope.
2026-05-31 10:34:50 +02:00

230 lines
8.9 KiB
Go

package controller_test
import (
"testing"
"galaxy/game/internal/model/game"
"galaxy/model/report"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestReportRace(t *testing.T) {
c, _ := newCache()
c.TurnCalculateVotes()
rep := c.InitReport(2)
assert.Equal(t, 2, int(rep.Turn))
c.ReportRace(Race_0_idx, rep, nil, nil)
assert.Equal(t, Race_0.Name, rep.Race)
assert.Equal(t, Race_0.ID, rep.RaceID)
assert.Equal(t, 0.1, float64(rep.Votes))
for i := range rep.Player {
p := &rep.Player[i]
switch p.ID {
case Race_0_ID:
assert.Equal(t, Race_0.Name, p.Name)
assert.Equal(t, 1.1, float64(p.Drive))
assert.Equal(t, 1.2, float64(p.Weapons))
assert.Equal(t, 1.3, float64(p.Shields))
assert.Equal(t, 1.4, float64(p.Cargo))
assert.Equal(t, 100., float64(p.Population))
assert.Equal(t, 100., float64(p.Industry))
assert.Equal(t, 2, int(p.Planets))
assert.Equal(t, 0.1, float64(p.Votes))
assert.Equal(t, "-", p.Relation)
case Race_1_ID:
assert.Equal(t, Race_1.Name, p.Name)
assert.Equal(t, 2.1, float64(p.Drive))
assert.Equal(t, 2.2, float64(p.Weapons))
assert.Equal(t, 2.3, float64(p.Shields))
assert.Equal(t, 2.4, float64(p.Cargo))
assert.Equal(t, 0., float64(p.Population))
assert.Equal(t, 0., float64(p.Industry))
assert.Equal(t, 1, int(p.Planets))
assert.Equal(t, 0., float64(p.Votes))
assert.Equal(t, "WAR", p.Relation)
}
}
}
func TestReportLocalShipClass(t *testing.T) {
c, _ := newCache()
r := &report.Report{}
assert.Len(t, r.LocalShipClass, 0)
c.ReportLocalShipClass(Race_0_idx, r)
assert.Len(t, r.LocalShipClass, 3)
for i := range r.LocalShipClass {
assert.NotEmpty(t, r.LocalShipClass[i].Name)
switch n := r.LocalShipClass[i].Name; n {
case Cruiser.Name:
assert.Equal(t, report.F(Cruiser.Drive.F()), r.LocalShipClass[i].Drive)
assert.Equal(t, Cruiser.Armament, r.LocalShipClass[i].Armament)
assert.Equal(t, report.F(Cruiser.Weapons.F()), r.LocalShipClass[i].Weapons)
assert.Equal(t, report.F(Cruiser.Shields.F()), r.LocalShipClass[i].Shields)
assert.Equal(t, report.F(Cruiser.Cargo.F()), r.LocalShipClass[i].Cargo)
case Race_0_Gunship:
assert.Equal(t, report.F(60.), r.LocalShipClass[i].Drive)
assert.Equal(t, uint(3), r.LocalShipClass[i].Armament)
assert.Equal(t, report.F(30.), r.LocalShipClass[i].Weapons)
assert.Equal(t, report.F(100.), r.LocalShipClass[i].Shields)
assert.Equal(t, report.F(0.), r.LocalShipClass[i].Cargo)
case Race_0_Freighter:
assert.Equal(t, report.F(8.), r.LocalShipClass[i].Drive)
assert.Equal(t, uint(0), r.LocalShipClass[i].Armament)
assert.Equal(t, report.F(0.), r.LocalShipClass[i].Weapons)
assert.Equal(t, report.F(2.), r.LocalShipClass[i].Shields)
assert.Equal(t, report.F(10.), r.LocalShipClass[i].Cargo)
default:
assert.Failf(t, "unexpected ship class", "name=%s", n)
}
}
}
// TestReportIncomingGroupVisibility checks that a group heading to one of the
// recipient's planets is reported only while within the recipient's visibility
// range (driveTech*30); beyond it the group is hidden even though it is inbound.
func TestReportIncomingGroupVisibility(t *testing.T) {
c, _ := newCache()
gi := c.CreateShipsUnsafe_T(Race_1_idx, c.MustShipClass(Race_1_idx, Race_1_Gunship).ID, R1_Planet_1_num, 5)
c.ShipGroup(gi).Destination = R0_Planet_0_num
// Within Race_0 visibility (driveTech 1.1 -> 33 ly), near Planet_2 (3,3).
c.ShipGroup(gi).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(5), Y: floatRef(5)}
rep := c.InitReport(1)
c.ReportIncomingGroup(Race_0_idx, rep)
assert.Len(t, rep.IncomingGroup, 1)
// Beyond the visibility of every Race_0 planet: hidden.
c.ShipGroup(gi).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(40), Y: floatRef(40)}
rep = c.InitReport(1)
c.ReportIncomingGroup(Race_0_idx, rep)
assert.Len(t, rep.IncomingGroup, 0)
}
// TestReportUnidentifiedGroup checks the three rules for the unidentified list:
// groups heading to the recipient's planets are excluded (they are "incoming"),
// only groups within visibility (driveTech*30) appear, and each group appears
// once even when several owned planets are in range.
func TestReportUnidentifiedGroup(t *testing.T) {
c, _ := newCache()
cls := c.MustShipClass(Race_1_idx, Race_1_Gunship).ID
// Not inbound to Race_0, within visibility of BOTH Planet_0 and Planet_2.
g0 := c.CreateShipsUnsafe_T(Race_1_idx, cls, R1_Planet_1_num, 1)
c.ShipGroup(g0).Destination = Uninhabited_Planet_3_num
c.ShipGroup(g0).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(5), Y: floatRef(5)}
// Inbound to a Race_0 planet -> reported as incoming, not unidentified.
g1 := c.CreateShipsUnsafe_T(Race_1_idx, cls, R1_Planet_1_num, 1)
c.ShipGroup(g1).Destination = R0_Planet_0_num
c.ShipGroup(g1).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(5), Y: floatRef(5)}
// Not inbound, beyond visibility -> hidden.
g2 := c.CreateShipsUnsafe_T(Race_1_idx, cls, R1_Planet_1_num, 1)
c.ShipGroup(g2).Destination = Uninhabited_Planet_3_num
c.ShipGroup(g2).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(40), Y: floatRef(40)}
rep := c.InitReport(1)
c.ReportUnidentifiedGroup(Race_0_idx, rep)
assert.Len(t, rep.UnidentifiedGroup, 1)
}
// TestReportOtherShipClassFromBattle checks that the class of a foreign ship
// met in a battle the recipient witnessed is surfaced in OtherShipClass, with
// its design looked up from the owner race's ship types, while the recipient's
// own class is skipped.
func TestReportOtherShipClassFromBattle(t *testing.T) {
c, _ := newCache()
br := &report.BattleReport{
Races: map[int]uuid.UUID{0: Race_0.ID, 1: Race_1.ID},
Ships: map[int]report.BattleReportGroup{
0: {Race: Race_1.Name, ClassName: Race_1_Gunship},
1: {Race: Race_0.Name, ClassName: Race_0_Gunship}, // recipient's own -> skipped
},
}
rep := c.InitReport(1)
c.ReportOtherShipClass(Race_0_idx, rep, []*report.BattleReport{br})
assert.Len(t, rep.OtherShipClass, 1)
g := rep.OtherShipClass[0]
assert.Equal(t, Race_1.Name, g.Race)
assert.Equal(t, Race_1_Gunship, g.Name)
assert.Equal(t, report.F(60.), g.Drive)
assert.Equal(t, uint(3), g.Armament)
assert.Equal(t, report.F(30.), g.Weapons)
assert.Equal(t, report.F(100.), g.Shields)
assert.Equal(t, report.F(0.), g.Cargo)
assert.Equal(t, report.F(220.), g.Mass)
}
// TestReportShipProductionIndex guards the report index: when the only
// ship-producing planet is not the first planet in the map, its entry must
// land at the compacted report index, not the planet's map index (which would
// write out of the grown slice and panic).
func TestReportShipProductionIndex(t *testing.T) {
c, _ := newCache()
assert.NoError(t, c.PlanetProduce(Race_0_idx, int(R0_Planet_2_num), game.ProductionShip, ShipType_Cruiser))
rep := c.InitReport(1)
c.ReportShipProduction(Race_0_idx, rep)
assert.Len(t, rep.ShipProduction, 1)
assert.Equal(t, R0_Planet_2_num, rep.ShipProduction[0].Planet)
}
// TestReportIncomingGroupRemainingDistance checks the reported distance is the
// remaining distance from the group's current hyperspace position to the
// destination, not the full origin-to-destination route.
func TestReportIncomingGroupRemainingDistance(t *testing.T) {
c, _ := newCache()
gi := c.CreateShipsUnsafe_T(Race_1_idx, c.MustShipClass(Race_1_idx, Race_1_Gunship).ID, R1_Planet_1_num, 1)
c.ShipGroup(gi).Destination = R0_Planet_0_num // Planet_0 at (1,1)
c.ShipGroup(gi).StateInSpace = &game.InSpace{Origin: R1_Planet_1_num, X: floatRef(5), Y: floatRef(5)}
rep := c.InitReport(1)
c.ReportIncomingGroup(Race_0_idx, rep)
assert.Len(t, rep.IncomingGroup, 1)
// current (5,5) -> dest (1,1) = sqrt(32) ≈ 5.657; the origin (2,2) -> dest
// route would be sqrt(2) ≈ 1.414.
assert.InDelta(t, 5.657, rep.IncomingGroup[0].Distance.F(), 0.01)
}
// TestReportExitWarnings checks the inactivity-removal warnings: the recipient
// gets a personal countdown only at TTL 1..5, other non-extinct races within 3
// turns are listed publicly, the recipient is excluded from its own public
// list, and extinct races never appear.
func TestReportExitWarnings(t *testing.T) {
c, _ := newCache()
c.Race(Race_0_idx).TTL = 5
c.Race(Race_1_idx).TTL = 2
c.Race(2).TTL = 2 // Race_Extinct: extinct, must never appear publicly
// Race_0's report: personal countdown 5; only Race_1 (TTL 2) is public.
r0 := &report.Report{}
c.ReportExitWarnings(Race_0_idx, r0)
assert.Equal(t, uint(5), r0.PersonalExitWarning)
assert.Len(t, r0.RacesLeavingSoon, 1)
assert.Equal(t, Race_1.Name, r0.RacesLeavingSoon[0].Race)
assert.Equal(t, uint(2), r0.RacesLeavingSoon[0].TurnsLeft)
// Race_1's report: personal countdown 2; Race_0 (TTL 5 > 3) is not public.
r1 := &report.Report{}
c.ReportExitWarnings(Race_1_idx, r1)
assert.Equal(t, uint(2), r1.PersonalExitWarning)
assert.Empty(t, r1.RacesLeavingSoon)
// TTL above the 5-turn window → no personal warning.
c.Race(Race_0_idx).TTL = 6
r0b := &report.Report{}
c.ReportExitWarnings(Race_0_idx, r0b)
assert.Zero(t, r0b.PersonalExitWarning)
}