holosphere 1.1.11 → 1.1.13

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.
package/test/auth.test.js DELETED
@@ -1,275 +0,0 @@
1
- import HoloSphere from '../holosphere.js';
2
- import { jest } from '@jest/globals';
3
-
4
- // Increase timeout for all tests
5
- jest.setTimeout(30000);
6
-
7
- describe('HoloSphere Authentication and Authorization', () => {
8
- let holoSphere;
9
- let strictHoloSphere;
10
- const testPassword = 'TestPass123!';
11
- const testHolon = 'test-holon';
12
- const testLens = 'test-lens';
13
- const PUBLIC_GLOBAL_TABLE = 'publicTestTable'; // For public global data
14
- const PRIVATE_GLOBAL_TABLE = 'veryPrivateGlobalTable'; // For all private global data tests
15
-
16
- beforeAll(async () => {
17
- holoSphere = new HoloSphere('test-app', false, null);
18
- strictHoloSphere = new HoloSphere('test-app-strict', true, null);
19
- // Wait for initialization
20
- await new Promise(resolve => setTimeout(resolve, 1000));
21
- });
22
-
23
- beforeEach(async () => {
24
- // Clean state before each test
25
- await holoSphere.deleteAll(testHolon, testLens);
26
- await holoSphere.deleteAllGlobal(PUBLIC_GLOBAL_TABLE);
27
- await holoSphere.deleteAllGlobal(PRIVATE_GLOBAL_TABLE, testPassword);
28
- await new Promise(resolve => setTimeout(resolve, 100));
29
- });
30
-
31
- afterEach(async () => {
32
- // Clean up test data
33
- try {
34
- if (holoSphere) {
35
- await holoSphere.deleteAll(testHolon, testLens);
36
- }
37
- if (strictHoloSphere) {
38
- await strictHoloSphere.deleteAll(testHolon, testLens);
39
- }
40
- } catch (error) {
41
- console.error('Error in afterEach cleanup:', error);
42
- }
43
- });
44
-
45
- afterAll(async () => {
46
- // Clean up all test data
47
- try {
48
- if (holoSphere) {
49
- await holoSphere.deleteAll(testHolon, testLens);
50
- await holoSphere.deleteAllGlobal(PUBLIC_GLOBAL_TABLE);
51
- await holoSphere.deleteAllGlobal(PRIVATE_GLOBAL_TABLE, testPassword);
52
- }
53
- if (strictHoloSphere) {
54
- await strictHoloSphere.deleteAll(testHolon, testLens);
55
- await strictHoloSphere.deleteAllGlobal(PUBLIC_GLOBAL_TABLE);
56
- await strictHoloSphere.deleteAllGlobal(PRIVATE_GLOBAL_TABLE, testPassword);
57
- }
58
- } catch (error) {
59
- console.error('Error during afterAll data cleanup:', error);
60
- }
61
-
62
- // Close HoloSphere instances
63
- try {
64
- if (holoSphere) {
65
- console.log('Closing non-strict HoloSphere instance...');
66
- await holoSphere.close();
67
- console.log('Non-strict HoloSphere instance closed.');
68
- }
69
- if (strictHoloSphere) {
70
- console.log('Closing strict HoloSphere instance...');
71
- await strictHoloSphere.close();
72
- console.log('Strict HoloSphere instance closed.');
73
- }
74
- } catch (error) {
75
- console.error('Error during afterAll close:', error);
76
- }
77
-
78
- // Add a slightly longer, more explicit wait after close calls
79
- console.log('Waiting extra time for cleanup...');
80
- await new Promise(resolve => setTimeout(resolve, 2000));
81
- console.log('Finished afterAll.');
82
- });
83
-
84
- describe('Authentication System', () => {
85
- it('should authenticate with password and handle auth failures', async () => {
86
- const testData = { id: 'test1', value: 'private-data' };
87
-
88
- // Test storing with authentication
89
- await holoSphere.put(testHolon, testLens, testData, testPassword);
90
- // Wait for data to be properly stored
91
- await new Promise(resolve => setTimeout(resolve, 100));
92
-
93
- // Test retrieving with wrong password
94
- const wrongResult = await holoSphere.get(testHolon, testLens, testData.id, 'wrong_password');
95
- expect(wrongResult).toBeNull();
96
-
97
- // Test retrieving with no password
98
- const noPassResult = await holoSphere.get(testHolon, testLens, testData.id);
99
- expect(noPassResult).toBeNull();
100
-
101
- // Test retrieving with correct password
102
- const correctResult = await holoSphere.get(testHolon, testLens, testData.id, testPassword);
103
- expect(correctResult).toEqual(testData); // Use full object comparison
104
- });
105
-
106
- it('should handle authentication errors gracefully', async () => {
107
- const testData = { id: 'test2', value: 'private-data' };
108
-
109
- // Store data with correct password
110
- await new Promise(resolve => setTimeout(resolve, 100)); // Added delay
111
- await holoSphere.put(testHolon, testLens, testData, testPassword);
112
-
113
- // Try to retrieve with wrong password
114
- const result = await holoSphere.get(testHolon, testLens, testData.id, 'wrong_password');
115
- expect(result).toBeNull();
116
- }, 10000);
117
- });
118
-
119
- describe('Schema Validation', () => {
120
- it('should only validate schema in strict mode', async () => {
121
- const schema = {
122
- type: 'object',
123
- properties: {
124
- id: { type: 'string' },
125
- value: { type: 'number' }
126
- },
127
- required: ['id', 'value']
128
- };
129
-
130
- // Set schema for both instances
131
- await holoSphere.setSchema(testLens, schema);
132
- await strictHoloSphere.setSchema(testLens, schema);
133
-
134
- const invalidData = { id: 'invalid' }; // Missing required 'value' field
135
-
136
- // Should work in non-strict mode
137
- await expect(holoSphere.put(testHolon, testLens, invalidData)).resolves.toBeTruthy();
138
-
139
- // Should fail in strict mode
140
- await expect(strictHoloSphere.put(testHolon, testLens, invalidData))
141
- .rejects.toThrow('Schema validation failed');
142
- }, 10000);
143
-
144
- it('should require schema in strict mode', async () => {
145
- const testData = { id: 'test', value: 123 };
146
-
147
- // Should work in non-strict mode without schema
148
- await expect(holoSphere.put(testHolon, 'nonExistentLens', testData)).resolves.toBeTruthy();
149
-
150
- // Delete any existing schema for the lens
151
- await strictHoloSphere.putGlobal('schemas', { id: 'nonExistentLens', schema: null });
152
-
153
- try {
154
- // This should throw an error in strict mode
155
- await strictHoloSphere.put(testHolon, 'nonExistentLens', testData);
156
- // If we get here, the test should fail
157
- expect('Expected an error').toBe('but none was thrown');
158
- } catch (error) {
159
- expect(error.message).toBe('Schema required in strict mode');
160
- }
161
- }, 10000);
162
- });
163
-
164
- describe('Private Data Operations', () => {
165
- it('should store and retrieve private data with password', async () => {
166
- const testData = { id: 'test3', value: 123 };
167
-
168
- await holoSphere.put(testHolon, testLens, testData, testPassword);
169
-
170
- const result = await holoSphere.get(testHolon, testLens, testData.id, testPassword);
171
- expect(result).toEqual(testData);
172
- });
173
-
174
- it('should handle public data access', async () => {
175
- const testData = { id: 'public', value: 456 };
176
-
177
- await holoSphere.put(testHolon, testLens, testData);
178
-
179
- const result = await holoSphere.get(testHolon, testLens, testData.id);
180
- expect(result).toEqual(testData);
181
- });
182
-
183
- it('should prevent unauthorized access to private data', async () => {
184
- const testData = { id: 'private', value: 789 };
185
-
186
- // Store with password
187
- await holoSphere.put(testHolon, testLens, testData, testPassword);
188
-
189
- // Try to retrieve without password
190
- const result = await holoSphere.get(testHolon, testLens, testData.id);
191
- expect(result).toBeNull();
192
- });
193
-
194
- it('should handle deletion of private data', async () => {
195
- const testData = { id: 'delete', value: 101 };
196
-
197
- await holoSphere.put(testHolon, testLens, testData, testPassword);
198
- await holoSphere.delete(testHolon, testLens, testData.id, testPassword);
199
-
200
- const result = await holoSphere.get(testHolon, testLens, testData.id, testPassword);
201
- expect(result).toBeNull();
202
- });
203
-
204
- it('should handle multiple private data versions', async () => {
205
- const versions = [
206
- { id: 'version1', value: 1 },
207
- { id: 'version2', value: 2 },
208
- { id: 'version3', value: 3 }
209
- ];
210
-
211
- // Clean up any existing data first
212
- await holoSphere.deleteAll(testHolon, testLens, testPassword);
213
-
214
- // Store each version
215
- for (const data of versions) {
216
- await holoSphere.put(testHolon, testLens, data, testPassword);
217
- }
218
-
219
- // Wait a bit for data to settle
220
- await new Promise(resolve => setTimeout(resolve, 1000));
221
-
222
- const results = await holoSphere.getAll(testHolon, testLens, testPassword);
223
- expect(results.length).toBe(versions.length);
224
- for (const version of versions) {
225
- expect(results).toContainEqual(expect.objectContaining(version));
226
- }
227
- }, 10000);
228
- });
229
-
230
- describe('Global Data Operations', () => {
231
- it('should handle private global data', async () => {
232
- const testData = { id: 'globalPrivateItem', value: 111 };
233
- await holoSphere.putGlobal(PRIVATE_GLOBAL_TABLE, testData, testPassword);
234
- const result = await holoSphere.getGlobal(PRIVATE_GLOBAL_TABLE, testData.id, testPassword);
235
- expect(result).toEqual(testData);
236
- });
237
-
238
- it('should handle public global data', async () => {
239
- const testData = { id: 'publicGlobalItem', value: 222 };
240
- await holoSphere.putGlobal(PUBLIC_GLOBAL_TABLE, testData);
241
- const result = await holoSphere.getGlobal(PUBLIC_GLOBAL_TABLE, testData.id);
242
- expect(result).toEqual(testData);
243
- });
244
-
245
- it('should handle getAllGlobal with private data', async () => {
246
- const testData = [
247
- { id: 'globalPrivate1', value: 1 },
248
- { id: 'globalPrivate2', value: 2 }
249
- ];
250
-
251
- // Clean up before this specific test (already done in beforeEach, but good for clarity)
252
- // await holoSphere.deleteAllGlobal(PRIVATE_GLOBAL_TABLE, testPassword); // This might be re-enabled later if tests pass without it
253
-
254
- for (const data of testData) {
255
- await holoSphere.putGlobal(PRIVATE_GLOBAL_TABLE, data, testPassword);
256
- }
257
-
258
- await new Promise(resolve => setTimeout(resolve, 1000)); // Settle time
259
-
260
- const results = await holoSphere.getAllGlobal(PRIVATE_GLOBAL_TABLE, testPassword);
261
- expect(results.length).toBe(testData.length);
262
- for (const data of testData) {
263
- expect(results).toContainEqual(expect.objectContaining(data));
264
- }
265
- }, 10000);
266
-
267
- it('should handle deleteGlobal with private data', async () => {
268
- const testData = { id: 'deleteGlobalPrivate', value: 333 };
269
- await holoSphere.putGlobal(PRIVATE_GLOBAL_TABLE, testData, testPassword);
270
- await holoSphere.deleteGlobal(PRIVATE_GLOBAL_TABLE, testData.id, testPassword);
271
- const result = await holoSphere.getGlobal(PRIVATE_GLOBAL_TABLE, testData.id, testPassword);
272
- expect(result).toBeNull();
273
- });
274
- });
275
- });
@@ -1,229 +0,0 @@
1
- import HoloSphere from '../holosphere.js';
2
- import { jest } from '@jest/globals';
3
-
4
- // Configure Jest
5
- jest.setTimeout(30000); // 30 second timeout
6
-
7
- // Utility to wait for GunDB propagation
8
- const waitForGun = (delay = 250) => new Promise(resolve => setTimeout(resolve, delay));
9
-
10
- describe('HoloSphere Deletion Tests', () => {
11
- const testAppName = 'test-app-deletion';
12
- const testHolon = 'testHolonDeletion';
13
- const testLens = 'testLensDeletion';
14
- const testGlobalTable = 'testGlobalTable';
15
- const testPassword = 'testPassword1234';
16
- let holoSphere;
17
-
18
- beforeAll(async () => {
19
- holoSphere = new HoloSphere(testAppName, false, null);
20
- });
21
-
22
- afterAll(async () => {
23
- // Clean up all test data
24
- await holoSphere.deleteAll(testHolon, testLens);
25
- await holoSphere.deleteAllGlobal(testGlobalTable);
26
-
27
- // Close Gun connections
28
- if (holoSphere) {
29
- await holoSphere.close();
30
- }
31
-
32
- // Wait for connections to close
33
- await new Promise(resolve => setTimeout(resolve, 1000));
34
- });
35
-
36
- describe('Basic Deletion', () => {
37
- test('should delete a single item properly', async () => {
38
- // Create test data
39
- const testData = { id: 'delete-test-1', value: 'delete me' };
40
-
41
- // Store data
42
- await holoSphere.put(testHolon, testLens, testData);
43
-
44
- // Verify data exists
45
- const storedData = await holoSphere.get(testHolon, testLens, testData.id);
46
- expect(storedData).toBeDefined();
47
- expect(storedData.value).toBe(testData.value);
48
-
49
- // Delete data
50
- const deleteResult = await holoSphere.delete(testHolon, testLens, testData.id);
51
- expect(deleteResult).toBe(true);
52
-
53
- // Verify data is deleted
54
- const deletedData = await holoSphere.get(testHolon, testLens, testData.id);
55
- expect(deletedData).toBeNull();
56
- });
57
-
58
- test('should delete a node properly', async () => {
59
- // Create test node data
60
- const nodeData = { value: 'node-to-delete' };
61
- const nodeKey = 'test-node-key';
62
-
63
- // Store node
64
- await holoSphere.putNode(testHolon, testLens, { id: nodeKey, value: nodeData });
65
-
66
- // Verify node exists
67
- const storedNode = await holoSphere.getNode(testHolon, testLens, 'value');
68
- expect(storedNode).toBeDefined();
69
-
70
- // Delete node
71
- const deleteResult = await holoSphere.deleteNode(testHolon, testLens, 'value');
72
- expect(deleteResult).toBe(true);
73
-
74
- // Verify node is deleted
75
- const deletedNode = await holoSphere.getNode(testHolon, testLens, 'value');
76
- expect(deletedNode).toBeNull();
77
- });
78
- });
79
-
80
- describe('Bulk Deletion', () => {
81
- test('should delete all items in a lens', async () => {
82
- // Create multiple test items
83
- const items = [
84
- { id: 'bulk-delete-1', value: 'bulk 1' },
85
- { id: 'bulk-delete-2', value: 'bulk 2' },
86
- { id: 'bulk-delete-3', value: 'bulk 3' }
87
- ];
88
-
89
- // Store all items
90
- for (const item of items) {
91
- await holoSphere.put(testHolon, testLens, item);
92
- }
93
-
94
- // Verify items exist
95
- const allItems = await holoSphere.getAll(testHolon, testLens);
96
- expect(allItems.length).toBeGreaterThanOrEqual(items.length);
97
-
98
- // Delete all items
99
- const deleteAllResult = await holoSphere.deleteAll(testHolon, testLens);
100
- expect(deleteAllResult).toBe(true);
101
-
102
- // Verify all items are deleted
103
- const remainingItems = await holoSphere.getAll(testHolon, testLens);
104
- expect(remainingItems.length).toBe(0);
105
- });
106
- });
107
-
108
- describe('Global Table Deletion', () => {
109
- test('should delete global items properly', async () => {
110
- // Create global test data
111
- const globalData = { id: 'global-delete-test', value: 'global delete me' };
112
-
113
- // 1. Store global data
114
- await holoSphere.putGlobal(testGlobalTable, globalData);
115
-
116
- // 2. Wait significantly for put to settle
117
- await waitForGun(1500); // Generous wait after put
118
-
119
- // 3. Delete global data
120
- const deleteResult = await holoSphere.deleteGlobal(testGlobalTable, globalData.id);
121
- expect(deleteResult).toBe(true);
122
-
123
- // 4. Wait for delete to settle
124
- await waitForGun(500); // Wait after delete
125
-
126
- // 5. Verify global data is deleted
127
- const deletedGlobalData = await holoSphere.getGlobal(testGlobalTable, globalData.id);
128
- expect(deletedGlobalData).toBeNull();
129
- });
130
-
131
- test('should delete all global items in a table', async () => {
132
- // Create multiple global test items
133
- const items = [
134
- { id: 'global-bulk-1', value: 'global bulk 1' },
135
- { id: 'global-bulk-2', value: 'global bulk 2' },
136
- { id: 'global-bulk-3', value: 'global bulk 3' }
137
- ];
138
-
139
- // Store all global items
140
- for (const item of items) {
141
- await holoSphere.putGlobal(testGlobalTable, item);
142
- }
143
-
144
- // Verify global items exist
145
- const allGlobalItems = await holoSphere.getAllGlobal(testGlobalTable);
146
- expect(allGlobalItems.length).toBeGreaterThanOrEqual(items.length);
147
-
148
- // Delete all global items
149
- const deleteAllResult = await holoSphere.deleteAllGlobal(testGlobalTable);
150
- expect(deleteAllResult).toBe(true);
151
-
152
- // Verify all global items are deleted
153
- const remainingGlobalItems = await holoSphere.getAllGlobal(testGlobalTable);
154
- expect(remainingGlobalItems.length).toBe(0);
155
- });
156
- });
157
-
158
- describe('Private Data Deletion', () => {
159
- test('should delete private data properly', async () => {
160
- // Create private test data
161
- const privateData = { id: 'private-delete-test', value: 'private delete me' };
162
-
163
- // Store private data
164
- await holoSphere.put(testHolon, testLens, privateData, testPassword);
165
-
166
- // Verify private data exists
167
- const storedPrivateData = await holoSphere.get(testHolon, testLens, privateData.id, testPassword);
168
- expect(storedPrivateData).toBeDefined();
169
- expect(storedPrivateData.value).toBe(privateData.value);
170
-
171
- // Delete private data
172
- const deleteResult = await holoSphere.delete(testHolon, testLens, privateData.id, testPassword);
173
- expect(deleteResult).toBe(true);
174
-
175
- // Verify private data is deleted
176
- const deletedPrivateData = await holoSphere.get(testHolon, testLens, privateData.id, testPassword);
177
- expect(deletedPrivateData).toBeNull();
178
- });
179
-
180
- test('should delete all private items in a lens', async () => {
181
- // Create multiple private test items
182
- const items = [
183
- { id: 'private-bulk-1', value: 'private bulk 1' },
184
- { id: 'private-bulk-2', value: 'private bulk 2' },
185
- { id: 'private-bulk-3', value: 'private bulk 3' }
186
- ];
187
-
188
- // Store all private items
189
- for (const item of items) {
190
- await holoSphere.put(testHolon, testLens, item, testPassword);
191
- }
192
-
193
- // Verify private items exist
194
- const allPrivateItems = await holoSphere.getAll(testHolon, testLens, testPassword);
195
- expect(allPrivateItems.length).toBeGreaterThanOrEqual(items.length);
196
-
197
- // Delete all private items
198
- const deleteAllResult = await holoSphere.deleteAll(testHolon, testLens, testPassword);
199
- expect(deleteAllResult).toBe(true);
200
-
201
- // Verify all private items are deleted
202
- const remainingPrivateItems = await holoSphere.getAll(testHolon, testLens, testPassword);
203
- expect(remainingPrivateItems.length).toBe(0);
204
- });
205
- });
206
-
207
- describe('Edge Cases', () => {
208
- test('should handle deletion of non-existent items gracefully', async () => {
209
- // Try to delete non-existent item
210
- const deleteResult = await holoSphere.delete(testHolon, testLens, 'non-existent-id');
211
- expect(deleteResult).toBe(true); // Gun returns success even for non-existent items
212
-
213
- // Try to delete non-existent global item
214
- const deleteGlobalResult = await holoSphere.deleteGlobal(testGlobalTable, 'non-existent-global-id');
215
- expect(deleteGlobalResult).toBe(true);
216
- });
217
-
218
- test('should handle invalid parameters gracefully', async () => {
219
- // Test with missing parameters
220
- await expect(holoSphere.delete(null, testLens, 'test-id')).rejects.toThrow();
221
- await expect(holoSphere.delete(testHolon, null, 'test-id')).rejects.toThrow();
222
- await expect(holoSphere.delete(testHolon, testLens, null)).rejects.toThrow();
223
-
224
- // Test with missing global parameters
225
- await expect(holoSphere.deleteGlobal(null, 'test-id')).rejects.toThrow();
226
- await expect(holoSphere.deleteGlobal(testGlobalTable, null)).rejects.toThrow();
227
- });
228
- });
229
- });