aboutsummaryrefslogtreecommitdiff
path: root/internal/ogtags/parse_test.go
blob: 54815b3ed3a1d3153bd0e14d66e492c4df96eac7 (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
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
package ogtags

import (
	"reflect"
	"strings"
	"testing"
	"time"

	"golang.org/x/net/html"
)

// TestExtractOGTags updated with correct expectations based on filtering logic
func TestExtractOGTags(t *testing.T) {
	// Use a cache instance that reflects the default approved lists
	testCache := NewOGTagCache("", false, time.Minute)
	// Manually set approved tags/prefixes based on the user request for clarity
	testCache.approvedTags = []string{"description"}
	testCache.approvedPrefixes = []string{"og:"}

	tests := []struct {
		name     string
		htmlStr  string
		expected map[string]string
	}{
		{
			name: "Basic OG tags", // Includes standard 'description' meta tag
			htmlStr: `<!DOCTYPE html>
				<html>
				<head>
					<meta property="og:title" content="Test Title" />
					<meta property="og:description" content="Test Description" />
					<meta name="description" content="Regular Description" />
					<meta name="keywords" content="test, keyword" />
				</head>
				<body></body>
				</html>`,
			expected: map[string]string{
				"og:title":       "Test Title",
				"og:description": "Test Description",
				"description":    "Regular Description",
			},
		},
		{
			name: "OG tags with name attribute",
			htmlStr: `<!DOCTYPE html>
				<html>
				<head>
					<meta name="og:title" content="Test Title" />
					<meta property="og:description" content="Test Description" />
					<meta name="twitter:card" content="summary" />
				</head>
				<body></body>
				</html>`,
			expected: map[string]string{
				"og:title":       "Test Title",
				"og:description": "Test Description",
				// twitter:card is still not approved
			},
		},
		{
			name: "No approved OG tags", // Contains only standard 'description'
			htmlStr: `<!DOCTYPE html>
				<html>
				<head>
					<meta name="description" content="Test Description" />
					<meta name="keywords" content="Test" />
				</head>
				<body></body>
				</html>`,
			expected: map[string]string{
				"description": "Test Description",
			},
		},
		{
			name: "Empty content",
			htmlStr: `<!DOCTYPE html>
				<html>
				<head>
					<meta property="og:title" content="" />
					<meta property="og:description" content="Test Description" />
				</head>
				<body></body>
				</html>`,
			expected: map[string]string{
				"og:title":       "",
				"og:description": "Test Description",
			},
		},
		{
			name: "Explicitly approved tag",
			htmlStr: `<!DOCTYPE html>
						<html>
						<head>
							<meta property="description" content="Approved Description Tag" />
						</head>
						<body></body>
						</html>`,
			expected: map[string]string{
				// This is approved because "description" is in cache.approvedTags
				"description": "Approved Description Tag",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			doc, err := html.Parse(strings.NewReader(tt.htmlStr))
			if err != nil {
				t.Fatalf("failed to parse HTML: %v", err)
			}

			ogTags := testCache.extractOGTags(doc)

			if !reflect.DeepEqual(ogTags, tt.expected) {
				t.Errorf("expected %v, got %v", tt.expected, ogTags)
			}
		})
	}
}

func TestIsOGMetaTag(t *testing.T) {
	tests := []struct {
		name       string
		nodeHTML   string
		targetNode string // Helper to find the right node in parsed fragment
		expected   bool
	}{
		{
			name:       "Meta OG tag",
			nodeHTML:   `<meta property="og:title" content="Test">`,
			targetNode: "meta",
			expected:   true,
		},
		{
			name:       "Regular meta tag",
			nodeHTML:   `<meta name="description" content="Test">`,
			targetNode: "meta",
			expected:   true,
		},
		{
			name:       "Not a meta tag",
			nodeHTML:   `<div>Test</div>`,
			targetNode: "div",
			expected:   false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			// Wrap the partial HTML in basic structure for parsing
			fullHTML := "<html><head>" + tt.nodeHTML + "</head><body></body></html>"
			doc, err := html.Parse(strings.NewReader(fullHTML))
			if err != nil {
				t.Fatalf("failed to parse HTML: %v", err)
			}

			// Find the target element node (meta or div based on targetNode)
			var node *html.Node
			var findNode func(*html.Node)
			findNode = func(n *html.Node) {
				// Skip finding if already found
				if node != nil {
					return
				}
				// Check if current node matches type and tag data
				if n.Type == html.ElementNode && n.Data == tt.targetNode {
					node = n
					return
				}
				// Recursively check children
				for c := n.FirstChild; c != nil; c = c.NextSibling {
					findNode(c)
				}
			}
			findNode(doc) // Start search from root

			if node == nil {
				t.Fatalf("Could not find target node '%s' in test HTML", tt.targetNode)
			}

			// Call the function under test
			result := isOGMetaTag(node)
			if result != tt.expected {
				t.Errorf("expected %v, got %v", tt.expected, result)
			}
		})
	}
}

