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\n Line2\r\n Line3" )
35+
36+ val content = JVMDocumentProvider .text(testFile)
37+ assertEquals(" Line1\n Line2\n Line3" , 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\r Line2\r Line3" )
46+
47+ val content = JVMDocumentProvider .text(testFile)
48+ assertEquals(" Line1\n Line2\n Line3" , 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\n World" )
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\n World\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\n World\n Test" )
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\n Test\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\n World" )
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(" \n World\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\n World" )
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(" \n Inserted\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+ }
0 commit comments