|
| 1 | +package cc.unitmesh.ide.javascript.provider; |
| 2 | + |
| 3 | +import cc.unitmesh.devti.gui.chat.ChatActionType |
| 4 | +import cc.unitmesh.devti.provider.context.ChatCreationContext |
| 5 | +import cc.unitmesh.devti.provider.context.ChatOrigin |
| 6 | +import com.intellij.lang.javascript.JavascriptLanguage |
| 7 | +import com.intellij.lang.javascript.dialects.TypeScriptLanguageDialect |
| 8 | +import com.intellij.lang.javascript.psi.ecmal4.JSClass |
| 9 | +import com.intellij.psi.PsiFile |
| 10 | +import com.intellij.psi.PsiFileFactory |
| 11 | +import com.intellij.testFramework.LightPlatformTestCase |
| 12 | +import kotlinx.coroutines.runBlocking |
| 13 | + |
| 14 | +class JavaScriptVersionProviderTest : LightPlatformTestCase() { |
| 15 | + private val code = """ |
| 16 | + export class Foo { |
| 17 | + constructor() { |
| 18 | + } |
| 19 | + } |
| 20 | + """.trimIndent() |
| 21 | + fun `testShould return true when isApplicable is called with supported creationContext`() { |
| 22 | + // given |
| 23 | + val file: PsiFile = |
| 24 | + PsiFileFactory.getInstance(project).createFileFromText(JavascriptLanguage.INSTANCE, code) |
| 25 | + val jsClazz = file.children.first() as JSClass |
| 26 | + val creationContext = ChatCreationContext( |
| 27 | + ChatOrigin.ChatAction, |
| 28 | + ChatActionType.GENERATE_TEST, |
| 29 | + sourceFile = file, |
| 30 | + element = jsClazz |
| 31 | + ) |
| 32 | + |
| 33 | + // when |
| 34 | + val provider = JavaScriptVersionProvider() |
| 35 | + val result = provider.isApplicable(project, creationContext) |
| 36 | + |
| 37 | + // then |
| 38 | + assertTrue(result) |
| 39 | + } |
| 40 | + |
| 41 | + fun testShouldReturnPreferTypeScriptWhenCallWithCollect() { |
| 42 | + // given |
| 43 | + val file: PsiFile = |
| 44 | + PsiFileFactory.getInstance(project).createFileFromText(JavascriptLanguage.INSTANCE, code) |
| 45 | + |
| 46 | + val jsClazz = file.children.first() as JSClass |
| 47 | + val creationContext = ChatCreationContext( |
| 48 | + ChatOrigin.ChatAction, |
| 49 | + ChatActionType.GENERATE_TEST, |
| 50 | + sourceFile = file, |
| 51 | + element = jsClazz |
| 52 | + ) |
| 53 | + |
| 54 | + // when |
| 55 | + val provider = JavaScriptVersionProvider() |
| 56 | + val result = runBlocking { provider.collect(project, creationContext) } |
| 57 | + |
| 58 | + // then |
| 59 | + assertEquals( |
| 60 | + "Prefer JavaScript language if the used language and toolset are not defined below or in the user messages", |
| 61 | + result.first().text |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
0 commit comments