Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import software.aws.toolkits.jetbrains.utils.rules.HeavyJavaCodeInsightTestFixtu
import software.aws.toolkits.jetbrains.utils.rules.addClass
import software.aws.toolkits.jetbrains.utils.rules.addModule
import software.aws.toolkits.jetbrains.utils.rules.addTestClass
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.test.assertNotNull

class CodeWhispererFileContextProviderTest {
Expand Down Expand Up @@ -230,6 +231,26 @@ class CodeWhispererFileContextProviderTest {
assertThat(serverContext.constructed()).hasSize(1)

whenever(providerContext.constructed()[0].queryInline(any(), any(), any())).thenThrow(RuntimeException("mock exception"))
whenever(providerContext.constructed()[0].isIndexComplete).thenReturn(AtomicBoolean(true))

val result = controller.queryInline("query", "filePath")
assertThat(result).isEmpty()
}
}
}

@Test
fun `projectContextController should return empty result if index is not ready`() = runTest {
mockConstruction(ProjectContextProvider::class.java).use { providerContext ->
mockConstruction(EncoderServer::class.java).use { serverContext ->
assertThat(providerContext.constructed()).hasSize(0)
assertThat(serverContext.constructed()).hasSize(0)
val controller = ProjectContextController(project, TestScope())
assertThat(providerContext.constructed()).hasSize(1)
assertThat(serverContext.constructed()).hasSize(1)

whenever(providerContext.constructed()[0].queryInline(any(), any(), any())).thenReturn(listOf(InlineBm25Chunk("foo", "path", 0.0)))
whenever(providerContext.constructed()[0].isIndexComplete).thenReturn(AtomicBoolean(false))

val result = controller.queryInline("query", "filePath")
assertThat(result).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class ProjectContextController(private val project: Project, private val cs: Cor
fun getProjectContextIndexComplete() = projectContextProvider.isIndexComplete.get()

suspend fun queryChat(prompt: String, timeout: Long?): List<RelevantDocument> {
if (!getProjectContextIndexComplete()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is actually allowed

logger.warn { "attempt to query project context while index for ${project.name} isn't ready" }
return emptyList()
}
try {
return projectContextProvider.query(prompt, timeout)
} catch (e: Exception) {
Expand All @@ -60,8 +64,13 @@ class ProjectContextController(private val project: Project, private val cs: Cor
}
}

suspend fun queryInline(query: String, filePath: String): List<InlineBm25Chunk> =
try {
suspend fun queryInline(query: String, filePath: String): List<InlineBm25Chunk> {
if (!getProjectContextIndexComplete()) {
logger.warn { "attempt to query project context while index for ${project.name} isn't ready" }
return emptyList()
}

return try {
projectContextProvider.queryInline(query, filePath, InlineContextTarget.CODEMAP)
} catch (e: Exception) {
var logStr = "error while querying inline for project context $e.message"
Expand All @@ -71,9 +80,14 @@ class ProjectContextController(private val project: Project, private val cs: Cor
logger.warn { logStr }
emptyList()
}
}

@RequiresBackgroundThread
fun updateIndex(filePaths: List<String>, mode: IndexUpdateMode) {
if (!getProjectContextIndexComplete()) {
return
}

try {
return projectContextProvider.updateIndex(filePaths, mode)
} catch (e: Exception) {
Expand Down
Loading