-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_search_optimization.js
More file actions
129 lines (118 loc) · 4.5 KB
/
test_search_optimization.js
File metadata and controls
129 lines (118 loc) · 4.5 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
const fs = require('fs');
const vm = require('vm');
const assert = require('assert');
// Read script.js
const code = fs.readFileSync('script.js', 'utf8');
// Mock DOM and Global Variables
const context = {
document: {
getElementById: () => ({ innerHTML: '', classList: { add: ()=>{}, remove: ()=>{} }, style: {} }),
querySelectorAll: () => [],
addEventListener: () => {},
body: { style: {} },
documentElement: { getAttribute: () => '', setAttribute: () => {} },
querySelector: () => ({ classList: { add: ()=>{}, remove: ()=>{} }, style: {} }),
},
window: {
matchMedia: () => ({ matches: false }),
location: { search: '' },
addEventListener: () => {},
history: { pushState: () => {}, replaceState: () => {} },
scrollTo: () => {},
},
localStorage: {
getItem: () => null,
setItem: () => {},
},
navigator: { userAgent: 'test', platform: 'test' },
firebase: {
auth: () => ({ onAuthStateChanged: () => {} }),
firestore: () => ({ collection: () => ({ doc: () => ({ onSnapshot: () => {} }) }) }),
apps: [],
initializeApp: () => {},
},
// Mock UI Elements
searchInput: { value: '', addEventListener: () => {} },
pdfGrid: { style: {}, innerHTML: '' },
pdfCount: { textContent: '' },
emptyState: { style: {} },
tabBtns: [],
filterBtns: [],
pdfModal: { addEventListener: () => {}, classList: { add: ()=>{}, remove: ()=>{} } },
shareModal: { addEventListener: () => {}, classList: { add: ()=>{}, remove: ()=>{} } },
modalShareBtn: { addEventListener: () => {} },
pdfViewer: { src: '' },
modalTitle: { textContent: '' },
shareLink: { value: '' },
toast: { style: {}, classList: { add: ()=>{}, remove: ()=>{} } },
toastMessage: { textContent: '' },
commentSidebar: { classList: { add: ()=>{}, remove: ()=>{} } },
commentsList: { innerHTML: '', appendChild: ()=>{} },
commentCount: { textContent: '' },
commentForm: { addEventListener: () => {} },
commentInput: { value: '' },
commentAuthor: { value: '' },
alomolePromo: { classList: { add: ()=>{}, remove: ()=>{} } },
closeAlomolePromo: { addEventListener: () => {} },
goToTopBtn: { addEventListener: () => {}, classList: { add: ()=>{}, remove: ()=>{} } },
maintenanceScreen: { classList: { add: ()=>{}, remove: ()=>{} } },
openCommentsBtn: { addEventListener: () => {} },
closeCommentsBtn: { addEventListener: () => {} },
classSelect: { addEventListener: () => {}, innerHTML: '', value: '' },
// Globals
console: console,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
Date: Date,
parseInt: parseInt,
isNaN: isNaN,
Math: Math,
fetch: () => Promise.resolve(),
URLSearchParams: class { get() { return null; } },
};
// Create Context
vm.createContext(context);
try {
vm.runInContext(code, context);
} catch (e) {
console.error("Error running script.js in VM:", e);
}
// Access functions from context
const { prepareSearchIndex, debounce } = context;
// --- TEST 1: prepareSearchIndex ---
console.log('Test 1: prepareSearchIndex');
const rawData = [
{ id: '1', title: 'Organic Chemistry', description: 'Reactions', category: 'Organic', author: 'Dr. Bond', uploadDate: new Date().toISOString() },
{ id: '2', title: 'Calculus', description: 'Integrals', category: 'Math', author: 'Newton', uploadDate: new Date('2020-01-01').toISOString() }
];
const processed = prepareSearchIndex(rawData);
try {
assert(processed[0]._searchStr.includes('organic chemistry'), 'Search string missing title');
assert(processed[0]._searchStr.includes('dr. bond'), 'Search string missing author');
assert(processed[0]._formattedDate, 'Formatted date missing');
assert(processed[0]._isNew === true, 'Should be new');
assert(processed[1]._isNew === false, 'Should be old');
console.log('PASS: prepareSearchIndex');
} catch (e) {
console.error('FAIL: prepareSearchIndex', e);
process.exit(1);
}
// --- TEST 2: debounce ---
console.log('Test 2: debounce');
let counter = 0;
const increment = () => counter++;
const debouncedInc = debounce(increment, 50);
debouncedInc();
debouncedInc();
debouncedInc();
setTimeout(() => {
try {
assert.strictEqual(counter, 1, 'Debounce should only execute once');
console.log('PASS: debounce');
} catch (e) {
console.error('FAIL: debounce', e);
process.exit(1);
}
}, 100);