11import * as vscode from "vscode" ; // The vscode module is mocked out. See: scripts/setup.jest.ts
22import { A4DFixActionProvider } from "../../../../lib/agentforce/a4d-fix-action-provider" ;
3- import { SpyLLMService , SpyLogger , StubLLMServiceProvider } from "../../stubs" ;
3+ import { SpyLLMService , SpyLogger , StubOrgConnectionService , StubLLMServiceProvider } from "../../stubs" ;
44import { StubCodeActionContext } from "../../vscode-stubs" ;
55import { messages } from "../../../../lib/messages" ;
66import { createTextDocument } from "jest-mock-vscode" ;
@@ -11,13 +11,15 @@ describe('AgentforceCodeActionProvider Tests', () => {
1111 let spyLLMService : SpyLLMService ;
1212 let llmServiceProvider : StubLLMServiceProvider ;
1313 let spyLogger : SpyLogger ;
14+ let stubOrgConnectionService : StubOrgConnectionService ;
1415 let actionProvider : A4DFixActionProvider ;
1516
1617 beforeEach ( ( ) => {
1718 spyLLMService = new SpyLLMService ( ) ;
1819 llmServiceProvider = new StubLLMServiceProvider ( spyLLMService ) ;
1920 spyLogger = new SpyLogger ( ) ;
20- actionProvider = new A4DFixActionProvider ( llmServiceProvider , spyLogger ) ;
21+ stubOrgConnectionService = new StubOrgConnectionService ( ) ;
22+ actionProvider = new A4DFixActionProvider ( llmServiceProvider , stubOrgConnectionService , spyLogger ) ;
2123 } ) ;
2224
2325 describe ( 'provideCodeActions Tests' , ( ) => {
@@ -68,7 +70,8 @@ describe('AgentforceCodeActionProvider Tests', () => {
6870 expect ( codeActions [ 2 ] . diagnostics ) . toEqual ( [ supportedDiag3 ] ) ;
6971 } ) ;
7072
71- it ( 'When the LLMService is unavailable, then warn once and return no code actions' , async ( ) => {
73+ // Authentication and LLM Service Availability Tests
74+ it ( 'When the LLMService is unavailable, then warn once and return no code actions and warn only once' , async ( ) => {
7275 llmServiceProvider . isLLMServiceAvailableReturnValue = false ;
7376 const context : vscode . CodeActionContext = new StubCodeActionContext ( { diagnostics : [ supportedDiag1 ] } ) ;
7477 const codeActions : vscode . CodeAction [ ] = await actionProvider . provideCodeActions ( sampleDocument , range , context ) ;
@@ -78,6 +81,45 @@ describe('AgentforceCodeActionProvider Tests', () => {
7881 expect ( spyLogger . warnCallHistory ) . toHaveLength ( 1 ) ;
7982 expect ( spyLogger . warnCallHistory [ 0 ] ) . toEqual ( { msg : messages . agentforce . a4dQuickFixUnavailable } ) ;
8083 } ) ;
84+
85+ it ( 'When user is not authenticated to an org, then return no code actions and warn once' , async ( ) => {
86+ stubOrgConnectionService . isAuthedReturnValue = false ;
87+ const context : vscode . CodeActionContext = new StubCodeActionContext ( { diagnostics : [ supportedDiag1 ] } ) ;
88+ const codeActions : vscode . CodeAction [ ] = await actionProvider . provideCodeActions ( sampleDocument , range , context ) ;
89+ await actionProvider . provideCodeActions ( sampleDocument , range , context ) ; // Sanity check that multiple calls do not produce additional warnings
90+
91+ expect ( codeActions ) . toHaveLength ( 0 ) ;
92+ expect ( spyLogger . warnCallHistory ) . toHaveLength ( 1 ) ;
93+ expect ( spyLogger . warnCallHistory [ 0 ] ) . toEqual ( { msg : messages . agentforce . a4dQuickFixUnauthenticatedOrg } ) ;
94+ } ) ;
95+
96+ it ( 'When user is authenticated and LLMService is available, then user does not see any warnings' , async ( ) => {
97+ stubOrgConnectionService . isAuthedReturnValue = true ;
98+ llmServiceProvider . isLLMServiceAvailableReturnValue = true ;
99+ const context : vscode . CodeActionContext = new StubCodeActionContext ( { diagnostics : [ supportedDiag1 ] } ) ;
100+ const codeActions : vscode . CodeAction [ ] = await actionProvider . provideCodeActions ( sampleDocument , range , context ) ;
101+
102+ expect ( codeActions ) . toHaveLength ( 1 ) ;
103+ expect ( spyLogger . warnCallHistory ) . toHaveLength ( 0 ) ;
104+ } ) ;
105+
106+ it ( 'When no supported diagnostics are in the context and user is not authenticated to an org, then do not warn' , async ( ) => {
107+ stubOrgConnectionService . isAuthedReturnValue = false ;
108+ const context : vscode . CodeActionContext = new StubCodeActionContext ( { diagnostics : [ unsupportedDiag1 ] } ) ;
109+ const codeActions : vscode . CodeAction [ ] = await actionProvider . provideCodeActions ( sampleDocument , range , context ) ;
110+
111+ expect ( codeActions ) . toHaveLength ( 0 ) ;
112+ expect ( spyLogger . warnCallHistory ) . toHaveLength ( 0 ) ;
113+ } ) ;
114+
115+ it ( 'When no supported diagnostics are in the context and LLM Service is unavailable, then do not warn' , async ( ) => {
116+ llmServiceProvider . isLLMServiceAvailableReturnValue = false ;
117+ const context : vscode . CodeActionContext = new StubCodeActionContext ( { diagnostics : [ unsupportedDiag1 ] } ) ;
118+ const codeActions : vscode . CodeAction [ ] = await actionProvider . provideCodeActions ( sampleDocument , range , context ) ;
119+
120+ expect ( codeActions ) . toHaveLength ( 0 ) ;
121+ expect ( spyLogger . warnCallHistory ) . toHaveLength ( 0 ) ;
122+ } ) ;
81123 } ) ;
82124} ) ;
83125
0 commit comments