Include latest and earliest record times in state #34

Merged
natercio merged 2 commits from state-time-boundaries into main 2026-10-12 23:09:40 +01:00
Showing only changes of commit b142ce49b3 - Show all commits
+25 -5
View File
@@ -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
}