Repository navigation
Expand file tree
/
Copy pathcontentScript.test.js
More file actions
367 lines (303 loc) · 11.9 KB
/
Copy pathcontentScript.test.js
File metadata and controls
367 lines (303 loc) · 11.9 KB
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
361
362
363
364
365
366
367
/**
* Unit tests for lib.js shared logic.
* Mocks browser/Chrome APIs to test in Node.js.
*/
const fs = require("fs");
const path = require("path");
// Load lib.js — defines global `AutoViewed` via `var`
eval(fs.readFileSync(path.join(__dirname, "lib.js"), "utf8"));
const {
cleanText,
globToRegex,
shouldAutoView,
extractFilePath,
findViewedToggle,
isAlreadyViewed,
scanFilesLogic,
} = AutoViewed;
// ---- Minimal DOM mock ----
class MockElement {
constructor(tag, attrs = {}, children = []) {
this.tagName = tag.toUpperCase();
this._attrs = { ...attrs };
this._children = children;
this._classList = new Set((attrs.class || "").split(" ").filter(Boolean));
this.textContent = attrs.textContent || "";
this.checked = attrs.checked || false;
this.parentElement = null;
this._clicked = false;
for (const child of children) {
child.parentElement = this;
}
}
getAttribute(name) {
return this._attrs[name] ?? null;
}
setAttribute(name, value) {
this._attrs[name] = value;
}
get children() {
return this._children;
}
querySelector(selector) {
const results = this._querySelectorAll(selector);
return results[0] || null;
}
querySelectorAll(selector) {
return this._querySelectorAll(selector);
}
_querySelectorAll(selector) {
const results = [];
const check = (el) => {
if (matchesSelector(el, selector)) results.push(el);
for (const child of el._children) check(child);
};
for (const child of this._children) check(child);
return results;
}
click() {
this._clicked = true;
}
}
// Simple selector matcher (supports tag, .class, [attr], [attr*="val"], [attr="val"], tag.class)
const matchesSelector = (el, selector) => {
// Handle comma-separated selectors
if (selector.includes(",")) {
return selector.split(",").some((s) => matchesSelector(el, s.trim()));
}
// Handle descendant selectors (space-separated)
const parts = selector.trim().split(/\s+/);
if (parts.length > 1) {
// For simplicity, just match the last part
return matchesSelector(el, parts[parts.length - 1]);
}
const s = parts[0];
// tag[attr*="val"]
let m = s.match(/^(\w+)?\[class\*="([^"]+)"\]$/);
if (m) {
const [, tag, substr] = m;
if (tag && el.tagName !== tag.toUpperCase()) return false;
return (el._attrs.class || "").includes(substr);
}
// tag[attr="val"]
m = s.match(/^(\w+)?\[(\w[\w-]*)="([^"]+)"\]$/);
if (m) {
const [, tag, attr, val] = m;
if (tag && el.tagName !== tag.toUpperCase()) return false;
return el._attrs[attr] === val;
}
// tag.class or .class
m = s.match(/^(\w+)?\.([A-Za-z_-][\w-]*)$/);
if (m) {
const [, tag, cls] = m;
if (tag && el.tagName !== tag.toUpperCase()) return false;
return el._classList.has(cls);
}
// a.Link--primary
m = s.match(/^(\w+)\.([A-Za-z_][\w-]*)$/);
if (m) {
const [, tag, cls] = m;
if (el.tagName !== tag.toUpperCase()) return false;
return el._classList.has(cls);
}
// Simple tag
if (/^\w+$/.test(s)) {
return el.tagName === s.toUpperCase();
}
return false;
};
// ---- Tests ----
let passed = 0;
let failed = 0;
const assert = (condition, msg) => {
if (condition) {
passed++;
console.log(` ✓ ${msg}`);
} else {
failed++;
console.error(` ✗ ${msg}`);
}
};
// ==== Test 1: extractFilePath handles root-level files ====
console.log("\n== Test 1: extractFilePath — root-level files (no slash) ==");
{
const link = new MockElement("a", { class: "Link--primary", textContent: "package.json" });
const fileEl = new MockElement("div", {}, [link]);
const result = extractFilePath(fileEl);
assert(result === "package.json", `Root file extracted: "${result}"`);
}
{
const link = new MockElement("a", { class: "Link--primary", textContent: ".gitignore" });
const fileEl = new MockElement("div", {}, [link]);
const result = extractFilePath(fileEl);
assert(result === ".gitignore", `Dotfile extracted: "${result}"`);
}
{
const link = new MockElement("a", { class: "Link--primary", textContent: "src/components/App.tsx" });
const fileEl = new MockElement("div", {}, [link]);
const result = extractFilePath(fileEl);
assert(result === "src/components/App.tsx", `Nested path extracted: "${result}"`);
}
{
const link = new MockElement("a", { class: "Link--primary", textContent: "\u200epackage.json\u200e" });
const fileEl = new MockElement("div", {}, [link]);
const result = extractFilePath(fileEl);
assert(result === "package.json", `Unicode markers stripped: "${result}"`);
}
{
const fileEl = new MockElement("div", {}, []);
const result = extractFilePath(fileEl);
assert(result === null, "No anchors returns null");
}
// ==== Test 2: processed.add timing — toggle not found ====
console.log("\n== Test 2: processed.add timing — toggle missing allows retry ==");
{
// File with path but no toggle button yet
const link = new MockElement("a", { class: "Link--primary", textContent: "src/index.ts" });
const fileEl = new MockElement("div", {}, [link]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
// First scan: toggle missing, should NOT be in processed
scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(!processed.has(fileEl), "Element NOT processed when toggle missing (retry 1)");
assert(retryCount.get(fileEl) === 1, "retryCount incremented to 1");
// Simulate toggle appearing on second scan
const toggle = new MockElement("button", {
class: "MarkAsViewedButton-abc123",
"aria-pressed": "false",
});
fileEl._children.push(toggle);
const result = scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(processed.has(fileEl), "Element processed after toggle appears");
assert(result.marked === 1, `Marked count: ${result.marked}`);
assert(toggle._clicked || result.clickTargets.includes(toggle), "Toggle enqueued for click");
}
// ==== Test 3: MAX_RETRIES causes give-up ====
console.log("\n== Test 3: MAX_RETRIES — gives up after limit ==");
{
// File with path but toggle never appears
const link = new MockElement("a", { class: "Link--primary", textContent: "src/missing.ts" });
const fileEl = new MockElement("div", {}, [link]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const MAX_RETRIES = 3;
for (let i = 0; i < MAX_RETRIES - 1; i++) {
scanFilesLogic([fileEl], processed, retryCount, activePatterns, MAX_RETRIES);
}
assert(!processed.has(fileEl), `Not processed after ${MAX_RETRIES - 1} retries`);
scanFilesLogic([fileEl], processed, retryCount, activePatterns, MAX_RETRIES);
assert(processed.has(fileEl), `Processed (given up) after ${MAX_RETRIES} retries`);
}
// ==== Test 4: MAX_RETRIES for path extraction failure ====
console.log("\n== Test 4: MAX_RETRIES — path extraction failure ==");
{
// Element with no anchors at all
const fileEl = new MockElement("div", {}, []);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const MAX_RETRIES = 3;
for (let i = 0; i < MAX_RETRIES - 1; i++) {
scanFilesLogic([fileEl], processed, retryCount, activePatterns, MAX_RETRIES);
}
assert(!processed.has(fileEl), `Not processed after ${MAX_RETRIES - 1} retries (no path)`);
scanFilesLogic([fileEl], processed, retryCount, activePatterns, MAX_RETRIES);
assert(processed.has(fileEl), `Processed (given up) after ${MAX_RETRIES} retries (no path)`);
}
// ==== Test 5: Pattern not matching marks as processed immediately ====
console.log("\n== Test 5: Pattern mismatch — processed immediately ==");
{
const link = new MockElement("a", { class: "Link--primary", textContent: "README.md" });
const fileEl = new MockElement("div", {}, [link]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(processed.has(fileEl), "Non-matching file processed immediately");
}
// ==== Test 6: Already viewed marks as processed immediately ====
console.log("\n== Test 6: Already viewed — processed immediately ==");
{
const link = new MockElement("a", { class: "Link--primary", textContent: "src/app.ts" });
const toggle = new MockElement("button", {
class: "MarkAsViewedButton-abc123",
"aria-pressed": "true",
});
const fileEl = new MockElement("div", {}, [link, toggle]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const result = scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(processed.has(fileEl), "Already-viewed file processed immediately");
assert(result.marked === 0, "No click enqueued for already-viewed file");
}
// ==== Test 7: globToRegex patterns ====
console.log("\n== Test 7: globToRegex — pattern matching ==");
{
assert(shouldAutoView("package.json", ["*.json"]), "*.json matches package.json");
assert(shouldAutoView("src/foo.ts", ["*.ts"]), "*.ts matches src/foo.ts");
assert(!shouldAutoView("src/foo.tsx", ["*.ts"]), "*.ts does NOT match src/foo.tsx");
assert(shouldAutoView("src/foo.tsx", ["*.tsx"]), "*.tsx matches src/foo.tsx");
assert(shouldAutoView("pnpm-lock.yaml", ["pnpm-lock.yaml"]), "Exact match works");
assert(shouldAutoView("a/b/c.js", ["*/b/*.js"]), "*/b/*.js matches a/b/c.js");
}
// ==== Test 8: SPA navigation resets state ====
console.log("\n== Test 8: State reset simulation ==");
{
let processed = new WeakSet();
let retryCount = new WeakMap();
const link = new MockElement("a", { class: "Link--primary", textContent: "src/app.ts" });
const toggle = new MockElement("button", {
class: "MarkAsViewedButton-abc123",
"aria-pressed": "false",
});
const fileEl = new MockElement("div", {}, [link, toggle]);
// First page load
scanFilesLogic([fileEl], processed, retryCount, ["*.ts"], 10);
assert(processed.has(fileEl), "File processed on first page");
// Simulate SPA navigation reset (what init() does)
processed = new WeakSet();
retryCount = new WeakMap();
// Same element after reset
toggle.setAttribute("aria-pressed", "false");
toggle._clicked = false;
const result = scanFilesLogic([fileEl], processed, retryCount, ["*.ts"], 10);
assert(result.marked === 1, "File re-processed after state reset");
}
// ==== Test 9: pendingRetry flag ====
console.log("\n== Test 9: pendingRetry — returned when elements need retry ==");
{
const link = new MockElement("a", { class: "Link--primary", textContent: "src/index.ts" });
// No toggle — should trigger pendingRetry
const fileEl = new MockElement("div", {}, [link]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const result = scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(result.pendingRetry === true, "pendingRetry is true when toggle missing");
assert(result.marked === 0, "No files marked when toggle missing");
}
{
// No path — should also trigger pendingRetry
const fileEl = new MockElement("div", {}, []);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const result = scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(result.pendingRetry === true, "pendingRetry is true when path missing");
}
{
// All elements fully processed — no pendingRetry
const link = new MockElement("a", { class: "Link--primary", textContent: "README.md" });
const fileEl = new MockElement("div", {}, [link]);
const processed = new WeakSet();
const retryCount = new WeakMap();
const activePatterns = ["*.ts"];
const result = scanFilesLogic([fileEl], processed, retryCount, activePatterns, 10);
assert(result.pendingRetry === false, "pendingRetry is false when all processed");
}
// ==== Summary ====
console.log(`\n== Results: ${passed} passed, ${failed} failed ==`);
process.exit(failed > 0 ? 1 : 0);