@@ -1614,4 +1614,103 @@ describe("Cline", () => {
1614
1614
} )
1615
1615
} )
1616
1616
} )
1617
+
1618
+ describe ( "abortTask" , ( ) => {
1619
+ it ( "should set abort flag and emit TaskAborted event" , async ( ) => {
1620
+ const task = new Task ( {
1621
+ provider : mockProvider ,
1622
+ apiConfiguration : mockApiConfig ,
1623
+ task : "test task" ,
1624
+ startTask : false ,
1625
+ } )
1626
+
1627
+ // Spy on emit method
1628
+ const emitSpy = vi . spyOn ( task , "emit" )
1629
+
1630
+ // Mock the dispose method to avoid actual cleanup
1631
+ vi . spyOn ( task , "dispose" ) . mockImplementation ( ( ) => { } )
1632
+
1633
+ // Call abortTask
1634
+ await task . abortTask ( )
1635
+
1636
+ // Verify abort flag is set
1637
+ expect ( task . abort ) . toBe ( true )
1638
+
1639
+ // Verify TaskAborted event was emitted
1640
+ expect ( emitSpy ) . toHaveBeenCalledWith ( "taskAborted" )
1641
+ } )
1642
+
1643
+ it ( "should be equivalent to clicking Cancel button functionality" , async ( ) => {
1644
+ const task = new Task ( {
1645
+ provider : mockProvider ,
1646
+ apiConfiguration : mockApiConfig ,
1647
+ task : "test task" ,
1648
+ startTask : false ,
1649
+ } )
1650
+
1651
+ // Mock the dispose method to track cleanup
1652
+ const disposeSpy = vi . spyOn ( task , "dispose" ) . mockImplementation ( ( ) => { } )
1653
+
1654
+ // Call abortTask
1655
+ await task . abortTask ( )
1656
+
1657
+ // Verify the same behavior as Cancel button
1658
+ expect ( task . abort ) . toBe ( true )
1659
+ expect ( disposeSpy ) . toHaveBeenCalled ( )
1660
+ } )
1661
+
1662
+ it ( "should work with TaskLike interface" , async ( ) => {
1663
+ const task = new Task ( {
1664
+ provider : mockProvider ,
1665
+ apiConfiguration : mockApiConfig ,
1666
+ task : "test task" ,
1667
+ startTask : false ,
1668
+ } )
1669
+
1670
+ // Cast to TaskLike to ensure interface compliance
1671
+ const taskLike = task as any // TaskLike interface from types package
1672
+
1673
+ // Verify abortTask method exists and is callable
1674
+ expect ( typeof taskLike . abortTask ) . toBe ( "function" )
1675
+
1676
+ // Mock the dispose method to avoid actual cleanup
1677
+ vi . spyOn ( task , "dispose" ) . mockImplementation ( ( ) => { } )
1678
+
1679
+ // Call abortTask through interface
1680
+ await taskLike . abortTask ( )
1681
+
1682
+ // Verify it works
1683
+ expect ( task . abort ) . toBe ( true )
1684
+ } )
1685
+
1686
+ it ( "should handle errors during disposal gracefully" , async ( ) => {
1687
+ const task = new Task ( {
1688
+ provider : mockProvider ,
1689
+ apiConfiguration : mockApiConfig ,
1690
+ task : "test task" ,
1691
+ startTask : false ,
1692
+ } )
1693
+
1694
+ // Mock dispose to throw an error
1695
+ const mockError = new Error ( "Disposal failed" )
1696
+ vi . spyOn ( task , "dispose" ) . mockImplementation ( ( ) => {
1697
+ throw mockError
1698
+ } )
1699
+
1700
+ // Spy on console.error to verify error is logged
1701
+ const consoleErrorSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
1702
+
1703
+ // abortTask should not throw even if dispose fails
1704
+ await expect ( task . abortTask ( ) ) . resolves . not . toThrow ( )
1705
+
1706
+ // Verify error was logged
1707
+ expect ( consoleErrorSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "Error during task" ) , mockError )
1708
+
1709
+ // Verify abort flag is still set
1710
+ expect ( task . abort ) . toBe ( true )
1711
+
1712
+ // Restore console.error
1713
+ consoleErrorSpy . mockRestore ( )
1714
+ } )
1715
+ } )
1617
1716
} )
0 commit comments