server/stationary_clinic_test.go (19740B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "reflect" 7 "slices" 8 "testing" 9 "time" 10 ) 11 12 func stationaryClinicUseRequest(sequence uint32) []byte { 13 request := shopUseRequest(sequence) 14 binary.BigEndian.PutUint32(request[40:44], 51005) 15 binary.BigEndian.PutUint32(request[44:48], 3221226655) 16 return request 17 } 18 19 func newStationaryClinicWorld(t *testing.T) (*server, *combatTestConn) { 20 t.Helper() 21 world, output := newEquipmentWorld(t) 22 player := world.characters[1] 23 player.level, player.playfield = 200, 1183 24 player.position = [3]float32{176.32516, 5.00036, 129} 25 player.stats.Abilities = characterAbilitiesStats{40, 40, 40, 40, 40, 40} 26 player.stats.Abilities.Stamina, player.stats.Abilities.Psychic = 320, 320 27 player.stats.Body.BodyDevelopment, player.stats.Body.NanoPool = 500, 500 28 player.stats.NanoAiding.Treatment = 0 29 player.health, player.nano = 1, 2 30 if player.maximumHealth() < 1001 || player.maximumNano() < 1002 { 31 t.Fatal("fixture cannot distinguish the authored 1000-point restoration from a refill") 32 } 33 if err := world.saveUser(player); err != nil { 34 t.Fatal(err) 35 } 36 if err := world.savePosition(player); err != nil { 37 t.Fatal(err) 38 } 39 inventory := player.inventory 40 inventory.Credits = 700 41 if err := world.saveInventory(player, inventory); err != nil { 42 t.Fatal(err) 43 } 44 if granted, err := world.grantItem(player, 101173); err != nil || !granted { 45 t.Fatal(granted, err) 46 } 47 output.packets = nil 48 return world, output 49 } 50 51 func TestStationaryClinicCommitsFeeResourcesAndSurgeryEffect(t *testing.T) { 52 world, output := newStationaryClinicWorld(t) 53 player, client := world.characters[1], world.clients[1] 54 items := slices.Clone(player.inventory.Items) 55 started := time.Now() 56 57 err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)) 58 59 if err != nil || player.inventory.Credits != 400 || player.health != 1001 || player.nano != 1002 || 60 player.nanoBonus(124) != 100 || len(player.activeNanos) != 1 || !slices.Equal(player.inventory.Items, items) { 61 t.Fatal("stationary clinic did not commit its complete fee, restoration and Treatment effect", err) 62 } 63 effect := player.activeNanos[0] 64 if effect.program != 157490 || effect.remainingAt(started) < 5*time.Minute || effect.remainingAt(time.Now()) > 5*time.Minute || 65 len(output.packets) == 0 || binary.BigEndian.Uint32(output.packets[0][29:33]) != 1 || player.usedNCU() != 0 || 66 !player.canMoveEquipment(items[1], 37, time.Now()) { 67 t.Fatal("clinic delivery or five-minute implant permission differs") 68 } 69 cashUpdates := 0 70 for _, packet := range output.packets { 71 if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[29:33]) == 61 { 72 cashUpdates++ 73 if binary.BigEndian.Uint32(packet[33:37]) != 400 { 74 t.Fatal("clinic published an uncommitted cash balance") 75 } 76 } 77 } 78 if cashUpdates != 1 { 79 t.Fatal("clinic did not publish its committed cash balance exactly once", cashUpdates) 80 } 81 cold := newTestInventoryServer(t).characters[1] 82 if cold.inventory.Credits != 400 || cold.health != 1001 || cold.nano != 1002 || cold.nanoBonus(124) != 100 || 83 !slices.Equal(cold.inventory.Items, items) || !cold.canMoveEquipment(items[1], 37, time.Now()) { 84 t.Fatal("cold clinic outcome lost cash, resources, effects or an item") 85 } 86 } 87 88 func TestStationaryClinicRejectsUnavailableUseWithoutEffects(t *testing.T) { 89 for _, scenario := range []string{"cash 299", "range", "vertical range", "wrong zone", "unknown clinic", "wrong type", "foreign owner", "dead", "loading", "replaced", "replay", "own combat", "incoming combat"} { 90 t.Run(scenario, func(t *testing.T) { 91 world, output := newStationaryClinicWorld(t) 92 player, client := world.characters[1], world.clients[1] 93 request := stationaryClinicUseRequest(1) 94 switch scenario { 95 case "cash 299": 96 inventory := player.inventory 97 inventory.Credits = 299 98 if err := world.saveInventory(player, inventory); err != nil { 99 t.Fatal(err) 100 } 101 case "range": 102 player.position[0] += 5.01 103 case "vertical range": 104 player.position[1] += 5.01 105 case "wrong zone": 106 player.playfield = 1180 107 case "unknown clinic": 108 binary.BigEndian.PutUint32(request[44:48], 999) 109 case "wrong type": 110 binary.BigEndian.PutUint32(request[40:44], 51035) 111 case "foreign owner": 112 binary.BigEndian.PutUint32(request[8:12], 2) 113 case "dead": 114 player.health = 0 115 case "loading": 116 client.loading = true 117 case "replaced": 118 world.clients[1] = &worldClient{id: 1} 119 case "replay": 120 client.useSequence = 1 121 case "own combat": 122 player.fighting = 1000000 123 case "incoming combat": 124 world.fighting[1000000] = &character{id: 1000000, fighting: 1} 125 } 126 inventory := player.inventory 127 health, nano := player.health, player.nano 128 before, err := readTestItemStore(world) 129 if err != nil { 130 t.Fatal(err) 131 } 132 output.packets = nil 133 134 err = world.handleStationaryClinicUse(client, request) 135 after, readErr := readTestItemStore(world) 136 137 if err != nil || readErr != nil || !bytes.Equal(before, after) || !reflect.DeepEqual(player.inventory, inventory) || 138 player.health != health || player.nano != nano || len(player.activeNanos) != 0 || player.canChangeImplants(time.Now()) { 139 t.Fatal("refused clinic use changed a fee, resource, item or permission", err, readErr) 140 } 141 for _, packet := range output.packets { 142 if binary.BigEndian.Uint32(packet[16:20]) != 0x52526858 || binary.BigEndian.Uint32(packet[29:33]) != 2 { 143 t.Fatal("refused clinic use published an effect") 144 } 145 } 146 }) 147 } 148 } 149 150 func TestStationaryClinicRefreshChargesOnceAndDoesNotStack(t *testing.T) { 151 world, output := newStationaryClinicWorld(t) 152 player, client := world.characters[1], world.clients[1] 153 inventory := player.inventory 154 inventory.Credits = 600 155 if err := world.saveInventory(player, inventory); err != nil { 156 t.Fatal(err) 157 } 158 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 159 t.Fatal(err) 160 } 161 deadline := player.activeNanos[0].expiresAt 162 output.packets = nil 163 164 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 165 t.Fatal(err) 166 } 167 168 if player.inventory.Credits != 300 || len(output.packets) != 0 || player.activeNanos[0].expiresAt != deadline { 169 t.Fatal("duplicate Use charged or extended the clinic effect") 170 } 171 172 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(2)); err != nil { 173 t.Fatal(err) 174 } 175 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(3)); err != nil { 176 t.Fatal(err) 177 } 178 cold := newTestInventoryServer(t).characters[1] 179 180 if player.inventory.Credits != 0 || len(player.activeNanos) != 1 || player.nanoBonus(124) != 100 || 181 !player.activeNanos[0].expiresAt.After(deadline) || cold.inventory.Credits != 0 || cold.nanoBonus(124) != 100 { 182 t.Fatal("clinic refresh stacked, failed the exact 300-credit boundary or lost cold state") 183 } 184 } 185 186 func TestStationaryClinicFailureRollsBackThenRetries(t *testing.T) { 187 for _, failure := range []string{"fee", "ignored fee", "rewritten fee", "resources", "ignored resources", "rewritten resources", "program", "ignored program", "commit"} { 188 t.Run(failure, func(t *testing.T) { 189 world, output := newStationaryClinicWorld(t) 190 player, client := world.characters[1], world.clients[1] 191 statement := "CREATE TRIGGER fault_clinic BEFORE UPDATE OF credits ON players BEGIN SELECT RAISE(ABORT, 'fee'); END" 192 switch failure { 193 case "ignored fee": 194 statement = "CREATE TRIGGER fault_clinic BEFORE UPDATE OF credits ON players BEGIN SELECT RAISE(IGNORE); END" 195 case "rewritten fee": 196 statement = "CREATE TRIGGER fault_clinic AFTER UPDATE OF credits ON players BEGIN UPDATE players SET credits=700 WHERE id=1; END" 197 case "resources": 198 statement = "CREATE TRIGGER fault_clinic BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(ABORT, 'resources'); END" 199 case "ignored resources": 200 statement = "CREATE TRIGGER fault_clinic BEFORE UPDATE OF health ON characters BEGIN SELECT RAISE(IGNORE); END" 201 case "rewritten resources": 202 statement = "CREATE TRIGGER fault_clinic AFTER UPDATE OF health ON characters BEGIN UPDATE characters SET health=1 WHERE id=1; END" 203 case "program": 204 statement = "CREATE TRIGGER fault_clinic BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(ABORT, 'program'); END" 205 case "ignored program": 206 statement = "CREATE TRIGGER fault_clinic BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END" 207 case "commit": 208 statement = "CREATE TABLE clinic_commit_failure (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fault_clinic AFTER INSERT ON active_nanos BEGIN INSERT INTO clinic_commit_failure VALUES(9999999); END" 209 } 210 if _, err := world.db.Exec(statement); err != nil { 211 t.Fatal(err) 212 } 213 output.packets = nil 214 215 failed := world.handleTerminalUse(client, stationaryClinicUseRequest(1)) 216 cold := newTestInventoryServer(t).characters[1] 217 218 if failed == nil || player.inventory.Credits != 700 || cold.inventory.Credits != 700 || 219 player.health != 1 || player.nano != 2 || cold.health != 1 || cold.nano != 2 || 220 len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || len(output.packets) != 0 { 221 t.Fatal("failed clinic outcome exposed a partial fee, resource, program or reply", failed) 222 } 223 224 if _, err := world.db.Exec("DROP TRIGGER fault_clinic"); err != nil { 225 t.Fatal(err) 226 } 227 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(2)); err != nil { 228 t.Fatal(err) 229 } 230 cold = newTestInventoryServer(t).characters[1] 231 232 if player.inventory.Credits != 400 || cold.inventory.Credits != 400 || player.health != 1001 || cold.health != 1001 || 233 player.nano != 1002 || cold.nano != 1002 || player.nanoBonus(124) != 100 || cold.nanoBonus(124) != 100 { 234 t.Fatal("clinic retry lost or repeated part of its outcome") 235 } 236 }) 237 } 238 } 239 240 func TestStationaryClinicExpiryAndPortablePermissionAreIndependent(t *testing.T) { 241 for _, other := range []string{"none", "portable remains", "portable expires first"} { 242 t.Run(other, func(t *testing.T) { 243 world, output := newStationaryClinicWorld(t) 244 player, client := world.characters[1], world.clients[1] 245 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 246 t.Fatal(err) 247 } 248 finish := player.activeNanos[0].expiresAt 249 if other == "portable remains" { 250 player.implantChangesUntil = finish.Add(time.Minute) 251 } 252 if other == "portable expires first" { 253 player.implantChangesUntil = finish.Add(-time.Minute) 254 } 255 output.packets = nil 256 257 if other == "portable expires first" { 258 world.advanceItemEffects(player.implantChangesUntil) 259 if !player.canChangeImplants(finish.Add(-time.Second)) { 260 t.Fatal("portable expiry removed stationary access") 261 } 262 } 263 world.advanceNanos(finish) 264 world.advanceNanos(finish.Add(time.Second)) 265 cold := newTestInventoryServer(t).characters[1] 266 267 if len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.nanoBonus(124) != 0 || cold.nanoBonus(124) != 0 || 268 player.canChangeImplants(finish) != (other == "portable remains") || player.inventory.Credits != 400 || cold.inventory.Credits != 400 { 269 t.Fatal("clinic expiry lost cash, retained Treatment or removed independent access") 270 } 271 removals := 0 272 for _, packet := range output.packets { 273 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 { 274 removals++ 275 } 276 } 277 if removals != 1 { 278 t.Fatal("clinic expiry did not remove exactly one native effect", removals) 279 } 280 }) 281 } 282 } 283 284 func TestStationaryClinicZoneRemovalCommitsWithDestination(t *testing.T) { 285 for _, failure := range []string{"none", "failed removal", "ignored removal"} { 286 t.Run(failure, func(t *testing.T) { 287 world, output := newStationaryClinicWorld(t) 288 player, client := world.characters[1], world.clients[1] 289 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 290 t.Fatal(err) 291 } 292 player.activeNanos = append(player.activeNanos, activeNano{program: 27169, caster: 1, remaining: time.Minute, expiresAt: time.Now().Add(time.Minute)}) 293 if err := world.saveActiveNanos(player, time.Now()); err != nil { 294 t.Fatal(err) 295 } 296 destination := player.userData() 297 destination.Playfield = 1180 298 if failure != "none" { 299 operation := "ABORT, 'zone removal'" 300 if failure == "ignored removal" { 301 operation = "IGNORE" 302 } 303 if _, err := world.db.Exec("CREATE TRIGGER fault_clinic_zone BEFORE DELETE ON active_nanos WHEN OLD.program_id=157490 BEGIN SELECT RAISE(" + operation + "); END"); err != nil { 304 t.Fatal(err) 305 } 306 } 307 output.packets = nil 308 309 err := world.updateUser(destination) 310 cold := newTestInventoryServer(t).characters[1] 311 312 if failure != "none" { 313 if err == nil || player.playfield != 1183 || cold.playfield != 1183 || player.nanoBonus(124) != 100 || 314 cold.nanoBonus(124) != 100 || world.clients[1] != client || len(output.packets) != 0 { 315 t.Fatal("failed zone removal published a destination or lost the clinic effect", err) 316 } 317 if _, err := world.db.Exec("DROP TRIGGER fault_clinic_zone"); err != nil { 318 t.Fatal(err) 319 } 320 if err := world.updateUser(destination); err != nil { 321 t.Fatal(err) 322 } 323 cold = newTestInventoryServer(t).characters[1] 324 } else if err != nil { 325 t.Fatal(err) 326 } 327 if player.playfield != 1180 || cold.playfield != 1180 || player.canChangeImplants(time.Now()) || cold.canChangeImplants(time.Now()) || 328 player.nanoBonus(124) != 0 || cold.nanoBonus(124) != 0 || len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || 329 player.activeNanos[0].program != 27169 || cold.activeNanos[0].program != 27169 || player.inventory.Credits != 400 || cold.inventory.Credits != 400 { 330 t.Fatal("zone transition retained stationary access or removed an unrelated program") 331 } 332 }) 333 } 334 } 335 336 func TestStationaryClinicLostReplyKeepsCommittedObserverOutcome(t *testing.T) { 337 world, _ := newStationaryClinicWorld(t) 338 player, client := world.characters[1], world.clients[1] 339 observer := &combatTestConn{} 340 world.characters[2] = &character{id: 2, playfield: 1183, health: 100} 341 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, ready: true, target: true} 342 client.conn = &failingCombatConn{} 343 344 failed := world.handleTerminalUse(client, stationaryClinicUseRequest(1)) 345 cold := newTestInventoryServer(t).characters[1] 346 347 if failed == nil || player.inventory.Credits != 400 || cold.inventory.Credits != 400 || 348 player.nanoBonus(124) != 100 || cold.nanoBonus(124) != 100 || cold.health != 1001 || cold.nano != 1002 { 349 t.Fatal("lost clinic reply reversed or partially committed the outcome", failed) 350 } 351 programs := 0 352 for _, packet := range observer.packets { 353 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 { 354 programs++ 355 } 356 } 357 if programs != 1 { 358 t.Fatal("failed owner reply suppressed the healthy observer's program", programs) 359 } 360 } 361 362 func TestStationaryClinicExpiryFailureRetainsEffectThenRetries(t *testing.T) { 363 for _, operation := range []string{"ABORT, 'expiry'", "IGNORE"} { 364 t.Run(operation, func(t *testing.T) { 365 world, output := newStationaryClinicWorld(t) 366 player, client := world.characters[1], world.clients[1] 367 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 368 t.Fatal(err) 369 } 370 finish := player.activeNanos[0].expiresAt 371 if _, err := world.db.Exec("CREATE TRIGGER fault_clinic_expiry BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(" + operation + "); END"); err != nil { 372 t.Fatal(err) 373 } 374 output.packets = nil 375 376 world.advanceNanos(finish) 377 cold := newTestInventoryServer(t).characters[1] 378 379 if len(player.activeNanos) != 1 || len(cold.activeNanos) != 1 || len(output.packets) != 0 || player.inventory.Credits != 400 || cold.inventory.Credits != 400 || 380 !player.canChangeImplants(finish) || !cold.canChangeImplants(finish) { 381 t.Fatal("failed expiry lost an effect or published removal") 382 } 383 384 if _, err := world.db.Exec("DROP TRIGGER fault_clinic_expiry"); err != nil { 385 t.Fatal(err) 386 } 387 world.advanceNanos(finish.Add(time.Second)) 388 cold = newTestInventoryServer(t).characters[1] 389 390 if len(player.activeNanos) != 0 || len(cold.activeNanos) != 0 || player.nanoBonus(124) != 0 || cold.nanoBonus(124) != 0 || 391 player.canChangeImplants(finish.Add(time.Second)) || player.inventory.Credits != 400 || cold.inventory.Credits != 400 { 392 t.Fatal("expiry retry did not remove exactly the timed permission and modifier") 393 } 394 }) 395 } 396 } 397 398 func TestStationaryClinicEffectRejectsOrdinaryCastingAndCancellation(t *testing.T) { 399 world, output := newStationaryClinicWorld(t) 400 player, client := world.characters[1], world.clients[1] 401 inventory := player.inventory 402 inventory.LearnedNanos = []uint32{157490} 403 if err := world.saveInventory(player, inventory); err != nil { 404 t.Fatal(err) 405 } 406 407 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 157490})[20:]); err != nil { 408 t.Fatal(err) 409 } 410 411 if client.nanoCast != nil || len(player.activeNanos) != 0 || player.inventory.Credits != 700 || player.nano != 2 { 412 t.Fatal("ordinary casting bypassed the clinic contract") 413 } 414 415 if err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)); err != nil { 416 t.Fatal(err) 417 } 418 output.packets = nil 419 if err := world.handleNanoRemoval(client, characterActionPacket(1, [5]uint32{65, 0, 0, 53019, 157490})[20:]); err != nil { 420 t.Fatal(err) 421 } 422 423 if len(player.activeNanos) != 1 || player.nanoBonus(124) != 100 || !player.canChangeImplants(time.Now()) || len(output.packets) != 0 || player.inventory.Credits != 400 { 424 t.Fatal("ordinary removal cancelled the protected clinic effect") 425 } 426 } 427 428 func TestStationaryClinicPreservesIndependentActionState(t *testing.T) { 429 for _, action := range []string{"equipment", "trade", "shop", "bank"} { 430 t.Run(action, func(t *testing.T) { 431 world, _ := newStationaryClinicWorld(t) 432 player, client := world.characters[1], world.clients[1] 433 switch action { 434 case "equipment": 435 if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment == nil { 436 t.Fatal("admit independent equipment move", err) 437 } 438 case "trade": 439 client.trade = &playerTrade{} 440 case "shop": 441 client.shop = &shopTrade{} 442 case "bank": 443 client.bank = &bankSession{} 444 } 445 trade, shop, bank := client.trade, client.shop, client.bank 446 deadline := time.Time{} 447 if client.equipment != nil { 448 deadline = client.equipment.readyAt 449 } 450 items := slices.Clone(player.inventory.Items) 451 452 err := world.handleTerminalUse(client, stationaryClinicUseRequest(1)) 453 454 if err != nil || player.inventory.Credits != 400 || player.nanoBonus(124) != 100 || 455 !slices.Equal(player.inventory.Items, items) || client.trade != trade || client.shop != shop || client.bank != bank { 456 t.Fatal("clinic use disturbed an independent action or refused an unauthored context", err) 457 } 458 if action == "equipment" { 459 if client.equipment == nil || client.equipment.itemID != 1 || client.equipment.source != 64 || 460 client.equipment.destination != 6 || client.equipment.readyAt != deadline { 461 t.Fatal("clinic changed the admitted move or deadline") 462 } 463 world.advanceEquipment(deadline) 464 items[0].Slot = 6 465 } 466 cold := newTestInventoryServer(t).characters[1] 467 if !slices.Equal(player.inventory.Items, items) || !slices.Equal(cold.inventory.Items, items) || cold.inventory.Credits != 400 || cold.nanoBonus(124) != 100 { 468 t.Fatal("clinic use or equipment completion lost a durable outcome") 469 } 470 }) 471 } 472 }