|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 2 | +import * as vscode from "vscode" |
| 3 | + |
| 4 | +import { API } from "../api" |
| 5 | +import { ClineProvider } from "../../core/webview/ClineProvider" |
| 6 | + |
| 7 | +vi.mock("vscode") |
| 8 | +vi.mock("../../core/webview/ClineProvider") |
| 9 | + |
| 10 | +describe("API#getTaskApiConversationHistoryLength", () => { |
| 11 | + let api: API |
| 12 | + let mockOutputChannel: vscode.OutputChannel |
| 13 | + let mockProvider: ClineProvider |
| 14 | + let mockGetTaskWithId: ReturnType<typeof vi.fn> |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + mockOutputChannel = { |
| 18 | + appendLine: vi.fn(), |
| 19 | + } as unknown as vscode.OutputChannel |
| 20 | + |
| 21 | + mockGetTaskWithId = vi.fn() |
| 22 | + |
| 23 | + mockProvider = { |
| 24 | + context: {} as vscode.ExtensionContext, |
| 25 | + getTaskWithId: mockGetTaskWithId, |
| 26 | + on: vi.fn(), |
| 27 | + } as unknown as ClineProvider |
| 28 | + |
| 29 | + api = new API(mockOutputChannel, mockProvider, undefined, true) |
| 30 | + }) |
| 31 | + |
| 32 | + it("returns the persisted api conversation history length", async () => { |
| 33 | + mockGetTaskWithId.mockResolvedValue({ |
| 34 | + apiConversationHistory: [{ role: "user" }, { role: "assistant" }], |
| 35 | + }) |
| 36 | + |
| 37 | + await expect(api.getTaskApiConversationHistoryLength("task-1")).resolves.toBe(2) |
| 38 | + }) |
| 39 | + |
| 40 | + it("returns 0 instead of throwing when the task is unavailable", async () => { |
| 41 | + mockGetTaskWithId.mockRejectedValue(new Error("Task not found")) |
| 42 | + |
| 43 | + await expect(api.getTaskApiConversationHistoryLength("missing-task")).resolves.toBe(0) |
| 44 | + }) |
| 45 | +}) |
0 commit comments