beads/cmd/bd/pour_wisp_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

216 lines
7.6 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/spf13/cobra"
)
// Regression coverage for mybd-u2r6: bd mol pour/wisp previously validated
// only variable *presence* (extractRequiredVariables), never the enum/pattern
// constraints cook enforces. Once resolveAndCookFormulaWithVars started
// calling formula.ValidateProvidedVars (mybd-u2r6 fix), pour/wisp needed to
// distinguish "the arg isn't a formula at all" (fall through to legacy
// proto-ID resolution) from "it IS a formula, but the given --var values
// violate a constraint" (report directly) without disturbing the missing-var
// hint path, which has its own better UX and must keep working unchanged.
const varValidationFormulaTOML = `formula = "pour-wisp-var-validation-test"
version = 1
type = "workflow"
[vars.policy]
required = true
enum = ["merge-completes", "tracking-only"]
[vars.slug]
pattern = "^[a-z]+$"
[[steps]]
id = "publish"
title = "Publish with {{policy}} / {{slug}}"
`
// writeVarValidationFormula writes the shared fixture under a temp GT_ROOT so
// pour/wisp's DefaultSearchPaths() resolution (which always passes a nil
// search-path list) discovers it by name without perturbing the real
// project's formula registry.
func writeVarValidationFormula(t *testing.T) {
t.Helper()
gtRoot := t.TempDir()
formulaDir := filepath.Join(gtRoot, ".beads", "formulas")
if err := os.MkdirAll(formulaDir, 0o755); err != nil {
t.Fatalf("mkdir formula dir: %v", err)
}
formulaPath := filepath.Join(formulaDir, "pour-wisp-var-validation-test.formula.toml")
if err := os.WriteFile(formulaPath, []byte(varValidationFormulaTOML), 0o600); err != nil {
t.Fatalf("write formula fixture: %v", err)
}
t.Setenv("GT_ROOT", gtRoot)
}
func makePourVarTestCmd(varFlags []string) *cobra.Command {
c := &cobra.Command{Use: "pour"}
c.Flags().Bool("dry-run", true, "")
c.Flags().StringArray("var", varFlags, "")
c.Flags().String("assignee", "", "")
c.Flags().StringSlice("attach", []string{}, "")
c.Flags().String("attach-type", "", "")
return c
}
func makeWispVarTestCmd(varFlags []string) *cobra.Command {
c := &cobra.Command{Use: "wisp"}
c.Flags().StringArray("var", varFlags, "")
c.Flags().Bool("dry-run", true, "")
c.Flags().Bool("root-only", false, "")
return c
}
func TestPourRejectsEnumViolatingVar(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runPour(makePourVarTestCmd([]string{"policy=bogus", "slug=abc"}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runPour accepted a value outside the declared enum")
}
if !strings.Contains(stderr, `not in allowed values`) {
t.Fatalf("runPour stderr = %q, want an enum-violation message", stderr)
}
}
func TestPourRejectsPatternViolatingVar(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runPour(makePourVarTestCmd([]string{"policy=merge-completes", "slug=NOT-LOWERCASE"}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runPour accepted a value violating the declared pattern")
}
if !strings.Contains(stderr, `does not match pattern`) {
t.Fatalf("runPour stderr = %q, want a pattern-violation message", stderr)
}
}
// TestPourMissingVarHintUnchanged asserts that a genuinely absent required
// var still surfaces through checkPourVars/missingVarHint's existing
// HandleErrorWithHint path, not the new enum/pattern validation error path.
func TestPourMissingVarHintUnchanged(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runPour(makePourVarTestCmd([]string{}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runPour accepted a formula with a missing required variable")
}
if !strings.Contains(stderr, "missing required variables") {
t.Fatalf("runPour stderr = %q, want the missing-required-variables message", stderr)
}
if !strings.Contains(stderr, "Hint: Provide them with: --var policy=") {
t.Fatalf("runPour stderr = %q, want the pour-specific --var hint text", stderr)
}
}
// TestPourRejectsExplicitlyEmptyRequiredVar covers the bead's headline repro:
// an unset shell variable interpolated into --var (e.g. --var
// policy=$UNSET_SHELL_VAR) arrives as an explicitly-*provided*, empty-string
// value rather than an absent flag, so it must hit ValidateProvidedVars'
// required-and-empty check rather than the missing-var hint path.
func TestPourRejectsExplicitlyEmptyRequiredVar(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runPour(makePourVarTestCmd([]string{"policy=", "slug=abc"}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runPour accepted an explicitly-empty value for a required variable")
}
if !strings.Contains(stderr, "is required and cannot be empty") {
t.Fatalf("runPour stderr = %q, want the required-and-empty message", stderr)
}
}
func TestWispRejectsEnumViolatingVar(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runWispCreate(makeWispVarTestCmd([]string{"policy=bogus", "slug=abc"}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runWispCreate accepted a value outside the declared enum")
}
if !strings.Contains(stderr, `not in allowed values`) {
t.Fatalf("runWispCreate stderr = %q, want an enum-violation message", stderr)
}
}
func TestWispRejectsPatternViolatingVar(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runWispCreate(makeWispVarTestCmd([]string{"policy=merge-completes", "slug=NOT-LOWERCASE"}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runWispCreate accepted a value violating the declared pattern")
}
if !strings.Contains(stderr, `does not match pattern`) {
t.Fatalf("runWispCreate stderr = %q, want a pattern-violation message", stderr)
}
}
// TestWispMissingVarHintUnchanged mirrors TestPourMissingVarHintUnchanged for
// wisp's own checkRequiredVars/firstMissingVar hint path.
func TestWispMissingVarHintUnchanged(t *testing.T) {
writeVarValidationFormula(t)
s := newTestStoreWithPrefix(t, filepath.Join(t.TempDir(), "test.db"), "test")
withWispTestGlobals(t, s, context.Background())
var runErr error
stderr := captureStderr(t, func() {
runErr = runWispCreate(makeWispVarTestCmd([]string{}), []string{"pour-wisp-var-validation-test"})
})
if runErr == nil {
t.Fatal("runWispCreate accepted a formula with a missing required variable")
}
if !strings.Contains(stderr, "missing required variables") {
t.Fatalf("runWispCreate stderr = %q, want the missing-required-variables message", stderr)
}
if !strings.Contains(stderr, "Hint: Provide them with: --var policy=") {
t.Fatalf("runWispCreate stderr = %q, want the wisp-specific --var hint text", stderr)
}
}