|
4 | 4 | "context" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "strings" |
7 | 8 | "testing" |
8 | 9 | "time" |
9 | 10 |
|
@@ -201,3 +202,105 @@ func TestEvent_SavingsPctComputedInRecent(t *testing.T) { |
201 | 202 | t.Errorf("expected SavingsPct=75.0, got %f", recent[0].SavingsPct) |
202 | 203 | } |
203 | 204 | } |
| 205 | + |
| 206 | +func TestDefaultPath(t *testing.T) { |
| 207 | + p, err := tracking.DefaultPath() |
| 208 | + if err != nil { |
| 209 | + t.Fatalf("DefaultPath: %v", err) |
| 210 | + } |
| 211 | + if !strings.HasSuffix(p, ".tok/tracker.db") { |
| 212 | + t.Errorf("expected path to end with .tok/tracker.db, got %q", p) |
| 213 | + } |
| 214 | + // Should be an absolute path. |
| 215 | + if !filepath.IsAbs(p) { |
| 216 | + t.Errorf("expected absolute path, got %q", p) |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func TestDefaultPath_FallbackToHOME(t *testing.T) { |
| 221 | + // HOME is set in test env by default, so DefaultPath uses it. |
| 222 | + // Verify the path actually lives under HOME. |
| 223 | + p, err := tracking.DefaultPath() |
| 224 | + if err != nil { |
| 225 | + t.Fatal(err) |
| 226 | + } |
| 227 | + home, _ := os.UserHomeDir() |
| 228 | + if !strings.HasPrefix(p, home) { |
| 229 | + t.Errorf("expected path under HOME %q, got %q", home, p) |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +func TestDefaultPath_FallbackToUSERPROFILE(t *testing.T) { |
| 234 | + // When HOME is empty and USERPROFILE is set (Windows-style), |
| 235 | + // DefaultPath should still succeed via the fallback chain. |
| 236 | + origHome := os.Getenv("HOME") |
| 237 | + origUserProfile := os.Getenv("USERPROFILE") |
| 238 | + t.Cleanup(func() { |
| 239 | + _ = os.Setenv("HOME", origHome) |
| 240 | + _ = os.Setenv("USERPROFILE", origUserProfile) |
| 241 | + }) |
| 242 | + _ = os.Unsetenv("HOME") |
| 243 | + profileDir := t.TempDir() |
| 244 | + _ = os.Setenv("USERPROFILE", profileDir) |
| 245 | + |
| 246 | + p, err := tracking.DefaultPath() |
| 247 | + if err != nil { |
| 248 | + t.Fatalf("DefaultPath: %v", err) |
| 249 | + } |
| 250 | + if !strings.HasPrefix(p, profileDir) { |
| 251 | + t.Errorf("expected path under USERPROFILE %q, got %q", profileDir, p) |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +func TestNew_DefaultPath(t *testing.T) { |
| 256 | + // Point HOME at a temp dir so New uses an isolated path. |
| 257 | + origHome := os.Getenv("HOME") |
| 258 | + t.Cleanup(func() { _ = os.Setenv("HOME", origHome) }) |
| 259 | + tmp := t.TempDir() |
| 260 | + _ = os.Setenv("HOME", tmp) |
| 261 | + |
| 262 | + ctx := context.Background() |
| 263 | + tr, err := tracking.New(ctx) |
| 264 | + if err != nil { |
| 265 | + t.Fatalf("New: %v", err) |
| 266 | + } |
| 267 | + t.Cleanup(func() { _ = tr.Close() }) |
| 268 | + |
| 269 | + // Verify the DB file was created. |
| 270 | + expectedDir := filepath.Join(tmp, ".tok") |
| 271 | + if _, err := os.Stat(expectedDir); err != nil { |
| 272 | + t.Errorf("expected dir %s to exist: %v", expectedDir, err) |
| 273 | + } |
| 274 | + |
| 275 | + // And basic operations work. |
| 276 | + if err := tr.Record(ctx, tracking.Event{ |
| 277 | + Command: "new", OriginalBytes: 100, CompressedBytes: 50, |
| 278 | + }); err != nil { |
| 279 | + t.Fatalf("Record: %v", err) |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +func TestNewAt_CreateDirError(t *testing.T) { |
| 284 | + // If mkdirAll fails, NewAt returns an error. |
| 285 | + // Use a path under a file (not a dir) so MkdirAll fails. |
| 286 | + tmp := t.TempDir() |
| 287 | + blocker := filepath.Join(tmp, "blocker") |
| 288 | + if err := os.WriteFile(blocker, []byte("x"), 0o600); err != nil { |
| 289 | + t.Fatal(err) |
| 290 | + } |
| 291 | + bad := filepath.Join(blocker, "tracker.db") |
| 292 | + _, err := tracking.NewAt(context.Background(), bad) |
| 293 | + if err == nil { |
| 294 | + t.Error("expected error for nested dir under a file") |
| 295 | + } |
| 296 | + if !strings.Contains(err.Error(), "create dir") { |
| 297 | + t.Errorf("expected 'create dir' in error, got %v", err) |
| 298 | + } |
| 299 | +} |
| 300 | + |
| 301 | +func TestNewAt_EmptyPath(t *testing.T) { |
| 302 | + _, err := tracking.NewAt(context.Background(), "") |
| 303 | + if err == nil { |
| 304 | + t.Error("expected error for empty path") |
| 305 | + } |
| 306 | +} |
0 commit comments