oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package web
2
3import (
4 "encoding/json"
5 "os"
6 "testing"
7)
8
9// Equipo's lines may carry Markdown links. Base links only web addresses;
10// everything else, raw HTML included, stays text.
11func TestRichText(t *testing.T) {
12 for in, want := range map[string]string{
13 "Published at https://site-sample.example.test/.": `Published at <a href="https://site-sample.example.test/" target="_blank" rel="noopener">https://site-sample.example.test/</a>.`,
14 "See [docs](https://docs.test/a?b=1&c=2)": `See <a href="https://docs.test/a?b=1&amp;c=2" target="_blank" rel="noopener">docs</a>`,
15 "[x](javascript:alert(1))": `[x](javascript:alert(1))`,
16 "[x](data:text/html,<b>hi</b>)": `[x](data:text/html,&lt;b&gt;hi&lt;/b&gt;)`,
17 "<script>alert(1)</script> <b>bold</b>": `&lt;script&gt;alert(1)&lt;/script&gt; &lt;b&gt;bold&lt;/b&gt;`,
18 "[<img src=x onerror=alert(1)>](https://a.test/)": `<a href="https://a.test/" target="_blank" rel="noopener">&lt;img src=x onerror=alert(1)&gt;</a>`,
19 "[x](/tasks/../admin)": `[x](/tasks/../admin)`,
20 "[x](//evil.test/)": `[x](//evil.test/)`,
21 "plain & simple": `plain &amp; simple`,
22 } {
23 if got := string(richText(in)); got != want {
24 t.Errorf("richText(%q)\n got %s\nwant %s", in, got, want)
25 }
26 }
27}
28
29// The Assistant never points at the team's pages: a /tasks/ link is
30// text, while a web address still links.
31func TestRichTextHasNoTaskLinks(t *testing.T) {
32 for in, want := range map[string]string{
33 "[x](/tasks/tk1)": `[x](/tasks/tk1)`,
34 "See [it](https://site.example.test/)": `See <a href="https://site.example.test/" target="_blank" rel="noopener">it</a>`,
35 } {
36 if got := string(richText(in)); got != want {
37 t.Errorf("richText(%q)\n got %s\nwant %s", in, got, want)
38 }
39 }
40}
41
42// The Assistant writes a little Markdown (plan decision D16): bold,
43// italics, bullet and numbered lists. Anything else stays text, links keep
44// their rules inside lists, and what is bold is still escaped.
45func TestRichTextMarkdown(t *testing.T) {
46 for _, c := range []struct{ in, want string }{
47 {"**Done.** Your site is up", `<strong>Done.</strong> Your site is up`},
48 {"*soon*", `<em>soon</em>`},
49 {"_soon_", `<em>soon</em>`},
50 {"a_b_c", `a_b_c`},
51 {"**<b>x</b>**", `<strong>&lt;b&gt;x&lt;/b&gt;</strong>`},
52 {"Three things:\n- Docs\n- Sheets\n- [Calendar](https://cal.test/)", `Three things:<ul><li>Docs</li><li>Sheets</li><li><a href="https://cal.test/" target="_blank" rel="noopener">Calendar</a></li></ul>`},
53 {"1. One\n2. Two", `<ol><li>One</li><li>Two</li></ol>`},
54 {"3) Three", `<ol start="3"><li>Three</li></ol>`},
55 {"- [x](javascript:alert(1))", `<ul><li>[x](javascript:alert(1))</li></ul>`},
56 {"** not bold **", `** not bold **`},
57 {"# Heading", `# Heading`},
58 // The newline next to a list goes; the others stay.
59 {"- a\n\nb\n* c\n1. d\n", "<ul><li>a</li></ul>\nb<ul><li>c</li></ul><ol><li>d</li></ol>"},
60 {"*a\nb* and _x_y and é_x_ and - not a list", "*a\nb* and _x_y and é_x_ and - not a list"},
61 {"*", `*`},
62 // As a page would hold it: line breaks are \n, and NULs go.
63 {"A\r\n- b\r\n- c\rD\x00E", "A<ul><li>b</li><li>c</li></ul>DE"},
64 {"- \n1.\n1000. x", "<ul><li> </li></ul>1.\n1000. x"},
65 } {
66 if got := string(richText(c.in)); got != c.want {
67 t.Errorf("richText(%q)\n got %s\nwant %s", c.in, got, c.want)
68 }
69 }
70}
71
72// The person's own words (Paul, 2026-11-26; Fable's S4): the Assistant's
73// little Markdown, with emphasis only at word boundaries. The samples are
74// the app's too (PersonMarkdownTests) and the script's (TestAskPersonMarkdown).
75func TestPersonText(t *testing.T) {
76 raw, err := os.ReadFile("testdata/person.json")
77 if err != nil {
78 t.Fatal(err)
79 }
80 var cases []struct{ Text, HTML string }
81 if err := json.Unmarshal(raw, &cases); err != nil {
82 t.Fatal(err)
83 }
84 for _, c := range cases {
85 if got := string(personText(c.Text)); got != c.HTML {
86 t.Errorf("personText(%q)\n got %s\nwant %s", c.Text, got, c.HTML)
87 }
88 }
89 // The Assistant's replies keep their rules.
90 if got := string(richText("2*3*4")); got != "2<em>3</em>4" {
91 t.Errorf("richText changed: %s", got)
92 }
93}
94
95// A lookup's answer: the person's Markdown with no links at all. The
96// samples are the app's too (testLookupMarkdownNoLinks).
97func TestLookupMarkdown(t *testing.T) {
98 raw, err := os.ReadFile("testdata/lookup.json")
99 if err != nil {
100 t.Fatal(err)
101 }
102 var cases []struct{ Text, HTML string }
103 if err := json.Unmarshal(raw, &cases); err != nil {
104 t.Fatal(err)
105 }
106 for _, c := range cases {
107 if got := string(lookupMarkdown(c.Text)); got != c.HTML {
108 t.Errorf("lookupMarkdown(%q)\n got %s\nwant %s", c.Text, got, c.HTML)
109 }
110 }
111}
112