Files
Docwell/Server/internal/ycrdt/ycrdt_test.go
T
tajniak81andClaude Opus 5.5 e927653e4b
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / go (Server) (push) Failing after 8m12s
CI / go (Web-App) (push) Successful in 11m16s
CI / frontend (push) Failing after 7m33s
CI / desktop (ubuntu-latest) (push) Successful in 1h25m49s
CI / mobile (push) Failing after 5m48s
CI / docker (push) Successful in 41m19s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add edgeless mode: pages as a canvas with their text as a live note
Phase D of docs/AFFINE.md (ADR-14).

The canvas lives in the page's Y.Doc next to its text, in
ydoc.getMap("surface"): one Y.Map per element, each field its own entry so
concurrent edits merge. The page's text is a note on the canvas bound to
the same "default" fragment, so typing on the canvas is typing in the page.
Canvas edits have their own undo history through a Y.UndoManager.

- New package @docwell/canvas: viewport with pan, zoom and pinch;
  selection, marquee, snapping, resize and rotate; notes, text, shapes
  (plain or scribbled), connectors (straight, curve, elbow, with arrowheads
  and labels), pen and eraser, frames, groups, pictures, embeds and link
  cards, and mind maps (right, left or both sides; Tab, Enter, arrows).
  A style bar over the selection; the last style per kind is the default.
- Presentation through frames, stickers and templates, PNG and SVG export.
- Page/Edgeless switch in the header, ?mode= in page and share links, a
  per-page default (pages.default_mode) and a per-device default for new
  pages; share links show the canvas read-only.
- Server: yrs writes the canvas into the document JSON as "surface",
  adds its text to search, and writes it back on restores and templates.
- Phone: the bridge draws the canvas read-only (protocol 3) and the page
  screen has a canvas button.

Strings are translated into all five languages and the user guide covers
edgeless mode.

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

210 lines
6.2 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package ycrdt
import (
"context"
"encoding/json"
"reflect"
"strings"
"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)
}
}
// A database node's records live in the "blocks" map (ADR-13): JSON in and
// out carries them in attrs.data, rows in "pos" order, bodies as nodes.
const database = `{"type":"doc","content":[
{"type":"database","attrs":{"id":"db1","data":{
"title":"Tasks",
"columns":[
{"id":"t","name":"Name","type":"title"},
{"id":"s","name":"Status","type":"select","options":[{"id":"o1","name":"Doing","color":"blue"}]}
],
"rows":[
{"id":"r1","createdAt":1,"cells":{"t":"Write the ADR","s":"o1"},
"body":[{"type":"paragraph","content":[{"type":"text","text":"notes here"}]}]},
{"id":"r2","createdAt":2,"cells":{"t":"Ship it"}}
],
"views":[{"id":"v1","name":"Table","type":"table","hidden":[],"filters":[],"filterMode":"and","sorts":[],"groupBy":null,"dateColumn":null,"widths":{}}]
}}},
{"type":"paragraph"}
]}`
func TestDatabaseRoundTrip(t *testing.T) {
ctx := context.Background()
p := newPool(t)
seed, err := p.Replace(ctx, nil, []byte(database))
if err != nil {
t.Fatal(err)
}
r, err := p.Read(ctx, seed)
if err != nil {
t.Fatal(err)
}
// Null view fields are left out, as Yjs leaves out undefined.
want := strings.Replace(database, `"groupBy":null,"dateColumn":null,`, "", 1)
equalJSON(t, r.JSON, want)
if want := "Tasks\nWrite the ADR Doing notes here\nShip it\n\n\n"; r.Text != want {
t.Fatalf("text = %q, want %q", r.Text, want)
}
}
// A page with an edgeless canvas: a shape, a note with a body of its own and
// a mind map, in drawing order (frames first).
const canvas = `{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Page text"}]}],` +
`"surface":{"elements":[` +
`{"id":"f1","type":"frame","index":-1,"x":0,"y":0,"w":400,"h":300,"title":"Plan"},` +
`{"id":"s1","type":"shape","index":1,"x":10,"y":10,"w":100,"h":60,"shape":"ellipse","text":"Idea"},` +
`{"id":"n1","type":"note","index":2,"x":200,"y":10,"w":240,"h":80,"body":[{"type":"paragraph","content":[{"type":"text","text":"Sticky"}]}]},` +
`{"id":"m1","type":"mindmap","index":3,"x":0,"y":400,"nodes":{"r":{"parent":"","pos":0,"text":"Root"}}}]}}`
func TestCanvasRoundTrip(t *testing.T) {
ctx := context.Background()
p := newPool(t)
seed, err := p.Replace(ctx, nil, []byte(canvas))
if err != nil {
t.Fatal(err)
}
r, err := p.Read(ctx, seed)
if err != nil {
t.Fatal(err)
}
equalJSON(t, r.JSON, canvas)
for _, word := range []string{"Page text", "Plan", "Idea", "Sticky", "Root"} {
if !strings.Contains(r.Text, word) {
t.Fatalf("text %q is missing %q", r.Text, word)
}
}
// A document without a surface keeps the canvas; an empty one clears it.
keep, err := p.Replace(ctx, [][]byte{seed}, []byte(`{"type":"doc","content":[]}`))
if err != nil {
t.Fatal(err)
}
merged, err := p.Merge(ctx, [][]byte{seed, keep})
if err != nil {
t.Fatal(err)
}
r, err = p.Read(ctx, merged)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(r.JSON), `"Sticky"`) {
t.Fatalf("canvas lost: %s", r.JSON)
}
clear, err := p.Replace(ctx, [][]byte{merged}, []byte(`{"type":"doc","content":[],"surface":{"elements":[]}}`))
if err != nil {
t.Fatal(err)
}
merged, err = p.Merge(ctx, [][]byte{merged, clear})
if err != nil {
t.Fatal(err)
}
r, err = p.Read(ctx, merged)
if err != nil {
t.Fatal(err)
}
equalJSON(t, r.JSON, `{"type":"doc","content":[],"surface":{"elements":[]}}`)
}