package main import ( "context" "fmt" "strings" "git.golem15.com/golem15/summercms/bonfire" "git.golem15.com/golem15/summercms/tide" ) func parityProxyCommand() bonfire.Command { return bonfire.Command{ Name: "parity:proxy", Description: "Record named HTTP sessions through a loopback reverse proxy", Flags: []bonfire.Flag{ {Name: "listen", Description: "Loopback bind address", Default: tide.DefaultListen}, {Name: "upstream", Description: "Fixed loopback HTTP origin", Default: tide.DefaultUpstream}, {Name: "session", Description: "Default session name when X-Parity-Session is absent"}, {Name: "rules", Description: "Committed YAML capture rules"}, {Name: "vars", Description: "Private mode-0600 variable store outside fixtures"}, {Name: "fixtures", Description: "Directory for nuxt/ and mcp/ session flows"}, }, Run: runParityProxy, } } func parityRecordCommand() bonfire.Command { return bonfire.Command{ Name: "parity:record", Description: "Record HTTP responses for a one-flow YAML spec", Flags: []bonfire.Flag{ {Name: "spec", Description: "YAML request spec path"}, {Name: "target", Description: "Base URL of the HTTP backend"}, {Name: "output", Description: "Destination fixture path"}, {Name: "rules", Description: "Committed YAML capture rules"}, {Name: "vars", Description: "Private mode-0600 variable store outside fixtures"}, }, Run: runParityRecord, } } func parityReplayCommand() bonfire.Command { return bonfire.Command{ Name: "parity:replay", Description: "Replay recorded fixtures against an HTTP backend", Flags: []bonfire.Flag{ {Name: "fixtures", Description: "Recorded YAML fixture path"}, {Name: "target", Description: "Base URL of the HTTP backend"}, {Name: "vars", Description: "Private mode-0600 variable store"}, }, Run: runParityReplay, } } func runParityProxy(ctx context.Context, in bonfire.Input, out bonfire.Output) error { rulesPath, err := requireFlag(in, "rules", "parity:proxy") if err != nil { return err } fixtures, err := requireFlag(in, "fixtures", "parity:proxy") if err != nil { return err } rules, err := tide.LoadRules(rulesPath) if err != nil { return err } listen, _ := in.Flag("listen") upstream, _ := in.Flag("upstream") session, _ := in.Flag("session") varsPath, _ := in.Flag("vars") proxy, err := tide.NewProxy(tide.ProxyConfig{ Listen: listen, Upstream: upstream, Session: session, Rules: rules, VarsPath: varsPath, Fixtures: fixtures, }) if err != nil { return err } out.Info(fmt.Sprintf("proxy listening on %s → %s", listen, upstream)) return proxy.ListenAndServe(ctx) } func runParityRecord(ctx context.Context, in bonfire.Input, out bonfire.Output) error { specPath, err := requireFlag(in, "spec", "parity:record") if err != nil { return err } target, err := requireFlag(in, "target", "parity:record") if err != nil { return err } output, err := requireFlag(in, "output", "parity:record") if err != nil { return err } spec, err := tide.LoadFlow(specPath) if err != nil { return err } cfg := tide.RecordConfig{Target: target} if rulesPath, ok := in.Flag("rules"); ok && strings.TrimSpace(rulesPath) != "" { rules, err := tide.LoadRules(rulesPath) if err != nil { return err } cfg.Rules = rules } if varsPath, ok := in.Flag("vars"); ok && strings.TrimSpace(varsPath) != "" { store, err := tide.OpenStore(varsPath) if err != nil { return err } cfg.Store = store } flow, err := tide.RecordFlow(ctx, spec, cfg) if err != nil { return err } if err := tide.SaveFlow(output, flow); err != nil { return err } out.Success(fmt.Sprintf("recorded %s", output)) return nil } func runParityReplay(ctx context.Context, in bonfire.Input, out bonfire.Output) error { path, err := requireFlag(in, "fixtures", "parity:replay") if err != nil { return err } target, err := requireFlag(in, "target", "parity:replay") if err != nil { return err } flow, err := tide.LoadFlow(path) if err != nil { return err } cfg := tide.ReplayConfig{Target: target} if varsPath, ok := in.Flag("vars"); ok && strings.TrimSpace(varsPath) != "" { store, err := tide.OpenStore(varsPath) if err != nil { return err } cfg.Store = store } result, err := tide.ReplayFlow(ctx, flow, cfg) if err != nil { return err } if result.OK { out.Success("replay matched") } return nil } func requireFlag(in bonfire.Input, name, cmd string) (string, error) { v, ok := in.Flag(name) v = strings.TrimSpace(v) if !ok || v == "" { return "", fmt.Errorf("%s requires --%s", cmd, name) } return v, nil }