|
4 | 4 | "os" |
5 | 5 | "path/filepath" |
6 | 6 | "testing" |
| 7 | + "time" |
7 | 8 |
|
8 | 9 | "github.com/stretchr/testify/assert" |
9 | 10 | "github.com/stretchr/testify/require" |
@@ -315,3 +316,79 @@ func TestExtractCategory(t *testing.T) { |
315 | 316 | assert.Empty(t, data.SessionStorage) |
316 | 317 | }) |
317 | 318 | } |
| 319 | + |
| 320 | +// Anchor: 2024-01-15T10:30:00Z. |
| 321 | +const anchorUnixSeconds = int64(1705314600) |
| 322 | + |
| 323 | +func TestFirefoxMicros_AnchorDate(t *testing.T) { |
| 324 | + got := firefoxMicros(anchorUnixSeconds * 1_000_000) |
| 325 | + want := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC) |
| 326 | + assert.Equal(t, want, got) |
| 327 | +} |
| 328 | + |
| 329 | +func TestFirefoxMicros_PrecisionPreserved(t *testing.T) { |
| 330 | + got := firefoxMicros(anchorUnixSeconds*1_000_000 + 123456) |
| 331 | + assert.Equal(t, 123456*int64(time.Microsecond), int64(got.Nanosecond())) |
| 332 | +} |
| 333 | + |
| 334 | +func TestFirefoxMillis_AnchorDate(t *testing.T) { |
| 335 | + got := firefoxMillis(anchorUnixSeconds * 1_000) |
| 336 | + want := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC) |
| 337 | + assert.Equal(t, want, got) |
| 338 | +} |
| 339 | + |
| 340 | +func TestFirefoxMillis_PrecisionPreserved(t *testing.T) { |
| 341 | + got := firefoxMillis(anchorUnixSeconds*1_000 + 789) |
| 342 | + assert.Equal(t, 789*int64(time.Millisecond), int64(got.Nanosecond())) |
| 343 | +} |
| 344 | + |
| 345 | +func TestFirefoxSeconds_AnchorDate(t *testing.T) { |
| 346 | + got := firefoxSeconds(anchorUnixSeconds) |
| 347 | + want := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC) |
| 348 | + assert.Equal(t, want, got) |
| 349 | +} |
| 350 | + |
| 351 | +func TestFirefoxHelpers_ZeroReturnsZeroTime(t *testing.T) { |
| 352 | + assert.True(t, firefoxMicros(0).IsZero(), "micros") |
| 353 | + assert.True(t, firefoxMillis(0).IsZero(), "millis") |
| 354 | + assert.True(t, firefoxSeconds(0).IsZero(), "seconds") |
| 355 | +} |
| 356 | + |
| 357 | +func TestFirefoxHelpers_NegativeReturnsZeroTime(t *testing.T) { |
| 358 | + assert.True(t, firefoxMicros(-1).IsZero(), "micros") |
| 359 | + assert.True(t, firefoxMillis(-1).IsZero(), "millis") |
| 360 | + assert.True(t, firefoxSeconds(-1).IsZero(), "seconds") |
| 361 | +} |
| 362 | + |
| 363 | +func TestFirefoxHelpers_AlwaysUTC(t *testing.T) { |
| 364 | + // assert.Same: pointer equality reliably catches any helper that |
| 365 | + // leaks time.Local, independent of the runner's configured TZ. |
| 366 | + assert.Same(t, time.UTC, firefoxMicros(anchorUnixSeconds*1_000_000).Location()) |
| 367 | + assert.Same(t, time.UTC, firefoxMillis(anchorUnixSeconds*1_000).Location()) |
| 368 | + assert.Same(t, time.UTC, firefoxSeconds(anchorUnixSeconds).Location()) |
| 369 | +} |
| 370 | + |
| 371 | +func TestFirefoxHelpers_SameMomentAcrossUnits(t *testing.T) { |
| 372 | + us := firefoxMicros(anchorUnixSeconds * 1_000_000) |
| 373 | + ms := firefoxMillis(anchorUnixSeconds * 1_000) |
| 374 | + s := firefoxSeconds(anchorUnixSeconds) |
| 375 | + assert.True(t, us.Equal(ms)) |
| 376 | + assert.True(t, ms.Equal(s)) |
| 377 | +} |
| 378 | + |
| 379 | +func TestFirefoxHelpers_OutOfJSONRangeReturnsZero(t *testing.T) { |
| 380 | + for _, tc := range []struct { |
| 381 | + name string |
| 382 | + got time.Time |
| 383 | + }{ |
| 384 | + {"seconds", firefoxSeconds(1 << 50)}, |
| 385 | + {"millis", firefoxMillis(1 << 60)}, |
| 386 | + {"micros", firefoxMicros(1 << 62)}, |
| 387 | + } { |
| 388 | + t.Run(tc.name, func(t *testing.T) { |
| 389 | + b, err := tc.got.MarshalJSON() |
| 390 | + require.NoError(t, err) |
| 391 | + assert.JSONEq(t, `"0001-01-01T00:00:00Z"`, string(b)) |
| 392 | + }) |
| 393 | + } |
| 394 | +} |
0 commit comments