server/control_test.go (3927B)
1 package main 2 3 import ( 4 "encoding/hex" 5 "net" 6 "net/http" 7 "net/http/httptest" 8 "strings" 9 "testing" 10 "time" 11 ) 12 13 func TestSetGameTimeUpdatesConnectedPlayer(t *testing.T) { 14 useNPCData(t, testNPCData()) 15 world := newTestServer(t) 16 player := world.newCharacter() 17 player.playfield = 800 18 client, connection := net.Pipe() 19 defer client.Close() 20 defer connection.Close() 21 world.clients[player.id] = &worldClient{conn: connection, ready: true, id: player.id} 22 request := httptest.NewRequest("POST", "/admin/time", strings.NewReader("hour=26&minute=45")) 23 request.Header.Set("Content-Type", "application/x-www-form-urlencoded") 24 response := httptest.NewRecorder() 25 done := make(chan struct{}) 26 27 startTestService(t, world) 28 client.SetDeadline(time.Now().Add(5 * time.Second)) 29 go func() { world.setGameTime(response, request); close(done) }() 30 packet, err := readPacket(client) 31 if err != nil { 32 t.Fatal(err) 33 } 34 <-done 35 36 if response.Code != http.StatusSeeOther || response.Header().Get("Location") != "/admin" { 37 t.Fatalf("time response: %d %v", response.Code, response.Header()) 38 } 39 if hex.EncodeToString(packet[12:]) != "00000001000000005f52412e0000c350000000010000000047bc16000000000000000001" { 40 t.Fatalf("original clock update: %x", packet) 41 } 42 page := httptest.NewRecorder() 43 world.controlPage(page, httptest.NewRequest("GET", "/admin", nil)) 44 if !strings.Contains(page.Body.String(), "Game time: 26:45") { 45 t.Fatalf("clock display: %s", page.Body.String()) 46 } 47 } 48 49 func TestSetGameTimeRejectsHourOutsideOriginalDay(t *testing.T) { 50 world := newTestServer(t) 51 request := httptest.NewRequest("POST", "/admin/time", strings.NewReader("hour=27&minute=0")) 52 request.Header.Set("Content-Type", "application/x-www-form-urlencoded") 53 response := httptest.NewRecorder() 54 55 world.setGameTime(response, request) 56 57 if response.Code != http.StatusBadRequest || world.gameSeconds != 48600 { 58 t.Fatalf("invalid time changed the clock: status %d, seconds %v", response.Code, world.gameSeconds) 59 } 60 } 61 62 func TestUserPageLinksToSeparateTeleportPage(t *testing.T) { 63 world := newTestServer(t) 64 player := world.newCharacter() 65 world.clients[player.id] = &worldClient{ready: true} 66 if err := world.saveUser(player); err != nil { 67 t.Fatal(err) 68 } 69 response := httptest.NewRecorder() 70 71 request := httptest.NewRequest("GET", "/admin/players/1", nil) 72 request.SetPathValue("character", "1") 73 world.userPage(response, request) 74 75 body := response.Body.String() 76 if strings.Contains(body, "<canvas") || !strings.Contains(body, "href='admin/players/1/teleport'") || !strings.Contains(body, "<h2>Character data</h2>") { 77 t.Fatal("player page must expose character data and link to teleport") 78 } 79 80 request = httptest.NewRequest("GET", "/admin/players/1/teleport", nil) 81 request.SetPathValue("character", "1") 82 response = httptest.NewRecorder() 83 world.teleportPage(response, request) 84 body = response.Body.String() 85 if !strings.Contains(body, "<select data-map-find>") || !strings.Contains(body, "action='admin/players/1/teleport'") { 86 t.Fatal("missing teleport map controls") 87 } 88 if strings.Index(body, "Teleport here</button>") > strings.Index(body, "<canvas") || !strings.Contains(body, "</canvas></div><script") { 89 t.Fatal("teleport controls must precede the map, with nothing below it") 90 } 91 } 92 93 func TestTeleportRejectsZoneWithoutOriginalAssets(t *testing.T) { 94 world := newTestServer(t) 95 player := world.newCharacter() 96 mux := http.NewServeMux() 97 mux.HandleFunc("POST /admin/players/{character}/teleport", world.teleportPlayer) 98 request := httptest.NewRequest("POST", "/admin/players/1/teleport", strings.NewReader("playfield=690")) 99 request.Header.Set("Content-Type", "application/x-www-form-urlencoded") 100 response := httptest.NewRecorder() 101 102 mux.ServeHTTP(response, request) 103 104 if response.Code != http.StatusBadRequest || player.playfield != 950 { 105 t.Fatalf("unavailable destination: status %d, playfield %d", response.Code, player.playfield) 106 } 107 }