|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/5stackgg/demo-parser/internal/discover" |
| 10 | + "github.com/golang/geo/r3" |
| 11 | +) |
| 12 | + |
| 13 | +func openGround(t *testing.T) string { |
| 14 | + t.Helper() |
| 15 | + return synthRevision(t, map[string][][9]float64{ |
| 16 | + "de_clip.tri.gz": { |
| 17 | + {-3000, -3000, 0, 3000, -3000, 0, 3000, 3000, 0}, |
| 18 | + {-3000, -3000, 0, 3000, 3000, 0, -3000, 3000, 0}, |
| 19 | + }, |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func discoverRequest(revision string) discover.Request { |
| 24 | + return discover.Request{ |
| 25 | + Map: "de_clip", |
| 26 | + Revision: revision, |
| 27 | + Target: r3.Vector{X: 900, Y: 300}, |
| 28 | + Radius: 24, |
| 29 | + Area: discover.Area{Min: r3.Vector{X: -64, Y: -64, Z: -32}, Max: r3.Vector{X: 64, Y: 64, Z: 32}}, |
| 30 | + Techniques: []discover.Technique{{}, {JumpThrow: true}}, |
| 31 | + Strengths: []discover.Strength{discover.Full}, |
| 32 | + BudgetMs: 20000, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestDiscoverEndpointFindsThrows(t *testing.T) { |
| 37 | + t.Setenv("MAP_MESH_CACHE", "4") |
| 38 | + w := post(t, handleDiscover, discoverRequest(openGround(t))) |
| 39 | + if w.Code != http.StatusOK { |
| 40 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 41 | + } |
| 42 | + var res discover.Response |
| 43 | + if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + if len(res.Candidates) == 0 || res.Truncated { |
| 47 | + t.Fatalf("%d candidates, truncated %v", len(res.Candidates), res.Truncated) |
| 48 | + } |
| 49 | + for _, c := range res.Candidates { |
| 50 | + if c.Distance > 24 || c.Name == "" { |
| 51 | + t.Fatalf("candidate %+v", c) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestDiscoverEndpointRejectsBadRequests(t *testing.T) { |
| 57 | + t.Setenv("MAP_MESH_CDN", "") |
| 58 | + cases := map[string]func(*discover.Request){ |
| 59 | + "no map": func(r *discover.Request) { r.Map = "" }, |
| 60 | + "no technique": func(r *discover.Request) { r.Techniques = nil }, |
| 61 | + "no radius": func(r *discover.Request) { r.Radius = 0 }, |
| 62 | + "escaping revision": func(r *discover.Request) { r.Revision = "../../../etc" }, |
| 63 | + } |
| 64 | + for name, mutate := range cases { |
| 65 | + t.Run(name, func(t *testing.T) { |
| 66 | + req := discoverRequest("") |
| 67 | + mutate(&req) |
| 68 | + if w := post(t, handleDiscover, req); w.Code != http.StatusBadRequest { |
| 69 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestDiscoverEndpoint404sAMapWithNoMesh(t *testing.T) { |
| 76 | + t.Setenv("MAP_MESH_CACHE", "4") |
| 77 | + req := discoverRequest(openGround(t)) |
| 78 | + req.Map = "de_nonexistent" |
| 79 | + if w := post(t, handleDiscover, req); w.Code != http.StatusNotFound { |
| 80 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestDiscoverEndpointRejectsNonPost(t *testing.T) { |
| 85 | + w := httptest.NewRecorder() |
| 86 | + handleDiscover(w, httptest.NewRequest(http.MethodGet, "/discover", nil)) |
| 87 | + if w.Code != http.StatusMethodNotAllowed { |
| 88 | + t.Fatalf("status %d", w.Code) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestDiscoverEndpointWritesLowercasePoints(t *testing.T) { |
| 93 | + t.Setenv("MAP_MESH_CACHE", "4") |
| 94 | + w := post(t, handleDiscover, discoverRequest(openGround(t))) |
| 95 | + if w.Code != http.StatusOK { |
| 96 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 97 | + } |
| 98 | + var res struct { |
| 99 | + Candidates []map[string]any `json:"candidates"` |
| 100 | + } |
| 101 | + if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + if len(res.Candidates) == 0 { |
| 105 | + t.Fatal("no candidates") |
| 106 | + } |
| 107 | + for _, field := range []string{"stand", "eye", "position", "velocity", "landing"} { |
| 108 | + v, ok := res.Candidates[0][field].(map[string]any) |
| 109 | + if !ok { |
| 110 | + t.Fatalf("%s is %T", field, res.Candidates[0][field]) |
| 111 | + } |
| 112 | + for _, axis := range []string{"x", "y", "z"} { |
| 113 | + if _, ok := v[axis].(float64); !ok { |
| 114 | + t.Fatalf("%s has no %q: %v", field, axis, v) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// A spot picked off a radar has no real height. Snapped, a guess 90 units off |
| 121 | +// still finds the throws onto the floor there; unsnapped it finds none. |
| 122 | +func TestDiscoverEndpointSnapsAGuessedHeightToTheFloor(t *testing.T) { |
| 123 | + t.Setenv("MAP_MESH_CACHE", "4") |
| 124 | + revision := openGround(t) |
| 125 | + |
| 126 | + req := discoverRequest(revision) |
| 127 | + req.Target.Z = 90 |
| 128 | + req.Area.Min.Z, req.Area.Max.Z = -200, 200 |
| 129 | + |
| 130 | + w := post(t, handleDiscover, req) |
| 131 | + if w.Code != http.StatusOK { |
| 132 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 133 | + } |
| 134 | + var unsnapped discover.Response |
| 135 | + if err := json.Unmarshal(w.Body.Bytes(), &unsnapped); err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + if len(unsnapped.Candidates) != 0 { |
| 139 | + t.Fatalf("unsnapped, %d candidates land 90 units under the target", len(unsnapped.Candidates)) |
| 140 | + } |
| 141 | + |
| 142 | + req.Snap = true |
| 143 | + w = post(t, handleDiscover, req) |
| 144 | + if w.Code != http.StatusOK { |
| 145 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 146 | + } |
| 147 | + var res struct { |
| 148 | + Candidates []map[string]any `json:"candidates"` |
| 149 | + Target struct{ X, Y, Z float64 } |
| 150 | + } |
| 151 | + if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + if len(res.Candidates) == 0 || res.Target.Z != 0 { |
| 155 | + t.Fatalf("snapped: %d candidates, target z %v", len(res.Candidates), res.Target.Z) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestFloorEndpoint(t *testing.T) { |
| 160 | + t.Setenv("MAP_MESH_CACHE", "4") |
| 161 | + revision := openGround(t) |
| 162 | + |
| 163 | + w := post(t, handleFloor, floorRequest{Map: "de_clip", Revision: revision, X: 10, Y: 10, Z: 64}) |
| 164 | + if w.Code != http.StatusOK { |
| 165 | + t.Fatalf("status %d: %s", w.Code, w.Body.String()) |
| 166 | + } |
| 167 | + var res floorResponse |
| 168 | + if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil { |
| 169 | + t.Fatal(err) |
| 170 | + } |
| 171 | + if !res.Found || res.Z != 0 { |
| 172 | + t.Fatalf("floor %+v, want found at 0", res) |
| 173 | + } |
| 174 | + |
| 175 | + w = post(t, handleFloor, floorRequest{Map: "de_clip", Revision: revision, X: 9000, Y: 9000, Z: 0}) |
| 176 | + if err := json.Unmarshal(w.Body.Bytes(), &res); err != nil || res.Found { |
| 177 | + t.Fatalf("off the map: %+v %v", res, err) |
| 178 | + } |
| 179 | +} |
0 commit comments