| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // testdata/editor.json is gates L1 and L3's Go half (spec §7), written by |
| 13 | // editor/fidelity.test.mjs (make editor-golden). L3 saves a body after |
| 14 | // each run of seeded edits, with the blocks the editor's document held: |
| 15 | // Go's Parse of that body must give the same blocks. L1 compares the |
| 16 | // editor's output with cleanBody's rule; clean is that rule's digest |
| 17 | // over the corpus as JavaScript computed it. |
| 18 | |
| 19 | type editorGolden struct { |
| 20 | Clean string `json:"clean"` |
| 21 | Bodies []struct { |
| 22 | Name string `json:"name"` |
| 23 | Body string `json:"body"` |
| 24 | Blocks []string `json:"blocks"` |
| 25 | } `json:"bodies"` |
| 26 | } |
| 27 | |
| 28 | func TestEditorBodies(t *testing.T) { |
| 29 | var golden editorGolden |
| 30 | readJSON(t, "testdata/editor.json", &golden) |
| 31 | if len(golden.Bodies) == 0 { |
| 32 | t.Fatal("testdata/editor.json holds no bodies") |
| 33 | } |
| 34 | for _, saved := range golden.Bodies { |
| 35 | got := editorShapes(Parse(saved.Body)) |
| 36 | if !slices.Equal(got, saved.Blocks) { |
| 37 | t.Errorf("%s: the editor saved %q for blocks %v; Go parses %v", |
| 38 | saved.Name, saved.Body, saved.Blocks, got) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // TestEditorCleanBody holds the editor's cleanBody (markup.mjs) to the |
| 44 | // stored body rule, strings.TrimRight(Normalise(body), " \t\n"), which is |
| 45 | // internal/mapa's cleanBody. |
| 46 | func TestEditorCleanBody(t *testing.T) { |
| 47 | var golden editorGolden |
| 48 | readJSON(t, "testdata/editor.json", &golden) |
| 49 | sum := sha256.New() |
| 50 | for _, body := range loadCorpus(t) { |
| 51 | stored := strings.TrimRight(Normalise(body.Body), " \t\n") |
| 52 | sum.Write([]byte(stored + "\x00")) |
| 53 | } |
| 54 | if got := hex.EncodeToString(sum.Sum(nil)); got != golden.Clean { |
| 55 | t.Errorf("cleanBody digest: Go %s, the editor %s (make editor-golden?)", |
| 56 | got, golden.Clean) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // editorShapes is each block as serialize.mjs's structure check describes |
| 61 | // it: its kind, a heading's level, and an item's list kind, depth and |
| 62 | // whether it opens a list, nesting as the renderer and the editor's |
| 63 | // fromBlocks nest it (depth is min(level, lists open); a change of kind |
| 64 | // opens a new list). |
| 65 | func editorShapes(blocks []Block) []string { |
| 66 | out := []string{} |
| 67 | var open []bool |
| 68 | for _, block := range blocks { |
| 69 | switch block.Kind { |
| 70 | case Heading: |
| 71 | level := min(max(block.Level, 1), 6) |
| 72 | out = append(out, fmt.Sprintf("heading:%d", level)) |
| 73 | case Item: |
| 74 | depth := min(block.Level, len(open)) |
| 75 | open = open[:min(len(open), depth+1)] |
| 76 | if len(open) == depth+1 && open[depth] != block.Ordered { |
| 77 | open = open[:depth] |
| 78 | } |
| 79 | start := len(open) != depth+1 |
| 80 | if start { |
| 81 | open = append(open, block.Ordered) |
| 82 | } |
| 83 | list := "bullet_list" |
| 84 | if block.Ordered { |
| 85 | list = "ordered_list" |
| 86 | } |
| 87 | out = append(out, fmt.Sprintf("item:%s:%d:%t", list, depth, start)) |
| 88 | continue |
| 89 | default: |
| 90 | out = append(out, block.Kind) |
| 91 | } |
| 92 | open = nil |
| 93 | } |
| 94 | return out |
| 95 | } |
| 96 | |