package chatmarkdown import ( "html" "regexp" "strings" "testing" "unicode/utf8" ) // corpus is coordinator replies as they are written: the recorded ones // (a bold opening line, a bold-led bullet list and a numbered list run // together while streaming) and the rest of the subset the chat renders. var corpus = []string{ // fakeclaude's markdownSample, which the browser test streams. "**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.", "**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", "I've logged [Fix the invoice total](/tasks/tks6kwzzdngr) in [Comercio](/projects/p_abc123). Engineering starts with a reproduction.", "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.", "## 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.", // Two paste boxes: a command and a prompt. "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.", "Café con ñandú, señor. ☕ **Ünïcödé** 🎉 works.\nA soft break, then\n\n---\n\nafter a rule.", } // labels are the paste box's words in these tests. var labels = Labels{Copy: "Copy", Copied: "Copied", Selected: "Selected", Prompt: "Prompt", Text: "Text", Command: "Command", CopyIcon: ``, CopiedIcon: ``} // button is the paste box's Copy button as these labels render it. func button(attrs string) string { return `` } // open is the draft of a fence still open: its box, Copy disabled. func open(lang, kind, text string) string { return `
` + text + ".*?`) // The draft of a whole reply is the reply: the end of a stream adds // nothing to the layout but what the last characters bring. func TestDraftOfTheWholeReplyIsTheReply(t *testing.T) { for _, reply := range corpus { if got, want := labels.RenderDraft(reply), labels.Render(reply); got != want { t.Errorf("draft of the whole reply differs\n draft: %q\nreply: %q", got, want) } } } // Every prefix of a reply, as it streams: no markup characters show (the // corpus has no literal ones), and what shows is the start of what the // finished reply shows. func TestDraftShowsNoMarkupAndOnlyTheReplysText(t *testing.T) { for _, reply := range corpus { final := visible(string(labels.Render(reply))) for i := 0; i <= len(reply); i++ { if i < len(reply) && !utf8.RuneStart(reply[i]) { continue } draft := string(labels.RenderDraft(reply[:i])) seen := visible(draft) if prose := visible(code.ReplaceAllString(draft, "")); strings.ContainsAny(prose, "*`[]#") || strings.Contains(prose, "](") { t.Fatalf("markup shows at %d %q: %q", i, reply[:i], seen) } if !strings.HasPrefix(final, seen) { t.Fatalf("draft at %d %q shows %q, not the start of %q", i, reply[:i], seen, final) } } } } func TestDraftPartialSyntax(t *testing.T) { for _, c := range []struct{ text, want string }{ // An unclosed ** or backtick is plain text until it closes. {"**Yes. This is", "
Yes. This is
\n"}, {"**Yes. This is**", "Yes. This is
\n"}, {"**closed** and **op", "closed and op
\n"}, {"*it", "it
\n"}, {"Run `go te", "Run go te
\n"}, {"Run `go test`", "Run go test
Para **open
\nNext
\n"}, // Arithmetic is not an opener. {"2 * 3", "2 * 3
\n"}, // A partial list marker or heading waits for more of its line. {"- **Guest:** a\n-", "Text
\n"}, {"## Pl", "Pl
\n"}, {"Text\n--", "Text
\n"}, // An open code fence is an open code block; a closing fence on // its way shows nothing. {"```go\nfmt.Println(", open("go", "code", "fmt.Println(\n")}, {"```go\nx := 1\n``", open("go", "code", "x := 1\n")}, {"``", ""}, // A fence's opening line waits for its end, so the box shows // with its label and its wrapping settled. {"Run:\n\n```ba", "Run:
\n"}, {"Run:\n\n```prompt", "Run:
\n"}, {"```prompt\n", open("Prompt", "prose", "")}, {"- step\n ```sh", "See Fix the inv
\n"}, {"See [Fix the invoice]", "See Fix the invoice
\n"}, {"See [Fix](/tasks/tk1", "See Fix
\n"}, {"See [Fix](/tasks/tk1)", "See Fix
\n"}, {"Code `a[b`", "Code a[b