func TestExtractMetaTagInfo(t *testing.T) {
	// Use a cache instance that reflects the default approved lists
	testCache := NewOGTagCache("", false, time.Minute)
	testCache.approvedTags = []string{"description"}
	testCache.approvedPrefixes = []string{"og:"}

	tests := []struct {
		name             string
		nodeHTML         string
		expectedProperty string
		expectedContent  string
	}{
		{
			name:             "OG title with property (approved by prefix)",
			nodeHTML:         `<meta property="og:title" content="Test Title">`,
			expectedProperty: "og:title",
			expectedContent:  "Test Title",
		},
		{
			name:             "OG description with name (approved by prefix)",
			nodeHTML:         `<meta name="og:description" content="Test Description">`,
			expectedProperty: "og:description",
			expectedContent:  "Test Description",
		},
		{
			name:             "Regular meta tag (name=description, approved by exact match)", // Updated name for clarity
			nodeHTML:         `<meta name="description" content="Test Description">`,
			expectedProperty: "description",
			expectedContent:  "Test Description",
		},
		{
			name:             "Regular meta tag (name=keywords, not approved)",
			nodeHTML:         `<meta name="keywords" content="Test Keywords">`,
			expectedProperty: "",
			expectedContent:  "Test Keywords",
		},
		{
			name:             "Twitter tag (not approved by default)",
			nodeHTML:         `<meta name="twitter:card" content="summary">`,
			expectedProperty: "",
			expectedContent:  "summary",
		},
		{
			name:             "No content (but approved property)",
			nodeHTML:         `<meta property="og:title">`,
			expectedProperty: "og:title",
			expectedContent:  "",
		},
		{
			name:             "No property/name attribute",
			nodeHTML:         `<meta content="No property">`,
			expectedProperty: "",
			expectedContent:  "No property",
		},
		{
			name:             "Explicitly approved tag with property attribute",
			nodeHTML:         `<meta property="description" content="Approved Description Tag">`,
			expectedProperty: "description", // Approved by exact match in approvedTags
			expectedContent:  "Approved Description Tag",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			fullHTML := "<html><head>" + tt.nodeHTML + "</head><body></body></html>"
			doc, err := html.Parse(strings.NewReader(fullHTML))
			if err != nil {
				t.Fatalf("failed to parse HTML: %v", err)
			}

			var node *html.Node
			var findMetaNode func(*html.Node)
			findMetaNode = func(n *html.Node) {
				if node != nil { // Stop searching once found
					return
				}
				if n.Type == html.ElementNode && n.Data == "meta" {
					node = n
					return
				}
				for c := n.FirstChild; c != nil; c = c.NextSibling {
					findMetaNode(c)
				}
			}
			findMetaNode(doc) // Start search from root

			if node == nil {
				// Handle cases where the input might not actually contain a meta tag, though all test cases do.
				// If the test case is *designed* not to have a meta tag, this check should be different.
				// But for these tests, failure to find implies an issue with the test setup or parser.
				t.Fatalf("Could not find meta node in test HTML: %s", tt.nodeHTML)
			}

			// Call extractMetaTagInfo using the test cache instance
			property, content := testCache.extractMetaTagInfo(node<