diff --git a/packages/cli/tests/case2-unit.test.mjs b/packages/cli/tests/case2-unit.test.mjs index fa8e8aaf4..6810cb4e6 100644 --- a/packages/cli/tests/case2-unit.test.mjs +++ b/packages/cli/tests/case2-unit.test.mjs @@ -107,9 +107,9 @@ const libraryFlags = (h) => ["--port", "443", "--business", "acme", "--public-ho "--allow-user", "melanie", "--proof-header", "X-Proxy-Proof", "--proof-file", h.proof]; const factoryFlags = () => ["--port", "443", "--business", "acme", "--tester-socket"]; -function unit(h, { profile = "library", node = USR_NODE, copy = COPY_A, extra = [], flags, env = {} } = {}) { +function unit(h, { profile = "library", node = USR_NODE, copy = COPY_A, extra = [], flags, env = {}, outer = ["--business", "acme"], argv } = {}) { const serve = flags ?? (profile === "factory" ? factoryFlags() : libraryFlags(h)); - const args = ["--user", "mosaic", "--home", h.home, "--business", "acme", "--copy", copy, "--node", node, "--profile", profile, ...extra, "--", ...serve]; + const args = argv ?? ["--user", "mosaic", "--home", h.home, ...outer, "--copy", copy, "--node", node, "--profile", profile, ...extra, "--", ...serve]; const r = spawnSync(UNIT, args, { encoding: "utf8", timeout: 30000, @@ -338,6 +338,71 @@ test("a clean read-only library render writes both units 0644 with the fixed lin ]); }); +// The selector (rename design r2): --org and --business are one option in each of two scopes, the +// script's own flags before -- and the serve flags after it. Nothing is counted across --; the serve +// selector is required, must be the unit's business, and renders as --business. +const SELECTORS = [["--business", "acme"], ["--org", "acme"], ["--business", "acme", "--org", "acme"], ["--org", "acme", "--business", "acme"]]; + +test("the unit takes --org or --business on either side of --, renders --business once, and re-installs as the same units", (t) => { + const h = hub(t); + assert.equal(unit(h, { profile: "factory", flags: ["--port", "443", "--business", "acme"] }).code, 0); + const before = [read(busFile(h)), read(webuiFile(h))]; + assert.deepEqual(lines(before[1], "ExecStart"), [`${USR_NODE} ${copyDir(h)}/packages/webui/src/cli.mjs serve --port 443 --business acme`]); + for (const outer of SELECTORS) { + for (const child of SELECTORS) { + const r = unit(h, { profile: "factory", outer, flags: ["--port", "443", ...child] }); + assert.equal(r.code, 0, `${outer.join(" ")} -- ${child.join(" ")}: ${r.err}`); + assert.deepEqual([read(busFile(h)), read(webuiFile(h))], before, `${outer.join(" ")} -- ${child.join(" ")}`); + } + } + // The selector keeps its place among the serve flags. + const g = hub(t); + assert.equal(unit(g, { profile: "factory", outer: ["--org", "acme"], flags: ["--org", "acme", "--port", "443", "--business", "acme"] }).code, 0); + assert.deepEqual(lines(read(webuiFile(g)), "ExecStart"), [`${USR_NODE} ${copyDir(g)}/packages/webui/src/cli.mjs serve --business acme --port 443`]); +}); + +test("the unit refuses a bad selector in either scope, and an outer and serve pair that differ, before writing a unit", (t) => { + const h = hub(t); + const serve = ["--port", "443", "--business", "acme"]; + const usage = "usage: case2-unit.sh --user NAME --home DIR --business ID --copy ID --node PATH [--profile factory|library] [--proxy] [--pm] [--claude PATH] -- SERVE FLAGS\n"; + const head = ["--user", "mosaic", "--home", h.home, "--copy", COPY_A, "--node", USR_NODE, "--profile", "factory"]; + for (const [label, argv, code, err] of [ + ["outer unequal, old first", [...head, "--business", "acme", "--org", "beta", "--", ...serve], 4, "case2-unit: --business acme and --org beta conflict\n"], + ["outer unequal, new first", [...head, "--org", "acme", "--business", "beta", "--", ...serve], 4, "case2-unit: --org acme and --business beta conflict\n"], + ["outer old repeated", [...head, "--business", "acme", "--business", "acme", "--", ...serve], 4, "case2-unit: --business given twice\n"], + ["outer new repeated", [...head, "--org", "acme", "--org", "acme", "--", ...serve], 4, "case2-unit: --org given twice\n"], + ["outer missing", [...head, "--org"], 4, usage], + ["outer empty", [...head, "--org", "", "--", ...serve], 4, usage], + ["outer none", [...head, "--", ...serve], 4, usage], + ["outer invalid", [...head, "--org", "Acme", "--", ...serve], 1, "case2-unit: --business must be a business id: Acme\n"], + ["outer flag-shaped", [...head, "--business", "-x", "--", ...serve], 1, "case2-unit: --business must be a business id: -x\n"], + ["outer takes no separator", [...head, "--org", "--", ...serve], 1, "case2-unit: --business must be a business id: --\n"], + ["serve unequal, old first", [...head, "--business", "acme", "--", "--port", "443", "--business", "acme", "--org", "beta"], 1, "case2-unit: serve --business acme and --org beta conflict\n"], + ["serve unequal, new first", [...head, "--business", "acme", "--", "--port", "443", "--org", "acme", "--business", "beta"], 1, "case2-unit: serve --org acme and --business beta conflict\n"], + ["serve old repeated", [...head, "--business", "acme", "--", "--port", "443", "--business", "acme", "--business", "acme"], 1, "case2-unit: serve flag --business given twice\n"], + ["serve new repeated", [...head, "--business", "acme", "--", "--port", "443", "--org", "acme", "--org", "acme"], 1, "case2-unit: serve flag --org given twice\n"], + ["serve missing", [...head, "--business", "acme", "--", "--port", "443", "--org"], 1, "case2-unit: serve flag --org needs a value\n"], + ["serve empty", [...head, "--business", "acme", "--", "--port", "443", "--org", ""], 1, "case2-unit: serve flag --org needs a value\n"], + ["serve none", [...head, "--org", "acme", "--", "--port", "443"], 1, "case2-unit: serve flag --business is required\n"], + ["serve invalid", [...head, "--business", "acme", "--", "--port", "443", "--org", "Acme"], 1, "case2-unit: serve --org Acme is not the unit's business acme\n"], + ["serve flag-shaped", [...head, "--business", "acme", "--", "--port", "443", "--org", "--tester-socket"], 1, "case2-unit: serve --org --tester-socket is not the unit's business acme\n"], + ["outer and serve differ", [...head, "--org", "acme", "--", "--port", "443", "--org", "beta"], 1, "case2-unit: serve --org beta is not the unit's business acme\n"], + ["serve and outer differ", [...head, "--org", "beta", "--", "--port", "443", "--business", "acme"], 1, "case2-unit: serve --business acme is not the unit's business beta\n"], + ["a selector as --copy's value", ["--user", "mosaic", "--home", h.home, "--business", "acme", "--copy", "--org", "--node", USR_NODE, "--profile", "factory", "--", ...serve], 1, "case2-unit: --copy must be a full 40-character lower-case commit id: --org\n"], + ]) { + const r = unit(h, { argv }); + assert.equal(r.code, code, `${label}: ${r.err}`); + assert.equal(r.err, err, label); + assert.equal(r.out, "", label); + } + // A selector spelling taken as a library serve flag's value is that flag's text. + const library = libraryFlags(h).map((f) => (f === "books.example" ? "--org" : f)); + refuses(unit(h, { flags: library }), /^case2-unit: public host must be a hostname, no port or scheme: --org\n$/); + assert.equal(existsSync(busFile(h)), false); + assert.equal(existsSync(webuiFile(h)), false); + assert.equal(existsSync(h.log), false); +}); + test("the read-only library profile explicitly refuses --send before writing a unit", (t) => { const h = hub(t); const r = unit(h, { flags: [...libraryFlags(h), "--send"] }); diff --git a/scripts/case2-unit.sh b/scripts/case2-unit.sh index 85371cacc..1151a774d 100755 --- a/scripts/case2-unit.sh +++ b/scripts/case2-unit.sh @@ -9,6 +9,12 @@ # [--profile factory|library] [--proxy] [--pm] [--claude PATH] # -- SERVE FLAGS # +# --org ID is the same option as --business ID (rename design r2), in each of +# two scopes: this script's own flags before --, and the serve flags after it. +# Each scope takes each spelling once, and a second spelling the same value; +# nothing is counted across --. The serve flags must name the selector, it must +# be the unit's business, and the unit renders it as --business ID. +# # Root runs it from a root-owned checkout of the reviewed commit outside # , never from the release, because can write the release. It # writes the bus and Console units (and the proxy unit under --proxy) under @@ -77,14 +83,35 @@ abs_plain() { [ "$1" = / ] || [ "${1%/}" = "$1" ] || die "$2 must not end in /: $1" } +# shellcheck source=scripts/selector.sh +. "$REPO/scripts/selector.sh" +business_id_ok() { [[ "$1" =~ ^[a-z0-9][a-z0-9._-]{0,63}$ ]]; } +# The outer selector: a missing value is a usage error, a flag-shaped or invalid one the +# business-id refusal, and a repeated or conflicting one a usage error that says why. +outer_refused() { + case "$1" in + missing) usage ;; + flag|invalid) die "--business must be a business id: $BUSINESS" ;; + *) echo "case2-unit: $2" >&2; exit 4 ;; + esac +} +# The serve selector: every refusal names the serve flag, as the other serve flags do. +serve_refused() { + case "$1" in + missing) die "serve flag $SERVE_NAME needs a value" ;; + flag|invalid) die "serve $SERVE_NAME $SERVE_VALUE is not the unit's business $BUSINESS" ;; + *) die "serve $2" ;; + esac +} PROFILE=library USER_NAME='' HOME_DIR='' BUSINESS='' COPY_ID='' NODE='' PROXY=0 PM=0 CLAUDE='' CLAUDE_SET=0 while [ $# -gt 0 ]; do case "$1" in - --profile|--user|--home|--business|--copy|--node|--claude) + --business|--org) BUSINESS="${2-}"; selector_take outer id outer_refused business_id_ok "$1" "${@:2:1}"; shift 2 ;; + --profile|--user|--home|--copy|--node|--claude) [ $# -ge 2 ] || usage case "$1" in --profile) PROFILE="$2" ;; --user) USER_NAME="$2" ;; --home) HOME_DIR="$2" ;; - --business) BUSINESS="$2" ;; --copy) COPY_ID="$2" ;; --node) NODE="$2" ;; + --copy) COPY_ID="$2" ;; --node) NODE="$2" ;; --claude) CLAUDE="$2"; CLAUDE_SET=1 ;; esac shift 2 ;; @@ -346,7 +373,7 @@ tester_location() { serve_flags() { local -A seen=() local flags='' name value - local port='' public_host='' auth_header='' allow_user='' proof_header='' proof_file='' tester_socket=0 sso=0 + local port='' public_host='' auth_header='' allow_user='' proof_header='' proof_file='' tester_socket=0 sso=0 selected=0 local i=0 n=${#SERVE[@]} while [ "$i" -lt "$n" ]; do name="${SERVE[$i]}" @@ -357,7 +384,7 @@ serve_flags() { --board) die "refusing --board: Case 2 runs no control board" ;; --sso) [ "$PROFILE" = factory ] || die "refusing --sso: the Console signs people in through the proxy, with the preview set and the proof pair (Q175 phase 2 is a follow-up)" ;; --send) [ "$PROFILE" = factory ] || die "the library profile is read-only; refusing --send until Case 2 supports --sso (#412)" ;; - --port|--business) ;; + --port|--business|--org) ;; --public-host|--auth-header|--allow-user|--proof-header|--proof-file) [ "$PROFILE" = library ] || die "the factory profile refuses $name: its host has no proxy" ;; *) die "unknown serve flag: $name" ;; @@ -369,10 +396,20 @@ serve_flags() { if [ "$name" = --send ]; then flags="$flags --send"; i=$((i + 1)); continue; fi [ $((i + 1)) -lt "$n" ] || die "serve flag $name needs a value" value="${SERVE[$((i + 1))]}" + if [ "$name" = --business ] || [ "$name" = --org ]; then + # One selector in the serve scope, bound to the unit's business, rendered once + # as --business where it first appears. + SERVE_NAME="$name" SERVE_VALUE="$value" + selector_take serve id serve_refused business_id_ok "$name" "$value" + [ "$value" = "$BUSINESS" ] || die "serve $name $value is not the unit's business $BUSINESS" + [ "$selected" = 1 ] || flags="$flags --business $value" + selected=1 + i=$((i + 2)) + continue + fi case "$name" in --port) port="$value" [[ "$port" =~ ^[1-9][0-9]{0,3}$ ]] && [ "$port" -le 1023 ] || die "--port must be 1..1023, so only the unit's capability can bind it: $port" ;; - --business) [ "$value" = "$BUSINESS" ] || die "serve --business $value is not the unit's business $BUSINESS" ;; --public-host) public_host="$value" case "$value" in [A-Za-z0-9]*) ;; *) die "public host must be a hostname, no port or scheme: $value" ;; esac case "$value" in *[!A-Za-z0-9.-]*) die "public host must be a hostname, no port or scheme: $value" ;; esac ;; @@ -392,7 +429,7 @@ serve_flags() { local required=(--port --business) [ "$PROFILE" = factory ] || required+=(--public-host --auth-header --allow-user --proof-header --proof-file) for name in "${required[@]}"; do - [ -n "${seen[$name]:-}" ] || die "serve flag $name is required" + [ -n "${seen[$name]:-}" ] || { [ "$name" = --business ] && [ -n "${seen[--org]:-}" ]; } || die "serve flag $name is required" done if [ "$PROFILE" = factory ]; then # #415: only the paired owner sign-in and writes. No preview header or