|
| 1 | +package com.devoxx.genie.service.cli.command; |
| 2 | + |
| 3 | +import com.devoxx.genie.model.spec.CliToolConfig; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.condition.EnabledIf; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.List; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +/** |
| 16 | + * Integration test that verifies Kimi CLI can be launched via ProcessBuilder |
| 17 | + * with the --prompt flag. Requires Kimi installed at /Users/stephan/.local/bin/kimi. |
| 18 | + */ |
| 19 | +class KimiCliCommandIT { |
| 20 | + |
| 21 | + private static final String KIMI_PATH = "/Users/stephan/.local/bin/kimi"; |
| 22 | + |
| 23 | + static boolean kimiInstalled() { |
| 24 | + return new java.io.File(KIMI_PATH).canExecute(); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + @EnabledIf("kimiInstalled") |
| 29 | + void testBuildProcessCommand() { |
| 30 | + KimiCliCommand command = new KimiCliCommand(); |
| 31 | + CliToolConfig config = CliToolConfig.builder() |
| 32 | + .type(CliToolConfig.CliType.KIMI) |
| 33 | + .executablePath(KIMI_PATH) |
| 34 | + .extraArgs(List.of("--yolo")) |
| 35 | + .mcpConfigFlag("--mcp-config-file") |
| 36 | + .build(); |
| 37 | + |
| 38 | + List<String> cmd = command.buildProcessCommand(config, "say hello", "/tmp/mcp.json"); |
| 39 | + |
| 40 | + assertThat(cmd).containsExactly( |
| 41 | + KIMI_PATH, |
| 42 | + "--yolo", |
| 43 | + "--mcp-config-file", "/tmp/mcp.json", |
| 44 | + "--prompt", "say hello" |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @EnabledIf("kimiInstalled") |
| 50 | + void testKimiProcessStartsWithPromptFlag() throws Exception { |
| 51 | + KimiCliCommand command = new KimiCliCommand(); |
| 52 | + CliToolConfig config = CliToolConfig.builder() |
| 53 | + .type(CliToolConfig.CliType.KIMI) |
| 54 | + .executablePath(KIMI_PATH) |
| 55 | + .extraArgs(List.of("--yolo")) |
| 56 | + .mcpConfigFlag("") |
| 57 | + .build(); |
| 58 | + |
| 59 | + List<String> cmd = command.buildProcessCommand(config, "Respond with only: OK", null); |
| 60 | + |
| 61 | + ProcessBuilder pb = new ProcessBuilder(cmd); |
| 62 | + pb.redirectErrorStream(false); |
| 63 | + pb.environment().putAll(System.getenv()); |
| 64 | + |
| 65 | + Process process = pb.start(); |
| 66 | + |
| 67 | + // Don't close stdin — Kimi crashes with BrokenPipeError if stdin is closed |
| 68 | + command.writePrompt(process, "Respond with only: OK"); |
| 69 | + |
| 70 | + // Read first few lines of stdout |
| 71 | + StringBuilder stdout = new StringBuilder(); |
| 72 | + Thread stdoutReader = new Thread(() -> { |
| 73 | + try (BufferedReader reader = new BufferedReader( |
| 74 | + new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { |
| 75 | + String line; |
| 76 | + int count = 0; |
| 77 | + while ((line = reader.readLine()) != null && count < 20) { |
| 78 | + stdout.append(line).append("\n"); |
| 79 | + count++; |
| 80 | + } |
| 81 | + } catch (Exception ignored) {} |
| 82 | + }); |
| 83 | + |
| 84 | + StringBuilder stderr = new StringBuilder(); |
| 85 | + Thread stderrReader = new Thread(() -> { |
| 86 | + try (BufferedReader reader = new BufferedReader( |
| 87 | + new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) { |
| 88 | + String line; |
| 89 | + int count = 0; |
| 90 | + while ((line = reader.readLine()) != null && count < 20) { |
| 91 | + stderr.append(line).append("\n"); |
| 92 | + count++; |
| 93 | + } |
| 94 | + } catch (Exception ignored) {} |
| 95 | + }); |
| 96 | + |
| 97 | + stdoutReader.setDaemon(true); |
| 98 | + stderrReader.setDaemon(true); |
| 99 | + stdoutReader.start(); |
| 100 | + stderrReader.start(); |
| 101 | + |
| 102 | + boolean exited = process.waitFor(60, TimeUnit.SECONDS); |
| 103 | + if (!exited) { |
| 104 | + process.destroyForcibly(); |
| 105 | + } |
| 106 | + |
| 107 | + stdoutReader.join(3000); |
| 108 | + stderrReader.join(3000); |
| 109 | + |
| 110 | + System.out.println("=== STDOUT ==="); |
| 111 | + System.out.println(stdout); |
| 112 | + System.out.println("=== STDERR ==="); |
| 113 | + System.out.println(stderr); |
| 114 | + System.out.println("=== EXIT CODE: " + (exited ? process.exitValue() : "TIMEOUT") + " ==="); |
| 115 | + |
| 116 | + // The process should not crash with BrokenPipeError |
| 117 | + assertThat(stderr.toString()).doesNotContain("BrokenPipeError"); |
| 118 | + } |
| 119 | +} |
0 commit comments