-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_run_roundtrip_test.go
More file actions
272 lines (253 loc) · 9.21 KB
/
Copy pathapp_run_roundtrip_test.go
File metadata and controls
272 lines (253 loc) · 9.21 KB
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
package main
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"geosense-infer/internal/analysis"
"geosense-infer/internal/store"
)
// newTestApp builds an App backed by a store in a temporary home, which is what
// openTestStore does for the store's own tests.
func newTestApp(t *testing.T) *App {
t.Helper()
tmp := t.TempDir()
t.Setenv("HOME", tmp)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmp, "config"))
st, err := store.Open()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
return &App{store: st}
}
func latestRunID(t *testing.T, a *App) string {
t.Helper()
runs, err := a.store.ListRuns(store.LocalUserID, 10)
if err != nil {
t.Fatal(err)
}
if len(runs) != 1 {
t.Fatalf("expected exactly one saved run, got %d", len(runs))
}
return runs[0].ID
}
// A reloaded run knows which run it is.
//
// RunID is stamped by Predict AFTER persisting, so the copy serialised to disk
// cannot carry it -- the id did not exist yet. LoadAnalysis returned that copy
// untouched, so a reopened analysis came back unable to say what it was, and
// every surface keyed on the id had to invent one. The map screen invented the
// literal "current"; the board then listed a run under the AOI label or, with
// none, "Analysis" -- a row indistinguishable from a saved run, which refused
// every action taken on it because the store has no run by that name.
//
// Asserted for a classification and for the products that return through the
// other branch, since each has its own return and only one of them was ever
// going to be remembered.
func TestLoadAnalysisStampsTheRunItReturns(t *testing.T) {
a := newTestApp(t)
// Built here rather than loaded: the point is a result with NO run id, and
// a fixture that happened to gain one would silently stop testing it.
res := analysis.PredictResult{NDates: 3}
body, err := json.Marshal(res)
if err != nil {
t.Fatal(err)
}
if _, err := a.store.SaveRun(store.InferenceRun{
UserID: store.LocalUserID,
ModelKind: "spectral",
PeriodStart: "2025-01-01",
PeriodEnd: "2025-12-31",
Status: "done",
Kind: store.RunKindClassification,
ResultJSON: string(body),
}); err != nil {
t.Fatal(err)
}
runID := latestRunID(t, a)
// The stored copy is the shape the defect came from: no id inside it.
stored, err := a.store.GetRun(store.LocalUserID, runID)
if err != nil {
t.Fatal(err)
}
var raw analysis.PredictResult
if err := json.Unmarshal([]byte(stored.ResultJSON), &raw); err != nil {
t.Fatal(err)
}
if raw.RunID != "" {
t.Fatalf("the fixture already carries a run id (%q); this test no longer covers the case it was written for", raw.RunID)
}
out, err := a.LoadAnalysis(runID)
if err != nil {
t.Fatal(err)
}
if out.RunID != runID {
t.Fatalf("LoadAnalysis returned run_id %q, want %q", out.RunID, runID)
}
}
// onePixelPNG is the smallest file that exercises the asset path: written from
// a data URI on save and read back into one on load. Its content is never
// examined, only its survival.
const onePixelPNG = "data:image/png;base64," +
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
/*
loadRecordedPayload reads a recorded sidecar payload from
internal/research/testdata and unmarshals the block under the given key.
It fails rather than skips when the file is missing or does not bind. A skip on
a fixture that is present is a test that reports success without running, and a
skip on one that is absent is a round trip nobody checked; both are failures
here.
*/
func loadRecordedPayload(t *testing.T, name, key string, dst any) {
t.Helper()
path := filepath.Join("internal", "research", "testdata", name)
raw, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
var wrapped map[string]json.RawMessage
if err := json.Unmarshal(raw, &wrapped); err != nil {
t.Fatalf("parse %s: %v", path, err)
}
block, ok := wrapped[key]
if !ok {
t.Fatalf("%s carries no %q key", path, key)
}
if err := json.Unmarshal(block, dst); err != nil {
t.Fatalf("unmarshal %s from %s: %v", key, path, err)
}
}
// TestMineralRunRoundTrip checks that a mineral map is saved under its own
// kind, restored through the mineral field, and keeps both class maps, every
// derived layer and its GeoTIFF, on a payload recorded from a real run: EMIT
// L2A V001 over the Quadrilatero Ferrifero, four passes compared per cell
// between 2024-07-04 and 2025-07-01, with three uncertainty draws.
func TestMineralRunRoundTrip(t *testing.T) {
a := newTestApp(t)
var res analysis.MineralAnalysis
loadRecordedPayload(t, "mineral_qf.json", "mineral", &res)
res.NormalizeNilSlices()
if len(res.Groups) != 2 || len(res.Scenes) == 0 || res.ObservedCells == 0 {
t.Fatalf("the recorded payload did not bind: %d groups, %d scenes, %d observed cells",
len(res.Groups), len(res.Scenes), res.ObservedCells)
}
if len(res.Groups[0].Classes) == 0 || len(res.Groups[1].Entries) == 0 {
t.Fatal("the class and reference tables did not bind")
}
// The recorded paths point into a work directory that never existed here.
tifSrc := filepath.Join(t.TempDir(), "mineral_map.tif")
if err := os.WriteFile(tifSrc, []byte("not a real geotiff, but a real file"), 0o600); err != nil {
t.Fatal(err)
}
res.GeoTIFF = tifSrc
for i := range res.Groups {
res.Groups[i].ClassPNG = ""
res.Groups[i].ClassURI = onePixelPNG
}
if len(res.Layers) == 0 || res.Selection == nil || len(res.Agreement) == 0 {
t.Fatalf("the derived figures did not bind: %d layers, selection %v, %d agreements",
len(res.Layers), res.Selection, len(res.Agreement))
}
for i := range res.Layers {
res.Layers[i].PNG = ""
res.Layers[i].URI = onePixelPNG
}
used := 0
for _, s := range res.Scenes {
if s.Cells > 0 {
used++
}
}
a.persistMineralRun(analysis.MineralRequest{Label: "Quadrilatero", Start: "2024-08-01", End: "2024-09-30"}, &res)
runID := latestRunID(t, a)
runs, _ := a.store.ListRuns(store.LocalUserID, 10)
if runs[0].Kind != store.RunKindMineral {
t.Fatalf("kind = %q, want %q", runs[0].Kind, store.RunKindMineral)
}
// Every pass compared is listed in Scenes; the run row counts those a cell
// was taken from.
if runs[0].NDates != used {
t.Fatalf("n_dates = %d, want the %d passes used", runs[0].NDates, used)
}
var summary struct {
Observed float64 `json:"mineral_observed_area_ha"`
AOI float64 `json:"mineral_aoi_area_ha"`
Top2 string `json:"mineral_group2_top_class"`
}
if err := json.Unmarshal([]byte(runs[0].SummaryJSON), &summary); err != nil {
t.Fatal(err)
}
// Identified areas without the ground they were taken over read as the
// whole area; the row carries both.
if summary.Observed != res.ObservedAreaHa || summary.AOI != res.AOIAreaHa || summary.AOI == 0 {
t.Fatalf("the run row lists no observed area beside its identified areas: %+v", summary)
}
if summary.Top2 != res.Groups[1].Classes[0].Class {
t.Fatalf("top class = %q, want %q", summary.Top2, res.Groups[1].Classes[0].Class)
}
stored, err := a.store.GetRun(store.LocalUserID, runID)
if err != nil {
t.Fatal(err)
}
if strings.Contains(stored.ResultJSON, "data:image") {
t.Fatal("a class map's base64 reached the database")
}
loaded, err := a.LoadAnalysis(runID)
if err != nil {
t.Fatal(err)
}
if loaded.Mineral == nil {
t.Fatal("the mineral run reopened with no mineral result")
}
m := loaded.Mineral
if m.RunID != runID {
t.Fatalf("run id = %q, want %q", m.RunID, runID)
}
for _, g := range m.Groups {
if !strings.HasPrefix(g.ClassURI, "data:image/png;base64,") {
t.Fatalf("group %d reopened without its class map", g.Group)
}
if !strings.HasPrefix(g.ClassPNG, a.store.RunsDir(runID)) {
t.Fatalf("group %d class map path %q is not in the run's folder", g.Group, g.ClassPNG)
}
}
if m.GeoTIFF == "" || !strings.HasPrefix(m.GeoTIFF, a.store.RunsDir(runID)) {
t.Fatalf("GeoTIFF path %q is not in the run's folder", m.GeoTIFF)
}
if m.ObservedCells != res.ObservedCells || len(m.Groups[1].Entries) != len(res.Groups[1].Entries) {
t.Fatal("the figures did not survive the round trip")
}
if len(m.Layers) != len(res.Layers) {
t.Fatalf("%d derived layers reopened, want %d", len(m.Layers), len(res.Layers))
}
for _, l := range m.Layers {
if !strings.HasPrefix(l.URI, "data:image/png;base64,") {
t.Fatalf("layer %q reopened without its raster", l.ID)
}
if !strings.HasPrefix(l.PNG, a.store.RunsDir(runID)) {
t.Fatalf("layer %q path %q is not in the run's folder", l.ID, l.PNG)
}
}
if m.Selection == nil || *m.Selection != *res.Selection || len(m.Agreement) != len(res.Agreement) ||
len(m.Positions) != len(res.Positions) || len(m.Confidence) != len(res.Confidence) {
t.Fatal("the pass selection, positions, confidence or agreement did not survive the round trip")
}
}
// TestMineralLayerFileNames refuses a layer id that is not a plain name, since
// the id comes from the sidecar and becomes part of a path.
func TestMineralLayerFileNames(t *testing.T) {
for id, want := range map[string]string{
"margin1": "mineral_layer_margin1.png",
"acid_sulfate": "mineral_layer_acid_sulfate.png",
"": "",
"../x": "",
"Pass": "",
"a/b": "",
} {
if got := mineralLayerPNG(id); got != want {
t.Errorf("mineralLayerPNG(%q) = %q, want %q", id, got, want)
}
}
}