| 1 | package notionmd |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 9 | ) |
| 10 | |
| 11 | // long is an address past markup.MaxAddress, ending in tail. |
| 12 | func long(tail string) string { |
| 13 | return "https://example.com/" + strings.Repeat("a", 2100) + tail |
| 14 | } |
| 15 | |
| 16 | // longLinkCases are Fable's round-4 shapes: what follows a long link, |
| 17 | // and what an address holds, each once swallowed, cut or fallen back. |
| 18 | func longLinkCases() map[string][]Run { |
| 19 | link := func(tail string) Run { return linked("the doc", long(tail)) } |
| 20 | return map[string][]Run{ |
| 21 | "letters after": {link(""), txt("s are here")}, |
| 22 | "possessive after": {link(""), txt("'s title")}, |
| 23 | "a path after": {link(""), txt("/x")}, |
| 24 | "a quote after": {link(""), txt("” and more")}, |
| 25 | "an ellipsis after": {link(""), txt("… and more")}, |
| 26 | "bold after": {link(""), bold("now")}, |
| 27 | "code after": {link(""), codeRun("x")}, |
| 28 | "another long link": {link("1"), link("2")}, |
| 29 | "brackets in it": {link("?f[0]=1&g[1]=2")}, |
| 30 | "angles in it": {link("?a=<b>")}, |
| 31 | "double brackets": {link("?f[[x]]=1")}, |
| 32 | "a backtick in it": {link("?q=`x`")}, |
| 33 | "a pipe in it": {link("?a=1|2")}, |
| 34 | "ends in a full stop": {link(".")}, |
| 35 | "ends in a quote": {link("'")}, |
| 36 | "an upper scheme": {linked("the doc", "HTTPS://example.com/"+ |
| 37 | strings.Repeat("b", 2100))}, |
| 38 | "the label is the address": {linked(long(""), long(""))}, |
| 39 | "the label is the raw address": {linked(paren, paren)}, |
| 40 | "an empty query": {link("?")}, |
| 41 | "a question at the end": {link("?a=1?")}, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // paren is a long address holding ( ), which the mapper encodes: a |
| 46 | // label equal to it is the address, not text to write again. |
| 47 | var paren = "https://example.com/a_(b)/" + strings.Repeat("a", 2100) |
| 48 | |
| 49 | // want is a long address as its link must read: the address itself, |
| 50 | // its scheme lower-cased and an empty query dropped, compared decoded, |
| 51 | // so the oracle is not the encoder under test (Fable round 5, L1). |
| 52 | func want(address string) string { |
| 53 | if scheme, rest, ok := strings.Cut(address, "://"); ok { |
| 54 | address = strings.ToLower(scheme) + "://" + rest |
| 55 | } |
| 56 | if strings.Count(address, "?") == 1 && strings.HasSuffix(address, "?") { |
| 57 | address = strings.TrimSuffix(address, "?") |
| 58 | } |
| 59 | return address |
| 60 | } |
| 61 | |
| 62 | // Fable round 4, M1: a long link is always "label (address)" with the |
| 63 | // address one autolink, byte for byte, whatever surrounds or fills it; |
| 64 | // never the fallback. |
| 65 | // |
| 66 | // Mutation (make mutate, first red 2026-11-17): the bare address is |
| 67 | // written as literal text, so neutralising and the loose check apply. |
| 68 | func TestM1LongLinksStayWhole(t *testing.T) { |
| 69 | for name, runs := range longLinkCases() { |
| 70 | result, err := Render(Page{ID: "long", Blocks: []Block{ |
| 71 | para(runs...)}}, Options{Resolve: fixtureResolver{}, |
| 72 | Images: fixtureImages{}}) |
| 73 | if err != nil || result.Fallbacks != 0 { |
| 74 | t.Errorf("%s: %v, %d fallbacks", name, err, result.Fallbacks) |
| 75 | continue |
| 76 | } |
| 77 | wanted := map[string]bool{} |
| 78 | long := 0 |
| 79 | for _, run := range runs { |
| 80 | if run.Link != "" { |
| 81 | wanted[want(run.Link)] = true |
| 82 | long++ |
| 83 | } |
| 84 | } |
| 85 | links := 0 |
| 86 | for _, span := range markup.Spans(result.Body) { |
| 87 | if span.Kind != markup.WebLink { |
| 88 | continue |
| 89 | } |
| 90 | links++ |
| 91 | decoded, err := url.PathUnescape(span.URL) |
| 92 | if err == nil { |
| 93 | delete(wanted, decoded) |
| 94 | } |
| 95 | } |
| 96 | if links != long { |
| 97 | t.Errorf("%s: %d link spans for %d links:\n%.300s", name, |
| 98 | links, long, result.Body) |
| 99 | } |
| 100 | if len(wanted) != 0 { |
| 101 | t.Errorf("%s: no link span holds the whole address:\n%.300s", |
| 102 | name, result.Body) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // The encodings themselves, pinned, so a wrong code is caught (round 5, |
| 108 | // L1: the decoded comparison alone would pass an encoder that wrote ] |
| 109 | // as %5D for [). |
| 110 | func TestM1LongAddressEncodings(t *testing.T) { |
| 111 | for raw, written := range map[string]string{ |
| 112 | "?f[0]=1": "?f%5B0%5D=1", |
| 113 | "?a=<b>": "?a=%3Cb%3E", |
| 114 | "?q=`x`": "?q=%60x%60", |
| 115 | "?a=1|2": "?a=1%7C2", |
| 116 | ".": "%2E", |
| 117 | "'": "%27", |
| 118 | "?": "", |
| 119 | "?a=1?": "?a=1%3F", |
| 120 | "#top?": "#top%3F", |
| 121 | } { |
| 122 | if got := bareAddress(long(raw)); got != long(written) { |
| 123 | t.Errorf("%q written %q, want %q", raw, |
| 124 | strings.TrimPrefix(got, long("")), written) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |