| 1 | package core_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/docs/internal/core" |
| 9 | ) |
| 10 | |
| 11 | func TestBuildWritesTheDocumentItDescribes(t *testing.T) { |
| 12 | ctx := context.Background() |
| 13 | rt, err := core.New(ctx) |
| 14 | if err != nil { |
| 15 | t.Fatal(err) |
| 16 | } |
| 17 | defer rt.Close(ctx) |
| 18 | |
| 19 | ops, err := core.EncodeBuild([]core.BuildBlock{ |
| 20 | {Heading: 1, Runs: []core.BuildRun{{Text: "Title"}}}, |
| 21 | {Runs: []core.BuildRun{{Text: "plain "}, {Marks: []string{"strong"}, Text: "bold"}}}, |
| 22 | {}, |
| 23 | }) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | update, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000001", 41, ops) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | |
| 32 | // Replayed into a second document, it is the same structure. |
| 33 | h, err := rt.NewDoc(ctx, "6f1a1d1e-0000-4000-8000-000000000001", 42) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | defer h.Close(ctx) |
| 38 | if err := h.Apply(ctx, update); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | got, err := h.JSON(ctx) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | want := `{"type":"doc","content":[{"type":"heading","attrs":{"level":1},"content":[{"type":"text","text":"Title"}]},{"type":"paragraph","content":[{"type":"text","text":"plain "},{"type":"text","text":"bold","marks":[{"type":"strong"}]}]},{"type":"paragraph","content":[]}]}` |
| 46 | if got != want { |
| 47 | t.Fatalf("replayed structure\n got %s\nwant %s", got, want) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestBuildRefusesAClientIDABrowserCannotRead(t *testing.T) { |
| 52 | ctx := context.Background() |
| 53 | rt, err := core.New(ctx) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | defer rt.Close(ctx) |
| 58 | ops, err := core.EncodeBuild([]core.BuildBlock{{Runs: []core.BuildRun{{Text: "x"}}}}) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if _, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000005", core.MaxBuildClientID+1, ops); !errors.Is(err, core.ErrBuildClientID) { |
| 63 | t.Errorf("got %v, want ErrBuildClientID", err) |
| 64 | } |
| 65 | if _, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000005", core.MaxBuildClientID, ops); err != nil { |
| 66 | t.Errorf("the largest safe id: %v", err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestBuildRefusesWhatTheBuilderRefuses(t *testing.T) { |
| 71 | ctx := context.Background() |
| 72 | rt, err := core.New(ctx) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | defer rt.Close(ctx) |
| 77 | |
| 78 | for name, blocks := range map[string][]core.BuildBlock{ |
| 79 | "unknown mark": {{Runs: []core.BuildRun{{Marks: []string{"link"}, Text: "x"}}}}, |
| 80 | "level 7": {{Heading: 7, Runs: []core.BuildRun{{Text: "x"}}}}, |
| 81 | "empty run": {{Runs: []core.BuildRun{{Text: ""}}}}, |
| 82 | "control": {{Runs: []core.BuildRun{{Text: "a\tb"}}}}, |
| 83 | } { |
| 84 | ops, err := core.EncodeBuild(blocks) |
| 85 | if err != nil { |
| 86 | t.Fatal(name, err) |
| 87 | } |
| 88 | if _, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000002", 1, ops); !errors.Is(err, core.ErrBuildMalformed) { |
| 89 | t.Errorf("%s: got %v, want ErrBuildMalformed", name, err) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | many := make([]core.BuildBlock, 50_001) |
| 94 | ops, err := core.EncodeBuild(many) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | if _, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000003", 1, ops); !errors.Is(err, core.ErrBuildLimit) { |
| 99 | t.Errorf("too many blocks: got %v, want ErrBuildLimit", err) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // The largest import the reader lets through (internal/docx's |
| 104 | // maxBlocks, maxRuns and maxText) must build inside Build's allowance and |
| 105 | // then replay inside the ordinary call limit, which is what every later |
| 106 | // opening of the document needs. |
| 107 | func TestBuildAtTheReadersLimitFitsTheCallTimeout(t *testing.T) { |
| 108 | ctx := context.Background() |
| 109 | rt, err := core.New(ctx) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | defer rt.Close(ctx) |
| 114 | |
| 115 | // 5,000 blocks of two runs, 52 bytes each: about 508 KiB of text in |
| 116 | // 10,000 runs. |
| 117 | text := make([]byte, 52) |
| 118 | for i := range text { |
| 119 | text[i] = 'a' + byte(i%26) |
| 120 | } |
| 121 | blocks := make([]core.BuildBlock, 5_000) |
| 122 | for i := range blocks { |
| 123 | blocks[i].Runs = []core.BuildRun{ |
| 124 | {Text: string(text)}, |
| 125 | {Marks: []string{"strong", "font_sans", "size_14"}, Text: string(text)}, |
| 126 | } |
| 127 | } |
| 128 | ops, err := core.EncodeBuild(blocks) |
| 129 | if err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | update, err := rt.Build(ctx, "6f1a1d1e-0000-4000-8000-000000000004", 1, ops) |
| 133 | if err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | h, err := rt.NewDoc(ctx, "6f1a1d1e-0000-4000-8000-000000000004", 2) |
| 137 | if err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | defer h.Close(ctx) |
| 141 | if err := h.Apply(ctx, update); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | if _, err := h.HTML(ctx); err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | } |
| 148 | |