Ariver
2026-07-13 2d1d4ad406228ef62ab078724cb7d1556e003d01
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package history
 
import (
    "encoding/json"
    "fmt"
    "os"
    "path/filepath"
    "testing"
    "time"
)
 
func TestAddDoesNotCapAtFiftyEntries(t *testing.T) {
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
    }
 
    for i := 0; i < 60; i++ {
        store.Add("history entry")
    }
 
    entries := store.GetAll()
    if len(entries) != 60 {
        t.Fatalf("expected 60 entries, got %d", len(entries))
    }
 
    fd := readHistoryFile(t, store.path)
    if len(fd.Entries) != 60 {
        t.Fatalf("expected 60 persisted entries, got %d", len(fd.Entries))
    }
}
 
func TestRetentionPrunesMemoryAndFile(t *testing.T) {
    now := time.Now()
    store := &Store{
        retentionDays: 7,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "new entry", Timestamp: now.UnixMilli()},
            {Text: "old entry", Timestamp: now.AddDate(0, 0, -8).UnixMilli()},
        },
    }
 
    entries := store.GetAll()
    if len(entries) != 1 {
        t.Fatalf("expected 1 retained entry, got %d", len(entries))
    }
    if entries[0].Text != "new entry" {
        t.Fatalf("expected new entry to be retained, got %q", entries[0].Text)
    }
 
    fd := readHistoryFile(t, store.path)
    if len(fd.Entries) != 1 {
        t.Fatalf("expected 1 persisted entry, got %d", len(fd.Entries))
    }
    if fd.Entries[0].Text != "new entry" {
        t.Fatalf("expected persisted new entry, got %q", fd.Entries[0].Text)
    }
}
 
func TestSetRetentionDaysRejectsUnsupportedValues(t *testing.T) {
    store := &Store{
        retentionDays: 30,
        path:          filepath.Join(t.TempDir(), "history.json"),
    }
 
    if err := store.SetRetentionDays(365); err == nil {
        t.Fatal("expected unsupported retention value to fail")
    }
    if got := store.GetRetentionDays(); got != 30 {
        t.Fatalf("expected retention to remain 30, got %d", got)
    }
}
 
func TestLoadNormalizesUnsupportedRetention(t *testing.T) {
    path := filepath.Join(t.TempDir(), "history.json")
    data, err := json.Marshal(fileData{
        RetentionDays: intPtr(365),
        Entries:       []Entry{{Text: "entry", Timestamp: time.Now().UnixMilli()}},
    })
    if err != nil {
        t.Fatal(err)
    }
    if err := os.WriteFile(path, data, 0644); err != nil {
        t.Fatal(err)
    }
 
    store := &Store{
        retentionDays: 30,
        path:          path,
    }
    store.load()
 
    if got := store.GetRetentionDays(); got != 30 {
        t.Fatalf("expected invalid loaded retention to normalize to 30, got %d", got)
    }
}
 
func TestGetUnexportedFiltersByScope(t *testing.T) {
    loc := time.FixedZone("UTC+8", 8*60*60)
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, loc)
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "today", Timestamp: now.Add(-time.Hour).UnixMilli()},
            {Text: "exported", Timestamp: now.Add(-2 * time.Hour).UnixMilli(), ExportedAt: now.UnixMilli()},
            {Text: "yesterday", Timestamp: now.AddDate(0, 0, -1).UnixMilli()},
            {Text: "old", Timestamp: now.AddDate(0, 0, -8).UnixMilli()},
        },
    }
 
    today := store.GetUnexported(ExportScopeToday, now, loc)
    if len(today) != 1 || today[0].Text != "today" {
        t.Fatalf("expected only today's unexported entry, got %+v", today)
    }
 
    last7Days := store.GetUnexported(ExportScopeLast7Days, now, loc)
    if len(last7Days) != 2 || last7Days[0].Text != "today" || last7Days[1].Text != "yesterday" {
        t.Fatalf("expected today and yesterday, got %+v", last7Days)
    }
 
    all := store.GetUnexported(ExportScopeAll, now, loc)
    if len(all) != 3 || all[0].Text != "today" || all[1].Text != "yesterday" || all[2].Text != "old" {
        t.Fatalf("expected all unexported entries, got %+v", all)
    }
}
 
