|
1 | 1 | package id |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
5 | 6 | "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + ulid "github.com/oklog/ulid/v2" |
6 | 10 | ) |
7 | 11 |
|
8 | 12 | // A representative ULID (Crockford base32, 26 chars) used across tests. |
9 | 13 | const sampleULID = "01KVBJCWYA4YW6J5M9GP655HZN" |
10 | 14 |
|
| 15 | +func TestCheckpointID_Time(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + // A ULID minted from a known instant recovers that instant (millisecond |
| 19 | + // precision), so remote-ref discovery can sort/display a checkpoint by its |
| 20 | + // real creation time from the ref name alone — no store read. |
| 21 | + want := time.UnixMilli(1700000000000).UTC() |
| 22 | + // Deterministic entropy (zeros); Time() only reads the timestamp prefix. |
| 23 | + minted := ulid.MustNew(ulid.Timestamp(want), bytes.NewReader(make([]byte, 16))) |
| 24 | + got, ok := CheckpointID(minted.String()).Time() |
| 25 | + if !ok { |
| 26 | + t.Fatalf("Time() ok = false for a valid ULID %q", minted) |
| 27 | + } |
| 28 | + if !got.Equal(want) { |
| 29 | + t.Errorf("Time() = %v, want %v", got, want) |
| 30 | + } |
| 31 | + |
| 32 | + // The canonical sample ULID also yields a non-zero time. |
| 33 | + if ts, ok := CheckpointID(sampleULID).Time(); !ok || ts.IsZero() { |
| 34 | + t.Errorf("Time() for sample ULID = (%v, %v), want a non-zero time", ts, ok) |
| 35 | + } |
| 36 | + |
| 37 | + // A legacy hex ID carries no timestamp. |
| 38 | + if _, ok := CheckpointID("a1b2c3d4e5f6").Time(); ok { |
| 39 | + t.Errorf("Time() ok = true for a legacy hex ID; want false") |
| 40 | + } |
| 41 | + |
| 42 | + // Non-ID / empty strings report no time. |
| 43 | + if _, ok := CheckpointID("").Time(); ok { |
| 44 | + t.Errorf("Time() ok = true for empty ID; want false") |
| 45 | + } |
| 46 | + if _, ok := CheckpointID("not-an-id").Time(); ok { |
| 47 | + t.Errorf("Time() ok = true for a non-ID string; want false") |
| 48 | + } |
| 49 | +} |
| 50 | + |
11 | 51 | func TestGenerateULID(t *testing.T) { |
12 | 52 | t.Parallel() |
13 | 53 | a, err := GenerateULID() |
|
0 commit comments