|
| 1 | +package io.github.easy4j.opencli.contract; |
| 2 | + |
| 3 | +import io.github.easy4j.opencli.OpenCliClient; |
| 4 | +import io.github.easy4j.opencli.OpenCliProperties; |
| 5 | +import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot; |
| 6 | +import io.github.easy4j.opencli.capability.OpenCliCapabilitySnapshot.Status; |
| 7 | +import io.github.easy4j.opencli.capability.OpenCliHelpScope; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Objects; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.io.TempDir; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +/** Opt-in real-package fixture, using the same launcher inputs as OpenCliInstalledLauncherIT. */ |
| 24 | +class OpenCliCapabilityOverrideIT { |
| 25 | + @Test |
| 26 | + void localAndPluginOverridesRefreshWithoutClaimingOriginOrExecutingHandler(@TempDir Path home) throws IOException { |
| 27 | + Path entry = requireFixture("OPENCLI_INSTALLED_ENTRY"); |
| 28 | + Path preload = home.resolve("isolated-home.mjs"); |
| 29 | + write(preload, "import os from 'node:os';\n" |
| 30 | + + "import { syncBuiltinESMExports } from 'node:module';\n" |
| 31 | + + "os.homedir = () => process.env.OPENCLI_FIXTURE_HOME;\n" |
| 32 | + + "syncBuiltinESMExports();\n"); |
| 33 | + Path marker = home.resolve("business-handler-ran"); |
| 34 | + OpenCliProperties properties = new OpenCliProperties(); |
| 35 | + properties.setExecutable(requireFixture("OPENCLI_INSTALLED_NODE").toString()); |
| 36 | + properties.setLeadingArguments(Arrays.asList("--import", preload.toString(), entry.toString())); |
| 37 | + properties.getEnvironment().put("OPENCLI_FIXTURE_HOME", home.toString()); |
| 38 | + properties.getEnvironment().put("OPENCLI_CONFIG_DIR", home.resolve("config").toString()); |
| 39 | + properties.getEnvironment().put("OPENCLI_CACHE_DIR", home.resolve("cache").toString()); |
| 40 | + properties.getEnvironment().put("OPENCLI_FIXTURE_MARKER", marker.toString()); |
| 41 | + properties.getEnvironment().put("HOME", home.toString()); |
| 42 | + properties.getEnvironment().put("CI", "1"); |
| 43 | + properties.getEnvironment().put("NO_COLOR", "1"); |
| 44 | + |
| 45 | + Path sdkHome = home.resolve(".opencli"); |
| 46 | + try (OpenCliClient client = new OpenCliClient(properties)) { |
| 47 | + OpenCliCapabilitySnapshot bundled = discover(client); |
| 48 | + assertFalse(bundled.getHelp().getRawHelp().contains("fixture metadata")); |
| 49 | + |
| 50 | + installOverride(sdkHome.resolve("clis/npm"), entry, "local fixture metadata"); |
| 51 | + OpenCliCapabilitySnapshot local = discover(client); |
| 52 | + assertTrue(local.getHelp().getRawHelp().contains("local fixture metadata")); |
| 53 | + assertNotEquals(bundled.getHelpSha256(), local.getHelpSha256()); |
| 54 | + |
| 55 | + Path plugin = installOverride(sdkHome.resolve("plugins/fixture"), entry, "plugin fixture metadata"); |
| 56 | + OpenCliCapabilitySnapshot overridden = discover(client); |
| 57 | + assertTrue(overridden.getHelp().getRawHelp().contains("plugin fixture metadata")); |
| 58 | + assertFalse(overridden.getHelp().getRawHelp().contains("local fixture metadata")); |
| 59 | + assertEquals(bundled.getConfigurationSha256(), overridden.getConfigurationSha256()); |
| 60 | + assertNotEquals(local.getHelpSha256(), overridden.getHelpSha256()); |
| 61 | + |
| 62 | + Files.delete(plugin); |
| 63 | + assertEquals(local.getHelpSha256(), discover(client).getHelpSha256()); |
| 64 | + assertFalse(Files.exists(marker), "metadata discovery must not invoke the adapter handler"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static OpenCliCapabilitySnapshot discover(OpenCliClient client) { |
| 69 | + OpenCliCapabilitySnapshot snapshot = client.discoverCapabilities(OpenCliHelpScope.SITE_NPM_SEARCH); |
| 70 | + assertEquals(Status.AVAILABLE, snapshot.getStatus(), snapshot.getReason()); |
| 71 | + assertEquals("1.8.8", snapshot.getObservedVersion()); |
| 72 | + assertEquals("UNKNOWN", snapshot.getCommandOrigin()); |
| 73 | + assertEquals("SITE_NPM_SEARCH", snapshot.getScope()); |
| 74 | + assertEquals("opencli npm search", snapshot.getHelp().getSchema().getCommand()); |
| 75 | + return snapshot; |
| 76 | + } |
| 77 | + |
| 78 | + private static Path installOverride(Path directory, Path entry, String description) throws IOException { |
| 79 | + Files.createDirectories(directory); |
| 80 | + write(directory.resolve("package.json"), "{\"type\":\"module\"}"); |
| 81 | + Path override = directory.resolve("search.js"); |
| 82 | + write(override, "import { cli, Strategy } from '" + entry.resolveSibling("registry.js").toUri() + "';\n" |
| 83 | + + "import { writeFileSync } from 'node:fs';\n" |
| 84 | + + "cli({site:'npm', name:'search', strategy:Strategy.PUBLIC, browser:false, access:'read',\n" |
| 85 | + + "description:'" + description + "',\n" |
| 86 | + + "args:[{name:'query', positional:true, required:true}],\n" |
| 87 | + + "async func() { writeFileSync(process.env.OPENCLI_FIXTURE_MARKER, 'executed'); return []; }});\n"); |
| 88 | + return override; |
| 89 | + } |
| 90 | + |
| 91 | + private static void write(Path file, String text) throws IOException { |
| 92 | + Files.write(file, text.getBytes(StandardCharsets.UTF_8)); |
| 93 | + } |
| 94 | + |
| 95 | + private static Path requireFixture(String name) { |
| 96 | + return Paths.get(Objects.requireNonNull(System.getenv(name), name + " fixture is required")); |
| 97 | + } |
| 98 | +} |
0 commit comments