|
| 1 | +## Byte-capture repro for the two margin bugs: |
| 2 | +## |
| 3 | +## 1. typing the char that fills the last column: caret and char omitted. |
| 4 | +## 2. multiple trailing spaces: all collapsed into the wrap, not just one. |
| 5 | +## |
| 6 | +## Drives the real stub binary under a PTY at width 12 and dumps the exact |
| 7 | +## bytes each keystroke paints, plus the grid state. |
| 8 | + |
| 9 | +import std/[json, os, strutils, unicode] |
| 10 | +import tty_expect, stub_helpers |
| 11 | +import ttty/grid |
| 12 | + |
| 13 | +proc newFixture(name: string): string = |
| 14 | + result = getCurrentDir() / "testdata" / "output" / "tty" / |
| 15 | + (name & "_" & $getCurrentProcessId()) |
| 16 | + if dirExists(result): removeDir(result) |
| 17 | + createDir(result); createDir(result / "data"); createDir(result / "run") |
| 18 | + |
| 19 | +proc writeConfiguredProvider(root: string) = |
| 20 | + createDir(root / "xdg" / "3code") |
| 21 | + writeFile(root / "xdg" / "3code" / "config", """ |
| 22 | +[settings] |
| 23 | +current = "stub.stub-model" |
| 24 | +
|
| 25 | +[provider] |
| 26 | +name = "stub" |
| 27 | +url = "stub://provider" |
| 28 | +key = "stub" |
| 29 | +family = "glm" |
| 30 | +models = "stub-model" |
| 31 | +""") |
| 32 | + |
| 33 | +proc stubEnv(root, responsesPath: string): seq[EnvVar] = |
| 34 | + createDir(root / "tmp") |
| 35 | + @[ |
| 36 | + (key: "TERM", val: "xterm-256color"), |
| 37 | + (key: "PATH", val: getEnv("PATH")), |
| 38 | + (key: "HOME", val: root), |
| 39 | + (key: "TMPDIR", val: root / "tmp"), |
| 40 | + (key: "XDG_CONFIG_HOME", val: root / "xdg"), |
| 41 | + (key: "XDG_DATA_HOME", val: root / "data"), |
| 42 | + (key: "THREECODE_STUB_RESPONSES", val: responsesPath), |
| 43 | + ] |
| 44 | + |
| 45 | +proc startStub(root: string; cols = DefaultTtyCols; rows = DefaultTtyRows): TtySession = |
| 46 | + newTtySession(ensureStubBinary(), args = ["-x", "-i"], cwd = root / "run", |
| 47 | + env = stubEnv(root, root / "run" / "stub_responses.json"), |
| 48 | + cols = cols, rows = rows) |
| 49 | + |
| 50 | +proc vis(b: string): string = |
| 51 | + ## escape bytes made readable |
| 52 | + for ch in b: |
| 53 | + case ch |
| 54 | + of '\x1b': result.add "<E>" |
| 55 | + of '\r': result.add "<CR>" |
| 56 | + of '\n': result.add "<LF>" |
| 57 | + of '\x7f': result.add "<DEL>" |
| 58 | + else: result.add ch |
| 59 | + |
| 60 | +proc dump(s: TtySession; label: string) = |
| 61 | + s.drain(150, recordFrame = true) |
| 62 | + echo "=== [", label, "] cursor row=", s.grid.row, " col=", s.grid.col, " ===" |
| 63 | + for i, row in s.grid.rows: |
| 64 | + var line = "" |
| 65 | + for cell in row: |
| 66 | + let ch = cell.rune.toUTF8 |
| 67 | + if cell.attrs.hasAttr(saReverse): line.add "[" & ch & "]" |
| 68 | + else: line.add ch |
| 69 | + echo align($i, 2), " |", line, "|" |
| 70 | + |
| 71 | +proc typeSlow(s: TtySession; text: string) = |
| 72 | + for ch in text: |
| 73 | + s.send $ch |
| 74 | + s.drain(40) |
| 75 | + |
| 76 | +when isMainModule: |
| 77 | + let root = newFixture("margin_bytes_a") |
| 78 | + writeConfiguredProvider(root) |
| 79 | + writeFile(root / "run" / "stub_responses.json", $(%*[ |
| 80 | + {"content": "ok", "usage": {"promptTokens": 1, "completionTokens": 1, |
| 81 | + "totalTokens": 2, "cachedTokens": 0}} |
| 82 | + ])) |
| 83 | + |
| 84 | + # Width 12: prompt "❯ " is 2 cells, 10 data cells fill the row. |
| 85 | + let tty = startStub(root, cols = 12, rows = 14) |
| 86 | + tty.expect "\u276f" |
| 87 | + tty.expectIdleCaret() |
| 88 | + discard tty.freshRaw() |
| 89 | + tty.advanceRawMark() |
| 90 | + dump(tty, "w12 idle") |
| 91 | + |
| 92 | + typeSlow(tty, "012345678") |
| 93 | + tty.expectIdleCaret() |
| 94 | + var raw = tty.freshRaw() |
| 95 | + echo "--- bytes for '012345678' ---" |
| 96 | + echo raw.vis |
| 97 | + tty.advanceRawMark() |
| 98 | + dump(tty, "nine chars") |
| 99 | + |
| 100 | + tty.send "9" |
| 101 | + tty.expectIdleCaret() |
| 102 | + raw = tty.freshRaw() |
| 103 | + echo "--- bytes for '9' (fills last cell) ---" |
| 104 | + echo raw.vis |
| 105 | + tty.advanceRawMark() |
| 106 | + dump(tty, "tenth char fills row") |
| 107 | + |
| 108 | + tty.send "x" |
| 109 | + tty.expectIdleCaret() |
| 110 | + raw = tty.freshRaw() |
| 111 | + echo "--- bytes for 'x' (wraps to next row) ---" |
| 112 | + echo raw.vis |
| 113 | + tty.advanceRawMark() |
| 114 | + dump(tty, "eleventh char wraps") |
| 115 | + tty.close() |
| 116 | + |
| 117 | + # Spaces: "abcdefg" + 5 spaces. abcdefg occupies cols 2..8; three spaces |
| 118 | + # fit (cols 9..11), the fourth is the margin break, the fifth belongs on |
| 119 | + # the next row. |
| 120 | + let root2 = newFixture("margin_bytes_b") |
| 121 | + writeConfiguredProvider(root2) |
| 122 | + writeFile(root2 / "run" / "stub_responses.json", $(%*[ |
| 123 | + {"content": "ok", "usage": {"promptTokens": 1, "completionTokens": 1, |
| 124 | + "totalTokens": 2, "cachedTokens": 0}} |
| 125 | + ])) |
| 126 | + let t2 = startStub(root2, cols = 12, rows = 14) |
| 127 | + t2.expect "\u276f" |
| 128 | + t2.expectIdleCaret() |
| 129 | + discard t2.freshRaw() |
| 130 | + t2.advanceRawMark() |
| 131 | + typeSlow(t2, "abcdefg") |
| 132 | + t2.expectIdleCaret() |
| 133 | + t2.advanceRawMark() |
| 134 | + dump(t2, "seven chars") |
| 135 | + for n in 1 .. 5: |
| 136 | + t2.send " " |
| 137 | + t2.expectIdleCaret() |
| 138 | + echo "--- bytes for space #", n, " ---" |
| 139 | + echo t2.freshRaw().vis |
| 140 | + t2.advanceRawMark() |
| 141 | + dump(t2, "after space #" & $n) |
| 142 | + t2.close() |
0 commit comments