func TestGetPageClampsAndReturnsCurrentSlice(t *testing.T) {
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
    }
    for i := 0; i < 12; i++ {
        store.entries = append(store.entries, Entry{
            Text:      fmt.Sprintf("entry %02d", i),
            Timestamp: int64(1000 - i),
        })
    }
 
    page := store.GetPage(2, 5)
    if page.Total != 12 || page.TotalPages != 3 || page.Page != 2 || page.PageSize != 5 {
        t.Fatalf("unexpected page metadata: %+v", page)
    }
    if len(page.Entries) != 5 || page.Entries[0].Text != "entry 05" || page.Entries[4].Text != "entry 09" {
        t.Fatalf("unexpected page entries: %+v", page.Entries)
    }
 
    last := store.GetPage(99, 5)
    if last.Page != 3 || len(last.Entries) != 2 || last.Entries[0].Text != "entry 10" {
        t.Fatalf("expected clamped last page, got %+v", last)
    }
 
    defaultSize := store.GetPage(1, 0)
    if defaultSize.PageSize != defaultPageSize {
        t.Fatalf("expected default page size %d, got %d", defaultPageSize, defaultSize.PageSize)
    }
}
 
func TestGetPageHandlesEmptyHistory(t *testing.T) {
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
    }
 
    page := store.GetPage(4, 100)
    if page.Total != 0 || page.TotalPages != 0 || page.Page != 1 || len(page.Entries) != 0 {
        t.Fatalf("unexpected empty page: %+v", page)
    }
}
 
func TestCountsSummarizesFullHistory(t *testing.T) {
    loc := time.FixedZone("UTC+8", 8*60*60)
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, loc)
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "today", Timestamp: now.Add(-time.Hour).UnixMilli()},
            {Text: "exported", Timestamp: now.Add(-2 * time.Hour).UnixMilli(), ExportedAt: now.UnixMilli()},
            {Text: "yesterday", Timestamp: now.AddDate(0, 0, -1).UnixMilli()},
            {Text: "old", Timestamp: now.AddDate(0, 0, -8).UnixMilli()},
            {Text: "corrupt exportedAt", Timestamp: now.Add(-3 * time.Hour).UnixMilli(), ExportedAt: -1},
        },
    }
 
    counts := store.Counts(now, loc)
    if counts.TotalCount != 5 {
        t.Fatalf("expected total 5, got %+v", counts)
    }
    if counts.ExportedCount != 1 {
        t.Fatalf("expected exported 1, got %+v", counts)
    }
    if counts.UnexportedAllCount != 4 || counts.UnexportedLast7DaysCount != 3 || counts.UnexportedTodayCount != 2 {
        t.Fatalf("unexpected unexported counts: %+v", counts)
    }
}
 
func TestMarkExportedPersistsExportedAt(t *testing.T) {
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
    exportedAt := now.Add(time.Minute).UnixMilli()
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "one", Timestamp: now.UnixMilli()},
            {Text: "two", Timestamp: now.Add(-time.Minute).UnixMilli()},
        },
    }
 
    updated, err := store.MarkExported([]int64{store.entries[0].Timestamp}, exportedAt)
    if err != nil {
        t.Fatal(err)
    }
    if updated != 1 {
        t.Fatalf("expected 1 updated entry, got %d", updated)
    }
    if store.entries[0].ExportedAt != exportedAt {
        t.Fatalf("expected exportedAt %d, got %d", exportedAt, store.entries[0].ExportedAt)
    }
    if store.entries[1].ExportedAt != 0 {
        t.Fatalf("expected unmatched entry to remain unexported, got %d", store.entries[1].ExportedAt)
    }
 
    fd := readHistoryFile(t, store.path)
    if len(fd.Entries) != 2 || fd.Entries[0].ExportedAt != exportedAt {
        t.Fatalf("expected exportedAt to persist, got %+v", fd.Entries)
    }
}
 
func TestMarkExportedRollsBackOnSaveFailure(t *testing.T) {
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
    path := filepath.Join(t.TempDir(), "history.json")
    if err := os.Mkdir(path, 0755); err != nil {
        t.Fatal(err)
    }
    store := &Store{
        retentionDays: 0,
        path:          path,
        entries: []Entry{
            {Text: "one", Timestamp: now.UnixMilli()},
        },
    }
 
    updated, err := store.MarkExported([]int64{store.entries[0].Timestamp}, now.UnixMilli())
    if err == nil {
        t.Fatal("expected save failure")
    }
    if updated != 0 {
        t.Fatalf("expected 0 updated entries after rollback, got %d", updated)
    }
    if store.entries[0].ExportedAt != 0 {
        t.Fatalf("expected exportedAt rollback, got %d", store.entries[0].ExportedAt)
    }
}
 
