| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  | // Copyright 2024 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { assert } from 'chai'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  | import type { WritableDB } from '../../sql/Interface'; | 
					
						
							|  |  |  | import { migrateConversationMessages, setupTests } from '../../sql/Server'; | 
					
						
							|  |  |  | import { createDB, insertData, getTableData } from './helpers'; | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('SQL/migrateConversationMessages', () => { | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |   let db: WritableDB; | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |     db = createDB(); | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  |     setupTests(db); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterEach(() => { | 
					
						
							|  |  |  |     db.close(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function compactify( | 
					
						
							|  |  |  |     message: Record<string, unknown> | 
					
						
							|  |  |  |   ): Record<string, unknown> { | 
					
						
							|  |  |  |     const { id, conversationId, json } = message; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       id, | 
					
						
							|  |  |  |       conversationId, | 
					
						
							|  |  |  |       json, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |   it('should leave irrelevant messages intact', () => { | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  |     insertData(db, 'messages', [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         id: 'irrelevant', | 
					
						
							|  |  |  |         conversationId: 'other', | 
					
						
							|  |  |  |         json: { | 
					
						
							|  |  |  |           conversationId: 'other', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |     migrateConversationMessages(db, 'obsolete', 'current'); | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert.deepStrictEqual(getTableData(db, 'messages').map(compactify), [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         id: 'irrelevant', | 
					
						
							|  |  |  |         conversationId: 'other', | 
					
						
							|  |  |  |         json: { | 
					
						
							|  |  |  |           conversationId: 'other', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |   it('should update conversationId and send state', () => { | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  |     insertData(db, 'messages', [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         id: 'no-send-state', | 
					
						
							|  |  |  |         conversationId: 'obsolete', | 
					
						
							|  |  |  |         json: { | 
					
						
							|  |  |  |           conversationId: 'obsolete', | 
					
						
							|  |  |  |           body: 'test', | 
					
						
							|  |  |  |           sendStateByConversationId: { | 
					
						
							|  |  |  |             other: 'Failed', | 
					
						
							|  |  |  |             obsolete: 'Read', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           editHistory: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               body: 'test2', | 
					
						
							|  |  |  |               sendStateByConversationId: { | 
					
						
							|  |  |  |                 other: 'Failed', | 
					
						
							|  |  |  |                 obsolete: 'Read', | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-22 11:16:33 -07:00
										 |  |  |     migrateConversationMessages(db, 'obsolete', 'current'); | 
					
						
							| 
									
										
										
										
											2024-04-17 09:00:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert.deepStrictEqual(getTableData(db, 'messages').map(compactify), [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         id: 'no-send-state', | 
					
						
							|  |  |  |         conversationId: 'current', | 
					
						
							|  |  |  |         json: { | 
					
						
							|  |  |  |           body: 'test', | 
					
						
							|  |  |  |           conversationId: 'current', | 
					
						
							|  |  |  |           sendStateByConversationId: { | 
					
						
							|  |  |  |             other: 'Failed', | 
					
						
							|  |  |  |             current: 'Read', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           editHistory: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               body: 'test2', | 
					
						
							|  |  |  |               sendStateByConversationId: { | 
					
						
							|  |  |  |                 other: 'Failed', | 
					
						
							|  |  |  |                 current: 'Read', | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ]); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |