Skip to content

Commit a578e9f

Browse files
authored
Cover JVMDocumentProvider with tests (#351)
Signed-off-by: Sergey Karpov <[email protected]>
1 parent 86ee696 commit a578e9f

File tree

2 files changed

+199
-0
lines changed

2 files changed

+199
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package ai.koog.rag.base.files
2+
3+
import ai.koog.rag.base.files.DocumentProvider.DocumentRange
4+
import ai.koog.rag.base.files.DocumentProvider.Position
5+
import kotlinx.coroutines.runBlocking
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.assertThrows
8+
import kotlin.io.path.readText
9+
import kotlin.io.path.writeText
10+
import kotlin.test.assertEquals
11+
12+
class JVMDocumentProviderTest : KoogTestBase() {
13+
14+
@Test
15+
fun `test document method returns the path`() {
16+
runBlocking {
17+
val result = JVMDocumentProvider.document(file1)
18+
assertEquals(file1, result)
19+
}
20+
}
21+
22+
@Test
23+
fun `test text method returns the content of the file`() {
24+
runBlocking {
25+
val content = JVMDocumentProvider.text(file1)
26+
assertEquals(testCode, content.toString())
27+
}
28+
}
29+
30+
@Test
31+
fun `test text method normalizes CRLF line endings`() {
32+
runBlocking {
33+
val testFile = tempDir.resolve("crlf.txt")
34+
testFile.writeText("Line1\r\nLine2\r\nLine3")
35+
36+
val content = JVMDocumentProvider.text(testFile)
37+
assertEquals("Line1\nLine2\nLine3", content.toString())
38+
}
39+
}
40+
41+
@Test
42+
fun `test text method normalizes CR line endings`() {
43+
runBlocking {
44+
val testFile = tempDir.resolve("cr.txt")
45+
testFile.writeText("Line1\rLine2\rLine3")
46+
47+
val content = JVMDocumentProvider.text(testFile)
48+
assertEquals("Line1\nLine2\nLine3", content.toString())
49+
}
50+
}
51+
52+
@Test
53+
fun `test setText method replaces entire file content`() {
54+
runBlocking {
55+
val testFile = tempDir.resolve("setText.txt")
56+
testFile.writeText("Original content")
57+
58+
JVMDocumentProvider.Edit.setText(testFile, "New content", null)
59+
60+
val newContent = testFile.readText()
61+
assertEquals("New content", newContent.normalizeForAssertion())
62+
}
63+
}
64+
65+
@Test
66+
fun `test setText method replaces content in range`() {
67+
runBlocking {
68+
val testFile = tempDir.resolve("setTextRange.txt")
69+
testFile.writeText("Hello\nWorld")
70+
71+
val range = DocumentRange(
72+
Position(0, 0),
73+
Position(0, 5)
74+
)
75+
76+
JVMDocumentProvider.Edit.setText(testFile, "Goodbye", range)
77+
78+
val newContent = testFile.readText()
79+
assertEquals("Goodbye\nWorld\n", newContent.normalizeForAssertion())
80+
}
81+
}
82+
83+
@Test
84+
fun `test setText method with multiline range`() {
85+
runBlocking {
86+
val testFile = tempDir.resolve("setTextMultiline.txt")
87+
testFile.writeText("Hello\nWorld\nTest")
88+
89+
val range = DocumentRange(
90+
Position(0, 0),
91+
Position(1, 5)
92+
)
93+
94+
JVMDocumentProvider.Edit.setText(testFile, "Replaced", range)
95+
96+
val newContent = testFile.readText()
97+
assertEquals("Replaced\nTest\n", newContent.normalizeForAssertion())
98+
}
99+
}
100+
101+
@Test
102+
fun `test setText method with empty text`() {
103+
runBlocking {
104+
val testFile = tempDir.resolve("setTextEmpty.txt")
105+
testFile.writeText("Hello\nWorld")
106+
107+
val range = DocumentRange(
108+
Position(0, 0),
109+
Position(0, 5)
110+
)
111+
112+
JVMDocumentProvider.Edit.setText(testFile, "", range)
113+
114+
val newContent = testFile.readText()
115+
assertEquals("\nWorld\n", newContent.normalizeForAssertion())
116+
}
117+
}
118+
119+
@Test
120+
fun `test setText method with invalid range throws exception`() {
121+
runBlocking {
122+
val testFile = tempDir.resolve("setTextInvalid.txt")
123+
testFile.writeText("Hello\nWorld")
124+
125+
val range = DocumentRange(
126+
Position(0, 10),
127+
Position(0, 5)
128+
)
129+
130+
assertThrows<IllegalArgumentException> {
131+
JVMDocumentProvider.Edit.setText(testFile, "test", range)
132+
}
133+
}
134+
}
135+
136+
@Test
137+
fun `test text method with empty file`() {
138+
runBlocking {
139+
val emptyFile = tempDir.resolve("empty.txt")
140+
emptyFile.writeText("")
141+
142+
val content = JVMDocumentProvider.text(emptyFile)
143+
assertEquals("", content.toString())
144+
}
145+
}
146+
147+
@Test
148+
fun `test setText method with empty file`() {
149+
runBlocking {
150+
val emptyFile = tempDir.resolve("emptySetText.txt")
151+
emptyFile.writeText("")
152+
153+
JVMDocumentProvider.Edit.setText(emptyFile, "New content", null)
154+
155+
val newContent = emptyFile.readText()
156+
assertEquals("New content", newContent.normalizeForAssertion())
157+
}
158+
}
159+
160+
@Test
161+
fun `test setText method with file containing only newlines`() {
162+
runBlocking {
163+
val newlinesFile = tempDir.resolve("newlines.txt")
164+
newlinesFile.writeText("\n\n\n")
165+
166+
val range = DocumentRange(
167+
Position(1, 0),
168+
Position(1, 0)
169+
)
170+
171+
JVMDocumentProvider.Edit.setText(newlinesFile, "Inserted", range)
172+
173+
val newContent = newlinesFile.readText()
174+
assertEquals("\nInserted\n\n", newContent.normalizeForAssertion())
175+
}
176+
}
177+
178+
@Test
179+
fun `test setText method modifying last character of file`() {
180+
runBlocking {
181+
val testFile = tempDir.resolve("lastChar.txt")
182+
testFile.writeText("Hello")
183+
184+
val range = DocumentRange(
185+
Position(0, 4),
186+
Position(0, 5)
187+
)
188+
189+
JVMDocumentProvider.Edit.setText(testFile, "p", range)
190+
191+
val newContent = testFile.readText()
192+
assertEquals("Hellp\n", newContent.normalizeForAssertion())
193+
}
194+
}
195+
}

rag/rag-base/src/jvmTest/kotlin/ai/koog/rag/base/files/KoogTestBase.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,8 @@ open class KoogTestBase {
113113
}
114114
return current
115115
}
116+
117+
fun String.normalizeForAssertion(): String {
118+
return this.replace("\r\n", "\n").replace("\r", "\n")
119+
}
116120
}

0 commit comments

Comments
 (0)