-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathsessionDatabase.test.ts
More file actions
527 lines (439 loc) · 17.2 KB
/
sessionDatabase.test.ts
File metadata and controls
527 lines (439 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import { tmpdir } from 'os';
import * as fs from 'fs/promises';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { SessionDatabase, runMigrations, sessionDatabaseMigrations, type ISessionDatabaseMigration } from '../../node/sessionDatabase.js';
import { FileEditKind } from '../../common/state/sessionState.js';
import type { Database } from '@vscode/sqlite3';
import { generateUuid } from '../../../../base/common/uuid.js';
import { join } from '../../../../base/common/path.js';
suite('SessionDatabase', () => {
const disposables = new DisposableStore();
let db: SessionDatabase | undefined;
let db2: SessionDatabase | undefined;
teardown(async () => {
disposables.clear();
await Promise.all([db?.close(), db2?.close()]);
});
ensureNoDisposablesAreLeakedInTestSuite();
/**
* Extends SessionDatabase to allow ejecting/injecting the raw sqlite3
* Database instance, enabling reopen tests with :memory: databases.
*/
class TestableSessionDatabase extends SessionDatabase {
static override async open(path: string, migrations: readonly ISessionDatabaseMigration[] = sessionDatabaseMigrations): Promise<TestableSessionDatabase> {
const inst = new TestableSessionDatabase(path, migrations);
await inst._ensureDb();
return inst;
}
/** Extract the raw db connection; this instance becomes inert. */
async ejectDb(): Promise<Database> {
const rawDb = await this._ensureDb();
this._dbPromise = undefined;
this._closed = true;
return rawDb;
}
/** Create a TestableSessionDatabase wrapping an existing raw db. */
static async fromDb(
rawDb: Database,
migrations: readonly ISessionDatabaseMigration[] = sessionDatabaseMigrations,
): Promise<TestableSessionDatabase> {
await runMigrations(rawDb, migrations);
const inst = new TestableSessionDatabase(':memory:', migrations);
inst._dbPromise = Promise.resolve(rawDb);
return inst;
}
}
// ---- Migration system -----------------------------------------------
suite('migrations', () => {
test('applies all migrations on a fresh database', async () => {
const migrations: ISessionDatabaseMigration[] = [
{ version: 1, sql: 'CREATE TABLE t1 (id INTEGER PRIMARY KEY)' },
{ version: 2, sql: 'CREATE TABLE t2 (id INTEGER PRIMARY KEY)' },
];
db = disposables.add(await SessionDatabase.open(':memory:', migrations));
const tables = (await db.getAllTables()).sort();
assert.deepStrictEqual(tables, ['t1', 't2']);
});
test('reopening with same migrations is a no-op', async () => {
const migrations: ISessionDatabaseMigration[] = [
{ version: 1, sql: 'CREATE TABLE t1 (id INTEGER PRIMARY KEY)' },
];
const db1 = await TestableSessionDatabase.open(':memory:', migrations);
const rawDb = await db1.ejectDb();
// Reopen — should not throw (table already exists, migration skipped)
db2 = disposables.add(await TestableSessionDatabase.fromDb(rawDb, migrations));
assert.deepStrictEqual(await db2.getAllTables(), ['t1']);
});
test('only applies new migrations on reopen', async () => {
const v1: ISessionDatabaseMigration[] = [
{ version: 1, sql: 'CREATE TABLE t1 (id INTEGER PRIMARY KEY)' },
];
const db1 = await TestableSessionDatabase.open(':memory:', v1);
const rawDb = await db1.ejectDb();
const v2: ISessionDatabaseMigration[] = [
...v1,
{ version: 2, sql: 'CREATE TABLE t2 (id INTEGER PRIMARY KEY)' },
];
db2 = disposables.add(await TestableSessionDatabase.fromDb(rawDb, v2));
const tables = (await db2.getAllTables()).sort();
assert.deepStrictEqual(tables, ['t1', 't2']);
});
test('rolls back on migration failure', async () => {
const migrations: ISessionDatabaseMigration[] = [
{ version: 1, sql: 'CREATE TABLE t1 (id INTEGER PRIMARY KEY)' },
{ version: 2, sql: 'THIS IS INVALID SQL' },
];
await assert.rejects(() => SessionDatabase.open(':memory:', migrations));
// A fresh :memory: open with valid migrations succeeds
db = disposables.add(await SessionDatabase.open(':memory:', [
{ version: 1, sql: 'CREATE TABLE t1 (id INTEGER PRIMARY KEY)' },
]));
assert.deepStrictEqual(await db.getAllTables(), ['t1']);
});
});
// ---- File edits -----------------------------------------------------
suite('file edits', () => {
test('store and retrieve a file edit', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/file.ts',
beforeContent: new TextEncoder().encode('before'),
afterContent: new TextEncoder().encode('after'),
addedLines: 5,
removedLines: 2,
});
const edits = await db.getFileEdits(['tc-1']);
assert.deepStrictEqual(edits, [{
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/file.ts',
originalPath: undefined,
addedLines: 5,
removedLines: 2,
}]);
});
test('retrieve multiple edits for a single tool call', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/a.ts',
beforeContent: new TextEncoder().encode('a-before'),
afterContent: new TextEncoder().encode('a-after'),
addedLines: undefined,
removedLines: undefined,
});
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/b.ts',
beforeContent: new TextEncoder().encode('b-before'),
afterContent: new TextEncoder().encode('b-after'),
addedLines: 1,
removedLines: 0,
});
const edits = await db.getFileEdits(['tc-1']);
assert.strictEqual(edits.length, 2);
assert.strictEqual(edits[0].filePath, '/workspace/a.ts');
assert.strictEqual(edits[1].filePath, '/workspace/b.ts');
});
test('retrieve edits across multiple tool calls', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/a.ts',
beforeContent: new Uint8Array(0),
afterContent: new TextEncoder().encode('hello'),
addedLines: undefined,
removedLines: undefined,
});
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-2',
kind: FileEditKind.Edit,
filePath: '/workspace/b.ts',
beforeContent: new Uint8Array(0),
afterContent: new TextEncoder().encode('world'),
addedLines: undefined,
removedLines: undefined,
});
const edits = await db.getFileEdits(['tc-1', 'tc-2']);
assert.strictEqual(edits.length, 2);
// Only tc-2
const edits2 = await db.getFileEdits(['tc-2']);
assert.strictEqual(edits2.length, 1);
assert.strictEqual(edits2[0].toolCallId, 'tc-2');
});
test('returns empty array for unknown tool call IDs', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
const edits = await db.getFileEdits(['nonexistent']);
assert.deepStrictEqual(edits, []);
});
test.skip('returns empty array when given empty array' /* Flaky https://github.com/microsoft/vscode/issues/306057 */, async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
const edits = await db.getFileEdits([]);
assert.deepStrictEqual(edits, []);
});
test('replace on conflict (same toolCallId + filePath)', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/file.ts',
beforeContent: new TextEncoder().encode('v1'),
afterContent: new TextEncoder().encode('v1-after'),
addedLines: 1,
removedLines: 0,
});
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/file.ts',
beforeContent: new TextEncoder().encode('v2'),
afterContent: new TextEncoder().encode('v2-after'),
addedLines: 3,
removedLines: 1,
});
const edits = await db.getFileEdits(['tc-1']);
assert.strictEqual(edits.length, 1);
assert.strictEqual(edits[0].addedLines, 3);
const content = await db.readFileEditContent('tc-1', '/workspace/file.ts');
assert.ok(content);
assert.deepStrictEqual(new TextDecoder().decode(content.beforeContent), 'v2');
});
test('readFileEditContent returns content on demand', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/file.ts',
beforeContent: new TextEncoder().encode('before'),
afterContent: new TextEncoder().encode('after'),
addedLines: undefined,
removedLines: undefined,
});
const content = await db.readFileEditContent('tc-1', '/workspace/file.ts');
assert.ok(content);
assert.deepStrictEqual(content.beforeContent, new TextEncoder().encode('before'));
assert.deepStrictEqual(content.afterContent, new TextEncoder().encode('after'));
});
test('readFileEditContent returns undefined for missing edit', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
const content = await db.readFileEditContent('tc-missing', '/no/such/file');
assert.strictEqual(content, undefined);
});
test('persists binary content correctly', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
const binary = new Uint8Array([0, 1, 2, 255, 128, 64]);
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-bin',
kind: FileEditKind.Edit,
filePath: '/workspace/image.png',
beforeContent: new Uint8Array(0),
afterContent: binary,
addedLines: undefined,
removedLines: undefined,
});
const content = await db.readFileEditContent('tc-bin', '/workspace/image.png');
assert.ok(content);
assert.deepStrictEqual(content.afterContent, binary);
});
test('auto-creates turn if it does not exist', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
// storeFileEdit should succeed even without a prior createTurn call
await db.storeFileEdit({
turnId: 'auto-turn',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/x',
beforeContent: new Uint8Array(0),
afterContent: new Uint8Array(0),
addedLines: undefined,
removedLines: undefined,
});
const edits = await db.getFileEdits(['tc-1']);
assert.strictEqual(edits.length, 1);
assert.strictEqual(edits[0].turnId, 'auto-turn');
});
});
// ---- Turns ----------------------------------------------------------
suite('turns', () => {
test('createTurn is idempotent', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.createTurn('turn-1'); // should not throw
});
test('deleteTurn cascades to file edits', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/a.ts',
beforeContent: new TextEncoder().encode('before'),
afterContent: new TextEncoder().encode('after'),
addedLines: undefined,
removedLines: undefined,
});
// Edits exist
assert.strictEqual((await db.getFileEdits(['tc-1'])).length, 1);
// Delete the turn — edits should be gone
await db.deleteTurn('turn-1');
assert.deepStrictEqual(await db.getFileEdits(['tc-1']), []);
});
test('deleteTurn only removes its own edits', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.createTurn('turn-2');
await db.storeFileEdit({
turnId: 'turn-1',
toolCallId: 'tc-1',
kind: FileEditKind.Edit,
filePath: '/workspace/a.ts',
beforeContent: new Uint8Array(0),
afterContent: new TextEncoder().encode('a'),
addedLines: undefined,
removedLines: undefined,
});
await db.storeFileEdit({
turnId: 'turn-2',
toolCallId: 'tc-2',
kind: FileEditKind.Edit,
filePath: '/workspace/b.ts',
beforeContent: new Uint8Array(0),
afterContent: new TextEncoder().encode('b'),
addedLines: undefined,
removedLines: undefined,
});
await db.deleteTurn('turn-1');
assert.deepStrictEqual(await db.getFileEdits(['tc-1']), []);
assert.strictEqual((await db.getFileEdits(['tc-2'])).length, 1);
});
test('deleteTurn is a no-op for unknown turn', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.deleteTurn('nonexistent'); // should not throw
});
});
// ---- Dispose --------------------------------------------------------
suite('dispose', () => {
test('methods throw after dispose', async () => {
db = await SessionDatabase.open(':memory:');
db.close();
await assert.rejects(
() => db!.createTurn('turn-1'),
/disposed/,
);
});
test('double dispose is safe', async () => {
db = await SessionDatabase.open(':memory:');
await db.close();
await db.close(); // should not throw
});
});
// ---- Lazy open ------------------------------------------------------
suite('lazy open', () => {
test('constructor does not open the database', () => {
db = new SessionDatabase(':memory:');
disposables.add(db);
// No error — the database is not opened until first use
});
test('first async call opens and migrates the database', async () => {
db = disposables.add(new SessionDatabase(':memory:'));
await db.createTurn('turn-1');
const edits = await db.getFileEdits(['nonexistent']);
assert.deepStrictEqual(edits, []);
});
test('multiple concurrent calls share the same open promise', async () => {
db = disposables.add(new SessionDatabase(':memory:'));
// Fire multiple calls concurrently — all should succeed
await Promise.all([
db.createTurn('turn-1'),
db.createTurn('turn-2'),
db.getFileEdits([]),
]);
});
test('dispose during open rejects subsequent calls', async () => {
db = new SessionDatabase(':memory:');
await db.close();
await assert.rejects(() => db!.createTurn('turn-1'), /disposed/);
});
});
// ---- Session metadata -----------------------------------------------
suite('session metadata', () => {
test('getMetadata returns undefined for missing key', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
assert.strictEqual(await db.getMetadata('nonexistent'), undefined);
});
test('setMetadata and getMetadata round-trip', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.setMetadata('customTitle', 'My Session');
assert.strictEqual(await db.getMetadata('customTitle'), 'My Session');
});
test('setMetadata overwrites existing value', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.setMetadata('customTitle', 'First');
await db.setMetadata('customTitle', 'Second');
assert.strictEqual(await db.getMetadata('customTitle'), 'Second');
});
test('metadata persists across reopen', async () => {
const db1 = disposables.add(await TestableSessionDatabase.open(':memory:'));
await db1.setMetadata('customTitle', 'Persistent Title');
const rawDb = await db1.ejectDb();
db = disposables.add(await TestableSessionDatabase.fromDb(rawDb));
assert.strictEqual(await db.getMetadata('customTitle'), 'Persistent Title');
});
test('migration v2 creates session_metadata table', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
const tables = await db.getAllTables();
assert.ok(tables.includes('session_metadata'));
});
});
// ---- vacuumInto -----------------------------------------------------
suite('vacuumInto', () => {
let tmpDir: string;
setup(async () => {
tmpDir = await fs.mkdtemp(join(tmpdir(), 'session-db-test-' + generateUuid()));
});
teardown(async () => {
await Promise.all([db?.close(), db2?.close()]);
db = db2 = undefined;
await fs.rm(tmpDir, { recursive: true, force: true });
});
test('produces a copy with the same data', async () => {
db = disposables.add(await SessionDatabase.open(':memory:'));
await db.createTurn('turn-1');
await db.setTurnEventId('turn-1', 'evt-1');
await db.setMetadata('key', 'value');
const targetPath = join(tmpDir, 'copy.db');
await db.vacuumInto(targetPath);
db2 = disposables.add(await SessionDatabase.open(targetPath));
assert.strictEqual(await db2.getTurnEventId('turn-1'), 'evt-1');
assert.strictEqual(await db2.getMetadata('key'), 'value');
});
});
});