Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/test/todoSqlQuery.spec.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
7
8
interface TodoItem {
9
id: string;
10
title: string;
11
description: string;
12
status: 'pending' | 'in_progress' | 'done' | 'blocked';
13
}
14
15
const mockQueryTodos = vi.fn<(dbPath: string) => Promise<TodoItem[]>>();
16
const mockTerminate = vi.fn();
17
18
vi.mock('../../../../../util/node/worker', () => ({
19
WorkerWithRpcProxy: class {
20
proxy = { queryTodos: mockQueryTodos };
21
terminate = mockTerminate;
22
},
23
}));
24
25
// Import after mock so the mock is in place
26
const { TodoSqlQuery } = await import('../todoSqlQuery');
27
28
describe('TodoSqlQuery', () => {
29
let query: InstanceType<typeof TodoSqlQuery>;
30
31
beforeEach(() => {
32
query = new TodoSqlQuery();
33
vi.clearAllMocks();
34
});
35
36
afterEach(() => {
37
query.dispose();
38
});
39
40
it('queryTodos passes the correct database path to the worker', async () => {
41
mockQueryTodos.mockResolvedValue([]);
42
await query.queryTodos('/some/session/dir');
43
expect(mockQueryTodos).toHaveBeenCalledWith(expect.stringMatching(/[\\/]some[\\/]session[\\/]dir[\\/]session\.db$/));
44
});
45
46
it('queryTodos returns items from the worker', async () => {
47
const items: TodoItem[] = [
48
{ id: '1', title: 'Task 1', description: '', status: 'pending' },
49
{ id: '2', title: 'Task 2', description: 'desc', status: 'done' },
50
];
51
mockQueryTodos.mockResolvedValue(items);
52
const result = await query.queryTodos('/session/dir');
53
expect(result).toEqual(items);
54
});
55
56
it('reuses the same worker across multiple queries', async () => {
57
mockQueryTodos.mockResolvedValue([]);
58
await query.queryTodos('/dir1');
59
await query.queryTodos('/dir2');
60
// The worker constructor is called once (lazy init), proxy is reused
61
expect(mockQueryTodos).toHaveBeenCalledTimes(2);
62
});
63
64
it('dispose terminates the worker', () => {
65
// Force worker creation by calling queryTodos
66
mockQueryTodos.mockResolvedValue([]);
67
void query.queryTodos('/dir');
68
query.dispose();
69
expect(mockTerminate).toHaveBeenCalledOnce();
70
});
71
72
it('dispose is safe to call when worker was never created', () => {
73
// Should not throw
74
query.dispose();
75
expect(mockTerminate).not.toHaveBeenCalled();
76
});
77
78
it('dispose is safe to call multiple times', () => {
79
mockQueryTodos.mockResolvedValue([]);
80
void query.queryTodos('/dir');
81
query.dispose();
82
query.dispose();
83
// Only one terminate call since the second dispose finds no worker
84
expect(mockTerminate).toHaveBeenCalledOnce();
85
});
86
});
87
88