| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/charmbracelet/colorprofile" |
| 8 | |
| 9 | "github.com/charmbracelet/x/ansi" |
| 10 | |
| 11 | "reasonix/internal/event" |
| 12 | ) |
| 13 | |
| 14 | // TestMathStreamingEndToEnd drives the real streaming path (ingestEvent → |
| 15 | // streamAnswer/flushableMarkdownPrefix → renderer.Render → commitPending) with |
| 16 | // the math chunked mid-formula across event.Text boundaries, the way a model |
| 17 | // actually streams tokens. It proves a half-written $...$ / $$...$$ is never |
| 18 | // flushed as raw LaTeX and the committed transcript shows finished Unicode. |
| 19 | func TestMathStreamingEndToEnd(t *testing.T) { |
| 20 | activeColorProfile = colorprofile.NoTTY |
| 21 | m := newTestChatTUI() |
| 22 | |
| 23 | chunks := []string{ |
| 24 | `Euler's identity is $e^{i\`, |
| 25 | "pi} + 1 = 0$, a classic.\n\nThe Basel sum:\n$$\\sum_{n=1}", |
| 26 | `}^{\infty} \frac{1}{n^2}`, |
| 27 | " = \\frac{\\pi^2}{6}$$\n\nThat is all.", |
| 28 | } |
| 29 | for _, c := range chunks { |
| 30 | m.ingestEvent(event.Event{Kind: event.Text, Text: c}) |
| 31 | mid := strings.Join(m.transcript, "\n") |
| 32 | for _, leak := range []string{`\pi`, `\sum`, `\frac`, `\infty`, `$e^{`, `$$\`} { |
| 33 | if strings.Contains(mid, leak) { |
| 34 | t.Fatalf("raw LaTeX %q leaked into a mid-stream flush: %q", leak, mid) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | m.ingestEvent(event.Event{Kind: event.Message}) |
| 40 | |
| 41 | out := strings.Join(m.transcript, "\n") |
| 42 | for _, want := range []string{"e^(iπ) + 1 = 0", "∑", "1/(n²)", "(π²)/6", "That is all."} { |
| 43 | if !strings.Contains(out, want) { |
| 44 | t.Errorf("final transcript missing %q:\n%s", want, out) |
| 45 | } |
| 46 | } |
| 47 | for _, leak := range []string{`\`, "$$", "$e"} { |
| 48 | if strings.Contains(out, leak) { |
| 49 | t.Errorf("final transcript still contains raw %q:\n%s", leak, out) |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // TestMathStreamingStyledPath repeats the commit with colour on so the real |
| 55 | // ANSI-styled output is exercised, then asserts the Unicode survives a strip. |
| 56 | func TestMathStreamingStyledPath(t *testing.T) { |
| 57 | activeColorProfile = colorprofile.ANSI256 |
| 58 | defer func() { activeColorProfile = colorprofile.NoTTY }() |
| 59 | m := newTestChatTUI() |
| 60 | |
| 61 | m.ingestEvent(event.Event{Kind: event.Text, Text: `The relation $a^2 + b^2 = c^2$ holds.`}) |
| 62 | m.ingestEvent(event.Event{Kind: event.Message}) |
| 63 | |
| 64 | out := strings.Join(m.transcript, "\n") |
| 65 | if !strings.Contains(out, "\x1b[") { |
| 66 | t.Errorf("expected ANSI escapes in coloured output, got %q", out) |
| 67 | } |
| 68 | if stripped := ansi.Strip(out); !strings.Contains(stripped, "a² + b² = c²") { |
| 69 | t.Errorf("math missing after ANSI strip: %q", stripped) |
| 70 | } |
| 71 | } |
| 72 |