diff options
Diffstat (limited to 'internal/worktime/db_test.go')
| -rw-r--r-- | internal/worktime/db_test.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/internal/worktime/db_test.go b/internal/worktime/db_test.go index d011a9e..f599fa1 100644 --- a/internal/worktime/db_test.go +++ b/internal/worktime/db_test.go @@ -129,6 +129,32 @@ func TestLoadAllMergesAndSortsEntries(t *testing.T) { } } +func TestLoadAllBackfillsMissingSourceFromHost(t *testing.T) { + dbDir := t.TempDir() + dbFile := filepath.Join(dbDir, "db.host-a.json") + content := `{ + "entries": { + "host-a": [ + {"action":"login","what":"work","epoch":10,"human":"h1"} + ] + } +}` + if err := os.WriteFile(dbFile, []byte(content), 0o644); err != nil { + t.Fatalf("WriteFile() error = %v", err) + } + + entries, err := LoadAll(dbDir) + if err != nil { + t.Fatalf("LoadAll() error = %v", err) + } + if len(entries) != 1 { + t.Fatalf("entries len = %d, want 1", len(entries)) + } + if entries[0].Source != "host-a" { + t.Fatalf("entries[0].Source = %q, want host-a", entries[0].Source) + } +} + func TestLoadAllOnMissingDirectoryReturnsEmptySlice(t *testing.T) { dbDir := filepath.Join(t.TempDir(), "does-not-exist") @@ -159,6 +185,32 @@ func TestLoadHostInvalidJSON(t *testing.T) { } } +func TestLoadAllAcceptsFloatValueEncoding(t *testing.T) { + dbDir := t.TempDir() + dbFile := filepath.Join(dbDir, "db.host-a.json") + content := `{ + "entries": { + "host-a": [ + {"action":"add","what":"work","epoch":10,"source":"host-a","human":"h1","value":31680.000000000004} + ] + } +}` + if err := os.WriteFile(dbFile, []byte(content), 0o644); err != nil { + t.Fatalf("WriteFile() error = %v", err) + } + + entries, err := LoadAll(dbDir) + if err != nil { + t.Fatalf("LoadAll() error = %v", err) + } + if len(entries) != 1 { + t.Fatalf("entries len = %d, want 1", len(entries)) + } + if entries[0].Value != 31680 { + t.Fatalf("entries[0].Value = %d, want 31680", entries[0].Value) + } +} + func TestLoadAllInvalidJSON(t *testing.T) { dbDir := t.TempDir() badFile := filepath.Join(dbDir, "db.host-a.json") |
