Przeglądaj źródła

added getClineStackSize() to ClineProvider and fixed its tests

ShayBC 10 miesięcy temu
rodzic
commit
655930cf5f

+ 5 - 0
src/core/webview/ClineProvider.ts

@@ -215,6 +215,11 @@ export class ClineProvider implements vscode.WebviewViewProvider {
 		return this.clineStack[this.clineStack.length - 1]
 	}
 
+	// returns the current clineStack length (how many cline objects are in the stack)
+	getClineStackSize(): number {
+		return this.clineStack.length
+	}
+
 	// remove the current task/cline instance (at the top of the stack), ao this task is finished
 	// and resume the previous task/cline instance (if it exists)
 	// this is used when a sub task is finished and the parent task needs to be resumed

+ 41 - 16
src/core/webview/__tests__/ClineProvider.test.ts

@@ -8,6 +8,7 @@ import { ExtensionMessage, ExtensionState } from "../../../shared/ExtensionMessa
 import { setSoundEnabled } from "../../../utils/sound"
 import { defaultModeSlug } from "../../../shared/modes"
 import { experimentDefault } from "../../../shared/experiments"
+import { Cline } from "../../Cline"
 
 // Mock custom-instructions module
 const mockAddCustomInstructions = jest.fn()
@@ -377,15 +378,43 @@ describe("ClineProvider", () => {
 	})
 
 	test("clearTask aborts current task", async () => {
+		// prepare the mock object
 		const mockAbortTask = jest.fn()
-		// @ts-ignore - accessing private property for testing
-		provider.cline = { abortTask: mockAbortTask }
+		const clineMock = { abortTask: mockAbortTask } as unknown as Cline
+
+		// add the mock object to the stack
+		provider.addClineToStack(clineMock)
 
+		// get the stack size before the abort call
+		const stackSizeBeforeAbort = provider.getClineStackSize()
+
+		// call the removeClineFromStack method so it will call the current cline abort and remove it from the stack
 		await provider.removeClineFromStack()
 
+		// get the stack size after the abort call
+		const stackSizeAfterAbort = provider.getClineStackSize()
+
+		// check if the abort method was called
 		expect(mockAbortTask).toHaveBeenCalled()
-		// @ts-ignore - accessing private property for testing
-		expect(provider.cline).toBeUndefined()
+
+		// check if the stack size was decreased
+		expect(stackSizeBeforeAbort - stackSizeAfterAbort).toBe(1)
+	})
+
+	test("addClineToStack adds multiple Cline instances to the stack", () => {
+		// prepare test data
+		const mockCline1 = { taskId: "test-task-id-1" } as unknown as Cline
+		const mockCline2 = { taskId: "test-task-id-2" } as unknown as Cline
+
+		// add Cline instances to the stack
+		provider.addClineToStack(mockCline1)
+		provider.addClineToStack(mockCline2)
+
+		// verify cline instances were added to the stack
+		expect(provider.getClineStackSize()).toBe(2)
+
+		// verify current cline instance is the last one added
+		expect(provider.getCurrentCline()).toBe(mockCline2)
 	})
 
 	test("getState returns correct initial state", async () => {
@@ -788,9 +817,8 @@ describe("ClineProvider", () => {
 				taskId: "test-task-id",
 				abortTask: jest.fn(),
 				handleWebviewAskResponse: jest.fn(),
-			}
-			// @ts-ignore - accessing private property for testing
-			provider.cline = mockCline
+			} as unknown as Cline
+			provider.addClineToStack(mockCline)
 
 			// Mock getTaskWithId
 			;(provider as any).getTaskWithId = jest.fn().mockResolvedValue({
@@ -841,9 +869,8 @@ describe("ClineProvider", () => {
 				taskId: "test-task-id",
 				abortTask: jest.fn(),
 				handleWebviewAskResponse: jest.fn(),
-			}
-			// @ts-ignore - accessing private property for testing
-			provider.cline = mockCline
+			} as unknown as Cline
+			provider.addClineToStack(mockCline)
 
 			// Mock getTaskWithId
 			;(provider as any).getTaskWithId = jest.fn().mockResolvedValue({
@@ -871,9 +898,8 @@ describe("ClineProvider", () => {
 				overwriteClineMessages: jest.fn(),
 				overwriteApiConversationHistory: jest.fn(),
 				taskId: "test-task-id",
-			}
-			// @ts-ignore - accessing private property for testing
-			provider.cline = mockCline
+			} as unknown as Cline
+			provider.addClineToStack(mockCline)
 
 			// Trigger message deletion
 			const messageHandler = (mockWebviewView.webview.onDidReceiveMessage as jest.Mock).mock.calls[0][0]
@@ -1377,9 +1403,8 @@ describe("ClineProvider", () => {
 			const mockCline = {
 				api: undefined,
 				abortTask: jest.fn(),
-			}
-			// @ts-ignore - accessing private property for testing
-			provider.cline = mockCline
+			} as unknown as Cline
+			provider.addClineToStack(mockCline)
 
 			const testApiConfig = {
 				apiProvider: "anthropic" as const,