oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chatmarkdown
2
3import (
4 "html"
5 "regexp"
6 "strings"
7 "testing"
8 "unicode/utf8"
9)
10
11// corpus is coordinator replies as they are written: the recorded ones
12// (a bold opening line, a bold-led bullet list and a numbered list run
13// together while streaming) and the rest of the subset the chat renders.
14var corpus = []string{
15 // fakeclaude's markdownSample, which the browser test streams.
16 "**Yes. This is the right shape for it.** The invoice total is counted twice when delivery is added.\n\nTwo people see it:\n\n- **Guest:** sees the total on the basket page.\n- **Customer:** sees it again on the receipt.\n\nWhat I'd do, in [Fix the invoice total](/tasks/tks6kwzzdngr):\n\n1. **Owner of the fix:** Engineering, on the checkout repository.\n2. **One complete change:** the total, the receipt and `go test ./checkout/...` for both.\n3. **Review:** an independent check before it merges.",
17 "**Yes. This is the right shape for it.** The invoice total is counted twice when delivery is added.\n\nTwo people see it:\n\n- **Guest:** sees the total on the basket page.\n- **Customer:** sees it again on the receipt.\n\nWhat I'd do:\n\n1. **Owner of the fix:** Engineering, on the checkout repository.\n2. **One complete change:** the total, the receipt and a test for both.\n3. **Review:** an independent check before it merges.\n",
18 "I've logged [Fix the invoice total](/tasks/tks6kwzzdngr) in [Comercio](/projects/p_abc123). Engineering starts with a reproduction.",
19 "Run `go test ./internal/checkout/...` first, then this:\n\n```go\nfunc total(lines []Line) int {\n\tsum := 0\n\tfor _, l := range lines {\n\t\tsum += l.Price\n\t}\n\treturn sum\n}\n```\n\nThat is *all* it needs.",
20 "## Plan\n\nThree steps, each **small**:\n\n- read the code\n- reproduce it\n- fix it, with *one* test\n\nNested:\n\n1. First\n - a detail\n - another\n2. Second\n\n> Quoted note.\n\nLast line with a hard break \nand its continuation.",
21 // Two paste boxes: a command and a prompt.
22 "Run this in a terminal:\n\n```bash\ngo test ./internal/checkout/... -run 'TestTotal' > out.txt && echo ok\n```\n\nThen paste this into the review:\n\n```prompt\nCheck that the total counts delivery once, & say which test shows it.\n```\n\nThat is all.",
23 "Café con ñandú, señor. ☕ **Ünïcödé** 🎉 works.\nA soft break, then\n\n---\n\nafter a rule.",
24}
25
26// labels are the paste box's words in these tests.
27var labels = Labels{Copy: "Copy", Copied: "Copied", Selected: "Selected", Prompt: "Prompt", Text: "Text", Command: "Command", CopyIcon: `<svg i="copy"></svg>`, CopiedIcon: `<svg i="check"></svg>`}
28
29// button is the paste box's Copy button as these labels render it.
30func button(attrs string) string {
31 return `<button type="button" equipo-paste-copy data-copied="Copied" data-selected="Selected"` + attrs + `><span equipo-paste-icon="copy"><svg i="copy"></svg></span><span equipo-paste-icon="copied"><svg i="check"></svg></span><span equipo-paste-word>Copy</span></button>`
32}
33
34// open is the draft of a fence still open: its box, Copy disabled.
35func open(lang, kind, text string) string {
36 return `<div equipo-paste="` + kind + `"><div equipo-paste-bar><span equipo-paste-lang>` + lang + `</span>` + button(` aria-disabled="true"`) + `</div><pre><code>` + text + "</code></pre></div>\n"
37}
38
39// visible is what a reader sees of rendered HTML: the text without tags,
40// entities resolved, runs of whitespace as one space.
41func visible(h string) string {
42 text := html.UnescapeString(regexp.MustCompile(`<[^>]*>`).ReplaceAllString(h, ""))
43 return strings.TrimSpace(strings.Join(strings.Fields(text), " "))
44}
45
46// code is a code block, whose text is the reply's own characters.
47var code = regexp.MustCompile(`(?s)<pre>.*?</pre>`)
48
49// The draft of a whole reply is the reply: the end of a stream adds
50// nothing to the layout but what the last characters bring.
51func TestDraftOfTheWholeReplyIsTheReply(t *testing.T) {
52 for _, reply := range corpus {
53 if got, want := labels.RenderDraft(reply), labels.Render(reply); got != want {
54 t.Errorf("draft of the whole reply differs\n draft: %q\nreply: %q", got, want)
55 }
56 }
57}
58
59// Every prefix of a reply, as it streams: no markup characters show (the
60// corpus has no literal ones), and what shows is the start of what the
61// finished reply shows.
62func TestDraftShowsNoMarkupAndOnlyTheReplysText(t *testing.T) {
63 for _, reply := range corpus {
64 final := visible(string(labels.Render(reply)))
65 for i := 0; i <= len(reply); i++ {
66 if i < len(reply) && !utf8.RuneStart(reply[i]) {
67 continue
68 }
69 draft := string(labels.RenderDraft(reply[:i]))
70 seen := visible(draft)
71 if prose := visible(code.ReplaceAllString(draft, "")); strings.ContainsAny(prose, "*`[]#") || strings.Contains(prose, "](") {
72 t.Fatalf("markup shows at %d %q: %q", i, reply[:i], seen)
73 }
74 if !strings.HasPrefix(final, seen) {
75 t.Fatalf("draft at %d %q shows %q, not the start of %q", i, reply[:i], seen, final)
76 }
77 }
78 }
79}
80
81func TestDraftPartialSyntax(t *testing.T) {
82 for _, c := range []struct{ text, want string }{
83 // An unclosed ** or backtick is plain text until it closes.
84 {"**Yes. This is", "<p>Yes. This is</p>\n"},
85 {"**Yes. This is**", "<p><strong>Yes. This is</strong></p>\n"},
86 {"**closed** and **op", "<p><strong>closed</strong> and op</p>\n"},
87 {"*it", "<p>it</p>\n"},
88 {"Run `go te", "<p>Run go te</p>\n"},
89 {"Run `go test`", "<p>Run <code>go test</code></p>\n"},
90 {"- **Guest:** sees it\n- **Cus", "<ul><li><strong>Guest:</strong> sees it</li>\n<li>Cus</li>\n</ul>\n"},
91 // A paragraph that has ended keeps what it has: it shows as the
92 // finished reply will.
93 {"Para **open\n\nNext", "<p>Para **open</p>\n<p>Next</p>\n"},
94 // Arithmetic is not an opener.
95 {"2 * 3", "<p>2 * 3</p>\n"},
96 // A partial list marker or heading waits for more of its line.
97 {"- **Guest:** a\n-", "<ul><li><strong>Guest:</strong> a</li>\n</ul>\n"},
98 {"- **Guest:** a\n- ", "<ul><li><strong>Guest:</strong> a</li>\n</ul>\n"},
99 {"1. **Owner**\n2", "<ol><li><strong>Owner</strong></li>\n</ol>\n"},
100 {"1. **Owner**\n2.", "<ol><li><strong>Owner</strong></li>\n</ol>\n"},
101 {"Text\n\n##", "<p>Text</p>\n"},
102 {"## Pl", "<p>Pl</p>\n"},
103 {"Text\n--", "<p>Text</p>\n"},
104 // An open code fence is an open code block; a closing fence on
105 // its way shows nothing.
106 {"```go\nfmt.Println(", open("go", "code", "fmt.Println(\n")},
107 {"```go\nx := 1\n``", open("go", "code", "x := 1\n")},
108 {"``", ""},
109 // A fence's opening line waits for its end, so the box shows
110 // with its label and its wrapping settled.
111 {"Run:\n\n```ba", "<p>Run:</p>\n"},
112 {"Run:\n\n```prompt", "<p>Run:</p>\n"},
113 {"```prompt\n", open("Prompt", "prose", "")},
114 {"- step\n ```sh", "<ul><li>step</li>\n</ul>\n"},
115 // A link on its way shows its words.
116 {"See [Fix the inv", "<p>See Fix the inv</p>\n"},
117 {"See [Fix the invoice]", "<p>See Fix the invoice</p>\n"},
118 {"See [Fix](/tasks/tk1", "<p>See Fix</p>\n"},
119 {"See [Fix](/tasks/tk1)", "<p>See <a href=\"/tasks/tk1\" rel=\"noopener\">Fix</a></p>\n"},
120 {"Code `a[b`", "<p>Code <code>a[b</code></p>\n"},
121 } {
122 if got := string(labels.RenderDraft(c.text)); got != c.want {
123 t.Errorf("RenderDraft(%q)\n got %q\nwant %q", c.text, got, c.want)
124 }
125 }
126}
127
128// The draft keeps the safe subset: the same rules as a finished reply.
129func TestDraftKeepsTheSafeSubset(t *testing.T) {
130 for _, raw := range []string{`<script>alert(1)</script>`, `<img src=x onerror=alert(1)>`, `[x](javascript:alert%281%29)`, "[x](javascript:alert(1)", `![x](https://evil.test/a)`, "<scr"} {
131 got := string(labels.RenderDraft(raw))
132 if strings.Contains(got, "<script") || strings.Contains(got, "<img") || strings.Contains(got, "<scr") || strings.Contains(got, `href="javascript:`) {
133 t.Fatal("unsafe", got)
134 }
135 }
136}
137
138// A fence still arriving is open in the draft, and Copy waits: it turns
139// on when the closing line comes, and only the button changes. A
140// finished message is never open, even if its fence never closed.
141func TestDraftOpenFenceWaitsToCopy(t *testing.T) {
142 text := "Run:\n\n```bash\ngo test ./...\n"
143 draft := string(labels.RenderDraft(text))
144 if !strings.Contains(draft, `aria-disabled="true"`) {
145 t.Fatalf("an open fence's Copy is not disabled: %s", draft)
146 }
147 // The closing line on its way is held back: still open.
148 if got := string(labels.RenderDraft(text + "```")); got != draft {
149 t.Fatalf("a closing line on its way changed the box: %s", got)
150 }
151 for _, closed := range []string{text + "```\n", text + "```\n\nDone."} {
152 got := string(labels.RenderDraft(closed))
153 if strings.Contains(got, "aria-disabled") {
154 t.Fatalf("a closed fence's Copy is disabled (%q): %s", closed, got)
155 }
156 if strings.Replace(draft, ` aria-disabled="true"`, "", 1) != got[:len(draft)-len(` aria-disabled="true"`)] {
157 t.Fatalf("the box changed when its fence closed\n open: %s\nclosed: %s", draft, got)
158 }
159 }
160 if got := string(labels.Render(text)); strings.Contains(got, "aria-disabled") {
161 t.Fatalf("a finished message's unclosed fence is disabled: %s", got)
162 }
163 // Two fences: the first closed, the second still open.
164 two := string(labels.RenderDraft("```sh\nls\n```\n\n```sh\npw"))
165 if strings.Count(two, "<button") != 2 || strings.Count(two, "aria-disabled") != 1 || strings.Index(two, "aria-disabled") < strings.LastIndex(two, "<button") {
166 t.Fatalf("only the last, open fence waits: %s", two)
167 }
168}
169