package web import ( "encoding/json" "os" "testing" ) // Equipo's lines may carry Markdown links. Base links only web addresses; // everything else, raw HTML included, stays text. func TestRichText(t *testing.T) { for in, want := range map[string]string{ "Published at https://site-sample.example.test/.": `Published at https://site-sample.example.test/.`, "See [docs](https://docs.test/a?b=1&c=2)": `See docs`, "[x](javascript:alert(1))": `[x](javascript:alert(1))`, "[x](data:text/html,hi)": `[x](data:text/html,<b>hi</b>)`, " bold": `<script>alert(1)</script> <b>bold</b>`, "[](https://a.test/)": `<img src=x onerror=alert(1)>`, "[x](/tasks/../admin)": `[x](/tasks/../admin)`, "[x](//evil.test/)": `[x](//evil.test/)`, "plain & simple": `plain & simple`, } { if got := string(richText(in)); got != want { t.Errorf("richText(%q)\n got %s\nwant %s", in, got, want) } } } // The Assistant never points at the team's pages: a /tasks/ link is // text, while a web address still links. func TestRichTextHasNoTaskLinks(t *testing.T) { for in, want := range map[string]string{ "[x](/tasks/tk1)": `[x](/tasks/tk1)`, "See [it](https://site.example.test/)": `See it`, } { if got := string(richText(in)); got != want { t.Errorf("richText(%q)\n got %s\nwant %s", in, got, want) } } } // The Assistant writes a little Markdown (plan decision D16): bold, // italics, bullet and numbered lists. Anything else stays text, links keep // their rules inside lists, and what is bold is still escaped. func TestRichTextMarkdown(t *testing.T) { for _, c := range []struct{ in, want string }{ {"**Done.** Your site is up", `Done. Your site is up`}, {"*soon*", `soon`}, {"_soon_", `soon`}, {"a_b_c", `a_b_c`}, {"**x**", `<b>x</b>`}, {"Three things:\n- Docs\n- Sheets\n- [Calendar](https://cal.test/)", `Three things:`}, {"1. One\n2. Two", `
  1. One
  2. Two
`}, {"3) Three", `
  1. Three
`}, {"- [x](javascript:alert(1))", ``}, {"** not bold **", `** not bold **`}, {"# Heading", `# Heading`}, // The newline next to a list goes; the others stay. {"- a\n\nb\n* c\n1. d\n", "\nb
  1. d
"}, {"*a\nb* and _x_y and é_x_ and - not a list", "*a\nb* and _x_y and é_x_ and - not a list"}, {"*", `*`}, // As a page would hold it: line breaks are \n, and NULs go. {"A\r\n- b\r\n- c\rD\x00E", "ADE"}, {"- \n1.\n1000. x", "1.\n1000. x"}, } { if got := string(richText(c.in)); got != c.want { t.Errorf("richText(%q)\n got %s\nwant %s", c.in, got, c.want) } } } // The person's own words (Paul, 2026-11-26; Fable's S4): the Assistant's // little Markdown, with emphasis only at word boundaries. The samples are // the app's too (PersonMarkdownTests) and the script's (TestAskPersonMarkdown). func TestPersonText(t *testing.T) { raw, err := os.ReadFile("testdata/person.json") if err != nil { t.Fatal(err) } var cases []struct{ Text, HTML string } if err := json.Unmarshal(raw, &cases); err != nil { t.Fatal(err) } for _, c := range cases { if got := string(personText(c.Text)); got != c.HTML { t.Errorf("personText(%q)\n got %s\nwant %s", c.Text, got, c.HTML) } } // The Assistant's replies keep their rules. if got := string(richText("2*3*4")); got != "234" { t.Errorf("richText changed: %s", got) } } // A lookup's answer: the person's Markdown with no links at all. The // samples are the app's too (testLookupMarkdownNoLinks). func TestLookupMarkdown(t *testing.T) { raw, err := os.ReadFile("testdata/lookup.json") if err != nil { t.Fatal(err) } var cases []struct{ Text, HTML string } if err := json.Unmarshal(raw, &cases); err != nil { t.Fatal(err) } for _, c := range cases { if got := string(lookupMarkdown(c.Text)); got != c.HTML { t.Errorf("lookupMarkdown(%q)\n got %s\nwant %s", c.Text, got, c.HTML) } } }