package chatmarkdown import ( "html" "regexp" "strings" "testing" ) func TestSafeSubset(t *testing.T) { got := string(labels.Render("**bold** *italic* `inline`\n\n- one\n- two\n\n```go\n`, ``, `[x](javascript:alert%281%29)`, `[x](data:text/html,evil)`, `[x](//evil.test)`, `[x](javascript:evil)`, `![x](https://evil.test/a)`} { got := string(labels.Render(raw)) if strings.Contains(got, "raw`)), "<b>") { t.Fatal("raw HTML not escaped") } } // The coordinator links a task by its title; on this site only task and // project pages become links. func TestSiteLinksOnlyTasksAndProjects(t *testing.T) { got := string(labels.Render("I've logged [Swipe back doesn't work](/tasks/tks6kwzzdngr) in [Woodstar](/projects/p_abc123).")) for _, want := range []string{`Swipe back doesn't work`, `Woodstar`} { if !strings.Contains(got, want) { t.Fatalf("missing %s: %s", want, got) } } for _, raw := range []string{`[x](/conversation/tasks/tk1/retry)`, `[x](/tasks/tk1/../../logout)`, `[x](/settings)`, `[x](tasks/tk1)`, `[x](/tasks/TK1)`, `[x](/tasks/tk1?a=b)`, `[x](/\\evil.test)`} { if got := string(labels.Render(raw)); strings.Contains(got, ". func TestFenceIsAPasteBox(t *testing.T) { got := string(labels.Render("Run this:\n\n```bash\ngo test ./...\n```\n")) want := `

Run this:

Command` + button("") + `
go test ./...
` if got != want { t.Fatalf("paste box\n got %q\nwant %q", got, want) } // No label: still a box, a code one. A prompt or text label is prose. for _, c := range []struct{ fence, kind, lang string }{{"```", "code", ""}, {"~~~", "code", ""}, {"```prompt", "prose", "Prompt"}, {"```Text", "prose", "Text"}, {"```go linenos", "code", "go"}, {"```c++", "code", "c++"}, {"```.env", "code", ".env"}} { got := string(labels.Render(c.fence + "\nx\n" + c.fence[:3])) if !strings.HasPrefix(got, `
`) { t.Fatalf("%s: not a %s box: %s", c.fence, c.kind, got) } if has := strings.Contains(got, ""+c.lang+""); has != (c.lang != "") || c.lang == "" && strings.Contains(got, "equipo-paste-lang") { t.Fatalf("%s: label: %s", c.fence, got) } } // An indented code block stays a plain one. if got := string(labels.Render(" indented\n")); got != "
indented\n
\n" { t.Fatalf("indented code: %s", got) } } // A box's label names what it holds: Prompt, Text, Command for a shell // (bash, sh, zsh, shell, console), whatever the case of the fence, or // the language as written. An unlabelled fence has none. Copy carries // its two icons, Copy's and the tick shown once it has copied, beside // its word. func TestPasteBoxLabels(t *testing.T) { for _, c := range []struct{ fence, label string }{ {"prompt", "Prompt"}, {"PROMPT", "Prompt"}, {"text", "Text"}, {"Text", "Text"}, {"bash", "Command"}, {"sh", "Command"}, {"zsh", "Command"}, {"shell", "Command"}, {"console", "Command"}, {"Bash", "Command"}, {"go", "go"}, {"Python", "Python"}, {"c++", "c++"}, {"txt", "txt"}, {"", ""}, } { got := string(labels.Render("```" + c.fence + "\nx\n```")) want := `
` + `` + c.label + `` + button("") + `
` if c.label == "" { want = `
` + button("") + `
` } if !strings.Contains(got, want) { t.Fatalf("%q: want %s in\n%s", c.fence, want, got) } if strings.Contains(string(labels.RenderDraft("```"+c.fence+"\nx\n")), want[:len(want)-len(button("")+`
`)]) != true { t.Fatalf("%q: the draft labels it otherwise", c.fence) } } } // A label shows only in a safe charset, escaped; anything else shows no // label and keeps the box. The button's words are escaped too. func TestPasteBoxLabelIsEscaped(t *testing.T) { for _, info := range []string{"" for _, render := range []func(string) string{func(s string) string { return string(labels.Render(s)) }, func(s string) string { return string(labels.RenderDraft(s)) }} { got := render("```go\n" + raw + "\n```\n") m := regexp.MustCompile(`(?s)
(.*)
`).FindStringSubmatch(got) if m == nil || strings.Contains(m[1], "