|
| 1 | +package messages |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "gotest.tools/v3/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFindURLSpans(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + text string |
| 13 | + wantURLs []string |
| 14 | + wantCols [][2]int // [startCol, endCol] pairs |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "no URLs", |
| 18 | + text: "hello world", |
| 19 | + wantURLs: nil, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "simple https URL", |
| 23 | + text: "visit https://example.com for more", |
| 24 | + wantURLs: []string{"https://example.com"}, |
| 25 | + wantCols: [][2]int{{6, 25}}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "http URL", |
| 29 | + text: "go to http://example.com/path", |
| 30 | + wantURLs: []string{"http://example.com/path"}, |
| 31 | + wantCols: [][2]int{{6, 29}}, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "URL at start", |
| 35 | + text: "https://example.com is a site", |
| 36 | + wantURLs: []string{"https://example.com"}, |
| 37 | + wantCols: [][2]int{{0, 19}}, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "URL at end", |
| 41 | + text: "visit https://example.com", |
| 42 | + wantURLs: []string{"https://example.com"}, |
| 43 | + wantCols: [][2]int{{6, 25}}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "URL with path and query", |
| 47 | + text: "see https://example.com/path?q=1&b=2#frag for details", |
| 48 | + wantURLs: []string{"https://example.com/path?q=1&b=2#frag"}, |
| 49 | + wantCols: [][2]int{{4, 41}}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "URL followed by period", |
| 53 | + text: "Visit https://example.com.", |
| 54 | + wantURLs: []string{"https://example.com"}, |
| 55 | + wantCols: [][2]int{{6, 25}}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "URL in parentheses", |
| 59 | + text: "(https://example.com)", |
| 60 | + wantURLs: []string{"https://example.com"}, |
| 61 | + wantCols: [][2]int{{1, 20}}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "URL with balanced parens in path", |
| 65 | + text: "see https://en.wikipedia.org/wiki/Go_(programming_language) for more", |
| 66 | + wantURLs: []string{"https://en.wikipedia.org/wiki/Go_(programming_language)"}, |
| 67 | + wantCols: [][2]int{{4, 59}}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "multiple URLs", |
| 71 | + text: "see https://a.com and https://b.com for info", |
| 72 | + wantURLs: []string{"https://a.com", "https://b.com"}, |
| 73 | + wantCols: [][2]int{{4, 17}, {22, 35}}, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + got := findURLSpans(tt.text) |
| 80 | + assert.Equal(t, len(tt.wantURLs), len(got), "span count mismatch") |
| 81 | + for i, span := range got { |
| 82 | + assert.Equal(t, tt.wantURLs[i], span.url, "url mismatch at index %d", i) |
| 83 | + assert.Equal(t, tt.wantCols[i][0], span.startCol, "startCol mismatch at index %d", i) |
| 84 | + assert.Equal(t, tt.wantCols[i][1], span.endCol, "endCol mismatch at index %d", i) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestURLAtPosition(t *testing.T) { |
| 91 | + tests := []struct { |
| 92 | + name string |
| 93 | + line string |
| 94 | + col int |
| 95 | + expected string |
| 96 | + }{ |
| 97 | + { |
| 98 | + name: "click on URL", |
| 99 | + line: "visit https://example.com for more", |
| 100 | + col: 10, |
| 101 | + expected: "https://example.com", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "click before URL", |
| 105 | + line: "visit https://example.com for more", |
| 106 | + col: 3, |
| 107 | + expected: "", |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "click after URL", |
| 111 | + line: "visit https://example.com for more", |
| 112 | + col: 28, |
| 113 | + expected: "", |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "click on URL start", |
| 117 | + line: "visit https://example.com for more", |
| 118 | + col: 6, |
| 119 | + expected: "https://example.com", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "click on URL last char", |
| 123 | + line: "visit https://example.com for more", |
| 124 | + col: 24, |
| 125 | + expected: "https://example.com", |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "line with ANSI codes", |
| 129 | + line: "visit \x1b[34mhttps://example.com\x1b[0m for more", |
| 130 | + col: 10, |
| 131 | + expected: "https://example.com", |
| 132 | + }, |
| 133 | + { |
| 134 | + name: "empty line", |
| 135 | + line: "", |
| 136 | + col: 0, |
| 137 | + expected: "", |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + got := urlAtPosition(tt.line, tt.col) |
| 144 | + assert.Equal(t, tt.expected, got) |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestBalanceParens(t *testing.T) { |
| 150 | + tests := []struct { |
| 151 | + input string |
| 152 | + expected string |
| 153 | + }{ |
| 154 | + {"https://example.com)", "https://example.com"}, |
| 155 | + {"https://example.com/wiki/Go_(lang)", "https://example.com/wiki/Go_(lang)"}, |
| 156 | + {"https://example.com", "https://example.com"}, |
| 157 | + } |
| 158 | + |
| 159 | + for _, tt := range tests { |
| 160 | + t.Run(tt.input, func(t *testing.T) { |
| 161 | + assert.Equal(t, tt.expected, balanceParens(tt.input)) |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments