@massalabs/gossip-sdk 0.0.2-dev.20260128094509 → 0.0.2-dev.20260128111120

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.
Files changed (142) hide show
  1. package/dist/api/messageProtocol/index.d.ts +19 -0
  2. package/dist/api/messageProtocol/index.js +26 -0
  3. package/dist/api/messageProtocol/mock.d.ts +12 -0
  4. package/{src/api/messageProtocol/mock.ts → dist/api/messageProtocol/mock.js} +2 -3
  5. package/dist/api/messageProtocol/rest.d.ts +22 -0
  6. package/dist/api/messageProtocol/rest.js +161 -0
  7. package/dist/api/messageProtocol/types.d.ts +61 -0
  8. package/dist/api/messageProtocol/types.js +6 -0
  9. package/dist/assets/generated/wasm/README.md +281 -0
  10. package/dist/assets/generated/wasm/gossip_wasm.d.ts +498 -0
  11. package/dist/assets/generated/wasm/gossip_wasm.js +1399 -0
  12. package/dist/assets/generated/wasm/gossip_wasm_bg.wasm +0 -0
  13. package/dist/assets/generated/wasm/gossip_wasm_bg.wasm.d.ts +68 -0
  14. package/dist/assets/generated/wasm/package.json +15 -0
  15. package/dist/config/protocol.d.ts +36 -0
  16. package/dist/config/protocol.js +77 -0
  17. package/dist/config/sdk.d.ts +82 -0
  18. package/dist/config/sdk.js +55 -0
  19. package/{src/contacts.ts → dist/contacts.d.ts} +10 -94
  20. package/dist/contacts.js +166 -0
  21. package/dist/core/SdkEventEmitter.d.ts +36 -0
  22. package/dist/core/SdkEventEmitter.js +59 -0
  23. package/dist/core/SdkPolling.d.ts +35 -0
  24. package/dist/core/SdkPolling.js +100 -0
  25. package/{src/core/index.ts → dist/core/index.d.ts} +0 -2
  26. package/dist/core/index.js +5 -0
  27. package/dist/crypto/bip39.d.ts +34 -0
  28. package/dist/crypto/bip39.js +62 -0
  29. package/dist/crypto/encryption.d.ts +37 -0
  30. package/dist/crypto/encryption.js +46 -0
  31. package/dist/db.d.ts +190 -0
  32. package/dist/db.js +311 -0
  33. package/dist/gossipSdk.d.ts +274 -0
  34. package/dist/gossipSdk.js +690 -0
  35. package/dist/index.d.ts +73 -0
  36. package/dist/index.js +77 -0
  37. package/dist/services/announcement.d.ts +43 -0
  38. package/dist/services/announcement.js +491 -0
  39. package/dist/services/auth.d.ts +37 -0
  40. package/dist/services/auth.js +76 -0
  41. package/dist/services/discussion.d.ts +63 -0
  42. package/dist/services/discussion.js +297 -0
  43. package/dist/services/message.d.ts +74 -0
  44. package/dist/services/message.js +826 -0
  45. package/dist/services/refresh.d.ts +41 -0
  46. package/dist/services/refresh.js +205 -0
  47. package/{src/sw.ts → dist/sw.d.ts} +1 -8
  48. package/dist/sw.js +10 -0
  49. package/dist/types/events.d.ts +80 -0
  50. package/dist/types/events.js +7 -0
  51. package/dist/types.d.ts +32 -0
  52. package/dist/types.js +7 -0
  53. package/dist/utils/base64.d.ts +10 -0
  54. package/dist/utils/base64.js +30 -0
  55. package/dist/utils/contacts.d.ts +42 -0
  56. package/dist/utils/contacts.js +113 -0
  57. package/dist/utils/discussions.d.ts +24 -0
  58. package/dist/utils/discussions.js +38 -0
  59. package/dist/utils/logs.d.ts +19 -0
  60. package/dist/utils/logs.js +89 -0
  61. package/dist/utils/messageSerialization.d.ts +64 -0
  62. package/dist/utils/messageSerialization.js +184 -0
  63. package/dist/utils/queue.d.ts +50 -0
  64. package/dist/utils/queue.js +110 -0
  65. package/dist/utils/type.d.ts +10 -0
  66. package/dist/utils/type.js +4 -0
  67. package/dist/utils/userId.d.ts +40 -0
  68. package/dist/utils/userId.js +90 -0
  69. package/dist/utils/validation.d.ts +50 -0
  70. package/dist/utils/validation.js +112 -0
  71. package/dist/utils.d.ts +30 -0
  72. package/{src/utils.ts → dist/utils.js} +9 -19
  73. package/dist/wasm/encryption.d.ts +56 -0
  74. package/{src/wasm/encryption.ts → dist/wasm/encryption.js} +22 -51
  75. package/dist/wasm/index.d.ts +10 -0
  76. package/{src/wasm/index.ts → dist/wasm/index.js} +1 -8
  77. package/dist/wasm/loader.d.ts +21 -0
  78. package/dist/wasm/loader.js +103 -0
  79. package/dist/wasm/session.d.ts +85 -0
  80. package/dist/wasm/session.js +226 -0
  81. package/dist/wasm/userKeys.d.ts +17 -0
  82. package/{src/wasm/userKeys.ts → dist/wasm/userKeys.js} +6 -13
  83. package/package.json +5 -1
  84. package/src/api/messageProtocol/index.ts +0 -53
  85. package/src/api/messageProtocol/rest.ts +0 -209
  86. package/src/api/messageProtocol/types.ts +0 -70
  87. package/src/config/protocol.ts +0 -97
  88. package/src/config/sdk.ts +0 -131
  89. package/src/core/SdkEventEmitter.ts +0 -91
  90. package/src/core/SdkPolling.ts +0 -134
  91. package/src/crypto/bip39.ts +0 -84
  92. package/src/crypto/encryption.ts +0 -77
  93. package/src/db.ts +0 -465
  94. package/src/gossipSdk.ts +0 -994
  95. package/src/index.ts +0 -211
  96. package/src/services/announcement.ts +0 -653
  97. package/src/services/auth.ts +0 -95
  98. package/src/services/discussion.ts +0 -380
  99. package/src/services/message.ts +0 -1055
  100. package/src/services/refresh.ts +0 -234
  101. package/src/types/events.ts +0 -108
  102. package/src/types.ts +0 -70
  103. package/src/utils/base64.ts +0 -39
  104. package/src/utils/contacts.ts +0 -161
  105. package/src/utils/discussions.ts +0 -55
  106. package/src/utils/logs.ts +0 -86
  107. package/src/utils/messageSerialization.ts +0 -257
  108. package/src/utils/queue.ts +0 -106
  109. package/src/utils/type.ts +0 -7
  110. package/src/utils/userId.ts +0 -114
  111. package/src/utils/validation.ts +0 -144
  112. package/src/wasm/loader.ts +0 -123
  113. package/src/wasm/session.ts +0 -276
  114. package/test/config/protocol.spec.ts +0 -31
  115. package/test/config/sdk.spec.ts +0 -163
  116. package/test/db/helpers.spec.ts +0 -142
  117. package/test/db/operations.spec.ts +0 -128
  118. package/test/db/states.spec.ts +0 -535
  119. package/test/integration/discussion-flow.spec.ts +0 -422
  120. package/test/integration/messaging-flow.spec.ts +0 -708
  121. package/test/integration/sdk-lifecycle.spec.ts +0 -325
  122. package/test/mocks/index.ts +0 -9
  123. package/test/mocks/mockMessageProtocol.ts +0 -100
  124. package/test/services/auth.spec.ts +0 -311
  125. package/test/services/discussion.spec.ts +0 -279
  126. package/test/services/message-deduplication.spec.ts +0 -299
  127. package/test/services/message-startup.spec.ts +0 -331
  128. package/test/services/message.spec.ts +0 -817
  129. package/test/services/refresh.spec.ts +0 -199
  130. package/test/services/session-status.spec.ts +0 -349
  131. package/test/session/wasm.spec.ts +0 -227
  132. package/test/setup.ts +0 -52
  133. package/test/utils/contacts.spec.ts +0 -156
  134. package/test/utils/discussions.spec.ts +0 -66
  135. package/test/utils/queue.spec.ts +0 -52
  136. package/test/utils/serialization.spec.ts +0 -120
  137. package/test/utils/userId.spec.ts +0 -120
  138. package/test/utils/validation.spec.ts +0 -223
  139. package/test/utils.ts +0 -212
  140. package/tsconfig.json +0 -26
  141. package/tsconfig.tsbuildinfo +0 -1
  142. package/vitest.config.ts +0 -28
