oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package notionmd
2
3import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8 "time"
9)
10
11// Fable M2: neutralising was quadratic to cubic in literal [ and ]: a
12// line of 32,000 [ took 35 s, and 100 runs of 1,000 [ before a link run
13// over seven minutes, and one such page wedged every run. Each must now
14// render in well under a second; the bound here is generous for a
15// loaded CI runner and far below the old times.
16//
17// Mutation (make mutate, first red 2026-11-17): openers rescans the
18// line from its start after each [ it replaces.
19func TestM2NeutralisingIsLinear(t *testing.T) {
20 var many []Run
21 for range 100 {
22 many = append(many, txt(strings.Repeat("[", 1000)))
23 }
24 many = append(many, linked("a link", "https://example.com/x"))
25 for name, block := range map[string]Block{
26 "32,000 [": para(txt(strings.Repeat("[", 32000))),
27 "100 runs of 1,000 [": para(many...),
28 "20,000 ](": para(txt(strings.Repeat(
29 "[a](https://e.com) ", 2000))),
30 "16,000 [[": para(txt(strings.Repeat("[[", 8000))),
31 } {
32 started := time.Now()
33 result, err := Render(Page{ID: "speed", Blocks: []Block{block}},
34 Options{Resolve: fixtureResolver{}, Images: fixtureImages{}})
35 took := time.Since(started)
36 if err != nil || result.Fallbacks != 0 {
37 t.Errorf("%s: %v, %d fallbacks", name, err, result.Fallbacks)
38 }
39 if took > 2*time.Second {
40 t.Errorf("%s took %v", name, took)
41 }
42 }
43}
44
45// Render stops when its context does: a Stop, or the run's deadline
46// for one page, ends a slow render with ErrRenderFailed and never
47// wedges the run.
48func TestRenderStopsWithItsContext(t *testing.T) {
49 ctx, cancel := context.WithCancel(context.Background())
50 cancel()
51 _, err := Render(Page{ID: "stopped", Blocks: []Block{para(txt("x"))}},
52 Options{Context: ctx, Resolve: fixtureResolver{},
53 Images: fixtureImages{}})
54 if !errors.Is(err, ErrRenderFailed) || !errors.Is(err, context.Canceled) {
55 t.Errorf("a cancelled render gave %v", err)
56 }
57}
58
59// Fable round 2, M1: shapes whose links share one long address, which
60// SafeURL read in full for every [: "[x](" repeated, closed once.
61//
62// Mutation (make mutate, first red 2026-11-17): the address bound
63// removed from markup.LinkAddress.
64func TestM1SharedAddressesAreLinear(t *testing.T) {
65 for name, text := range map[string]string{
66 "25,000 [x](": strings.Repeat("[x](", 25000) + "a)",
67 "50,000 [x](": strings.Repeat("[x](", 50000) + "a)",
68 "25,000 [x](http": strings.Repeat("[x](http://e.com/", 25000) +
69 "a)",
70 } {
71 started := time.Now()
72 _, err := Render(Page{ID: "shared", Blocks: []Block{
73 para(txt(text))}}, Options{Resolve: fixtureResolver{},
74 Images: fixtureImages{}})
75 if took := time.Since(started); took > 2*time.Second ||
76 err != nil {
77 t.Errorf("%s took %v (%v)", name, took, err)
78 }
79 }
80}
81
82// endsAfter is a context whose Err is nil for its first n asks, then
83// Canceled: a deadline arriving part-way through a render.
84type endsAfter struct {
85 context.Context
86 asks, n int
87}
88
89func (ending *endsAfter) Err() error {
90 ending.asks++
91 if ending.asks > ending.n {
92 return context.Canceled
93 }
94 return nil
95}
96
97// Fable round 3, L7: the deadline reaches inside a block, line by line.
98// A context that ends after the check before the block is caught by the
99// lines' own checks, and the render fails.
100//
101// Mutation (make mutate, first red 2026-11-17): neutralise checks no
102// context.
103func TestM1TheDeadlineReachesInsideABlock(t *testing.T) {
104 lines := strings.Repeat("a line\n", 50) + "the last"
105 ending := &endsAfter{Context: context.Background(), n: 2}
106 _, err := Render(Page{ID: "inside", Blocks: []Block{para(txt(lines))}},
107 Options{Context: ending, Resolve: fixtureResolver{},
108 Images: fixtureImages{}})
109 if !errors.Is(err, context.Canceled) {
110 t.Errorf("a deadline inside a block gave %v", err)
111 }
112}
113