Files
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

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

113 lines
2.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
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)
}
}