|
| 1 | +package world |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/gob" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestLocationInitValidation(t *testing.T) { |
| 13 | + t.Run("should validate inputs", func(t *testing.T) { |
| 14 | + loc := &Location{} |
| 15 | + |
| 16 | + _, err := loc.init("ns", "", 0, 0) |
| 17 | + assert.ErrorIs(t, err, ErrLocationRequiredId) |
| 18 | + |
| 19 | + _, err = loc.init("", "id", 0, 0) |
| 20 | + assert.ErrorIs(t, err, ErrLocationRequiredNamespace) |
| 21 | + |
| 22 | + _, err = loc.init("ns", "id", 100, 0) |
| 23 | + assert.ErrorIs(t, err, ErrLocationInvalidLatitude) |
| 24 | + |
| 25 | + _, err = loc.init("ns", "id", 0, 200) |
| 26 | + assert.ErrorIs(t, err, ErrLocationInvalidLongitude) |
| 27 | + |
| 28 | + created, err := loc.init("ns", "id", 1, 1) |
| 29 | + assert.NoError(t, err) |
| 30 | + assert.Equal(t, "ns", created.Ns()) |
| 31 | + assert.Equal(t, "id", created.Id()) |
| 32 | + }) |
| 33 | +} |
| 34 | + |
| 35 | +func TestLocationString(t *testing.T) { |
| 36 | + loc, err := NewLocation("ns", "id", 1.5, 2.5) |
| 37 | + assert.NoError(t, err) |
| 38 | + assert.Equal(t, "ns,id,1.500000,2.500000", loc.String()) |
| 39 | +} |
| 40 | + |
| 41 | +func TestWorldSerialization(t *testing.T) { |
| 42 | + world := NewWorld() |
| 43 | + assert.NoError(t, world.Save("ns1", "loc1", 10, 20)) |
| 44 | + assert.NoError(t, world.Save("ns2", "loc2", -10, -20)) |
| 45 | + |
| 46 | + serialized := world.ToBytes() |
| 47 | + assert.NotEmpty(t, serialized) |
| 48 | + |
| 49 | + restored := NewWorldFromBytes(serialized) |
| 50 | + |
| 51 | + loc1, ok := restored.GetLocation("ns1", "loc1") |
| 52 | + assert.True(t, ok) |
| 53 | + assert.Equal(t, 10.0, loc1.Lat()) |
| 54 | + assert.Equal(t, 20.0, loc1.Lon()) |
| 55 | + |
| 56 | + loc2, ok := restored.GetLocation("ns2", "loc2") |
| 57 | + assert.True(t, ok) |
| 58 | + assert.Equal(t, -10.0, loc2.Lat()) |
| 59 | + assert.Equal(t, -20.0, loc2.Lon()) |
| 60 | +} |
| 61 | + |
| 62 | +func TestWorldFromBytesInvalidPayload(t *testing.T) { |
| 63 | + assert.Panics(t, func() { |
| 64 | + _ = NewWorldFromBytes([]byte("not-a-gob")) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestWorldFromBytesSaveError(t *testing.T) { |
| 69 | + type serialLocation struct { |
| 70 | + ID string |
| 71 | + Lat float64 |
| 72 | + Lon float64 |
| 73 | + } |
| 74 | + |
| 75 | + type serialNamespace struct { |
| 76 | + Name string |
| 77 | + Locations []serialLocation |
| 78 | + } |
| 79 | + |
| 80 | + type serialWorld struct { |
| 81 | + Namespaces []serialNamespace |
| 82 | + } |
| 83 | + |
| 84 | + invalid := serialWorld{Namespaces: []serialNamespace{{Name: "ns", Locations: []serialLocation{{ID: "bad", Lat: 200, Lon: 0}}}}} |
| 85 | + |
| 86 | + var buf bytes.Buffer |
| 87 | + encoder := gob.NewEncoder(&buf) |
| 88 | + err := encoder.Encode(invalid) |
| 89 | + assert.NoError(t, err) |
| 90 | + |
| 91 | + assert.Panics(t, func() { |
| 92 | + _ = NewWorldFromBytes(buf.Bytes()) |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +func TestWorldNamespacePanics(t *testing.T) { |
| 97 | + world := &World{namespaces: map[string]*Namespace{"panicNS": nil}, mu: sync.RWMutex{}} |
| 98 | + |
| 99 | + assert.PanicsWithValue(t, ErrUnexpectedNilNamespace, func() { |
| 100 | + world.Save("panicNS", "id", 0, 0) |
| 101 | + }) |
| 102 | + |
| 103 | + assert.PanicsWithValue(t, ErrUnexpectedNilNamespace, func() { |
| 104 | + world.Delete("panicNS", "id") |
| 105 | + }) |
| 106 | + |
| 107 | + assert.PanicsWithValue(t, ErrUnexpectedNilNamespace, func() { |
| 108 | + world.QueryRange("panicNS", 0, 1, 0, 1) |
| 109 | + }) |
| 110 | + |
| 111 | + assert.PanicsWithValue(t, ErrUnexpectedNilNamespace, func() { |
| 112 | + _, _ = world.GetLocation("panicNS", "id") |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +func TestWorldSaveValidationError(t *testing.T) { |
| 117 | + world := NewWorld() |
| 118 | + |
| 119 | + err := world.Save("ns", "id", 200, 0) |
| 120 | + assert.ErrorIs(t, err, ErrLocationInvalidLatitude) |
| 121 | +} |
| 122 | + |
| 123 | +func TestNamespaceUpdateError(t *testing.T) { |
| 124 | + ns := NewNamespace("ns") |
| 125 | + _, err := ns.SaveLocation("id", 1, 1) |
| 126 | + assert.NoError(t, err) |
| 127 | + |
| 128 | + _, err = ns.SaveLocation("id", 200, 1) |
| 129 | + assert.ErrorIs(t, err, ErrLocationInvalidLatitude) |
| 130 | +} |
| 131 | + |
| 132 | +func TestNamespaceInsertOutOfBounds(t *testing.T) { |
| 133 | + ns := &Namespace{Name: "ns", locations: map[string]*Location{}, tree: NewQuadTree(0, 1, 0, 1)} |
| 134 | + |
| 135 | + _, err := ns.SaveLocation("id", 2, 2) |
| 136 | + assert.ErrorIs(t, err, ErrTreeLocationOutOfBounds) |
| 137 | +} |
| 138 | + |
| 139 | +func TestMergeErrorPath(t *testing.T) { |
| 140 | + world1 := NewWorld() |
| 141 | + badWorld := NewWorld() |
| 142 | + |
| 143 | + // Inject an invalid location directly to bypass validation and force a merge failure. |
| 144 | + badWorld.namespaces["ns"] = &Namespace{locations: map[string]*Location{"bad": {id: "bad", lat: 200, lon: 0, ns: "ns"}}, tree: NewQuadTree(-90, 90, -180, 180)} |
| 145 | + |
| 146 | + assert.Panics(t, func() { |
| 147 | + world1.Merge(badWorld) |
| 148 | + }) |
| 149 | +} |
| 150 | + |
| 151 | +func TestTreeInsertOutOfBounds(t *testing.T) { |
| 152 | + node := NewTreeNode(0, 1, 0, 1, 1) |
| 153 | + loc, err := NewLocation("ns", "id", 2, 2) |
| 154 | + assert.NoError(t, err) |
| 155 | + |
| 156 | + assert.ErrorIs(t, node.insert(loc), ErrTreeLocationOutOfBounds) |
| 157 | +} |
| 158 | + |
| 159 | +func TestTreeDivideNoOpWhenAlreadyDivided(t *testing.T) { |
| 160 | + node := NewTreeNode(0, 1, 0, 1, 1) |
| 161 | + node.IsDivided = true |
| 162 | + |
| 163 | + node.divide() |
| 164 | + |
| 165 | + assert.True(t, node.IsDivided) |
| 166 | +} |
| 167 | + |
| 168 | +func TestTreeQueryRangeNoOverlap(t *testing.T) { |
| 169 | + node := NewTreeNode(0, 1, 0, 1, 1) |
| 170 | + results := node.QueryRange(2, 3, 2, 3) |
| 171 | + |
| 172 | + assert.Empty(t, results) |
| 173 | +} |
| 174 | + |
| 175 | +func TestTreeDivideFallbacks(t *testing.T) { |
| 176 | + node := NewTreeNode(0, 10, 0, 10, 1) |
| 177 | + |
| 178 | + locNW, err := NewLocation("ns", "nw", 1, 1) |
| 179 | + assert.NoError(t, err) |
| 180 | + assert.NoError(t, node.insert(locNW)) |
| 181 | + |
| 182 | + locSE, err := NewLocation("ns", "se", 1, 9) |
| 183 | + assert.NoError(t, err) |
| 184 | + assert.NoError(t, node.insert(locSE)) |
| 185 | + |
| 186 | + assert.True(t, node.IsDivided) |
| 187 | + assert.Equal(t, node.SE, locSE.Node) |
| 188 | +} |
| 189 | + |
| 190 | +func TestTreeInsertRelocatesExistingNode(t *testing.T) { |
| 191 | + tree := NewQuadTree(-90, 90, -180, 180) |
| 192 | + |
| 193 | + loc, err := NewLocation("ns", "id", -80, -170) |
| 194 | + assert.NoError(t, err) |
| 195 | + assert.NoError(t, tree.Insert(loc)) |
| 196 | + |
| 197 | + err = loc.Update(80, 170) |
| 198 | + assert.NoError(t, err) |
| 199 | + |
| 200 | + assert.NoError(t, tree.Insert(loc)) |
| 201 | + assert.NotNil(t, loc.Node) |
| 202 | + assert.True(t, loc.Node.Lat1 >= 0) |
| 203 | +} |
| 204 | + |
| 205 | +func TestTreeDivideWithNilLocationPanics(t *testing.T) { |
| 206 | + node := NewTreeNode(0, 10, 0, 10, 1) |
| 207 | + node.Objects["nil"] = nil |
| 208 | + |
| 209 | + assert.Panics(t, func() { |
| 210 | + node.divide() |
| 211 | + }) |
| 212 | +} |
| 213 | + |
| 214 | +func TestTreeInsertRemovesFromPreviousNode(t *testing.T) { |
| 215 | + node := NewTreeNode(0, 1, 0, 1, 2) |
| 216 | + previous := NewTreeNode(0, 1, 0, 1, 2) |
| 217 | + |
| 218 | + loc, err := NewLocation("ns", "reassign", 0.5, 0.5) |
| 219 | + assert.NoError(t, err) |
| 220 | + |
| 221 | + loc.SetNode(previous) |
| 222 | + previous.Objects[loc.Id()] = loc |
| 223 | + |
| 224 | + assert.NoError(t, node.insert(loc)) |
| 225 | + _, exists := previous.Objects[loc.Id()] |
| 226 | + assert.False(t, exists) |
| 227 | +} |
| 228 | + |
| 229 | +func TestTreeInsertNilLocation(t *testing.T) { |
| 230 | + node := NewTreeNode(0, 1, 0, 1, 1) |
| 231 | + |
| 232 | + assert.ErrorIs(t, node.insert(nil), ErrTreeLocationNil) |
| 233 | +} |
0 commit comments