-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathCodexCliProviderTest.kt
More file actions
141 lines (119 loc) · 4.81 KB
/
Copy pathCodexCliProviderTest.kt
File metadata and controls
141 lines (119 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package net.portswigger.mcp.providers
import burp.api.montoya.logging.Logging
import burp.api.montoya.persistence.PersistedObject
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import net.portswigger.mcp.config.McpConfig
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
class CodexCliProviderTest {
@TempDir
lateinit var tempDir: Path
private lateinit var logging: Logging
private lateinit var config: McpConfig
@BeforeEach
fun setup() {
val storage = mutableMapOf<String, Any>()
val persistedObject = mockk<PersistedObject>().apply {
every { getBoolean(any()) } answers {
val key = firstArg<String>()
storage[key] as? Boolean ?: when (key) {
"enabled" -> true
else -> false
}
}
every { getString(any()) } answers {
val key = firstArg<String>()
storage[key] as? String ?: when (key) {
"host" -> "127.0.0.1"
else -> ""
}
}
every { getInteger(any()) } answers {
val key = firstArg<String>()
storage[key] as? Int ?: when (key) {
"port" -> 9876
else -> 0
}
}
every { setBoolean(any(), any()) } answers {
storage[firstArg()] = secondArg<Boolean>()
}
every { setString(any(), any()) } answers {
storage[firstArg()] = secondArg<String>()
}
every { setInteger(any(), any()) } answers {
storage[firstArg()] = secondArg<Int>()
}
}
logging = mockk<Logging>().apply {
every { logToOutput(any<String>()) } returns Unit
every { logToError(any<String>()) } returns Unit
}
config = McpConfig(persistedObject, logging)
}
@Test
fun `install should create Codex config when missing`() {
val proxyJarManager = mockk<ProxyJarManager>().apply {
every { getProxyJar() } returns Path.of("/tmp/mcp-proxy-all.jar")
}
val provider = CodexCliProvider(logging, proxyJarManager, tempDir)
val result = provider.install(config)
val configPath = tempDir.resolve(".codex").resolve("config.toml")
val content = configPath.toFile().readText()
assertTrue(configPath.toFile().exists())
assertEquals("Installation successful. Please restart Codex CLI if it is currently running.", result)
assertTrue(content.contains("[mcp_servers.burp]"))
assertTrue(content.contains("command = \"java\""))
assertTrue(content.contains("/tmp/mcp-proxy-all.jar"))
assertTrue(content.contains("http://127.0.0.1:9876"))
verify { logging.logToOutput("Installed Burp MCP Server to Codex CLI config") }
}
@Test
fun `install should replace existing burp table and preserve other config`() {
val configPath = tempDir.resolve(".codex").resolve("config.toml")
configPath.parent.toFile().mkdirs()
configPath.toFile().writeText(
"""
model = "gpt-5-codex"
[features]
multi_agent = true
[mcp_servers.burp]
command = "java"
args = ["-jar", "/tmp/old.jar", "--sse-url", "http://localhost:9999"]
[mcp_servers.other]
command = "uvx"
args = ["context7"]
""".trimIndent()
)
val proxyJarManager = mockk<ProxyJarManager>().apply {
every { getProxyJar() } returns Path.of("/tmp/mcp-proxy-all.jar")
}
val provider = CodexCliProvider(logging, proxyJarManager, tempDir)
provider.install(config)
val content = configPath.toFile().readText()
assertTrue(content.contains("model = \"gpt-5-codex\""))
assertTrue(content.contains("[features]"))
assertTrue(content.contains("[mcp_servers.other]"))
assertTrue(content.contains("/tmp/mcp-proxy-all.jar"))
assertTrue(!content.contains("/tmp/old.jar"))
}
@Test
fun `upsertTomlTable should append missing table`() {
val provider = CodexCliProvider(logging, mockk(relaxed = true), tempDir)
val updated = provider.upsertTomlTable(
existingContent = "model = \"gpt-5-codex\"\n",
tableHeader = "[mcp_servers.burp]",
newBlock = "[mcp_servers.burp]\ncommand = \"java\""
)
assertEquals(
"model = \"gpt-5-codex\"\n\n[mcp_servers.burp]\ncommand = \"java\"\n",
updated
)
}
}