73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package ident_test
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
"time"
|
|
|
|
"messenger/internal/platform/ident"
|
|
)
|
|
|
|
func TestNewProducesVersion7(t *testing.T) {
|
|
id := ident.New()
|
|
if got := id.Version(); got != 7 {
|
|
t.Fatalf("version = %d, want 7", got)
|
|
}
|
|
}
|
|
|
|
func TestNewIsStrictlyIncreasingWithinProcess(t *testing.T) {
|
|
prev := ident.New()
|
|
for i := 0; i < 20000; i++ {
|
|
next := ident.New()
|
|
if bytes.Compare(next[:], prev[:]) <= 0 {
|
|
t.Fatalf("id %s is not greater than previous %s at iteration %d", next, prev, i)
|
|
}
|
|
prev = next
|
|
}
|
|
}
|
|
|
|
func TestTimeOfMatchesWallClockAtMillisecondPrecision(t *testing.T) {
|
|
before := time.Now().Truncate(time.Millisecond)
|
|
id := ident.New()
|
|
after := time.Now().Add(time.Millisecond)
|
|
|
|
got := ident.TimeOf(id)
|
|
if got.Location() != time.UTC {
|
|
t.Fatalf("TimeOf must return UTC, got %v", got.Location())
|
|
}
|
|
if got.Before(before) || got.After(after) {
|
|
t.Fatalf("TimeOf = %v, want within [%v, %v]", got, before, after)
|
|
}
|
|
if got.Nanosecond()%int(time.Millisecond) != 0 {
|
|
t.Fatalf("TimeOf must be truncated to milliseconds, got %v", got)
|
|
}
|
|
}
|
|
|
|
func TestParseAcceptsCanonicalForm(t *testing.T) {
|
|
id := ident.New()
|
|
parsed, err := ident.Parse(id.String())
|
|
if err != nil {
|
|
t.Fatalf("Parse(%q) error: %v", id, err)
|
|
}
|
|
if parsed != id {
|
|
t.Fatalf("Parse round trip mismatch: %s != %s", parsed, id)
|
|
}
|
|
}
|
|
|
|
func TestParseRejectsNonCanonicalForms(t *testing.T) {
|
|
id := ident.New().String()
|
|
for _, raw := range []string{
|
|
"",
|
|
"not-a-uuid",
|
|
"{" + id + "}",
|
|
"urn:uuid:" + id,
|
|
id[:32] + id[33:], // hex without one dash
|
|
id + "0",
|
|
" " + id,
|
|
} {
|
|
if _, err := ident.Parse(raw); err == nil {
|
|
t.Errorf("Parse(%q) must fail", raw)
|
|
}
|
|
}
|
|
}
|