Files
mtusi2026lab7/internal/platform/ident/ident_test.go
T
kudesn1k 8054913ef9
Application / e2e (pull_request) Blocked by required conditions
Application / unit (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
2026-11-30 10:54:41 +03:00

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)
}
}