aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/logging_test.go (1983B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"io"
      6 	"log"
      7 	"net/http/httptest"
      8 	"strings"
      9 	"testing"
     10 )
     11 
     12 func TestPlayerLogsStayHighLevelWithOptionalTraces(t *testing.T) {
     13 	for _, tracing := range []bool{false, true} {
     14 		t.Run(map[bool]string{false: "default", true: "trace"}[tracing], func(t *testing.T) {
     15 			world := newCheckpointWorld(t)
     16 			var operational, trace bytes.Buffer
     17 			previousLog, previousTrace := log.Writer(), traceLog.Writer()
     18 			log.SetOutput(&operational)
     19 			traceLog.SetOutput(io.Discard)
     20 			if tracing {
     21 				traceLog.SetOutput(&trace)
     22 			}
     23 			t.Cleanup(func() {
     24 				log.SetOutput(previousLog)
     25 				traceLog.SetOutput(previousTrace)
     26 			})
     27 			input := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})
     28 			for range 20 {
     29 				input = append(input, npcMovementPacket(world.characters[1], 1)...)
     30 				input = append(input, npcMovementPacket(world.characters[1], 2)...)
     31 			}
     32 			connection := &combatInterestConn{input: bytes.NewReader(input)}
     33 
     34 			shutdown := startTestService(t, world)
     35 			if err := world.serveWorldClient(&worldClient{conn: connection}); err != nil {
     36 				t.Fatal(err)
     37 			}
     38 			world.httpHandler().ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/missing", nil))
     39 			if err := shutdown(); err != nil {
     40 				t.Fatal(err)
     41 			}
     42 
     43 			if strings.Count(operational.String(), "\n") != 2 ||
     44 				!strings.Contains(operational.String(), `Player connected: "Explorer" (character 1) from 192.0.2.1:4321`) ||
     45 				!strings.Contains(operational.String(), `Player disconnected: "Explorer" (character 1) from 192.0.2.1:4321`) {
     46 				t.Fatal("unexpected operational log", operational.String())
     47 			}
     48 			if tracing {
     49 				for _, message := range []string{"World C2S:", "World S2C:", "Character 1 action=1 position=", "HTTP GET /missing"} {
     50 					if !strings.Contains(trace.String(), message) {
     51 						t.Fatal("missing diagnostic trace", message)
     52 					}
     53 				}
     54 			} else if trace.Len() != 0 {
     55 				t.Fatal("trace output enabled by default", trace.String())
     56 			}
     57 		})
     58 	}
     59 }