diff --git a/internal/json_store_test.go b/internal/json_store_test.go index 6ec0539..93856fe 100644 --- a/internal/json_store_test.go +++ b/internal/json_store_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/nmoniz/any2anexoj/internal" "github.com/nmoniz/any2anexoj/internal/mocks" @@ -84,13 +85,25 @@ func TestJSONStore_RoundTrip(t *testing.T) { // confirming the inline form is parseable guards against accidentally // double-encoding it again in the future). var parsed struct { - Queues map[string][]struct { + Latest time.Time `json:"latest"` + Earliest time.Time `json:"earliest"` + Queues map[string][]struct { RecordData json.RawMessage `json:"record_data"` } `json:"queues"` } if err := json.Unmarshal(data, &parsed); err != nil { t.Fatalf("saved state file is not valid JSON: %v", err) } + + // The latest/earliest bounds must be persisted alongside the queues. + // Every record in this test returns testRecordTime, so both bounds must + // equal it exactly. + if !parsed.Latest.Equal(testRecordTime) { + t.Errorf("state latest = %v; want %v", parsed.Latest, testRecordTime) + } + if !parsed.Earliest.Equal(testRecordTime) { + t.Errorf("state earliest = %v; want %v", parsed.Earliest, testRecordTime) + } for symbol, lots := range parsed.Queues { for i, lot := range lots { if len(lot.RecordData) == 0 { @@ -341,13 +354,20 @@ func TestNewJSONStore_ValidatesArguments(t *testing.T) { } } -// newRecord builds a MockRecord whose Symbol() returns the given symbol. -// The JSONStore only reads Symbol() off the loaded Record during tests -// (quantity/price/filled come from the persisted struct fields), so all -// other Record methods can be left as default-mocked values. +// testRecordTime is the deterministic timestamp returned by every record +// built in this file. Using a fixed value keeps the latest/earliest bounds +// written by JSONStore.Save predictable and assertable. +var testRecordTime = time.Date(2024, 6, 15, 12, 0, 0, 0, time.UTC) + +// newRecord builds a MockRecord whose Symbol() returns the given symbol and +// whose Timestamp() returns testRecordTime. The JSONStore only reads Symbol() +// and Timestamp() off the Record during tests (quantity/price/filled come +// from the persisted struct fields), so all other Record methods can be left +// as default-mocked values. func newRecord(ctrl *gomock.Controller, symbol string) *mocks.MockRecord { r := mocks.NewMockRecord(ctrl) r.EXPECT().Symbol().Return(symbol).AnyTimes() + r.EXPECT().Timestamp().Return(testRecordTime).AnyTimes() return r }