-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.test.js
93 lines (82 loc) · 3.22 KB
/
contentScript.test.js
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
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
describe('YouTube Spot Saver Function Tests', () => {
let browser;
let page;
beforeAll(async () => {
browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
page = await browser.newPage();
// Inject mock chrome object
await page.evaluateOnNewDocument(() => {
window.chrome = {
runtime: {
onMessage: {
addListener: function () { }
}
},
storage: {
sync: { get: () => {}, set: () => {} }
}
};
});
await page.goto('https://www.youtube.com');
const contentScript = fs.readFileSync(path.resolve(__dirname, '../js/contentScript.js'), 'utf8');
await page.evaluate(contentScript);
});
afterAll(async () => {
await browser.close();
});
test('should store video timestamp in localStorage', async () => {
await page.evaluate(() => {
const timestamp = 3.22;
const videoId = 'i7vEpeljeZA';
storeVideoData(videoId, timestamp);
});
const storedData = await page.evaluate(() => localStorage.getItem('yt-ss-i7vEpeljeZA'));
const data = JSON.parse(storedData);
expect(data.timestamp).toBe(3.22);
});
test('should get video timestamp from localStorage', async () => {
await page.evaluate(() => {
const videoId = 'i7vEpeljeZA';
const timestamp = 3.22;
const duration = 12.021
storeVideoData(videoId, timestamp, duration);
});
const videoData = await page.evaluate(() => getVideoData('i7vEpeljeZA'));
expect(videoData.timestamp).toBe(3.22);
expect(videoData.duration).toBe(12.021);
});
test('should return null for expired video timestamp', async () => {
await page.evaluate(() => {
const videoId = 'i7vEpeljeZA';
const data = {
timestamp: 3,
duration: 12.021,
savedAt: Date.now() - (1000 * 60 * 60 * 25) // 25 hours ago
};
localStorage.setItem(`yt-ss-${videoId}`, JSON.stringify(data));
});
const data = await page.evaluate(() => getVideoData('i7vEpeljeZA'));
expect(data).toBeNull();
});
test('should return null for non-existent video timestamp', async () => {
const data = await page.evaluate(() => getVideoData('nonexistent'));
expect(data).toBeNull();
});
test('should clean up expired storage', async () => {
await page.evaluate(() => {
const videoId = 'i7vEpeljeZA';
const data = {
timestamp: 3,
duration: 12.021,
savedAt: Date.now() - (1000 * 60 * 60 * 25) // 25 hours ago
};
localStorage.setItem(`yt-ss-${videoId}`, JSON.stringify(data));
cleanUpExpiredStorage();
});
const storedData = await page.evaluate(() => localStorage.getItem('yt-ss-i7vEpeljeZA'));
expect(storedData).toBeNull();
});
});