beads/cmd/bd/mol_wisp_proxied_var_validation_test.go
matt wilkie 2af843a601
fix(formula): validate provided-empty and enum/pattern var values in pour/wisp/bond, not just cook (#5253)
* fix(formula): validate enum/pattern for explicitly-empty --var values (mybd-s8vb)

ValidateVars conflated "not provided" with "provided empty": any val==""
skipped enum/pattern/required-empty checks entirely, so `--var policy=`
(e.g. from an unset shell variable) silently passed validation and
substituted an empty string. Now only a genuinely absent var (not present
in the values map) skips further validation; a var explicitly provided as
empty is checked against required/enum/pattern like any other value.

Agent-Signature: claude-sonnet-5-medium on behalf of matt wilkie

* fix(formula): enforce enum/pattern var validation in pour/wisp (mybd-u2r6)

bd mol pour and bd mol wisp only ever checked variable *presence*
(extractRequiredVariables), never the enum/pattern constraints cook
already enforced in runtime mode. A --var value violating a declared
enum or pattern silently passed through both commands.

resolveAndCookFormulaWithVars (shared by cook, pour, wisp, mol bond, and
mol seed) now calls formula.ValidateProvidedVars on the caller-supplied
vars right after formula resolution. That helper is deliberately
presence-agnostic: it only validates variables present in the map,
leaving "entirely missing" errors to each caller's own UX.

pour/wisp still need their friendlier HandleErrorWithHint missing-var
message to fire for genuinely absent vars, so a new formula.ErrVarValidation
sentinel lets them distinguish "not a formula at all" (fall through to the
legacy proto-ID lookup) from "is a formula, but the given values are
invalid" (report directly instead of masking it as "not found").

Agent-Signature: claude-sonnet-5-medium on behalf of matt wilkie

* fix(formula): close review gaps in proxied-server var validation (mybd-u2r6)

- mol/wisp proxied-server pour paths swallowed formula.ErrVarValidation and
  fell through to proto-ID resolution, reporting a misleading "not found"
  error instead of the enum/pattern/required-empty violation. Add the same
  errors.Is(err, formula.ErrVarValidation) branch pour.go/wisp.go already use.
- mol_seed.go verifyFormula and mol_bond.go resolveOrCookToSubgraph
  double-wrapped ErrVarValidation in a misleading "not accessible"/"not
  found" message; return it unwrapped instead.
- Fix a stale comment in cook.go: ValidateProvidedVars's shared path does
  not cover runCook, which validates separately via its own
  formula.ValidateVars call under --mode=runtime.
- Add a pour test for the bead's headline repro (--var policy=$UNSET_SHELL_VAR,
  i.e. an explicitly-empty provided value) and hermetic proxied-server
  pour/wisp-create tests (Docker-backed shared dolt container) covering the
  same swallowed-error regression on those paths.

Agent-Signature: claude-sonnet-5-medium on behalf of matt wilkie

* fix(formula): treat no-default vars as required for the provided-empty check

Codex review round: a var with no default is effectively required — the
command paths (extractRequiredVariables) demand a value for it — so
--var name= (an unset shell variable) must be rejected the same way as for
required=true. Defaulted, unconstrained vars still tolerate explicit empty.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie

* fix(mol): validate --var values in bond dry-run resolution too

Codex review round: bd mol bond --dry-run resolved formulas through
resolveOrDescribe, which never saw the vars, so a preview reported success
for --var values the identical real command rejects. Both local and
proxied dry-run paths now validate provided vars at resolution.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie
2026-08-01 17:16:57 -07:00

79 lines
2.8 KiB
Go

//go:build cgo
package main
import (
"strings"
"testing"
"github.com/steveyegge/beads/internal/formula"
)
// Regression coverage for mybd-u2r6 on the proxied-server pour/wisp paths
// (runPourProxiedServer / runWispCreateProxiedServer). The non-proxied
// runPour/runWispCreate paths already distinguish "arg IS a formula but the
// --var values violate an enum/pattern/required-empty constraint" (report
// formula.ErrVarValidation directly) from "arg is not a formula at all" (fall
// through to proto-ID resolution) — see pour_wisp_var_validation_test.go. The
// proxied-server variants swallowed that same error and fell through
// regardless, producing a misleading "not found as formula or proto ID"/"not
// found as formula or proto" message instead of the validation error.
func varValidationFixtureFormula(name string) *formula.Formula {
return &formula.Formula{
Formula: name,
Version: 1,
Type: formula.TypeWorkflow,
Vars: map[string]*formula.VarDef{
"policy": {
Required: true,
Enum: []string{"merge-completes", "tracking-only"},
},
},
Steps: []*formula.Step{
{ID: "publish", Title: "Publish with {{policy}}", Type: "task"},
},
}
}
func TestProxiedServerPourRejectsEnumViolatingVar(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "ppvv")
writeFormulaFixture(t, p, varValidationFixtureFormula("proxied-pour-var-validation-test"))
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "pour", "proxied-pour-var-validation-test", "--var", "policy=bogus")
if err == nil {
t.Fatalf("expected an enum-violation error, got stdout:%s stderr:%s", stdout, stderr)
}
combined := stdout + stderr
if !strings.Contains(combined, "not in allowed values") {
t.Errorf("expected an enum-violation message, got: %s", combined)
}
if strings.Contains(combined, "not found as formula or proto ID") {
t.Errorf("validation error was masked by the proto-ID fallback message: %s", combined)
}
}
func TestProxiedServerWispCreateRejectsEnumViolatingVar(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
p := newSharedProxiedProject(t, bd, "pwvv")
writeFormulaFixture(t, p, varValidationFixtureFormula("proxied-wisp-var-validation-test"))
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "wisp", "create", "proxied-wisp-var-validation-test", "--var", "policy=bogus")
if err == nil {
t.Fatalf("expected an enum-violation error, got stdout:%s stderr:%s", stdout, stderr)
}
combined := stdout + stderr
if !strings.Contains(combined, "not in allowed values") {
t.Errorf("expected an enum-violation message, got: %s", combined)
}
if strings.Contains(combined, "not found as formula or proto") {
t.Errorf("validation error was masked by the proto-ID fallback message: %s", combined)
}
}