Path: blob/main/extensions/copilot/src/extension/chatSessions/copilotcli/node/test/todoSqlQuery.spec.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';67interface TodoItem {8id: string;9title: string;10description: string;11status: 'pending' | 'in_progress' | 'done' | 'blocked';12}1314const mockQueryTodos = vi.fn<(dbPath: string) => Promise<TodoItem[]>>();15const mockTerminate = vi.fn();1617vi.mock('../../../../../util/node/worker', () => ({18WorkerWithRpcProxy: class {19proxy = { queryTodos: mockQueryTodos };20terminate = mockTerminate;21},22}));2324// Import after mock so the mock is in place25const { TodoSqlQuery } = await import('../todoSqlQuery');2627describe('TodoSqlQuery', () => {28let query: InstanceType<typeof TodoSqlQuery>;2930beforeEach(() => {31query = new TodoSqlQuery();32vi.clearAllMocks();33});3435afterEach(() => {36query.dispose();37});3839it('queryTodos passes the correct database path to the worker', async () => {40mockQueryTodos.mockResolvedValue([]);41await query.queryTodos('/some/session/dir');42expect(mockQueryTodos).toHaveBeenCalledWith(expect.stringMatching(/[\\/]some[\\/]session[\\/]dir[\\/]session\.db$/));43});4445it('queryTodos returns items from the worker', async () => {46const items: TodoItem[] = [47{ id: '1', title: 'Task 1', description: '', status: 'pending' },48{ id: '2', title: 'Task 2', description: 'desc', status: 'done' },49];50mockQueryTodos.mockResolvedValue(items);51const result = await query.queryTodos('/session/dir');52expect(result).toEqual(items);53});5455it('reuses the same worker across multiple queries', async () => {56mockQueryTodos.mockResolvedValue([]);57await query.queryTodos('/dir1');58await query.queryTodos('/dir2');59// The worker constructor is called once (lazy init), proxy is reused60expect(mockQueryTodos).toHaveBeenCalledTimes(2);61});6263it('dispose terminates the worker', () => {64// Force worker creation by calling queryTodos65mockQueryTodos.mockResolvedValue([]);66void query.queryTodos('/dir');67query.dispose();68expect(mockTerminate).toHaveBeenCalledOnce();69});7071it('dispose is safe to call when worker was never created', () => {72// Should not throw73query.dispose();74expect(mockTerminate).not.toHaveBeenCalled();75});7677it('dispose is safe to call multiple times', () => {78mockQueryTodos.mockResolvedValue([]);79void query.queryTodos('/dir');80query.dispose();81query.dispose();82// Only one terminate call since the second dispose finds no worker83expect(mockTerminate).toHaveBeenCalledOnce();84});85});868788