peerpigeon 0.0.7 โ†’ 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,329 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * PeerPigeon Storage Spaces Test
5
+ *
6
+ * This example demonstrates the new storage space separation feature:
7
+ * - Private space: Only owner can read/write, encrypted
8
+ * - Public space: Anyone can read, only owner can write
9
+ * - Frozen space: Immutable once set, anyone can read
10
+ *
11
+ * Key protection: prevents overwrites across different spaces
12
+ */
13
+
14
+ import { PeerPigeonMesh } from '../../src/PeerPigeonMesh.js';
15
+
16
+ class StorageSpacesTest {
17
+ constructor() {
18
+ this.mesh = null;
19
+ this.storage = null;
20
+ this.testResults = [];
21
+ }
22
+
23
+ async init() {
24
+ console.log('๐Ÿš€ Initializing PeerPigeon Mesh with Storage Spaces...');
25
+
26
+ // Initialize mesh with all features enabled
27
+ this.mesh = new PeerPigeonMesh({
28
+ enableWebDHT: true,
29
+ enableCrypto: true,
30
+ enableDistributedStorage: true
31
+ });
32
+
33
+ await this.mesh.init();
34
+ this.storage = this.mesh.distributedStorage;
35
+
36
+ console.log(`โœ… Mesh initialized with peer ID: ${this.mesh.peerId.substring(0, 16)}...`);
37
+ console.log(`๐Ÿ“ฆ Storage enabled: ${this.storage.isEnabled()}`);
38
+ console.log(`๐Ÿ”ง Space enforcement: ${this.storage.config.spaceEnforcement}`);
39
+ console.log('');
40
+ }
41
+
42
+ async runTest(testName, testFn) {
43
+ console.log(`๐Ÿงช Running test: ${testName}`);
44
+ try {
45
+ await testFn();
46
+ console.log(`โœ… Test passed: ${testName}`);
47
+ this.testResults.push({ name: testName, status: 'PASS' });
48
+ } catch (error) {
49
+ console.error(`โŒ Test failed: ${testName} - ${error.message}`);
50
+ this.testResults.push({ name: testName, status: 'FAIL', error: error.message });
51
+ }
52
+ console.log('');
53
+ }
54
+
55
+ async testPrivateSpace() {
56
+ await this.runTest('Private Space Storage', async () => {
57
+ const key = 'user-profile';
58
+ const value = { name: 'Alice', secret: 'my-password' };
59
+
60
+ // Store in private space (default)
61
+ await this.storage.store(key, value);
62
+
63
+ // Should be accessible to owner
64
+ const retrieved = await this.storage.retrieve(key);
65
+ if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
66
+ throw new Error('Private data not properly stored/retrieved');
67
+ }
68
+
69
+ console.log(' โœ“ Private space data stored and retrieved by owner');
70
+ });
71
+
72
+ await this.runTest('Private Space Explicit', async () => {
73
+ const key = 'explicit-private';
74
+ const value = { confidential: 'data' };
75
+
76
+ // Store explicitly in private space
77
+ await this.storage.store(key, value, { space: 'private' });
78
+
79
+ const retrieved = await this.storage.retrieve('private:explicit-private');
80
+ if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
81
+ throw new Error('Explicit private space data not working');
82
+ }
83
+
84
+ console.log(' โœ“ Explicit private space storage working');
85
+ });
86
+ }
87
+
88
+ async testPublicSpace() {
89
+ await this.runTest('Public Space Storage', async () => {
90
+ const key = 'app-config';
91
+ const value = { version: '1.0.0', features: ['chat', 'files'] };
92
+
93
+ // Store in public space
94
+ await this.storage.store(key, value, { space: 'public' });
95
+
96
+ // Retrieve with space prefix
97
+ const retrieved = await this.storage.retrieve('public:app-config');
98
+ if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
99
+ throw new Error('Public space data not properly stored/retrieved');
100
+ }
101
+
102
+ console.log(' โœ“ Public space data stored and retrieved');
103
+ });
104
+
105
+ await this.runTest('Public Space Legacy', async () => {
106
+ const key = 'legacy-public';
107
+ const value = { public: true };
108
+
109
+ // Store using legacy isPublic option
110
+ await this.storage.store(key, value, { isPublic: true });
111
+
112
+ const retrieved = await this.storage.retrieve(key);
113
+ if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
114
+ throw new Error('Legacy public storage not working');
115
+ }
116
+
117
+ console.log(' โœ“ Legacy public storage compatibility working');
118
+ });
119
+ }
120
+
121
+ async testFrozenSpace() {
122
+ await this.runTest('Frozen Space Storage', async () => {
123
+ const key = 'immutable-data';
124
+ const value = { constant: 'never-changes', timestamp: Date.now() };
125
+
126
+ // Store in frozen space
127
+ await this.storage.store(key, value, { space: 'frozen' });
128
+
129
+ const retrieved = await this.storage.retrieve('frozen:immutable-data');
130
+ if (JSON.stringify(retrieved) !== JSON.stringify(value)) {
131
+ throw new Error('Frozen space data not properly stored/retrieved');
132
+ }
133
+
134
+ console.log(' โœ“ Frozen space data stored and retrieved');
135
+ });
136
+
137
+ await this.runTest('Frozen Space Write Protection', async () => {
138
+ const key = 'immutable-data';
139
+
140
+ try {
141
+ // Try to overwrite frozen data in a different space - should fail
142
+ await this.storage.store(key, { modified: 'data' }, { space: 'private' });
143
+ throw new Error('Frozen space allowed overwrite - this should not happen');
144
+ } catch (error) {
145
+ if (error.message.includes('already exists in space') || error.message.includes('Write access denied')) {
146
+ console.log(' โœ“ Frozen space properly protected from overwrites');
147
+ } else {
148
+ throw error;
149
+ }
150
+ }
151
+ });
152
+ }
153
+
154
+ async testSpaceSeparation() {
155
+ await this.runTest('Space Separation Protection', async () => {
156
+ const baseKey = 'protected-key';
157
+
158
+ // Store in private space first
159
+ await this.storage.store(baseKey, { space: 'private', data: 'secret' });
160
+
161
+ try {
162
+ // Try to store same base key in public space - should fail
163
+ await this.storage.store(baseKey, { space: 'public', data: 'public' }, { space: 'public' });
164
+ throw new Error('Space separation failed - allowed cross-space overwrite');
165
+ } catch (error) {
166
+ if (error.message.includes('already exists in space')) {
167
+ console.log(' โœ“ Space separation properly prevents cross-space overwrites');
168
+ } else {
169
+ throw error;
170
+ }
171
+ }
172
+ });
173
+
174
+ await this.runTest('Different Base Keys Work', async () => {
175
+ // These should all work as they have different base keys
176
+ await this.storage.store('key1', { data: 'private' }); // private:key1
177
+ await this.storage.store('key2', { data: 'public' }, { space: 'public' }); // public:key2
178
+ await this.storage.store('key3', { data: 'frozen' }, { space: 'frozen' }); // frozen:key3
179
+
180
+ const private1 = await this.storage.retrieve('key1');
181
+ const public2 = await this.storage.retrieve('public:key2');
182
+ const frozen3 = await this.storage.retrieve('frozen:key3');
183
+
184
+ if (!private1 || !public2 || !frozen3) {
185
+ throw new Error('Different base keys not working properly');
186
+ }
187
+
188
+ console.log(' โœ“ Different base keys work in all spaces');
189
+ });
190
+ }
191
+
192
+ async testSpaceAwareRetrieval() {
193
+ await this.runTest('Space-Aware Retrieval', async () => {
194
+ // Store data in specific spaces
195
+ await this.storage.store('test-key', { space: 'private' }); // private:test-key
196
+ await this.storage.store('other-key', { space: 'public' }, { space: 'public' }); // public:other-key
197
+
198
+ // Test retrieval with and without space prefixes
199
+ const privateData = await this.storage.retrieve('private:test-key');
200
+ const publicData = await this.storage.retrieve('public:other-key');
201
+
202
+ if (!privateData || !publicData) {
203
+ throw new Error('Space-aware retrieval not working');
204
+ }
205
+
206
+ // Test automatic space detection
207
+ const autoPrivate = await this.storage.retrieve('test-key'); // Should find private:test-key
208
+ if (!autoPrivate) {
209
+ throw new Error('Automatic space detection not working');
210
+ }
211
+
212
+ console.log(' โœ“ Space-aware retrieval working correctly');
213
+ });
214
+ }
215
+
216
+ async testDisableSpaceEnforcement() {
217
+ await this.runTest('Disable Space Enforcement', async () => {
218
+ // Disable space enforcement
219
+ this.storage.config.spaceEnforcement = false;
220
+
221
+ try {
222
+ // This should work now
223
+ await this.storage.store('enforcement-test', { data: 'private' });
224
+ await this.storage.store('enforcement-test', { data: 'public' }, { space: 'public' });
225
+
226
+ console.log(' โœ“ Space enforcement can be disabled');
227
+ } finally {
228
+ // Re-enable for other tests
229
+ this.storage.config.spaceEnforcement = true;
230
+ }
231
+ });
232
+ }
233
+
234
+ async runAllTests() {
235
+ console.log('๐Ÿงช Starting Storage Spaces Test Suite');
236
+ console.log('='.repeat(60));
237
+ console.log('');
238
+
239
+ await this.testPrivateSpace();
240
+ await this.testPublicSpace();
241
+ await this.testFrozenSpace();
242
+ await this.testSpaceSeparation();
243
+ await this.testSpaceAwareRetrieval();
244
+ await this.testDisableSpaceEnforcement();
245
+
246
+ this.printSummary();
247
+ }
248
+
249
+ printSummary() {
250
+ console.log('='.repeat(60));
251
+ console.log('๐Ÿ STORAGE SPACES TEST SUMMARY');
252
+ console.log('='.repeat(60));
253
+
254
+ const passed = this.testResults.filter(r => r.status === 'PASS').length;
255
+ const failed = this.testResults.filter(r => r.status === 'FAIL').length;
256
+ const total = this.testResults.length;
257
+
258
+ console.log(`Total Tests: ${total}`);
259
+ console.log(`โœ… Passed: ${passed}`);
260
+ console.log(`โŒ Failed: ${failed}`);
261
+ console.log(`Success Rate: ${((passed / total) * 100).toFixed(1)}%`);
262
+ console.log('');
263
+
264
+ if (failed > 0) {
265
+ console.log('Failed Tests:');
266
+ this.testResults.filter(r => r.status === 'FAIL').forEach(result => {
267
+ console.log(` โŒ ${result.name}: ${result.error}`);
268
+ });
269
+ console.log('');
270
+ }
271
+
272
+ if (passed === total) {
273
+ console.log('๐ŸŽ‰ All tests passed! Storage spaces are working perfectly.');
274
+ } else {
275
+ console.log('โš ๏ธ Some tests failed. Please review the errors above.');
276
+ }
277
+
278
+ console.log('');
279
+ console.log('๐Ÿ“Š Storage Spaces Features Tested:');
280
+ console.log(' โ€ข Private space (encrypted, owner-only access)');
281
+ console.log(' โ€ข Public space (readable by all, owner-writable)');
282
+ console.log(' โ€ข Frozen space (immutable, readable by all)');
283
+ console.log(' โ€ข Cross-space overwrite protection');
284
+ console.log(' โ€ข Space-aware key retrieval');
285
+ console.log(' โ€ข Legacy compatibility');
286
+ console.log(' โ€ข Space enforcement toggle');
287
+ }
288
+
289
+ async cleanup() {
290
+ if (this.mesh) {
291
+ console.log('๐Ÿงน Cleaning up test environment...');
292
+ try {
293
+ await this.storage.clear();
294
+ console.log('โœ… Test data cleared');
295
+ } catch (error) {
296
+ console.warn('โš ๏ธ Failed to clear test data:', error.message);
297
+ }
298
+ }
299
+ }
300
+ }
301
+
302
+ // Main execution
303
+ async function main() {
304
+ const test = new StorageSpacesTest();
305
+
306
+ try {
307
+ await test.init();
308
+ await test.runAllTests();
309
+ } catch (error) {
310
+ console.error('๐Ÿ’ฅ Test suite failed to run:', error);
311
+ } finally {
312
+ await test.cleanup();
313
+ process.exit(0);
314
+ }
315
+ }
316
+
317
+ // Handle graceful shutdown
318
+ process.on('SIGINT', async () => {
319
+ console.log('\n๐Ÿ›‘ Received SIGINT, shutting down gracefully...');
320
+ process.exit(0);
321
+ });
322
+
323
+ process.on('SIGTERM', async () => {
324
+ console.log('\n๐Ÿ›‘ Received SIGTERM, shutting down gracefully...');
325
+ process.exit(0);
326
+ });
327
+
328
+ // Run the test suite
329
+ main().catch(console.error);