| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "net/http" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // replayDeck is the replay cases' deck: a heading, and a text block of two |
| 13 | // paragraphs with nothing in code, so the code face is not on the page. |
| 14 | const replayDeck = `[{"op":"set_meta","theme":"paper","title":"Replay"}, |
| 15 | {"op":"add_slide","id":"slideone0001","layout":"flow","template":"title-body"}, |
| 16 | {"op":"add_block","slide":"slideone0001","id":"head00000001","type":"heading","region":"heading","props":{"level":1},"markdown":"Replay"}, |
| 17 | {"op":"add_block","slide":"slideone0001","id":"text00000001","type":"text","region":"body","markdown":"First paragraph.\n\nSecond paragraph."}]` |
| 18 | |
| 19 | // A paste that waited for its font, made after a save was refused, is |
| 20 | // kept across the resync in Chromium and WebKit — Claude Fable's eleven |
| 21 | // cases on #27 and #88, through the real editor (fixtures/engines/ |
| 22 | // replay.mjs). Anchored to the refused text, its Yjs updates cannot be |
| 23 | // carried; its ProseMirror transaction is replayed onto the rebuilt text |
| 24 | // instead (fixtures/carry.mjs). Ten keep it — on the page, once, never |
| 25 | // leaving it once there — and on the server, once, in the same words; the |
| 26 | // paste into a paragraph the refused Enter made cannot be replayed, and is |
| 27 | // left out cleanly: nothing posted, the save line saying failed. |
| 28 | func TestFontEnginesAWaitingPasteSurvivesARefusedSave(t *testing.T) { |
| 29 | out, fr, err := runEnginesRig(t, "replay.mjs") |
| 30 | if err != nil { |
| 31 | t.Fatalf("a paste that waited for its font was not kept across the resync: %v\n%s", err, out) |
| 32 | } |
| 33 | n := 0 |
| 34 | for _, line := range strings.Split(out, "\n") { |
| 35 | rest, ok := strings.CutPrefix(line, "RESULT ") |
| 36 | if !ok { |
| 37 | continue |
| 38 | } |
| 39 | var r struct { |
| 40 | Label, Deck string |
| 41 | Want []string |
| 42 | } |
| 43 | if err := json.Unmarshal([]byte(rest), &r); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | n++ |
| 47 | got := serverParagraphs(t, fr, r.Deck) |
| 48 | if strings.Join(got, "\n") != strings.Join(r.Want, "\n") { |
| 49 | t.Errorf("%s: the server holds %q, want %q", r.Label, got, r.Want) |
| 50 | } |
| 51 | } |
| 52 | if n != 22 { |
| 53 | t.Errorf("%d cases reported, want 22 (eleven in each engine)", n) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // serverParagraphs is the text block's paragraphs as the server holds |
| 58 | // them, each the words of its runs. |
| 59 | func serverParagraphs(t *testing.T, fr *fontRig, sqid string) []string { |
| 60 | t.Helper() |
| 61 | view := apiJSON(t, fr.app, http.MethodGet, "/api/v1/decks/"+sqid, fr.token, "", http.StatusOK) |
| 62 | b, _ := json.Marshal(view) |
| 63 | var d struct { |
| 64 | Slides []struct { |
| 65 | Blocks []struct { |
| 66 | ID string `json:"id"` |
| 67 | Text [][]struct { |
| 68 | T string `json:"t"` |
| 69 | } `json:"text"` |
| 70 | } `json:"blocks"` |
| 71 | } `json:"slides"` |
| 72 | } |
| 73 | if err := json.Unmarshal(b, &d); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | for _, s := range d.Slides { |
| 77 | for _, bl := range s.Blocks { |
| 78 | if bl.ID != "text00000001" { |
| 79 | continue |
| 80 | } |
| 81 | var out []string |
| 82 | for _, p := range bl.Text { |
| 83 | var w strings.Builder |
| 84 | for _, r := range p { |
| 85 | w.WriteString(r.T) |
| 86 | } |
| 87 | out = append(out, w.String()) |
| 88 | } |
| 89 | return out |
| 90 | } |
| 91 | } |
| 92 | t.Fatalf("no text block in %s", b) |
| 93 | return nil |
| 94 | } |
| 95 | |