| 1 | //go:build browser |
| 2 | |
| 3 | package docs_test |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/carlosframework/docs/internal/docs" |
| 12 | ) |
| 13 | |
| 14 | // A paste that waited for its font, made after a save was refused, is |
| 15 | // kept across the resync in Chromium and WebKit — Claude Fable's eleven |
| 16 | // cases on #88, through the real editor (fixtures/engines/replay.mjs). |
| 17 | // Anchored to the refused text, its Yjs updates cannot be carried; its |
| 18 | // ProseMirror transaction is replayed onto the rebuilt document instead |
| 19 | // (fixtures/carry.mjs). Ten keep it — on the sheet, once, never leaving it |
| 20 | // once there — and on the server, once, in the same words; the paste into |
| 21 | // a paragraph the refused Enter made cannot be replayed, and is left out |
| 22 | // cleanly: nothing posted, the save line saying failed. |
| 23 | func TestFontEnginesAWaitingPasteSurvivesARefusedSave(t *testing.T) { |
| 24 | out, app, err := runEnginesApp(t, "replay.mjs") |
| 25 | if err != nil { |
| 26 | t.Fatalf("a paste that waited for its font was not kept across the resync: %v\n%s", err, out) |
| 27 | } |
| 28 | n := 0 |
| 29 | for _, line := range strings.Split(out, "\n") { |
| 30 | rest, ok := strings.CutPrefix(line, "RESULT ") |
| 31 | if !ok { |
| 32 | continue |
| 33 | } |
| 34 | var r struct { |
| 35 | Label, Deck string |
| 36 | Want []string |
| 37 | } |
| 38 | if err := json.Unmarshal([]byte(rest), &r); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | n++ |
| 42 | got := serverParagraphs(t, app, r.Deck) |
| 43 | if strings.Join(got, "\n") != strings.Join(r.Want, "\n") { |
| 44 | t.Errorf("%s: the server holds %q, want %q", r.Label, got, r.Want) |
| 45 | } |
| 46 | } |
| 47 | if n != 22 { |
| 48 | t.Errorf("%d cases reported, want 22 (eleven in each engine)", n) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // serverParagraphs is the document's top-level blocks as the server holds |
| 53 | // them, each the words in it. |
| 54 | func serverParagraphs(t *testing.T, app *docs.App, sqid string) []string { |
| 55 | t.Helper() |
| 56 | doc, err := app.Store().GetFor(context.Background(), app.TestActorForPerson(t, app.TestPerson(t, "engines@example.com")), sqid) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | structure, err := app.Store().JSON(context.Background(), doc) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | type node struct { |
| 65 | Text string `json:"text"` |
| 66 | Content []node `json:"content"` |
| 67 | } |
| 68 | var root node |
| 69 | if err := json.Unmarshal([]byte(structure), &root); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | var words func(n node) string |
| 73 | words = func(n node) string { |
| 74 | s := n.Text |
| 75 | for _, c := range n.Content { |
| 76 | s += words(c) |
| 77 | } |
| 78 | return s |
| 79 | } |
| 80 | var out []string |
| 81 | for _, c := range root.Content { |
| 82 | out = append(out, words(c)) |
| 83 | } |
| 84 | return out |
| 85 | } |
| 86 | |