|
1 | | -import { persistMessage } from '../database/chatRepository'; |
| 1 | +import { forkChat, persistMessage } from '../database/chatRepository'; |
2 | 2 | import type { SQLiteDatabase } from 'expo-sqlite'; |
3 | 3 |
|
| 4 | +type TransactionCallback = Parameters< |
| 5 | + SQLiteDatabase['withTransactionAsync'] |
| 6 | +>[0]; |
| 7 | + |
4 | 8 | jest.mock('expo-sqlite', () => ({ |
5 | 9 | useSQLiteContext: jest.fn(() => ({})), |
6 | 10 | })); |
@@ -67,3 +71,236 @@ describe('persistMessage with imagePath', () => { |
67 | 71 | ); |
68 | 72 | }); |
69 | 73 | }); |
| 74 | + |
| 75 | +describe('forkChat', () => { |
| 76 | + it('creates a branch chat and copies messages up to the target message', async () => { |
| 77 | + const runAsync = jest |
| 78 | + .fn() |
| 79 | + .mockResolvedValueOnce({ lastInsertRowId: 10 }) |
| 80 | + .mockResolvedValueOnce({ lastInsertRowId: 101 }) |
| 81 | + .mockResolvedValueOnce({ lastInsertRowId: 102 }) |
| 82 | + .mockResolvedValue({ lastInsertRowId: 0 }); |
| 83 | + const getFirstAsync = jest.fn().mockResolvedValue({ |
| 84 | + id: 1, |
| 85 | + title: 'Original', |
| 86 | + modelId: 7, |
| 87 | + lastUsed: 1, |
| 88 | + }); |
| 89 | + const getAllAsync = jest |
| 90 | + .fn() |
| 91 | + .mockResolvedValueOnce([ |
| 92 | + { |
| 93 | + id: 1, |
| 94 | + chatId: 1, |
| 95 | + role: 'user', |
| 96 | + content: 'one', |
| 97 | + timestamp: 100, |
| 98 | + }, |
| 99 | + { |
| 100 | + id: 2, |
| 101 | + chatId: 1, |
| 102 | + role: 'assistant', |
| 103 | + content: 'two', |
| 104 | + timestamp: 200, |
| 105 | + modelName: 'model', |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 3, |
| 109 | + chatId: 1, |
| 110 | + role: 'user', |
| 111 | + content: 'three', |
| 112 | + timestamp: 300, |
| 113 | + }, |
| 114 | + ]) |
| 115 | + .mockResolvedValueOnce([]); |
| 116 | + const mockDb = { |
| 117 | + runAsync, |
| 118 | + getFirstAsync, |
| 119 | + getAllAsync, |
| 120 | + withTransactionAsync: async (callback: TransactionCallback) => callback(), |
| 121 | + } as SQLiteDatabase; |
| 122 | + |
| 123 | + const newChatId = await forkChat(mockDb, 1, 2); |
| 124 | + |
| 125 | + expect(newChatId).toBe(10); |
| 126 | + expect(runAsync).toHaveBeenCalledWith( |
| 127 | + `INSERT INTO chats (title, modelId, lastUsed) VALUES (?, ?, ?)`, |
| 128 | + ['Original', 7, expect.any(Number)] |
| 129 | + ); |
| 130 | + expect(runAsync).toHaveBeenCalledWith( |
| 131 | + expect.stringContaining('INSERT INTO messages'), |
| 132 | + [10, 'user', 'one', 100, '', 0, 0, null, null] |
| 133 | + ); |
| 134 | + expect(runAsync).toHaveBeenCalledWith( |
| 135 | + expect.stringContaining('INSERT INTO messages'), |
| 136 | + [10, 'assistant', 'two', 200, 'model', 0, 0, null, null] |
| 137 | + ); |
| 138 | + expect(runAsync).toHaveBeenCalledWith( |
| 139 | + expect.stringContaining('INSERT INTO chatBranches'), |
| 140 | + [10, 102, 1, 2, 'Original', 'two'] |
| 141 | + ); |
| 142 | + expect(runAsync).toHaveBeenCalledWith( |
| 143 | + expect.stringContaining('INSERT INTO chatSettings'), |
| 144 | + [10, 1] |
| 145 | + ); |
| 146 | + expect(runAsync).toHaveBeenCalledWith( |
| 147 | + expect.stringContaining('INSERT INTO chatSources'), |
| 148 | + [10, 1] |
| 149 | + ); |
| 150 | + }); |
| 151 | + |
| 152 | + it('throws when the target message is not in the original chat', async () => { |
| 153 | + const mockDb = { |
| 154 | + runAsync: jest.fn(), |
| 155 | + getFirstAsync: jest.fn().mockResolvedValue({ |
| 156 | + id: 1, |
| 157 | + title: 'Original', |
| 158 | + modelId: 7, |
| 159 | + lastUsed: 1, |
| 160 | + }), |
| 161 | + getAllAsync: jest.fn().mockResolvedValueOnce([ |
| 162 | + { |
| 163 | + id: 1, |
| 164 | + chatId: 1, |
| 165 | + role: 'user', |
| 166 | + content: 'one', |
| 167 | + timestamp: 100, |
| 168 | + }, |
| 169 | + ]), |
| 170 | + withTransactionAsync: async (callback: TransactionCallback) => callback(), |
| 171 | + } as SQLiteDatabase; |
| 172 | + |
| 173 | + await expect(forkChat(mockDb, 1, 999)).rejects.toThrow( |
| 174 | + 'Message 999 not found in chat 1' |
| 175 | + ); |
| 176 | + expect(mockDb.runAsync).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + |
| 179 | + it('preserves existing branch markers when forking from a branch', async () => { |
| 180 | + const runAsync = jest |
| 181 | + .fn() |
| 182 | + .mockResolvedValueOnce({ lastInsertRowId: 10 }) |
| 183 | + .mockResolvedValueOnce({ lastInsertRowId: 101 }) |
| 184 | + .mockResolvedValueOnce({ lastInsertRowId: 102 }) |
| 185 | + .mockResolvedValueOnce({ lastInsertRowId: 103 }) |
| 186 | + .mockResolvedValue({ lastInsertRowId: 0 }); |
| 187 | + const mockDb = { |
| 188 | + runAsync, |
| 189 | + getFirstAsync: jest.fn().mockResolvedValue({ |
| 190 | + id: 1, |
| 191 | + title: 'Fork 1', |
| 192 | + modelId: 7, |
| 193 | + lastUsed: 1, |
| 194 | + }), |
| 195 | + getAllAsync: jest |
| 196 | + .fn() |
| 197 | + .mockResolvedValueOnce([ |
| 198 | + { |
| 199 | + id: 1, |
| 200 | + chatId: 1, |
| 201 | + role: 'user', |
| 202 | + content: 'one', |
| 203 | + timestamp: 100, |
| 204 | + }, |
| 205 | + { |
| 206 | + id: 2, |
| 207 | + chatId: 1, |
| 208 | + role: 'assistant', |
| 209 | + content: 'two', |
| 210 | + timestamp: 200, |
| 211 | + }, |
| 212 | + { |
| 213 | + id: 3, |
| 214 | + chatId: 1, |
| 215 | + role: 'assistant', |
| 216 | + content: 'three', |
| 217 | + timestamp: 300, |
| 218 | + }, |
| 219 | + ]) |
| 220 | + .mockResolvedValueOnce([ |
| 221 | + { |
| 222 | + id: 1, |
| 223 | + chatId: 1, |
| 224 | + afterMessageId: 2, |
| 225 | + sourceChatId: 8, |
| 226 | + sourceMessageId: 20, |
| 227 | + sourceChatTitle: 'Original', |
| 228 | + sourceMessagePreview: 'two', |
| 229 | + createdAt: 1, |
| 230 | + }, |
| 231 | + ]), |
| 232 | + withTransactionAsync: async (callback: TransactionCallback) => callback(), |
| 233 | + } as SQLiteDatabase; |
| 234 | + |
| 235 | + await forkChat(mockDb, 1, 3); |
| 236 | + |
| 237 | + expect(runAsync).toHaveBeenCalledWith( |
| 238 | + expect.stringContaining('INSERT INTO chatBranches'), |
| 239 | + [10, 102, 8, 20, 'Original', 'two'] |
| 240 | + ); |
| 241 | + expect(runAsync).toHaveBeenCalledWith( |
| 242 | + expect.stringContaining('INSERT INTO chatBranches'), |
| 243 | + [10, 103, 1, 3, 'Fork 1', 'three'] |
| 244 | + ); |
| 245 | + }); |
| 246 | + |
| 247 | + it('replaces an existing branch marker at the target message with the latest branch marker', async () => { |
| 248 | + const runAsync = jest |
| 249 | + .fn() |
| 250 | + .mockResolvedValueOnce({ lastInsertRowId: 10 }) |
| 251 | + .mockResolvedValueOnce({ lastInsertRowId: 101 }) |
| 252 | + .mockResolvedValueOnce({ lastInsertRowId: 102 }) |
| 253 | + .mockResolvedValue({ lastInsertRowId: 0 }); |
| 254 | + const mockDb = { |
| 255 | + runAsync, |
| 256 | + getFirstAsync: jest.fn().mockResolvedValue({ |
| 257 | + id: 1, |
| 258 | + title: 'Fork 1', |
| 259 | + modelId: 7, |
| 260 | + lastUsed: 1, |
| 261 | + }), |
| 262 | + getAllAsync: jest |
| 263 | + .fn() |
| 264 | + .mockResolvedValueOnce([ |
| 265 | + { |
| 266 | + id: 1, |
| 267 | + chatId: 1, |
| 268 | + role: 'user', |
| 269 | + content: 'one', |
| 270 | + timestamp: 100, |
| 271 | + }, |
| 272 | + { |
| 273 | + id: 2, |
| 274 | + chatId: 1, |
| 275 | + role: 'assistant', |
| 276 | + content: 'two', |
| 277 | + timestamp: 200, |
| 278 | + }, |
| 279 | + ]) |
| 280 | + .mockResolvedValueOnce([ |
| 281 | + { |
| 282 | + id: 1, |
| 283 | + chatId: 1, |
| 284 | + afterMessageId: 2, |
| 285 | + sourceChatId: 8, |
| 286 | + sourceMessageId: 20, |
| 287 | + sourceChatTitle: 'Original', |
| 288 | + sourceMessagePreview: 'old marker', |
| 289 | + createdAt: 1, |
| 290 | + }, |
| 291 | + ]), |
| 292 | + withTransactionAsync: async (callback: TransactionCallback) => callback(), |
| 293 | + } as SQLiteDatabase; |
| 294 | + |
| 295 | + await forkChat(mockDb, 1, 2); |
| 296 | + |
| 297 | + expect(runAsync).not.toHaveBeenCalledWith( |
| 298 | + expect.stringContaining('INSERT INTO chatBranches'), |
| 299 | + [10, 102, 8, 20, 'Original', 'old marker'] |
| 300 | + ); |
| 301 | + expect(runAsync).toHaveBeenCalledWith( |
| 302 | + expect.stringContaining('INSERT INTO chatBranches'), |
| 303 | + [10, 102, 1, 2, 'Fork 1', 'two'] |
| 304 | + ); |
| 305 | + }); |
| 306 | +}); |
0 commit comments