gitstack

grindlemire/go-tui code browser

8.1 KB Go 360 lines 2026-06-03 ยท d15bb9f raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package gopls

import (
	"testing"
)

func TestSourceMapTuiToGo(t *testing.T) {
	type tc struct {
		mappings []Mapping
		tuiLine  int
		tuiCol   int
		wantLine int
		wantCol  int
		wantOk   bool
	}

	tests := map[string]tc{
		"exact match start": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			tuiLine:  5,
			tuiCol:   10,
			wantLine: 10,
			wantCol:  5,
			wantOk:   true,
		},
		"within mapping": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			tuiLine:  5,
			tuiCol:   15, // 5 chars into the mapping
			wantLine: 10,
			wantCol:  10, // 5 + 5 offset
			wantOk:   true,
		},
		"no match": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			tuiLine:  7, // different line
			tuiCol:   10,
			wantLine: 7, // returns original
			wantCol:  10,
			wantOk:   false,
		},
		"before mapping column": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			tuiLine:  5,
			tuiCol:   5, // before mapping starts
			wantLine: 5,
			wantCol:  5,
			wantOk:   false,
		},
		"after mapping column": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			tuiLine:  5,
			tuiCol:   35, // after mapping ends (10 + 20 = 30)
			wantLine: 5,
			wantCol:  35,
			wantOk:   false,
		},
		"empty mappings": {
			mappings: []Mapping{},
			tuiLine:  5,
			tuiCol:   10,
			wantLine: 5,
			wantCol:  10,
			wantOk:   false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			sm := NewSourceMap()
			for _, m := range tt.mappings {
				sm.AddMapping(m)
			}

			gotLine, gotCol, gotOk := sm.TuiToGo(tt.tuiLine, tt.tuiCol)

			if gotLine != tt.wantLine {
				t.Errorf("TuiToGo() gotLine = %d, want %d", gotLine, tt.wantLine)
			}
			if gotCol != tt.wantCol {
				t.Errorf("TuiToGo() gotCol = %d, want %d", gotCol, tt.wantCol)
			}
			if gotOk != tt.wantOk {
				t.Errorf("TuiToGo() gotOk = %v, want %v", gotOk, tt.wantOk)
			}
		})
	}
}

func TestSourceMapGoToTui(t *testing.T) {
	type tc struct {
		mappings []Mapping
		goLine   int
		goCol    int
		wantLine int
		wantCol  int
		wantOk   bool
	}

	tests := map[string]tc{
		"exact match start": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			goLine:   10,
			goCol:    5,
			wantLine: 5,
			wantCol:  10,
			wantOk:   true,
		},
		"within mapping": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			goLine:   10,
			goCol:    15, // 10 chars into the mapping
			wantLine: 5,
			wantCol:  20, // 10 + 10 offset
			wantOk:   true,
		},
		"no match": {
			mappings: []Mapping{
				{TuiLine: 5, TuiCol: 10, GoLine: 10, GoCol: 5, Length: 20},
			},
			goLine:   7, // different line
			goCol:    5,
			wantLine: 7, // returns original
			wantCol:  5,
			wantOk:   false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			sm := NewSourceMap()
			for _, m := range tt.mappings {
				sm.AddMapping(m)
			}

			gotLine, gotCol, gotOk := sm.GoToTui(tt.goLine, tt.goCol)

			if gotLine != tt.wantLine {
				t.Errorf("GoToTui() gotLine = %d, want %d", gotLine, tt.wantLine)
			}
			if gotCol != tt.wantCol {
				t.Errorf("GoToTui() gotCol = %d, want %d", gotCol, tt.wantCol)
			}
			if gotOk != tt.wantOk {
				t.Errorf("GoToTui() gotOk = %v, want %v", gotOk, tt.wantOk)
			}
		})
	}
}

func TestSourceMapRoundTrip(t *testing.T) {
	type tc struct {
		mapping Mapping
		offset  int // offset within the mapping to test
	}

	tests := map[string]tc{
		"start of mapping": {
			mapping: Mapping{TuiLine: 3, TuiCol: 8, GoLine: 7, GoCol: 12, Length: 15},
			offset:  0,
		},
		"middle of mapping": {
			mapping: Mapping{TuiLine: 3, TuiCol: 8, GoLine: 7, GoCol: 12, Length: 15},
			offset:  7,
		},
		"end of mapping": {
			mapping: Mapping{TuiLine: 3, TuiCol: 8, GoLine: 7, GoCol: 12, Length: 15},
			offset:  14, // Length - 1
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			sm := NewSourceMap()
			sm.AddMapping(tt.mapping)

			// Start from TUI position
			tuiLine := tt.mapping.TuiLine
			tuiCol := tt.mapping.TuiCol + tt.offset

			// Convert to Go
			goLine, goCol, ok := sm.TuiToGo(tuiLine, tuiCol)
			if !ok {
				t.Fatalf("TuiToGo failed to find mapping")
			}

			// Convert back to TUI
			backLine, backCol, ok := sm.GoToTui(goLine, goCol)
			if !ok {
				t.Fatalf("GoToTui failed to find mapping")
			}

			// Should match original
			if backLine != tuiLine || backCol != tuiCol {
				t.Errorf("Round trip failed: started at (%d, %d), got back (%d, %d)",
					tuiLine, tuiCol, backLine, backCol)
			}
		})
	}
}

