Files
Docwell/Server/internal/ycrdt/ycrdt_test.go
T
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

114 lines
2.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ycrdt
import (
"context"
"encoding/json"
"reflect"
"testing"
)
func newPool(t *testing.T) *Pool {
t.Helper()
ctx := context.Background()
rt, err := New(ctx, "")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { rt.Close(ctx) })
return NewPool(rt, 2)
}
// Written the way y-prosemirror reads it back: marks always carry attrs.
const first = `{"type":"doc","content":[
{"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Plan"}]},
{"type":"paragraph","content":[
{"type":"text","text":"Ship "},
{"type":"text","marks":[{"type":"bold","attrs":{}}],"text":"collab"},
{"type":"text","text":" with "},
{"type":"text","marks":[{"type":"link","attrs":{"href":"https://example.com"}}],"text":"cursors 🎉"}
]},
{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","content":[{"type":"text","text":"one"}]}]}]},
{"type":"paragraph"}
]}`
const second = `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Restored"}]}]}`
func equalJSON(t *testing.T, got []byte, want string) {
t.Helper()
var g, w any
if err := json.Unmarshal(got, &g); err != nil {
t.Fatalf("bad json %s: %v", got, err)
}
json.Unmarshal([]byte(want), &w)
if !reflect.DeepEqual(g, w) {
t.Fatalf("got %s\nwant %s", got, want)
}
}
func TestReplaceRoundTrip(t *testing.T) {
ctx := context.Background()
p := newPool(t)
seed, err := p.Replace(ctx, nil, []byte(first))
if err != nil {
t.Fatal(err)
}
r, err := p.Read(ctx, seed)
if err != nil {
t.Fatal(err)
}
equalJSON(t, r.JSON, first)
// Marks must not leak into the search text (yrs's get_string adds <bold> tags).
if want := "Plan\nShip collab with cursors 🎉\none\n\n"; r.Text != want {
t.Fatalf("text = %q, want %q", r.Text, want)
}
// Replacing on top of existing state yields an incremental update.
change, err := p.Replace(ctx, [][]byte{seed}, []byte(second))
if err != nil {
t.Fatal(err)
}
snap, err := p.Compact(ctx, [][]byte{seed, change})
if err != nil {
t.Fatal(err)
}
if r, err = p.Read(ctx, snap); err != nil {
t.Fatal(err)
}
equalJSON(t, r.JSON, second)
if r.Text != "Restored\n" {
t.Fatalf("text = %q", r.Text)
}
// A peer that only has the seed catches up through Diff.
sv, err := p.StateVector(ctx, seed)
if err != nil {
t.Fatal(err)
}
missing, err := p.Diff(ctx, snap, sv)
if err != nil {
t.Fatal(err)
}
caughtUp, err := p.Compact(ctx, [][]byte{seed, missing})
if err != nil {
t.Fatal(err)
}
if r, _ = p.Read(ctx, caughtUp); string(r.JSON) == "" {
t.Fatal("empty")
}
equalJSON(t, r.JSON, second)
}
func TestReplaceRejectsNonDoc(t *testing.T) {
p := newPool(t)
if _, err := p.Replace(context.Background(), nil, []byte(`{"type":"paragraph"}`)); err == nil {
t.Fatal("expected an error")
}
// The pool replaced the failed instance and still works.
if _, err := p.Replace(context.Background(), nil, []byte(second)); err != nil {
t.Fatal(err)
}
}