Skip to content

Commit

Permalink
Entity completion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-athome committed Feb 10, 2025
1 parent 32192c2 commit 43c64db
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package it.casaricci.hass.plugin

import com.intellij.json.JsonFileType
import com.intellij.json.psi.JsonFile
import com.intellij.json.psi.JsonProperty
import com.intellij.json.psi.JsonStringLiteral
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFileFactory
import com.intellij.util.ResourceUtil
import it.casaricci.hass.plugin.services.HassRemoteRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import java.io.FileInputStream

class MockHassRemoteRepository(project: Project, jsonStatesPath: String?, jsonServicePath: String?) :
HassRemoteRepository(project, CoroutineScope(Dispatchers.IO)) {

private var states: Collection<JsonStringLiteral>? = null
private var services: Collection<JsonProperty>? = null

init {
if (jsonStatesPath != null) {
val jsonStatesData = ResourceUtil.loadText(FileInputStream(jsonStatesPath))
val jsonStatesFile = PsiFileFactory.getInstance(project)
.createFileFromText("ha_states.json", JsonFileType.INSTANCE, jsonStatesData) as JsonFile

states = getStates(jsonStatesFile)
}

if (jsonServicePath != null) {
val jsonServicesData = ResourceUtil.loadText(FileInputStream(jsonServicePath))
val jsonServicesFile = PsiFileFactory.getInstance(project)
.createFileFromText("ha_services.json", JsonFileType.INSTANCE, jsonServicesData) as JsonFile

services = getServices(jsonServicesFile)
}
}

override fun getStates(module: Module, vararg excludeDomains: String): Collection<JsonStringLiteral>? {
return states
}

override fun getServices(module: Module): Collection<JsonProperty>? {
return services
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package it.casaricci.hass.plugin.completion

import com.intellij.codeInsight.completion.CompletionType
import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixture4TestCase
import com.intellij.testFramework.replaceService
import it.casaricci.hass.plugin.MockHassRemoteRepository
import it.casaricci.hass.plugin.services.HassRemoteRepository
import org.junit.Test

class EntityCompletionTest : LightPlatformCodeInsightFixture4TestCase() {
Expand All @@ -10,7 +13,7 @@ class EntityCompletionTest : LightPlatformCodeInsightFixture4TestCase() {

@Test
fun testLocalCompletion() {
myFixture.configureByFiles("configuration.yaml", "scripts.yaml")
myFixture.configureByFiles("configuration-local.yaml", "scripts.yaml")
myFixture.complete(CompletionType.BASIC)
val lookupElementStrings = myFixture.lookupElementStrings
assertNotNull(lookupElementStrings)
Expand All @@ -24,7 +27,22 @@ class EntityCompletionTest : LightPlatformCodeInsightFixture4TestCase() {

@Test
fun testRemoteCompletion() {
// TODO we need somehow to inject states.json into systemDir
// mock entity states
myFixture.project.replaceService(HassRemoteRepository::class.java, MockHassRemoteRepository(project,
"$testDataPath/states.json", null),
testRootDisposable)

myFixture.configureByFiles("configuration-remote.yaml", "scripts.yaml")
myFixture.complete(CompletionType.BASIC)
val lookupElementStrings = myFixture.lookupElementStrings
assertNotNull(lookupElementStrings)
assertSameElements(
lookupElementStrings!!,
"script.script_test2",
"script.do_some_action",
"script.do_some_other_action",
"scene.house_entrance"
)
}

}
5 changes: 5 additions & 0 deletions src/test/resources/completion/entity/configuration-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# used for completion with local data only (i.e. no states or services cache)
script:
script_test1:
sequence:
- action: <caret>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# used for completion with remote data only (i.e. with states cache)
script:
script_test2:
sequence:
- action: scene.turn_on
target:
entity_id: <caret>

# TODO add some other supported local entities (e.g. input_text, automation, ...)
6 changes: 0 additions & 6 deletions src/test/resources/completion/entity/configuration.yaml

This file was deleted.

0 comments on commit 43c64db

Please sign in to comment.