oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "archive/zip"
5 "bytes"
6 "context"
7 "encoding/json"
8 "io"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "slices"
13 "strings"
14 "testing"
15
16 "github.com/carlosframework/docs/internal/core"
17 "github.com/carlosframework/docs/internal/docx"
18 "github.com/carlosframework/docs/internal/export"
19)
20
21// interopDocx is a SYNTHESIZED .docx (not a real Google export) with
22// every block and mark an import can write: six heading levels, empty
23// paragraphs, faces, ladder sizes, bold, italic, text from a link, a
24// table cell and a suggestion, and characters past the BMP.
25func interopDocx(t *testing.T) []byte {
26 t.Helper()
27 const ns = `xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"`
28 var body strings.Builder
29 for level := 1; level <= 6; level++ {
30 body.WriteString(`<w:p><w:pPr><w:pStyle w:val="Heading` + string(rune('0'+level)) + `"/></w:pPr><w:r><w:t>Level</w:t></w:r></w:p>`)
31 }
32 body.WriteString(`<w:p/>`)
33 body.WriteString(`<w:p><w:r><w:t xml:space="preserve">plain </w:t></w:r>` +
34 `<w:r><w:rPr><w:rFonts w:ascii="Cambria"/><w:b/><w:sz w:val="28"/></w:rPr><w:t>Caladea 14 bold</w:t></w:r>` +
35 `<w:r><w:rPr><w:rFonts w:ascii="Courier New"/><w:i/><w:sz w:val="16"/></w:rPr><w:t xml:space="preserve"> mono 8 italic</w:t></w:r>` +
36 `<w:hyperlink r:id="rId1"><w:r><w:t xml:space="preserve"> linked 🦀 café</w:t></w:r></w:hyperlink></w:p>`)
37 body.WriteString(`<w:tbl><w:tr><w:tc><w:p><w:r><w:rPr><w:sz w:val="144"/></w:rPr><w:t>cell at 72</w:t></w:r></w:p></w:tc></w:tr></w:tbl>`)
38 body.WriteString(`<w:p><w:ins w:id="1" w:author="x"><w:r><w:t>suggested</w:t></w:r></w:ins></w:p>`)
39 var buf bytes.Buffer
40 z := zip.NewWriter(&buf)
41 for name, part := range map[string]string{
42 "word/styles.xml": `<w:styles ` + ns + `><w:style w:type="paragraph" w:styleId="Heading1"><w:name w:val="heading 1"/></w:style></w:styles>`,
43 "word/document.xml": `<w:document ` + ns + `><w:body>` + body.String() + `</w:body></w:document>`,
44 } {
45 w, err := z.Create(name)
46 if err != nil {
47 t.Fatal(err)
48 }
49 io.WriteString(w, part)
50 }
51 if err := z.Close(); err != nil {
52 t.Fatal(err)
53 }
54 return buf.Bytes()
55}
56
57// The editor must be able to open what an import writes, as it is, with
58// the client ids the converter actually draws: fixed small ids in a
59// fixture cannot show that an id past 2^53 is unreadable in a browser,
60// which is exactly how the first version of this path broke. So this
61// runs the real convertImport, many times, and hands each update to
62// Yjs and y-prosemirror under the editor's own schema
63// (fixtures/import-interop.mjs).
64func TestImportedUpdatesOpenInTheEditorUnchanged(t *testing.T) {
65 node, err := exec.LookPath("node")
66 modules := filepath.Join("..", "..", "fixtures", "node_modules", "y-prosemirror")
67 if _, statErr := os.Stat(modules); err != nil || statErr != nil {
68 if os.Getenv("DOCS_REQUIRE_WASM") == "1" {
69 t.Fatal("the gate needs node and fixtures/node_modules for this check")
70 }
71 t.Skip("needs node and `npm ci` in fixtures/")
72 }
73
74 ctx := context.Background()
75 rt, err := core.New(ctx)
76 if err != nil {
77 t.Fatal(err)
78 }
79 defer rt.Close(ctx)
80
81 data := interopDocx(t)
82 read, err := docx.Import(data)
83 if err != nil {
84 t.Fatal(err)
85 }
86 want := canonicalBlocks(buildBlocks(read.Document))
87
88 for i := 0; i < 12; i++ {
89 imp, err := convertImport(ctx, rt, data)
90 if err != nil {
91 t.Fatal(err)
92 }
93 if imp.builder > core.MaxBuildClientID || imp.builder == imp.clientID {
94 t.Fatalf("builder id %d (row id %d)", imp.builder, imp.clientID)
95 }
96 cmd := exec.Command(node, "import-interop.mjs")
97 cmd.Dir = filepath.Join("..", "..", "fixtures")
98 cmd.Stdin = bytes.NewReader(imp.update)
99 var stderr bytes.Buffer
100 cmd.Stderr = &stderr
101 out, err := cmd.Output()
102 if err != nil {
103 t.Fatalf("import %d: %v: %s", i, err, stderr.String())
104 }
105 var got struct {
106 Clients []float64 `json:"clients"`
107 Doc json.RawMessage `json:"doc"`
108 }
109 if err := json.Unmarshal(out, &got); err != nil {
110 t.Fatal(err)
111 }
112 if len(got.Clients) != 1 || uint64(got.Clients[0]) != imp.builder {
113 t.Fatalf("import %d: clients %v, want only the builder's %d", i, got.Clients, imp.builder)
114 }
115 pm, err := export.Parse("", string(got.Doc))
116 if err != nil {
117 t.Fatal(err)
118 }
119 if g := canonicalBlocks(buildBlocks(pm)); !slices.Equal(g, want) {
120 t.Fatalf("import %d: the editor reads\n%q\nwant\n%q", i, g, want)
121 }
122 }
123}
124