Files
Docwell/Server/internal/directory/position_test.go
T
tajniak81andClaude Opus 5.5 a96c6ad3ce Add spaces, the page tree and the Docwell design system
Spaces and pages:
- Spaces inside workspaces, open or private, with admin/editor/commenter/viewer
  roles that cascade from the workspace role (rules in internal/access)
- Page tree per space with fractional position keys, move with cycle checks,
  trash that hides whole subtrees, restore and permanent delete
- OpenAPI, TypeScript client, and web app sidebar tree with drag-and-drop,
  page, space, trash and space settings views (EN + PL)

Design system:
- Tokens, type scale, radii and shadows from the design kit in
  @docwell/ui/theme.css; Inter, Source Serif 4 and JetBrains Mono bundled
- Vue ports of Button, TextField, Badge, Callout, Icon, Breadcrumbs,
  SlashMenu and PageTreeItem, plus Logo; favicons and web manifest
- Web app and admin panel restyled on the new tokens, in light and dark

Also: fix the Vite dev server crashing on /config.json, and stop tracking
the local Design/ and docs/ folders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 11:14:31 +01:00

111 lines
2.7 KiB
Go

package directory
import (
"fmt"
"math/rand/v2"
"slices"
"strings"
"testing"
)
func checkBetween(t *testing.T, a, b string) string {
t.Helper()
k := keyBetween(a, b)
if k <= a || (b != "" && k >= b) || strings.HasSuffix(k, "0") {
t.Fatalf("keyBetween(%q, %q) = %q", a, b, k)
}
return k
}
func TestKeyBetween(t *testing.T) {
first := checkBetween(t, "", "")
checkBetween(t, "", first)
checkBetween(t, first, "")
checkBetween(t, "V", "W")
checkBetween(t, "V", "V1")
checkBetween(t, "z", "")
checkBetween(t, "", "01")
checkBetween(t, "Vz", "W")
}
// TestKeyBetweenRandom inserts at random spots and checks the list stays sorted.
func TestKeyBetweenRandom(t *testing.T) {
r := rand.New(rand.NewPCG(1, 2))
keys := []string{}
for range 5000 {
i := r.IntN(len(keys) + 1)
a, b := "", ""
if i > 0 {
a = keys[i-1]
}
if i < len(keys) {
b = keys[i]
}
keys = slices.Insert(keys, i, checkBetween(t, a, b))
}
if !slices.IsSorted(keys) {
t.Fatal("keys are not sorted")
}
}
// TestAppendGrowth: appending is the common case; keys must grow slowly.
func TestAppendGrowth(t *testing.T) {
last := ""
for range 300 {
last = checkBetween(t, last, "")
}
if len(last) > maxKeyLen {
t.Fatalf("300 appends made a %d-character key", len(last))
}
}
func TestEvenKeys(t *testing.T) {
for _, n := range []int{0, 1, 2, 30, 31, 61, 62, 1000, 5000} {
keys := evenKeys(n)
if len(keys) != n || !slices.IsSorted(keys) || len(slices.Compact(slices.Clone(keys))) != n {
t.Fatalf("evenKeys(%d) is not %d distinct sorted keys", n, n)
}
for _, k := range keys {
if strings.HasSuffix(k, "0") {
t.Fatalf("evenKeys(%d) produced %q", n, k)
}
}
if n > 1 {
checkBetween(t, keys[0], keys[1])
}
}
}
func recs(keys ...string) []pageRecord {
out := make([]pageRecord, len(keys))
for i, k := range keys {
out[i] = pageRecord{ID: fmt.Sprint("p", i), Position: k}
}
return out
}
func TestPlacement(t *testing.T) {
sib := recs("A", "M", "Z")
for index, want := range map[int][2]string{0: {"", "A"}, 1: {"A", "M"}, 3: {"Z", ""}, 99: {"Z", ""}, -5: {"", "A"}} {
key, renumber := placement(sib, index)
if key <= want[0] || (want[1] != "" && key >= want[1]) || renumber != nil {
t.Errorf("index %d: key %q, renumber %v", index, key, renumber)
}
}
// Keys at the length limit force a renumber that keeps the order.
long := strings.Repeat("z", maxKeyLen)
sib = recs("V", long)
key, renumber := placement(sib, 2)
if len(renumber) == 0 {
t.Fatal("expected a renumber")
}
order := []string{renumber["p0"], renumber["p1"], key}
if renumber["p0"] == "" {
order[0] = "V"
}
if !slices.IsSorted(order) || len(key) > 2 {
t.Fatalf("renumbered order %v", order)
}
}