server/admin_weather_test.go (4662B)
1 package main 2 3 import ( 4 "encoding/binary" 5 "net/url" 6 "strings" 7 "testing" 8 "time" 9 ) 10 11 func TestAdminWeatherSendsOriginalTargetPacket(t *testing.T) { 12 world := newAdminTestServer(t) 13 player := &character{id: 1, playfield: 705, position: [3]float32{960, 16.5, 510}, health: 100} 14 world.characters[1] = player 15 connection := &combatTestConn{} 16 world.clients[1] = &worldClient{id: 1, ready: true, target: true, conn: connection} 17 shutdown := startTestService(t, world) 18 handler := world.httpHandler() 19 setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=long-test-password&password_confirm=long-test-password", nil) 20 cookie := setup.Result().Cookies()[0] 21 22 selected := adminRequest(handler, "POST", "/admin/weather", url.Values{"weather": {"2"}}.Encode(), cookie) 23 invalid := adminRequest(handler, "POST", "/admin/weather", "weather=8", cookie) 24 page := adminRequest(handler, "GET", "/admin", "", cookie) 25 26 query := &zoneEnvironmentQuery{} 27 callTestZone(t, world, 1, query) 28 if err := shutdown(); err != nil { 29 t.Fatal(err) 30 } 31 if selected.Code != 303 || query.weather != 2 || invalid.Code != 400 || len(connection.packets) != 1 { 32 t.Fatal("weather state", selected.Code, query.weather, invalid.Code, len(connection.packets)) 33 } 34 packet := connection.packets[0] 35 if len(packet) != 72 || binary.BigEndian.Uint32(packet[16:20]) != 0x0c5a5d6d || packet[41] != 0 || packet[42] != 70 || packet[49] != 255 || packet[50] != 255 || packet[51] != 255 { 36 t.Fatalf("native weather packet %x", packet) 37 } 38 if binary.BigEndian.Uint16(packet[29:31]) != 1 || binary.BigEndian.Uint32(packet[31:35]) != 4 || binary.BigEndian.Uint16(packet[35:37]) != 1 { 39 t.Fatalf("weather lifetime %x", packet) 40 } 41 if !strings.Contains(page.Body.String(), "value='2' selected>Rain") || strings.Contains(page.Body.String(), ">Automatic</option>") { 42 t.Fatal("weather setting not reflected") 43 } 44 } 45 46 func TestAdminWeatherReturnsToAutomatic(t *testing.T) { 47 world := newAdminTestServer(t) 48 world.characters[1] = &character{id: 1, playfield: 705} 49 connection := &combatTestConn{} 50 world.clients[1] = &worldClient{id: 1, ready: true, target: true, conn: connection} 51 shutdown := startTestService(t, world) 52 handler := world.httpHandler() 53 setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=admin&password_confirm=admin", nil) 54 cookie := setup.Result().Cookies()[0] 55 started := time.Now() 56 selected := adminRequest(handler, "POST", "/admin/weather", "weather=2", cookie) 57 query := &zoneEnvironmentQuery{} 58 callTestZone(t, world, 1, query) 59 if selected.Code != 303 || query.weatherUntil.Before(started.Add(5*time.Minute)) || query.weatherUntil.After(time.Now().Add(5*time.Minute)) { 60 t.Fatal("weather event duration", selected.Code, query.weatherUntil) 61 } 62 63 // Advance the zone through the event deadline without waiting five minutes. 64 var before, after, beforePackets, afterPackets int 65 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 66 state.advancePlayerMovement(query.weatherUntil.Add(-time.Second)) 67 before, beforePackets = state.weather, len(connection.packets) 68 state.advancePlayerMovement(query.weatherUntil) 69 state.advancePlayerMovement(query.weatherUntil.Add(time.Hour)) 70 after, afterPackets = state.weather, len(connection.packets) 71 return nil 72 })) 73 if before != 2 || beforePackets != 2 || after != 0 || afterPackets != 2 { 74 t.Fatal("weather expired early or kept renewing", before, beforePackets, after, afterPackets) 75 } 76 77 // The directory also presents an expired event as automatic weather. 78 if err := world.world.operate(func(*worldCoordinator) error { 79 return world.world.publishEnvironment(zoneWeather{2, time.Now().Add(-time.Second)}) 80 }); err != nil { 81 t.Fatal(err) 82 } 83 page := adminRequest(handler, "GET", "/admin", "", cookie) 84 if !strings.Contains(page.Body.String(), ">Choose weather</option>") { 85 t.Fatal("expired weather remained selected") 86 } 87 if err := shutdown(); err != nil { 88 t.Fatal(err) 89 } 90 } 91 92 func TestAdminWeatherDoesNotRestoreAfterRestart(t *testing.T) { 93 useNPCData(t, testNPCData()) 94 world := newTestServer(t) 95 shutdown := startTestService(t, world) 96 handler := world.httpHandler() 97 setup := adminRequest(handler, "POST", "/admin/login", "username=Operator&password=long-test-password&password_confirm=long-test-password", nil) 98 cookie := setup.Result().Cookies()[0] 99 100 response := adminRequest(handler, "POST", "/admin/weather", "weather=4", cookie) 101 if err := shutdown(); err != nil { 102 t.Fatal(err) 103 } 104 restarted := newServerForTest(t) 105 106 if response.Code != 303 || restarted.weather != 0 { 107 t.Fatal("temporary weather restored", response.Code, restarted.weather) 108 } 109 }