|
| 1 | +import io.github.easy4j.opencli.OpenCliExecutionTarget; |
| 2 | +import io.github.easy4j.opencli.OpenCliProperties; |
| 3 | +import io.github.easy4j.opencli.core.OpenCliExecutor; |
| 4 | +import io.github.easy4j.opencli.core.OpenCliRemoteExecutionDetails; |
| 5 | +import io.github.easy4j.opencli.core.OpenCliResult; |
| 6 | +import io.github.easy4j.opencli.exception.OpenCliNonZeroExitException; |
| 7 | +import java.net.URI; |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +public final class OpenCliAdminLoopbackProbe { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + URI endpoint = URI.create(args[0]); |
| 14 | + require("127.0.0.1".equals(endpoint.getHost()), "test Agent must be on IPv4 loopback"); |
| 15 | + try (OpenCliExecutor executor = executor(endpoint, false)) { |
| 16 | + OpenCliResult help = executor.invoke("npm", "search", "fixture", "--help=true", "-f", "json"); |
| 17 | + verify(help, true); |
| 18 | + require(help.getStdout().contains("opencli npm search"), "structured CLI help is missing"); |
| 19 | + require(Objects.isNull(help.getRemoteRawHttpBody()), "raw capture must be off by default"); |
| 20 | + |
| 21 | + try { |
| 22 | + executor.invoke("npm", "search", "-f", "json"); |
| 23 | + throw new AssertionError("missing CLI query unexpectedly succeeded"); |
| 24 | + } catch (OpenCliNonZeroExitException failure) { |
| 25 | + OpenCliResult result = failure.getPartialResult(); |
| 26 | + verify(result, false); |
| 27 | + require(Objects.isNull(result.getRemoteRawHttpBody()), "failed response was captured by default"); |
| 28 | + require(result.getStderr().contains("opencli exit"), "the Agent's business error is missing"); |
| 29 | + } |
| 30 | + } |
| 31 | + try (OpenCliExecutor executor = executor(endpoint, true)) { |
| 32 | + OpenCliResult help = executor.invoke("npm", "search", "fixture", "--help=true", "-f", "json"); |
| 33 | + verify(help, true); |
| 34 | + require(Objects.nonNull(help.getRemoteRawHttpBody()) && help.getRemoteRawHttpBody().contains("opencli npm search"), |
| 35 | + "explicit capture did not retain the original Agent body"); |
| 36 | + } |
| 37 | + System.out.println("PASS: real Agent help, missing-argument failure, and opt-in raw HTTP capture"); |
| 38 | + } |
| 39 | + |
| 40 | + private static OpenCliExecutor executor(URI endpoint, boolean capture) { |
| 41 | + OpenCliProperties properties = new OpenCliProperties(); |
| 42 | + properties.setExecutionTarget(OpenCliExecutionTarget.REMOTE_AGENT_HTTP); |
| 43 | + properties.setRemoteAgentBaseUrl(endpoint.toString()); |
| 44 | + properties.setRemoteCollectMode("bridge"); |
| 45 | + properties.setRemoteCaptureRawHttpResponse(capture); |
| 46 | + return new OpenCliExecutor(properties); |
| 47 | + } |
| 48 | + |
| 49 | + private static void verify(OpenCliResult result, boolean success) { |
| 50 | + require(result.isSuccess() == success, "wrong business status"); |
| 51 | + require(Integer.valueOf(success ? 0 : 1).equals(result.getExitCode()), "wrong mapped exit value"); |
| 52 | + OpenCliRemoteExecutionDetails details = result.getRemoteExecutionDetails(); |
| 53 | + require(details.getObservedHttpStatus().orElse(-1) == 200, "HTTP status was lost"); |
| 54 | + require(details.getOutcome() == (success ? OpenCliRemoteExecutionDetails.Outcome.SUCCESS |
| 55 | + : OpenCliRemoteExecutionDetails.Outcome.COMMAND_FAILURE), "wrong outcome classification"); |
| 56 | + require(details.getExitCodeSource() == OpenCliRemoteExecutionDetails.ExitCodeSource.SYNTHETIC_COMPATIBILITY, |
| 57 | + "legacy mapping must be marked synthetic"); |
| 58 | + require(!details.getObservedRemoteExitCode().isPresent(), "legacy response contains no CLI exit field"); |
| 59 | + require(Objects.isNull(result.getExecutionDetails()), "legacy response contains no local process evidence"); |
| 60 | + } |
| 61 | + |
| 62 | + private static void require(boolean condition, String message) { |
| 63 | + if (!condition) { |
| 64 | + throw new AssertionError(message); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments