summaryrefslogtreecommitdiff
path: root/internal/ui/widgets/tokeniser_test.go
blob: 11c4d638ff3569c60ad87cb1eff0a06b8a2f7d24 (plain)
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
package widgets

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// ── detectContentType ────────────────────────────────────────────────────────

func TestDetectContentType_JSON(t *testing.T) {
	lines := []string{"POST /foo HTTP/1.1", "Content-Type: application/json", ""}
	assert.Equal(t, "json", detectContentType(lines))
}

func TestDetectContentType_HTML(t *testing.T) {
	lines := []string{"GET /foo HTTP/1.1", "Content-Type: text/html; charset=utf-8", ""}
	assert.Equal(t, "html", detectContentType(lines))
}

func TestDetectContentType_Plain(t *testing.T) {
	lines := []string{"GET /foo HTTP/1.1", "Content-Type: text/plain", ""}
	assert.Equal(t, "text", detectContentType(lines))
}

func TestDetectContentType_Missing(t *testing.T) {
	lines := []string{"GET /foo HTTP/1.1", "Accept: */*", ""}
	assert.Equal(t, "text", detectContentType(lines))
}

func TestDetectContentType_AfterBlankLineIgnored(t *testing.T) {
	lines := []string{"GET /foo HTTP/1.1", "", "Content-Type: application/json"}
	assert.Equal(t, "text", detectContentType(lines))
}

func TestDetectContentType_CaseInsensitive(t *testing.T) {
	lines := []string{"GET /foo HTTP/1.1", "content-type: APPLICATION/JSON", ""}
	assert.Equal(t, "json", detectContentType(lines))
}

// ── tokeniseFirstLine ────────────────────────────────────────────────────────

func TestTokeniseFirstLine_Request(t *testing.T) {
	tokens := tokeniseFirstLine("GET /search?q=foo HTTP/1.1")

	require.Len(t, tokens, 3)
	assert.Equal(t, tvKindMethod, tokens[0].Kind)
	assert.Equal(t, tvKindPath, tokens[1].Kind)
	assert.Equal(t, tvKindVersion, tokens[2].Kind)
}

func TestTokeniseFirstLine_ResponseStatusKinds(t *testing.T) {
	tests := []struct {
		line     string
		wantKind tvTokenKind
	}{
		{"HTTP/1.1 200 OK", tvKindStatus2xx},
		{"HTTP/1.1 201 Created", tvKindStatus2xx},
		{"HTTP/1.1 301 Moved Permanently", tvKindStatus3xx},
		{"HTTP/1.1 404 Not Found", tvKindStatus4xx},
		{"HTTP/1.1 500 Internal Server Error", tvKindStatus5xx},
	}

	for _, tt := range tests {
		t.Run(tt.line, func(t *testing.T) {
			tokens := tokeniseFirstLine(tt.line)
			require.GreaterOrEqual(t, len(tokens), 2)
			assert.Equal(t, tt.wantKind, tokens[1].Kind)
		})
	}
}

// ── tokeniseHTTPMeta ─────────────────────────────────────────────────────────

func TestTokeniseHTTPMeta_Header(t *testing.T) {
	tokens := tokeniseHTTPMeta("Content-Type: application/json", false)

	require.Len(t, tokens, 3)
	assert.Equal(t, tvKindHdrName, tokens[0].Kind)
	assert.Equal(t, "Content-Type", tokens[0].Text)
	assert.Equal(t, tvKindHdrColon, tokens[1].Kind)
	assert.Equal(t, tvKindHdrValue, tokens[2].Kind)
}

func TestTokeniseHTTPMeta_EmptyLine(t *testing.T) {
	tokens := tokeniseHTTPMeta("", false)

	require.Len(t, tokens, 1)
	assert.Equal(t, tvKindPlain, tokens[0].Kind)
	assert.Equal(t, "", tokens[0].Text)
}

func TestTokeniseHTTPMeta_NoColon(t *testing.T) {
	tokens := tokeniseHTTPMeta("plaintext", false)

	require.Len(t, tokens, 1)
	assert.Equal(t, tvKindPlain, tokens[0].Kind)
}

// ── tokeniseJSON ─────────────────────────────────────────────────────────────

func TestTokeniseJSON_KeyAndString(t *testing.T) {
	tokens := tokeniseJSON(`"name": "alice"`)

	kinds := map[tvTokenKind]bool{}
	for _, tok := range tokens {
		kinds[tok.Kind] = true
	}
	assert.True(t, kinds[tvKindJSONKey], "expected tvKindJSONKey token")
	assert.True(t, kinds[tvKindJSONStr], "expected tvKindJSONStr token")
}

func TestTokeniseJSON_Number(t *testing.T) {
	tokens := tokeniseJSON(`"count": 42`)

	found := false
	for _, tok := range tokens {
		if tok.Kind == tvKindJSONNum && tok.Text == "42" {
			found = true
		}
	}
	assert.True(t, found, "expected number token '42'")
}

func TestTokeniseJSON_BoolAndNull(t *testing.T) {
	for _, kw := range []string{"true", "false", "null"} {
		t.Run(kw, func(t *testing.T) {
			tokens := tokeniseJSON(`"flag": ` + kw)
			found := false
			for _, tok := range tokens {
				if tok.Kind == tvKindJSONBool && tok.Text == kw {
					found = true
				}
			}
			assert.True(t, found, "expected tvKindJSONBool token %q", kw)
		})
	}
}

func TestTokeniseJSON_Punctuation(t *testing.T) {
	for _, ch := range []string{"{", "}", "[", "]", ","} {
		t.Run(ch, func(t *testing.T) {
			tokens := tokeniseJSON(ch)
			require.Len(t, tokens, 1)
			assert.Equal(t, tvKindJSONPunct, tokens[0].Kind)
		})
	}
}

func TestTokeniseJSON_EscapedQuoteInsideString(t *testing.T) {
	tokens := tokeniseJSON(`"say \"hi\""`)

	strCount := 0
	for _, tok := range tokens {
		if tok.Kind == tvKindJSONStr || tok.Kind == tvKindJSONKey {
			strCount++
		}
	}
	assert.Equal(t, 1, strCount, "escaped quote should produce exactly 1 string token")
}

func TestTokeniseJSON_DoubleBackslashBeforeQuote(t *testing.T) {
	tokens := tokeniseJSON(`"\\"`)

	strCount := 0
	for _, tok := range tokens {
		if tok.Kind == tvKindJSONStr || tok.Kind == tvKindJSONKey {
			strCount++
		}
	}
	assert.Equal(t, 1, strCount, "double backslash before closing quote should produce exactly 1 string token")
}

func TestTokeniseJSON_Empty(t *testing.T) {
	tokens := tokeniseJSON("")

	require.Len(t, tokens, 1)
	assert.Equal(t, tvKindPlain, tokens[0].Kind)
}

// ── wrapTokens ───────────────────────────────────────────────────────────────

func TestWrapTokens_ShortLineNoWrap(t *testing.T) {
	tokens := []tvToken{{Text: "hello", Kind: tvKindPlain}}
	lines := wrapTokens(tokens, "hello", 80)

	require.Len(t, lines, 1)
	assert.Equal(t, "hello", lines[0].Raw)
}

func TestWrapTokens_LongLineWraps(t *testing.T) {
	long := strings.Repeat("x", 100)
	tokens := []tvToken{{Text: long, Kind: tvKindPlain}}
	lines := wrapTokens(tokens, long, 40)

	assert.Greater(t, len(lines), 1, "expected wrapping into multiple lines")

	total := 0
	for _, l := range lines {
		total += len([]rune(l.Raw))
	}
	assert.Equal(t, 100, total, "all characters must be preserved across wrapped lines")
}

func TestWrapTokens_JSONStringNotSplit(t *testing.T) {
	str := `"` + strings.Repeat("a", 50) + `"`
	tokens := []tvToken{{Text: str, Kind: tvKindJSONStr}}
	lines := wrapTokens(tokens, str, 20)

	assert.Len(t, lines, 1, "JSON string must not be split across lines")
}