Files
mtusi2026lab5/internal/platform/ident/ident_test.go
T

85 lines
2.0 KiB
Go

package ident_test
import (
"bytes"
"regexp"
"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)
}
}
}
func TestNewAliasIs16HexAndRandom(t *testing.T) {
a, b := ident.NewAlias(), ident.NewAlias()
hex16 := regexp.MustCompile(`^[0-9a-f]{16}$`)
if !hex16.MatchString(a) || !hex16.MatchString(b) {
t.Fatalf("aliases %q, %q are not 16 lowercase hex characters", a, b)
}
if a == b {
t.Fatalf("two aliases are equal: %q", a)
}
}