| 1 | package core_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "math" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/docs/internal/core" |
| 11 | ) |
| 12 | |
| 13 | func TestInitialUpdateUsesRealCollaborativeCore(t *testing.T) { |
| 14 | ctx := context.Background() |
| 15 | rt, err := core.New(ctx) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | defer rt.Close(ctx) |
| 20 | update, err := rt.InitialUpdate(ctx, "fresh-initial", math.MaxUint32, []core.InitialBlock{{Level: 2, Text: "Agenda 😀"}, {Text: "<script>& Café\n二"}}) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | h, err := rt.NewDoc(ctx, "fresh-initial", 42) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | defer h.Close(ctx) |
| 29 | if err = h.Apply(ctx, update); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | html, err := h.HTML(ctx) |
| 33 | if err != nil || !strings.Contains(html, "<h2>Agenda 😀</h2>") || !strings.Contains(html, "<script>&") { |
| 34 | t.Fatal(html, err) |
| 35 | } |
| 36 | raw, err := h.JSON(ctx) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | var tree struct { |
| 41 | Content []struct { |
| 42 | Type string |
| 43 | Attrs struct{ Level int } |
| 44 | Content []struct{ Text string } |
| 45 | } |
| 46 | } |
| 47 | if err = json.Unmarshal([]byte(raw), &tree); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if len(tree.Content) != 2 || tree.Content[0].Attrs.Level != 2 || tree.Content[1].Content[0].Text != "<script>& Café\n二" { |
| 51 | t.Fatal(raw) |
| 52 | } |
| 53 | for _, clientID := range []uint64{uint64(math.MaxUint32) + 1, math.MaxInt64} { |
| 54 | if _, err = rt.InitialUpdate(ctx, "bad-client", clientID, []core.InitialBlock{{Text: "safe text"}}); err == nil { |
| 55 | t.Fatal("unsafe client ID accepted") |
| 56 | } |
| 57 | } |
| 58 | for _, blocks := range [][]core.InitialBlock{{{Level: 7}}, {{Text: string([]byte{255})}}, {{Text: strings.Repeat("x", 128*1024+1)}}, make([]core.InitialBlock, 201)} { |
| 59 | if _, err = rt.InitialUpdate(ctx, "bad-initial", 43, blocks); err == nil { |
| 60 | t.Fatal("invalid blocks accepted") |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |