-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstorage.test.js
More file actions
145 lines (126 loc) · 3.94 KB
/
Copy pathstorage.test.js
File metadata and controls
145 lines (126 loc) · 3.94 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
/**
* Tests for Podkey storage functions
* Note: These tests require Chrome extension APIs, so they may need mocking
*/
import { describe, it } from 'node:test';
import assert from 'node:assert';
// Mock chrome.storage for testing. The extension uses two areas: session
// (in-memory private key) and local (persisted public key + settings), so the
// mock backs each area with its own store.
function makeArea (store) {
return {
get: async (keys) => {
const result = {};
if (Array.isArray(keys)) {
keys.forEach(key => {
result[key] = store[key];
});
} else if (keys) {
Object.keys(keys).forEach(key => {
result[key] = store[key] || keys[key];
});
} else {
return { ...store };
}
return result;
},
set: async (items) => {
Object.assign(store, items);
},
remove: async (keys) => {
if (Array.isArray(keys)) {
keys.forEach(key => delete store[key]);
} else {
delete store[keys];
}
}
};
}
const mockStorage = {
localData: {},
sessionData: {}
};
mockStorage.local = makeArea(mockStorage.localData);
mockStorage.session = makeArea(mockStorage.sessionData);
// Set up global chrome mock
global.chrome = { storage: mockStorage };
// Import after setting up mock
const {
storeKeypair,
getKeypair,
hasKeypair,
deleteKeypair,
addTrustedOrigin,
isTrustedOrigin,
getAutoSign,
setAutoSign
} = await import('../src/storage.js');
describe('Storage Functions', () => {
// Clear storage before each test
const clearStorage = () => {
mockStorage.localData = {};
mockStorage.sessionData = {};
mockStorage.local = makeArea(mockStorage.localData);
mockStorage.session = makeArea(mockStorage.sessionData);
};
describe('storeKeypair / getKeypair', () => {
it('should store and retrieve a keypair', async () => {
clearStorage();
const privateKey = 'a'.repeat(64);
const publicKey = 'b'.repeat(64);
await storeKeypair(privateKey, publicKey);
const retrieved = await getKeypair();
assert(retrieved, 'Keypair should be retrieved');
assert.strictEqual(retrieved.privateKey, privateKey);
assert.strictEqual(retrieved.publicKey, publicKey);
});
it('should return null if keypair does not exist', async () => {
clearStorage();
const retrieved = await getKeypair();
assert.strictEqual(retrieved, null);
});
});
describe('hasKeypair', () => {
it('should return false when no keypair exists', async () => {
clearStorage();
assert.strictEqual(await hasKeypair(), false);
});
it('should return true when keypair exists', async () => {
clearStorage();
await storeKeypair('a'.repeat(64), 'b'.repeat(64));
assert.strictEqual(await hasKeypair(), true);
});
});
describe('deleteKeypair', () => {
it('should delete stored keypair', async () => {
clearStorage();
await storeKeypair('a'.repeat(64), 'b'.repeat(64));
assert.strictEqual(await hasKeypair(), true);
await deleteKeypair();
assert.strictEqual(await hasKeypair(), false);
});
});
describe('trusted origins', () => {
it('should add and check trusted origin', async () => {
clearStorage();
const origin = 'https://example.com';
assert.strictEqual(await isTrustedOrigin(origin), false);
await addTrustedOrigin(origin);
assert.strictEqual(await isTrustedOrigin(origin), true);
});
});
describe('auto-sign', () => {
it('should get default auto-sign value', async () => {
clearStorage();
const autoSign = await getAutoSign();
assert.strictEqual(typeof autoSign, 'boolean');
});
it('should set and get auto-sign value', async () => {
clearStorage();
await setAutoSign(false);
assert.strictEqual(await getAutoSign(), false);
await setAutoSign(true);
assert.strictEqual(await getAutoSign(), true);
});
});
});