func TestVirtualFileCache(t *testing.T) {
	type tc struct {
		operations func(c *VirtualFileCache)
		tuiURI     string
		wantFound  bool
	}

	tests := map[string]tc{
		"put and get": {
			operations: func(c *VirtualFileCache) {
				c.Put("file:///test.gsx", "file:///test_gsx_generated.go", "content", NewSourceMap(), 1)
			},
			tuiURI:    "file:///test.gsx",
			wantFound: true,
		},
		"get nonexistent": {
			operations: func(c *VirtualFileCache) {},
			tuiURI:     "file:///nonexistent.gsx",
			wantFound:  false,
		},
		"put and remove": {
			operations: func(c *VirtualFileCache) {
				c.Put("file:///test.gsx", "file:///test_gsx_generated.go", "content", NewSourceMap(), 1)
				c.Remove("file:///test.gsx")
			},
			tuiURI:    "file:///test.gsx",
			wantFound: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			cache := NewVirtualFileCache()
			tt.operations(cache)

			got := cache.Get(tt.tuiURI)
			if tt.wantFound && got == nil {
				t.Error("expected to find cached file, got nil")
			}
			if !tt.wantFound && got != nil {
				t.Errorf("expected not to find cached file, got %+v", got)
			}
		})
	}
}

func TestVirtualFileCacheGetByGoURI(t *testing.T) {
	cache := NewVirtualFileCache()
	sm := NewSourceMap()

	cache.Put("file:///a.gsx", "file:///a_gsx_generated.go", "content a", sm, 1)
	cache.Put("file:///b.gsx", "file:///b_gsx_generated.go", "content b", sm, 1)

	// Find by Go URI
	got := cache.GetByGoURI("file:///a_gsx_generated.go")
	if got == nil {
		t.Fatal("expected to find cached file by Go URI")
	}
	if got.TuiURI != "file:///a.gsx" {
		t.Errorf("got TuiURI %s, want file:///a.gsx", got.TuiURI)
	}

	// Find nonexistent
	got = cache.GetByGoURI("file:///nonexistent_gsx_generated.go")
	if got != nil {
		t.Error("expected nil for nonexistent Go URI")
	}
}

func TestTuiURIToGoURI(t *testing.T) {
	type tc struct {
		tuiURI  string
		wantURI string
	}

	tests := map[string]tc{
		"gsx extension": {
			tuiURI:  "file:///path/to/file.gsx",
			wantURI: "file:///path/to/file_gsx_generated.go",
		},
		"no extension": {
			tuiURI:  "file:///path/to/file",
			wantURI: "file:///path/to/file_generated.go",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := TuiURIToGoURI(tt.tuiURI)
			if got != tt.wantURI {
				t.Errorf("TuiURIToGoURI(%q) = %q, want %q", tt.tuiURI, got, tt.wantURI)
			}
		})
	}
}

func TestGoURIToTuiURI(t *testing.T) {
	type tc struct {
		goURI   string
		wantURI string
	}

	tests := map[string]tc{
		"generated suffix": {
			goURI:   "file:///path/to/file_gsx_generated.go",
			wantURI: "file:///path/to/file.gsx",
		},
		"no suffix": {
			goURI:   "file:///path/to/regular.go",
			wantURI: "file:///path/to/regular.go",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := GoURIToTuiURI(tt.goURI)
			if got != tt.wantURI {
				t.Errorf("GoURIToTuiURI(%q) = %q, want %q", tt.goURI, got, tt.wantURI)
			}
		})
	}
}

func TestIsVirtualGoFile(t *testing.T) {
	type tc struct {
		uri  string
		want bool
	}

	tests := map[string]tc{
		"virtual file":  {uri: "file:///test_gsx_generated.go", want: true},
		"regular go":    {uri: "file:///test.go", want: false},
		"gsx file":      {uri: "file:///test.gsx", want: false},
		"almost suffix": {uri: "file:///test_generated.go", want: false},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := IsVirtualGoFile(tt.uri)
			if got != tt.want {
				t.Errorf("IsVirtualGoFile(%q) = %v, want %v", tt.uri, got, tt.want)
			}
		})
	}
}