func TestMarkExportedReplacesNonPositiveExportedAt(t *testing.T) {
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
    exportedAt := now.Add(time.Minute).UnixMilli()
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "negative", Timestamp: now.UnixMilli(), ExportedAt: -1},
        },
    }
 
    updated, err := store.MarkExported([]int64{store.entries[0].Timestamp}, exportedAt)
    if err != nil {
        t.Fatal(err)
    }
    if updated != 1 {
        t.Fatalf("expected 1 updated entry, got %d", updated)
    }
    if store.entries[0].ExportedAt != exportedAt {
        t.Fatalf("expected exportedAt to be replaced, got %d", store.entries[0].ExportedAt)
    }
}
 
func TestClearExportedRemovesOnlyPositiveExportedAt(t *testing.T) {
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
    store := &Store{
        retentionDays: 0,
        path:          filepath.Join(t.TempDir(), "history.json"),
        entries: []Entry{
            {Text: "exported", Timestamp: now.UnixMilli(), ExportedAt: now.UnixMilli()},
            {Text: "unexported", Timestamp: now.Add(-time.Minute).UnixMilli()},
            {Text: "zero", Timestamp: now.Add(-2 * time.Minute).UnixMilli(), ExportedAt: 0},
            {Text: "negative", Timestamp: now.Add(-3 * time.Minute).UnixMilli(), ExportedAt: -1},
        },
    }
 
    result, err := store.ClearExported()
    if err != nil {
        t.Fatal(err)
    }
    if result.DeletedCount != 1 || result.RemainingCount != 3 {
        t.Fatalf("unexpected result: %+v", result)
    }
    if len(store.entries) != 3 {
        t.Fatalf("expected 3 remaining entries, got %d", len(store.entries))
    }
    for _, entry := range store.entries {
        if entry.Text == "exported" {
            t.Fatalf("exported entry was not removed: %+v", store.entries)
        }
    }
 
    fd := readHistoryFile(t, store.path)
    if len(fd.Entries) != 3 {
        t.Fatalf("expected persisted 3 entries, got %+v", fd.Entries)
    }
}
 
func TestClearExportedRollsBackOnSaveFailure(t *testing.T) {
    now := time.Date(2026, 5, 30, 15, 0, 0, 0, time.UTC)
    path := filepath.Join(t.TempDir(), "history.json")
    if err := os.Mkdir(path, 0755); err != nil {
        t.Fatal(err)
    }
    store := &Store{
        retentionDays: 0,
        path:          path,
        entries: []Entry{
            {Text: "exported", Timestamp: now.UnixMilli(), ExportedAt: now.UnixMilli()},
            {Text: "unexported", Timestamp: now.Add(-time.Minute).UnixMilli()},
        },
    }
 
    result, err := store.ClearExported()
    if err == nil {
        t.Fatal("expected save failure")
    }
    if result.DeletedCount != 0 || result.RemainingCount != 0 {
        t.Fatalf("expected empty result after rollback, got %+v", result)
    }
    if len(store.entries) != 2 || store.entries[0].Text != "exported" {
        t.Fatalf("expected entries rollback, got %+v", store.entries)
    }
}
 
func TestLoadOldHistoryWithoutExportedAtDefaultsToUnexported(t *testing.T) {
    path := filepath.Join(t.TempDir(), "history.json")
    data := []byte(`{"retentionDays":0,"entries":[{"text":"old format","timestamp":1780134620883}]}`)
    if err := os.WriteFile(path, data, 0644); err != nil {
        t.Fatal(err)
    }
 
    store := &Store{
        retentionDays: 30,
        path:          path,
    }
    store.load()
 
    entries := store.GetAll()
    if len(entries) != 1 {
        t.Fatalf("expected 1 entry, got %d", len(entries))
    }
    if entries[0].ExportedAt != 0 {
        t.Fatalf("expected old entry to load as unexported, got exportedAt=%d", entries[0].ExportedAt)
    }
}
 
func readHistoryFile(t *testing.T, path string) fileData {
    t.Helper()
    data, err := os.ReadFile(path)
    if err != nil {
        t.Fatal(err)
    }
    var fd fileData
    if err := json.Unmarshal(data, &fd); err != nil {
        t.Fatal(err)
    }
    return fd
}
 
func intPtr(v int) *int {
    return &v
}