mitbringsl/backend/internal/sync/hlc_test.go
Tronax 895725b5e5
Backend Phase C: Sync-Kern + Caddy behind-proxy
Sync-Kern:
- internal/sync/hlc.go: Hybrid Logical Clock (wall_ms<<16|counter)
  Tick/Now/After, global mutex, strikt monoton + kausal korrekt
- internal/sync/hlc_test.go: Unit-Tests (monoton, kausal, keine Duplikate)

Store-Schicht:
- internal/store/opstore.go: AppendOps idempotent via UNIQUE(client_id,
  client_seq) ON CONFLICT DO NOTHING; LWW-Projektion (list_create/
  rename/delete, item_add/update/remove) in derselben Transaktion;
  PullOps mit Cursor (seq > since, 500er Pages)
- internal/store/liststore.go: CreateList / GetLists / GetList
- internal/store/itemstore.go: GetItems (nicht-gelöschte Items)
- internal/store/suggeststore.go: Search (pg_trgm + LIKE-fallback, 10)

HTTP-Handler:
- internal/httpapi/lists.go: GET/POST /api/lists, GET /api/lists/{id}
- internal/httpapi/ops.go: POST /api/lists/{id}/ops (Push),
  GET /api/lists/{id}/ops (Pull ?since=)
- internal/httpapi/suggest.go: GET /api/suggestions?q=
- internal/httpapi/api.go: alle Routen verdrahtet (RequireAuth)

Deployment:
- deploy/Caddyfile.behind-proxy: auto_https off, trusted_proxies
- deploy/Caddyfile: X-Forwarded-Proto hinzugefügt, Kommentar aktualisiert
- deploy/docker-compose.yml: CADDY_HTTP_PORT + CADDY_HTTPS_PORT
- deploy/.env.example: Caddy-Port-Variablen dokumentiert

go build ./... && go vet ./... && go test ./... 
HLC-Tests: monoton, kausal, keine Duplikate 
AGENTS.md: Phase C vollständig 
2026-08-05 19:56:05 +02:00

61 lines
1.1 KiB
Go

package sync
import (
"testing"
)
func TestTick_StrictlyMonotonic(t *testing.T) {
// Reset global state.
clock.mu.Lock()
clock.last = 0
clock.mu.Unlock()
prev := int64(0)
for i := 0; i < 1000; i++ {
v := Tick(0)
if v <= prev {
t.Fatalf("iteration %d: Tick() = %d, want > %d", i, v, prev)
}
prev = v
}
}
func TestTick_CausalOrdering(t *testing.T) {
clock.mu.Lock()
clock.last = 0
clock.mu.Unlock()
// Simulate a client HLC far in the future.
future := int64(9_999_999_999) << counterBits
v := Tick(future)
if !After(v, future) {
t.Fatalf("Tick(future) = %d, want > %d", v, future)
}
}
func TestTick_NoDuplicates(t *testing.T) {
clock.mu.Lock()
clock.last = 0
clock.mu.Unlock()
seen := make(map[int64]bool)
for i := 0; i < 10_000; i++ {
v := Tick(0)
if seen[v] {
t.Fatalf("duplicate HLC value %d at iteration %d", v, i)
}
seen[v] = true
}
}
func TestAfter(t *testing.T) {
if !After(2, 1) {
t.Fatal("After(2,1) must be true")
}
if After(1, 2) {
t.Fatal("After(1,2) must be false")
}
if After(1, 1) {
t.Fatal("After(1,1) must be false")
}
}