aboutsummaryrefslogtreecommitdiff
path: root/internal/cache/cache_test.go
blob: 9560ff2a75d1a2d3ce3676d33f587d521dea8b22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package cache

import (
	"encoding/json"
	"os"
	"path/filepath"
	"sync"
	"testing"
)

func tmpCache(t *testing.T) *Cache {
	t.Helper()
	return New(filepath.Join(t.TempDir(), "sub", "cache.json"))
}

func TestGeoRoundTrip(t *testing.T) {
	c := tmpCache(t)
	if _, ok := c.Geo("Krakow"); ok {
		t.Fatal("empty cache reported a hit")
	}
	want := Geo{Lat: 50.0617, Lon: 19.9373, Label: "Krakow, PL", Country: "PL"}
	if err := c.PutGeo("Krakow", want); err != nil {
		t.Fatal(err)
	}
	got, ok := c.Geo("Krakow")
	if !ok || got != want {
		t.Fatalf("got %+v (%v), want %+v", got, ok, want)
	}
}

// "" is a real answer -- not in Poland -- and must be distinguishable from
// never having asked, or every foreign location re-queries GUGiK forever.
func TestTerytEmptyStringIsARealAnswer(t *testing.T) {
	c := tmpCache(t)
	if _, ok := c.Teryt("52.5200,13.4000"); ok {
		t.Fatal("empty cache reported a hit")
	}
	if err := c.PutTeryt("52.5200,13.4000", ""); err != nil {
		t.Fatal(err)
	}
	code, ok := c.Teryt("52.5200,13.4000")
	if !ok {
		t.Fatal("a cached empty code must report as present")
	}
	if code != "" {
		t.Fatalf("code = %q, want empty", code)
	}
}

func TestCorruptFileIsTreatedAsEmpty(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, "cache.json")
	if err := os.WriteFile(path, []byte("{not json"), 0o644); err != nil {
		t.Fatal(err)
	}
	c := New(path)
	if _, ok := c.Geo("anything"); ok {
		t.Fatal("corrupt cache must read as empty, not error")
	}
	if err := c.PutGeo("x", Geo{Lat: 1}); err != nil {
		t.Fatalf("must be able to overwrite a corrupt cache: %v", err)
	}
}

func TestSaveLeavesNoTempFiles(t *testing.T) {
	dir := t.TempDir()
	c := New(filepath.Join(dir, "cache.json"))
	if err := c.PutGeo("a", Geo{Lat: 1}); err != nil {
		t.Fatal(err)
	}
	entries, _ := filepath.Glob(filepath.Join(dir, "*.tmp"))
	if len(entries) != 0 {
		t.Fatalf("temp files left behind: %v", entries)
	}
}

// The whole point of the atomic write: concurrent writers must never leave a
// file that fails to parse.
func TestConcurrentWritesKeepValidJSON(t *testing.T) {
	path := filepath.Join(t.TempDir(), "cache.json")
	var wg sync.WaitGroup
	for i := 0; i < 16; i++ {
		wg.Add(1)
		go func(i int) {
			defer wg.Done()
			New(path).PutGeo("place", Geo{Lat: float64(i)})
		}(i)
	}
	wg.Wait()

	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	var s store
	if err := json.Unmarshal(data, &s); err != nil {
		t.Fatalf("cache is not valid JSON after concurrent writes: %v\n%s", err, data)
	}
}

// The Python implementation shares this file and stores geo entries as
// [lat, lon, label, country]. If Go writes an object instead, Python crashes on
// its own cache -- which is exactly what happened once.
func TestGeoIsStoredAsAnArrayForPythonInterop(t *testing.T) {
	path := filepath.Join(t.TempDir(), "cache.json")
	c := New(path)
	if err := c.PutGeo("Krakow", Geo{Lat: 50.0617, Lon: 19.9373, Label: "Krakow, PL", Country: "PL"}); err != nil {
		t.Fatal(err)
	}
	data, _ := os.ReadFile(path)
	var probe struct {
		Geo map[string][]any `json:"geo"`
	}
	if err := json.Unmarshal(data, &probe); err != nil {
		t.Fatalf("geo must decode as arrays: %v\n%s", err, data)
	}
	entry := probe.Geo["Krakow"]
	if len(entry) != 4 {
		t.Fatalf("geo entry = %v, want 4 elements", entry)
	}
	if entry[2] != "Krakow, PL" {
		t.Errorf("third element must be the label, got %v", entry[2])
	}
}

// A cache written in the Python shape must load here unchanged.
func TestReadsPythonWrittenCache(t *testing.T) {
	path := filepath.Join(t.TempDir(), "cache.json")
	body := `{"geo":{"Krakow":[50.06174,19.93732,"Krakow, PL","PL"]},"teryt":{"50.0617,19.9373":"1261"}}`
	if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
	c := New(path)
	g, ok := c.Geo("Krakow")
	if !ok || g.Label != "Krakow, PL" || g.Country != "PL" {
		t.Fatalf("got %+v (%v)", g, ok)
	}
	if code, ok := c.Teryt("50.0617,19.9373"); !ok || code != "1261" {
		t.Fatalf("teryt = %q (%v)", code, ok)
	}
}

