Files
floci-cli/src/test/java/io/floci/cli/unit/GlobalOptionsTest.java
T
Hector Ventura 6dc8fc9df4 refactor!: unify the four product command trees behind ProductProfile (#16)
* refactor!: unify the four product command trees behind ProductProfile

Every shared command now has ONE implementation in commands/,
parameterized by ProductProfile (the single source of per-product
config: endpoint, container, image, env prefix, control prefix,
display name). The gcp/az/oci command classes become ~8-line shims
(GcpStartCommand extends StartCommand { super(ProductProfile.GCP); })
that carry only their per-product help description; the three
near-identical *GlobalOptions clones are deleted. Each product tree
drops from ~1,360 to ~530 lines and copy-drift becomes impossible.

Mechanics: per-product option defaults are constructor-set field
values rendered in help via ${DEFAULT-VALUE} (annotation defaults are
compile-time constants); commands pre-initialize their GlobalOptions
mixin with the profile (picocli uses a non-null mixin field instance
as-is). Verified against a captured baseline of 480 CLI outputs
(every --help x 5 trees, env/status/doctor JSON, exit codes, routing)
plus new pinning tests: ProductProfileTest, ProductTreesParsingTest,
DoctorCheckListTest, GlobalOptionsTest.

Behavior changes, all deliberate:
- snapshot commands in gcp/az/oci trees now perform the real
  /snapshots API calls with graceful degradation and exit 1 while the
  server lacks the endpoints (previously: note + exit 0)
- product logs/services gained --service/--mode, and services now
  resolves the endpoint from the container port mapping in all trees
- the AWS tree brands itself 'Floci AWS' in banners and help,
  matching the other products
- snapshot errors link the product issue tracker instead of TODO.md
- completion generates from spec.root() instead of a hardcoded
  FlociCli instance (output unchanged)

BREAKING CHANGE: 'floci gcp|az|oci snapshot export/import' (and bare
'floci snapshot ...' with a non-AWS default product) now exit 1
instead of 0 while server-side snapshot support is pending; CI
scripts relying on the silent success of stubbed snapshot commands
will fail.

* fix: snapshot save/load/list/delete resolve the container host port

Same port auto-detection status/services/wait use — a container started
with --port <n> is now reachable by the snapshot commands without an
explicit --endpoint. Applies to all four trees via the unified base
commands.
2026-07-28 21:45:52 -05:00

80 lines
2.9 KiB
Java

package io.floci.cli.unit;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.function.UnaryOperator;
import static org.junit.jupiter.api.Assertions.*;
class GlobalOptionsTest {
private static UnaryOperator<String> env(Map<String, String> vars) {
return vars::get;
}
@Test
void productDefaultsWhenEnvUnset() {
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of()));
assertEquals("http://localhost:4588", gcp.endpoint);
assertEquals("floci-gcp", gcp.container);
GlobalOptions oci = new GlobalOptions(ProductProfile.OCI, env(Map.of()));
assertEquals("http://localhost:4599", oci.endpoint);
assertEquals("floci-oci", oci.container);
}
@Test
void envVarOverridesDefault() {
GlobalOptions az = new GlobalOptions(ProductProfile.AZ,
env(Map.of("FLOCI_AZ_ENDPOINT", "http://remote:9999", "FLOCI_AZ_CONTAINER", "custom")));
assertEquals("http://remote:9999", az.endpoint);
assertEquals("custom", az.container);
}
@Test
void wrongProductEnvVarIsIgnored() {
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP,
env(Map.of("FLOCI_ENDPOINT", "http://aws-only:1")));
assertEquals("http://localhost:4588", gcp.endpoint);
}
@Test
void emptyEnvVarCountsAsSet() {
// Matches picocli ${VAR:-fallback} semantics: only null falls back.
GlobalOptions aws = new GlobalOptions(ProductProfile.AWS,
env(Map.of("FLOCI_ENDPOINT", "")));
assertEquals("", aws.endpoint);
}
@Test
void noArgConstructorIsAws() {
GlobalOptions options = new GlobalOptions();
assertEquals(ProductProfile.AWS, options.product);
// Cannot assert endpoint literal here: real env may set FLOCI_ENDPOINT.
}
@Test
void endpointFromPortsUsesProductFallbackPortForPortlessEndpoint() {
// Endpoint URL without a port → the product's default port must be matched.
for (ProductProfile p : new ProductProfile[]{
ProductProfile.AWS, ProductProfile.GCP, ProductProfile.AZ, ProductProfile.OCI}) {
GlobalOptions options = new GlobalOptions(p, env(Map.of()));
String result = options.endpointFromPorts(
"12345->" + p.defaultPort() + "/tcp", "http://localhost");
assertEquals("http://localhost:12345", result, p.name());
}
}
@Test
void endpointFromPortsMatchesExplicitPort() {
GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of()));
assertEquals("http://localhost:9999",
gcp.endpointFromPorts("9999->4588/tcp", "http://localhost:4588"));
assertEquals("http://localhost:4588",
gcp.endpointFromPorts("9999->1111/tcp", "http://localhost:4588"));
}
}