@@ -1,311 +0,0 @@
1
- /**
2
- * AuthService tests
3
- */
4
-
5
- import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
6
- import {
7
- AuthService,
8
- getPublicKeyErrorMessage,
9
- PUBLIC_KEY_NOT_FOUND_ERROR,
10
- PUBLIC_KEY_NOT_FOUND_MESSAGE,
11
- FAILED_TO_FETCH_ERROR,
12
- FAILED_TO_FETCH_MESSAGE,
13
- FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR,
14
- } from '../../src/services/auth';
15
- import { db, UserProfile } from '../../src/db';
16
- import type { IMessageProtocol } from '../../src/api/messageProtocol/types';
17
- import {
18
- UserPublicKeys,
19
- UserKeys,
20
- generate_user_keys,
21
- } from '../../src/assets/generated/wasm/gossip_wasm';
22
- import { encodeUserId } from '../../src/utils/userId';
23
- import { encodeToBase64, decodeFromBase64 } from '../../src/utils/base64';
24
- import { ensureWasmInitialized } from '../../src/wasm';
25
-
26
- function createMockProtocol(
27
- overrides: Partial<IMessageProtocol> = {}
28
- ): IMessageProtocol {
29
- return {
30
- fetchMessages: vi.fn().mockResolvedValue([]),
31
- sendMessage: vi.fn().mockResolvedValue(undefined),
32
- sendAnnouncement: vi.fn().mockResolvedValue('1'),
33
- fetchAnnouncements: vi.fn().mockResolvedValue([]),
34
- fetchPublicKeyByUserId: vi.fn().mockResolvedValue(''),
35
- postPublicKey: vi.fn().mockResolvedValue('hash'),
36
- changeNode: vi.fn().mockResolvedValue({ success: true }),
37
- ...overrides,
38
- };
39
- }
40
-
41
- function createUserProfile(
42
- userId: string,
43
- overrides: Partial<UserProfile> = {}
44
- ): UserProfile {
45
- return {
46
- userId,
47
- username: 'testuser',
48
- security: {
49
- encKeySalt: new Uint8Array(32),
50
- authMethod: 'password' as const,
51
- mnemonicBackup: {
52
- encryptedMnemonic: new Uint8Array(64),
53
- createdAt: new Date(),
54
- backedUp: false,
55
- },
56
- },
57
- session: new Uint8Array(32),
58
- status: 'online' as const,
59
- lastSeen: new Date(),
60
- createdAt: new Date(),
61
- updatedAt: new Date(),
62
- ...overrides,
63
- };
64
- }
65
-
66
- describe('getPublicKeyErrorMessage', () => {
67
- it('should return specific message for "Public key not found" error', () => {
68
- const error = new Error(PUBLIC_KEY_NOT_FOUND_ERROR);
69
- const result = getPublicKeyErrorMessage(error);
70
- expect(result).toBe(PUBLIC_KEY_NOT_FOUND_MESSAGE);
71
- });
72
-
73
- it('should return specific message for "Failed to fetch" error', () => {
74
- const error = new Error(FAILED_TO_FETCH_ERROR);
75
- const result = getPublicKeyErrorMessage(error);
76
- expect(result).toBe(FAILED_TO_FETCH_MESSAGE);
77
- });
78
-
79
- it('should return generic message with error details for other errors', () => {
80
- const error = new Error('Network timeout');
81
- const result = getPublicKeyErrorMessage(error);
82
- expect(result).toContain(FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR);
83
- expect(result).toContain('Network timeout');
84
- });
85
-
86
- it('should handle non-Error objects', () => {
87
- const error = 'String error';
88
- const result = getPublicKeyErrorMessage(error);
89
- expect(result).toContain(FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR);
90
- expect(result).toContain('String error');
91
- });
92
-
93
- it('should handle null/undefined gracefully', () => {
94
- const result1 = getPublicKeyErrorMessage(null);
95
- const result2 = getPublicKeyErrorMessage(undefined);
96
- expect(result1).toContain(FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR);
97
- expect(result2).toContain(FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR);
98
- });
99
- });
100
-
101
- describe('AuthService', () => {
102
- let mockMessageProtocol: IMessageProtocol;
103
- let authService: AuthService;
104
- let testUserId: string;
105
- let testUserIdBytes: Uint8Array;
106
- let testPublicKeys: UserPublicKeys;
107
- let userKeys: UserKeys | null = null;
108
-
109
- beforeEach(async () => {
110
- await ensureWasmInitialized();
111
-
112
- if (!db.isOpen()) {
113
- await db.open();
114
- }
115
- await Promise.all(db.tables.map(table => table.clear()));
116
-
117
- testUserIdBytes = new Uint8Array(32).fill(42);
118
- testUserId = encodeUserId(testUserIdBytes);
119
-
120
- userKeys = generate_user_keys('test-passphrase-' + Date.now());
121
- testPublicKeys = userKeys.public_keys();
122
-
123
- mockMessageProtocol = createMockProtocol();
124
- authService = new AuthService(db, mockMessageProtocol);
125
- });
126
-
127
- afterEach(async () => {
128
- if (testPublicKeys) {
129
- testPublicKeys.free();
130
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
131
- testPublicKeys = null as any;
132
- }
133
- if (userKeys) {
134
- userKeys.free();
135
- userKeys = null;
136
- }
137
- vi.clearAllMocks();
138
- });
139
-
140
- describe('fetchPublicKeyByUserId', () => {
141
- it('should successfully fetch and decode public key', async () => {
142
- const publicKeyBytes = testPublicKeys.to_bytes();
143
- const base64PublicKey = encodeToBase64(publicKeyBytes);
144
-
145
- vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId).mockResolvedValue(
146
- base64PublicKey
147
- );
148
-
149
- const result = await authService.fetchPublicKeyByUserId(testUserId);
150
-
151
- expect(result).toHaveProperty('publicKey');
152
- expect(result).not.toHaveProperty('error');
153
- expect(result.publicKey).toBeInstanceOf(UserPublicKeys);
154
- expect(mockMessageProtocol.fetchPublicKeyByUserId).toHaveBeenCalledWith(
155
- testUserIdBytes
156
- );
157
- });
158
-
159
- it('should return error when public key is not found', async () => {
160
- const error = new Error(PUBLIC_KEY_NOT_FOUND_ERROR);
161
- vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId).mockRejectedValue(
162
- error
163
- );
164
-
165
- const result = await authService.fetchPublicKeyByUserId(testUserId);
166
-
167
- expect(result).toHaveProperty('error');
168
- expect(result).not.toHaveProperty('publicKey');
169
- expect(result.error).toBe(PUBLIC_KEY_NOT_FOUND_MESSAGE);
170
- });
171
-
172
- it('should return error when fetch fails', async () => {
173
- const error = new Error(FAILED_TO_FETCH_ERROR);
174
- vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId).mockRejectedValue(
175
- error
176
- );
177
-
178
- const result = await authService.fetchPublicKeyByUserId(testUserId);
179
-
180
- expect(result).toHaveProperty('error');
181
- expect(result.error).toBe(FAILED_TO_FETCH_MESSAGE);
182
- });
183
-
184
- it('should return error for network errors', async () => {
185
- const error = new Error('Network timeout');
186
- vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId).mockRejectedValue(
187
- error
188
- );
189
-
190
- const result = await authService.fetchPublicKeyByUserId(testUserId);
191
-
192
- expect(result).toHaveProperty('error');
193
- expect(result.error).toContain(
194
- FAILED_TO_RETRIEVE_CONTACT_PUBLIC_KEY_ERROR
195
- );
196
- expect(result.error).toContain('Network timeout');
197
- });
198
-
199
- it('should decode userId correctly before fetching', async () => {
200
- const publicKeyBytes = testPublicKeys.to_bytes();
201
- const base64PublicKey = encodeToBase64(publicKeyBytes);
202
-
203
- vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId).mockResolvedValue(
204
- base64PublicKey
205
- );
206
-
207
- await authService.fetchPublicKeyByUserId(testUserId);
208
-
209
- expect(mockMessageProtocol.fetchPublicKeyByUserId).toHaveBeenCalledTimes(
210
- 1
211
- );
212
- const calledWith = vi.mocked(mockMessageProtocol.fetchPublicKeyByUserId)
213
- .mock.calls[0][0];
214
- expect(Array.from(calledWith)).toEqual(Array.from(testUserIdBytes));
215
- });
216
- });
217
-
218
- describe('ensurePublicKeyPublished', () => {
219
- it('should not publish if last push was less than one week ago', async () => {
220
- const profile = createUserProfile(testUserId, {
221
- lastPublicKeyPush: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
222
- });
223
-
224
- await db.userProfile.add(profile);
225
-
226
- await authService.ensurePublicKeyPublished(testPublicKeys, testUserId);
227
-
228
- expect(mockMessageProtocol.postPublicKey).not.toHaveBeenCalled();
229
- });
230
-
231
- it('should publish if last push was more than one week ago', async () => {
232
- const profile = createUserProfile(testUserId, {
233
- lastPublicKeyPush: new Date(Date.now() - 8 * 24 * 60 * 60 * 1000),
234
- });
235
-
236
- await db.userProfile.add(profile);
237
-
238
- vi.mocked(mockMessageProtocol.postPublicKey).mockResolvedValue('hash123');
239
-
240
- await authService.ensurePublicKeyPublished(testPublicKeys, testUserId);
241
-
242
- expect(mockMessageProtocol.postPublicKey).toHaveBeenCalledTimes(1);
243
- const calledWith = vi.mocked(mockMessageProtocol.postPublicKey).mock
244
- .calls[0][0];
245
- expect(calledWith).toBe(encodeToBase64(testPublicKeys.to_bytes()));
246
-
247
- const updatedProfile = await db.userProfile.get(testUserId);
248
- expect(updatedProfile?.lastPublicKeyPush).toBeDefined();
249
- expect(updatedProfile?.lastPublicKeyPush?.getTime()).toBeGreaterThan(
250
- profile.lastPublicKeyPush!.getTime()
251
- );
252
- });
253
-
254
- it('should publish if lastPublicKeyPush is undefined', async () => {
255
- const profile = createUserProfile(testUserId);
256
-
257
- await db.userProfile.add(profile);
258
-
259
- vi.mocked(mockMessageProtocol.postPublicKey).mockResolvedValue('hash123');
260
-
261
- await authService.ensurePublicKeyPublished(testPublicKeys, testUserId);
262
-
263
- expect(mockMessageProtocol.postPublicKey).toHaveBeenCalledTimes(1);
264
-
265
- const updatedProfile = await db.userProfile.get(testUserId);
266
- expect(updatedProfile?.lastPublicKeyPush).toBeDefined();
267
- });
268
-
269
- it('should publish if last push was exactly one week ago', async () => {
270
- const oneWeekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
271
- const profile = createUserProfile(testUserId, {
272
- lastPublicKeyPush: oneWeekAgo,
273
- });
274
-
275
- await db.userProfile.add(profile);
276
-
277
- vi.mocked(mockMessageProtocol.postPublicKey).mockResolvedValue('hash123');
278
-
279
- await authService.ensurePublicKeyPublished(testPublicKeys, testUserId);
280
-
281
- expect(mockMessageProtocol.postPublicKey).toHaveBeenCalledTimes(1);
282
- });
283
-
284
- it('should throw error if user profile not found', async () => {
285
- await expect(
286
- authService.ensurePublicKeyPublished(testPublicKeys, testUserId)
287
- ).rejects.toThrow('User profile not found');
288
-
289
- expect(mockMessageProtocol.postPublicKey).not.toHaveBeenCalled();
290
- });
291
-
292
- it('should encode public keys to base64 before posting', async () => {
293
- const profile = createUserProfile(testUserId, {
294
- lastPublicKeyPush: new Date(Date.now() - 8 * 24 * 60 * 60 * 1000),
295
- });
296
-
297
- await db.userProfile.add(profile);
298
-
299
- vi.mocked(mockMessageProtocol.postPublicKey).mockResolvedValue('hash123');
300
-
301
- await authService.ensurePublicKeyPublished(testPublicKeys, testUserId);
302
-
303
- const calledWith = vi.mocked(mockMessageProtocol.postPublicKey).mock
304
- .calls[0][0];
305
- const decoded = decodeFromBase64(calledWith);
306
- const originalBytes = testPublicKeys.to_bytes();
307
-
308
- expect(Array.from(decoded)).toEqual(Array.from(originalBytes));
309
- });
310
- });
311
- });
@@ -1,279 +0,0 @@
1
- /**
2
- * DiscussionService tests
3
- *
4
- * Includes message status reset behavior for session renewal.
5
- */
6
-
7
- import { describe, it, expect, beforeEach } from 'vitest';
8
- import {
9
- db,
10
- MessageStatus,
11
- MessageDirection,
12
- MessageType,
13
- DiscussionStatus,
14
- DiscussionDirection,
15
- } from '../../src/db';
16
- import { encodeUserId } from '../../src/utils/userId';
17
-
18
- // ============================================================================
19
- // DiscussionService renew message reset behavior
20
- // ============================================================================
21
-
22
- const RENEW_OWNER_USER_ID = encodeUserId(new Uint8Array(32).fill(11));
23
- const RENEW_CONTACT_USER_ID = encodeUserId(new Uint8Array(32).fill(12));
24
-
25
- async function simulateRenewMessageReset(
26
- ownerUserId: string,
27
- contactUserId: string
28
- ): Promise<number> {
29
- return await db.messages
30
- .where('[ownerUserId+contactUserId]')
31
- .equals([ownerUserId, contactUserId])
32
- .and(
33
- message =>
34
- message.direction === MessageDirection.OUTGOING &&
35
- (message.status === MessageStatus.SENDING ||
36
- message.status === MessageStatus.FAILED ||
37
- message.status === MessageStatus.SENT)
38
- )
39
- .modify({
40
- status: MessageStatus.WAITING_SESSION,
41
- encryptedMessage: undefined,
42
- seeker: undefined,
43
- });
44
- }
45
-
46
- describe('DiscussionService renew message reset behavior', () => {
47
- beforeEach(async () => {
48
- if (!db.isOpen()) {
49
- await db.open();
50
- }
51
- await Promise.all(db.tables.map(table => table.clear()));
52
-
53
- await db.discussions.add({
54
- ownerUserId: RENEW_OWNER_USER_ID,
55
- contactUserId: RENEW_CONTACT_USER_ID,
56
- direction: DiscussionDirection.INITIATED,
57
- status: DiscussionStatus.ACTIVE,
58
- unreadCount: 0,
59
- createdAt: new Date(),
60
- updatedAt: new Date(),
61
- });
62
- });
63
-
64
- it('should reset SENT messages to WAITING_SESSION when renewing session', async () => {
65
- const sentMessageId = await db.messages.add({
66
- ownerUserId: RENEW_OWNER_USER_ID,
67
- contactUserId: RENEW_CONTACT_USER_ID,
68
- content: 'Already sent message',
69
- type: MessageType.TEXT,
70
- direction: MessageDirection.OUTGOING,
71
- status: MessageStatus.SENT,
72
- timestamp: new Date(),
73
- seeker: new Uint8Array(32).fill(1),
74
- encryptedMessage: new Uint8Array(64).fill(2),
75
- });
76
-
77
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
78
-
79
- const sentMessage = await db.messages.get(sentMessageId);
80
- expect(sentMessage?.status).toBe(MessageStatus.WAITING_SESSION);
81
- expect(sentMessage?.seeker).toBeUndefined();
82
- expect(sentMessage?.encryptedMessage).toBeUndefined();
83
- });
84
-
85
- it('should NOT reset DELIVERED messages when renewing session', async () => {
86
- const deliveredMessageId = await db.messages.add({
87
- ownerUserId: RENEW_OWNER_USER_ID,
88
- contactUserId: RENEW_CONTACT_USER_ID,
89
- content: 'Delivered message',
90
- type: MessageType.TEXT,
91
- direction: MessageDirection.OUTGOING,
92
- status: MessageStatus.DELIVERED,
93
- timestamp: new Date(),
94
- seeker: new Uint8Array(32).fill(3),
95
- encryptedMessage: new Uint8Array(64).fill(4),
96
- });
97
-
98
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
99
-
100
- const deliveredMessage = await db.messages.get(deliveredMessageId);
101
- expect(deliveredMessage?.status).toBe(MessageStatus.DELIVERED);
102
- expect(deliveredMessage?.seeker).toBeDefined();
103
- expect(deliveredMessage?.encryptedMessage).toBeDefined();
104
- });
105
-
106
- it('should NOT reset READ messages when renewing session', async () => {
107
- const readMessageId = await db.messages.add({
108
- ownerUserId: RENEW_OWNER_USER_ID,
109
- contactUserId: RENEW_CONTACT_USER_ID,
110
- content: 'Read message',
111
- type: MessageType.TEXT,
112
- direction: MessageDirection.OUTGOING,
113
- status: MessageStatus.READ,
114
- timestamp: new Date(),
115
- seeker: new Uint8Array(32).fill(5),
116
- encryptedMessage: new Uint8Array(64).fill(6),
117
- });
118
-
119
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
120
-
121
- const readMessage = await db.messages.get(readMessageId);
122
- expect(readMessage?.status).toBe(MessageStatus.READ);
123
- expect(readMessage?.seeker).toBeDefined();
124
- });
125
-
126
- it('should reset SENDING messages to WAITING_SESSION when renewing', async () => {
127
- const sendingMessageId = await db.messages.add({
128
- ownerUserId: RENEW_OWNER_USER_ID,
129
- contactUserId: RENEW_CONTACT_USER_ID,
130
- content: 'Sending message',
131
- type: MessageType.TEXT,
132
- direction: MessageDirection.OUTGOING,
133
- status: MessageStatus.SENDING,
134
- timestamp: new Date(),
135
- seeker: new Uint8Array(32).fill(5),
136
- encryptedMessage: new Uint8Array(64).fill(6),
137
- });
138
-
139
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
140
-
141
- const sendingMessage = await db.messages.get(sendingMessageId);
142
- expect(sendingMessage?.status).toBe(MessageStatus.WAITING_SESSION);
143
- expect(sendingMessage?.seeker).toBeUndefined();
144
- expect(sendingMessage?.encryptedMessage).toBeUndefined();
145
- });
146
-
147
- it('should reset FAILED messages to WAITING_SESSION when renewing', async () => {
148
- const failedMessageId = await db.messages.add({
149
- ownerUserId: RENEW_OWNER_USER_ID,
150
- contactUserId: RENEW_CONTACT_USER_ID,
151
- content: 'Failed message',
152
- type: MessageType.TEXT,
153
- direction: MessageDirection.OUTGOING,
154
- status: MessageStatus.FAILED,
155
- timestamp: new Date(),
156
- seeker: new Uint8Array(32).fill(7),
157
- encryptedMessage: new Uint8Array(64).fill(8),
158
- });
159
-
160
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
161
-
162
- const failedMessage = await db.messages.get(failedMessageId);
163
- expect(failedMessage?.status).toBe(MessageStatus.WAITING_SESSION);
164
- expect(failedMessage?.seeker).toBeUndefined();
165
- expect(failedMessage?.encryptedMessage).toBeUndefined();
166
- });
167
-
168
- it('should keep WAITING_SESSION messages unchanged when renewing', async () => {
169
- const waitingMessageId = await db.messages.add({
170
- ownerUserId: RENEW_OWNER_USER_ID,
171
- contactUserId: RENEW_CONTACT_USER_ID,
172
- content: 'Waiting message',
173
- type: MessageType.TEXT,
174
- direction: MessageDirection.OUTGOING,
175
- status: MessageStatus.WAITING_SESSION,
176
- timestamp: new Date(),
177
- });
178
-
179
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
180
-
181
- const waitingMessage = await db.messages.get(waitingMessageId);
182
- expect(waitingMessage?.status).toBe(MessageStatus.WAITING_SESSION);
183
- });
184
-
185
- it('should handle mixed message statuses correctly on renew', async () => {
186
- const sentId = await db.messages.add({
187
- ownerUserId: RENEW_OWNER_USER_ID,
188
- contactUserId: RENEW_CONTACT_USER_ID,
189
- content: 'Sent',
190
- type: MessageType.TEXT,
191
- direction: MessageDirection.OUTGOING,
192
- status: MessageStatus.SENT,
193
- timestamp: new Date(),
194
- seeker: new Uint8Array(32).fill(1),
195
- encryptedMessage: new Uint8Array(64).fill(1),
196
- });
197
-
198
- const sendingId = await db.messages.add({
199
- ownerUserId: RENEW_OWNER_USER_ID,
200
- contactUserId: RENEW_CONTACT_USER_ID,
201
- content: 'Sending',
202
- type: MessageType.TEXT,
203
- direction: MessageDirection.OUTGOING,
204
- status: MessageStatus.SENDING,
205
- timestamp: new Date(),
206
- seeker: new Uint8Array(32).fill(2),
207
- encryptedMessage: new Uint8Array(64).fill(2),
208
- });
209
-
210
- const failedId = await db.messages.add({
211
- ownerUserId: RENEW_OWNER_USER_ID,
212
- contactUserId: RENEW_CONTACT_USER_ID,
213
- content: 'Failed',
214
- type: MessageType.TEXT,
215
- direction: MessageDirection.OUTGOING,
216
- status: MessageStatus.FAILED,
217
- timestamp: new Date(),
218
- });
219
-
220
- const deliveredId = await db.messages.add({
221
- ownerUserId: RENEW_OWNER_USER_ID,
222
- contactUserId: RENEW_CONTACT_USER_ID,
223
- content: 'Delivered',
224
- type: MessageType.TEXT,
225
- direction: MessageDirection.OUTGOING,
226
- status: MessageStatus.DELIVERED,
227
- timestamp: new Date(),
228
- seeker: new Uint8Array(32).fill(3),
229
- encryptedMessage: new Uint8Array(64).fill(3),
230
- });
231
-
232
- const waitingId = await db.messages.add({
233
- ownerUserId: RENEW_OWNER_USER_ID,
234
- contactUserId: RENEW_CONTACT_USER_ID,
235
- content: 'Waiting',
236
- type: MessageType.TEXT,
237
- direction: MessageDirection.OUTGOING,
238
- status: MessageStatus.WAITING_SESSION,
239
- timestamp: new Date(),
240
- });
241
-
242
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
243
-
244
- const sent = await db.messages.get(sentId);
245
- expect(sent?.status).toBe(MessageStatus.WAITING_SESSION);
246
- expect(sent?.seeker).toBeUndefined();
247
-
248
- const sending = await db.messages.get(sendingId);
249
- expect(sending?.status).toBe(MessageStatus.WAITING_SESSION);
250
- expect(sending?.seeker).toBeUndefined();
251
-
252
- const failed = await db.messages.get(failedId);
253
- expect(failed?.status).toBe(MessageStatus.WAITING_SESSION);
254
-
255
- const delivered = await db.messages.get(deliveredId);
256
- expect(delivered?.status).toBe(MessageStatus.DELIVERED);
257
- expect(delivered?.seeker).toBeDefined();
258
-
259
- const waiting = await db.messages.get(waitingId);
260
- expect(waiting?.status).toBe(MessageStatus.WAITING_SESSION);
261
- });
262
-
263
- it('should NOT reset incoming messages when renewing', async () => {
264
- const incomingId = await db.messages.add({
265
- ownerUserId: RENEW_OWNER_USER_ID,
266
- contactUserId: RENEW_CONTACT_USER_ID,
267
- content: 'Incoming message',
268
- type: MessageType.TEXT,
269
- direction: MessageDirection.INCOMING,
270
- status: MessageStatus.DELIVERED,
271
- timestamp: new Date(),
272
- });
273
-
274
- await simulateRenewMessageReset(RENEW_OWNER_USER_ID, RENEW_CONTACT_USER_ID);
275
-
276
- const incoming = await db.messages.get(incomingId);
277
- expect(incoming?.status).toBe(MessageStatus.DELIVERED);
278
- });
279
- });