// writeCache puts raw bytes where the cache expects its file.
func writeCache(t *testing.T, c *Cache, body string) {
	t.Helper()
	if err := os.MkdirAll(filepath.Dir(c.path), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(c.path, []byte(body), 0o644); err != nil {
		t.Fatal(err)
	}
}

// One unreadable entry used to cost the whole file: load treated the parse
// error as "empty cache", and the next save wrote that emptiness over
// everything that was still fine.
func TestOneMalformedEntryDoesNotDestroyTheOthers(t *testing.T) {
	c := tmpCache(t)
	writeCache(t, c, `{"geo":{"Gdansk":[54.35227,18.64912,"Gdansk, PL","PL"],"Broken":[1]},"teryt":{"50.0617,19.9373":"1261"}}`)

	if err := c.PutGeo("Krakow", Geo{Lat: 50.0617, Lon: 19.9373, Label: "Krakow, PL", Country: "PL"}); err != nil {
		t.Fatal(err)
	}
	if _, ok := c.Geo("Gdansk"); !ok {
		t.Error("a good entry was destroyed by an unrelated malformed one")
	}
	if _, ok := c.Geo("Krakow"); !ok {
		t.Error("the new entry was not stored")
	}
	if code, ok := c.Teryt("50.0617,19.9373"); !ok || code != "1261" {
		t.Errorf("teryt section lost too: got %q, %v", code, ok)
	}
	if _, ok := c.Geo("Broken"); ok {
		t.Error("the malformed entry should be dropped, not resurrected")
	}
}

// A hand-edit that breaks the whole document must not cost the file. The cache
// is disposable, but whatever was typed into it is not, so it moves aside
// rather than being overwritten -- and the run still gets a working cache.
func TestAnUnparseableFileIsMovedAsideNotDestroyed(t *testing.T) {
	c := tmpCache(t)
	const broken = `{"geo":{"Gdansk":[54.35227,`
	writeCache(t, c, broken)

	if err := c.PutGeo("Krakow", Geo{Lat: 50.0617, Lon: 19.9373}); err != nil {
		t.Fatalf("the tool must keep working: %v", err)
	}
	kept, err := os.ReadFile(c.path + ".bad")
	if err != nil {
		t.Fatalf("the unparseable file was not preserved: %v", err)
	}
	if string(kept) != broken {
		t.Errorf("preserved copy differs:\n got %s\nwant %s", kept, broken)
	}
	if _, ok := c.Geo("Krakow"); !ok {
		t.Error("the fresh cache did not take the new entry")
	}
}

func TestPutTerytAlsoMovesAnUnparseableFileAside(t *testing.T) {
	c := tmpCache(t)
	const broken = `{oops`
	writeCache(t, c, broken)
	if err := c.PutTeryt("50.0617,19.9373", "1261"); err != nil {
		t.Fatalf("the tool must keep working: %v", err)
	}
	kept, err := os.ReadFile(c.path + ".bad")
	if err != nil || string(kept) != broken {
		t.Errorf("unparseable file not preserved: %q, %v", kept, err)
	}
	if code, ok := c.Teryt("50.0617,19.9373"); !ok || code != "1261" {
		t.Errorf("fresh cache did not take the entry: %q, %v", code, ok)
	}
}

// The file is read by a human at least as often as by the program.
func TestSaveWritesOneEntryPerLine(t *testing.T) {
	c := tmpCache(t)
	if err := c.PutGeo("Krakow", Geo{Lat: 50.06174, Lon: 19.93732, Label: "Krakow, PL", Country: "PL"}); err != nil {
		t.Fatal(err)
	}
	if err := c.PutGeo("Chiang Mai", Geo{Lat: 18.79038, Lon: 98.98468, Label: "Chiang Mai, TH", Country: "TH"}); err != nil {
		t.Fatal(err)
	}
	if err := c.PutTeryt("50.0617,19.9373", "1261"); err != nil {
		t.Fatal(err)
	}
	body, err := os.ReadFile(c.path)
	if err != nil {
		t.Fatal(err)
	}
	want := `{
  "geo": {
    "Chiang Mai": [18.79038, 98.98468, "Chiang Mai, TH", "TH"],
    "Krakow": [50.06174, 19.93732, "Krakow, PL", "PL"]
  },
  "teryt": {
    "50.0617,19.9373": "1261"
  }
}
`
	if string(body) != want {
		t.Errorf("got:\n%s\nwant:\n%s", body, want)
	}
}

func TestSavedFileIsStillValidJSONForTheOtherImplementation(t *testing.T) {
	c := tmpCache(t)
	want := Geo{Lat: 50.06174, Lon: 19.93732, Label: "Krakow, PL", Country: "PL"}
	if err := c.PutGeo("Krakow", want); err != nil {
		t.Fatal(err)
	}
	body, err := os.ReadFile(c.path)
	if err != nil {
		t.Fatal(err)
	}
	var got struct {
		Geo map[string][]any `json:"geo"`
	}
	if err := json.Unmarshal(body, &got); err != nil {
		t.Fatalf("a stock JSON parser could not read it: %v", err)
	}
	row := got.Geo["Krakow"]
	if len(row) != 4 {
		t.Fatalf("got %d fields, want the 4-element shape Python reads: %v", len(row), row)
	}
	if row[2] != "Krakow, PL" {
		t.Errorf("label field is %v, want the third element", row[2])
	}
}

func TestEmptyCacheSavesReadableJSON(t *testing.T) {
	c := tmpCache(t)
	if err := c.PutTeryt("52.5200,13.4000", ""); err != nil {
		t.Fatal(err)
	}
	body, _ := os.ReadFile(c.path)
	if !json.Valid(body) {
		t.Fatalf("invalid JSON with an empty geo section:\n%s", body)
	}
}