server/nano_visibility_test.go (5717B)
1 package main 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func TestNanoVisibilityRejectsAdmissionAndRefundsObstructedCompletion(t *testing.T) { 9 for _, kind := range []string{"direct", "periodic", "admin", "friendly"} { 10 t.Run(kind, func(t *testing.T) { 11 var world *server 12 program := uint32(29618) 13 switch kind { 14 case "direct": 15 world, _ = newNanoOutcomeWorld(t, 100) 16 case "periodic": 17 world, _ = newPeriodicNanoWorld(t) 18 program = 43376 19 case "admin": 20 fixture := controlNanoFixtures(t)[0] 21 world, _ = newCombatNanoWorld(t, combatNanoFixture{Profession: fixture.Profession, Crystal: fixture.Crystal}) 22 if err := world.handleOwnedItemUse(world.clients[1], backpackUseRequest(1, 64, 1)[20:]); err != nil { 23 t.Fatal(err) 24 } 25 program = fixture.Program 26 case "friendly": 27 world, _ = newNanoBuffWorld(t) 28 program = 27169 29 } 30 addCombatWall(world) 31 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 32 if kind == "friendly" { 33 recipient := *player 34 recipient.id, recipient.name, recipient.position = 2, "Recipient", target.position 35 world.characters[2], world.nextCharacter, target = &recipient, 2, &recipient 36 world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, target: true} 37 if err := world.saveUser(target); err != nil { 38 t.Fatal(err) 39 } 40 } 41 nano, health := player.nano, target.health 42 request := characterActionPacket(1, [5]uint32{19, 50000, target.id, 53019, program})[20:] 43 44 if err := world.handleNanoCast(client, request); err != nil { 45 t.Fatal(err) 46 } 47 48 if client.nanoCast != nil || player.nano != nano || player.nanoReserved != 0 || !player.nanoRechargeUntil.IsZero() || target.health != health { 49 t.Fatal("obstructed admission reserved energy or started an action") 50 } 51 target.position[0] = 12 52 if err := world.handleNanoCast(client, request); err != nil { 53 t.Fatal(err) 54 } 55 if client.nanoCast == nil || player.nanoReserved == 0 { 56 t.Fatal("clear cast was not admitted") 57 } 58 at := client.nanoCast.readyAt.Add(time.Nanosecond) 59 target.position[0] = 16 60 world.advanceNanos(at) 61 world.advanceNanos(at.Add(time.Minute)) 62 var storedNano, storedReserve uint32 63 if err := world.db.QueryRow("SELECT nano,nano_reserved FROM characters WHERE id=1").Scan(&storedNano, &storedReserve); err != nil { 64 t.Fatal(err) 65 } 66 if client.nanoCast != nil || player.nano != nano || player.nanoReserved != 0 || storedNano != nano || storedReserve != 0 || 67 !player.nanoRechargeUntil.IsZero() || target.health != health || len(target.activeNanos) != 0 || len(target.threat) != 0 || 68 len(target.damageContributors) != 0 || target.corpse != nil || player.experience != 0 { 69 t.Fatal("obstructed completion failed to refund exactly once or applied an effect") 70 } 71 target.position[0] = 12 72 if err := world.handleNanoCast(client, request); err != nil { 73 t.Fatal(err) 74 } 75 if client.nanoCast == nil { 76 t.Fatal("refunded cast could not be retried with a clear path") 77 } 78 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 79 if player.nano >= nano || player.nanoReserved != 0 || kind == "direct" && target.health >= health || 80 kind != "direct" && len(target.activeNanos) != 1 { 81 t.Fatal("clear retry failed to commit its resource debit and effect") 82 } 83 }) 84 } 85 } 86 87 func TestNanoVisibilityFailedRefundCannotExecuteWhenPathReopens(t *testing.T) { 88 world, _ := newNanoOutcomeWorld(t, 1) 89 addCombatWall(world) 90 player, target, client := world.characters[1], world.characters[1000000], world.clients[1] 91 target.position[0] = 12 92 if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, target.id, 53019, 29618})[20:]); err != nil { 93 t.Fatal(err) 94 } 95 at := client.nanoCast.readyAt.Add(time.Nanosecond) 96 if _, err := world.db.Exec("CREATE TRIGGER fail_refund BEFORE UPDATE OF nano_reserved ON characters WHEN NEW.id=1 AND NEW.nano_reserved=0 BEGIN SELECT RAISE(FAIL, 'test refund'); END"); err != nil { 97 t.Fatal(err) 98 } 99 target.position[0] = 16 100 101 world.advanceNanos(at) 102 target.position[0] = 12 103 world.advanceNanos(at.Add(time.Second)) 104 105 if client.nanoCast == nil || !client.nanoCast.cancelled || player.nano != 21 || player.nanoReserved != 9 || target.health != 1 || target.corpse != nil { 106 t.Fatal("failed refund let the reopened path resurrect a cancelled cast") 107 } 108 if _, err := world.db.Exec("DROP TRIGGER fail_refund"); err != nil { 109 t.Fatal(err) 110 } 111 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 112 world.advanceNanos(at.Add(time.Minute)) 113 if client.nanoCast != nil || player.nano != 30 || player.nanoReserved != 0 || target.health != 1 || target.corpse != nil || player.experience != 0 { 114 t.Fatal("refund retry changed the target or duplicated the resource return") 115 } 116 } 117 118 func TestNanoVisibilityDoesNotInterruptInstalledPeriodicDamageOrSelfBuffs(t *testing.T) { 119 t.Run("periodic", func(t *testing.T) { 120 world, _ := newPeriodicNanoWorld(t) 121 addCombatWall(world) 122 target := world.characters[1000000] 123 target.position[0] = 12 124 at := castPeriodicNano(t, world, 43376) 125 target.position[0] = 16 126 127 world.advanceNanos(at.Add(22500 * time.Millisecond)) 128 129 if target.health != 91 || target.activeNanos[0].ticksLeft != 5 { 130 t.Fatal("wall interrupted an already installed periodic effect") 131 } 132 }) 133 t.Run("self", func(t *testing.T) { 134 world, _ := newNanoBuffWorld(t) 135 addCombatWall(world) 136 player := world.characters[1] 137 player.position[0] = 15 138 139 castTestBuff(t, world, 1, 1, 27169) 140 141 if player.effectiveAbility(16) != 22 || player.nano != 56 || len(player.activeNanos) != 1 { 142 t.Fatal("self buff unnecessarily intersected scenery at its origin") 143 } 144 }) 145 }