-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_notepad_pinning.js
More file actions
68 lines (59 loc) · 2.5 KB
/
Copy pathtest_notepad_pinning.js
File metadata and controls
68 lines (59 loc) · 2.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
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const sourcePath = path.join(__dirname, 'public', 'sidebar.js');
const source = fs.readFileSync(sourcePath, 'utf8')
.replace(/export function /g, 'function ')
+ '\nmodule.exports = { getRecentNotepadId, getPinnedNotepadId, getNewestCreatedNotepadId, getStartupNotepadId };\n';
const entries = new Map();
const localStorage = {
getItem(key) {
return entries.has(key) ? entries.get(key) : null;
},
setItem(key, value) {
entries.set(key, String(value));
}
};
const context = {
module: { exports: {} },
exports: {},
localStorage,
JSON,
Map,
Set,
Date,
Number,
String,
Array
};
vm.runInNewContext(source, context, { filename: sourcePath });
const {
getRecentNotepadId,
getPinnedNotepadId,
getNewestCreatedNotepadId,
getStartupNotepadId
} = context.module.exports;
const notepads = [
{ id: 'old-created', name: 'Old', createdAt: 10, updatedAt: 20 },
{ id: 'pinned-older', name: 'Pinned old', createdAt: 30, updatedAt: 40, pinned: true, pinnedAt: 50 },
{ id: 'pinned-newer', name: 'Pinned new', createdAt: 60, updatedAt: 70, pinned: true, pinnedAt: 80 },
{ id: 'newest-created', name: 'Newest', createdAt: 90, updatedAt: 95 }
];
localStorage.setItem('dumbpad_recents', JSON.stringify([
{ id: 'old-created', touchedAt: 200 },
{ id: 'missing', touchedAt: 300 },
{ id: 'newest-created', touchedAt: 100 }
]));
assert.strictEqual(getRecentNotepadId(notepads), 'old-created', 'recent selection should ignore deleted IDs and use the newest local record');
assert.strictEqual(getPinnedNotepadId(notepads), 'pinned-newer', 'pinned selection should prefer the latest pinned item');
assert.strictEqual(getNewestCreatedNotepadId(notepads), 'newest-created', 'created fallback should select the newest created item');
assert.strictEqual(getStartupNotepadId(notepads), 'old-created', 'startup should prefer the most recently opened existing article');
localStorage.setItem('dumbpad_recents', '[]');
assert.strictEqual(getStartupNotepadId(notepads), 'pinned-newer', 'startup should fall back to a pinned article when there is no recent article');
assert.strictEqual(
getStartupNotepadId(notepads.map(notepad => ({ ...notepad, pinned: false, pinnedAt: undefined }))),
'newest-created',
'startup should fall back to the newest created article without recent or pinned articles'
);
console.log('Notepad pinning checks passed');