feat(02-01): record and replay one route through the summer CLI
- Add a generic tide flow schema with YAML fixture IO and HTTP record/replay - Register parity:record and parity:replay on the bonfire summer tool - Diff JSON scalars at $.path and non-JSON bodies at the changed byte offset Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -29,6 +29,8 @@ func toolCommands() []bonfire.Command {
|
||||
makePluginCommand(),
|
||||
addPluginCommand(),
|
||||
devCommand(),
|
||||
parityRecordCommand(),
|
||||
parityReplayCommand(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
95
cmd/summer/parity.go
Normal file
95
cmd/summer/parity.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.golem15.com/golem15/summercms/bonfire"
|
||||
"git.golem15.com/golem15/summercms/tide"
|
||||
)
|
||||
|
||||
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"},
|
||||
},
|
||||
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"},
|
||||
},
|
||||
Run: runParityReplay,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
flow, err := tide.RecordFlow(ctx, spec, tide.RecordConfig{Target: target})
|
||||
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
|
||||
}
|
||||
result, err := tide.ReplayFlow(ctx, flow, tide.ReplayConfig{Target: target})
|
||||
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
|
||||
}
|
||||
120
cmd/summer/parity_test.go
Normal file
120
cmd/summer/parity_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/bonfire"
|
||||
)
|
||||
|
||||
func TestParityCommands(t *testing.T) {
|
||||
names := commandNames()
|
||||
for _, want := range []string{"parity:record", "parity:replay"} {
|
||||
if !containsName(names, want) {
|
||||
t.Fatalf("missing %s in %v", want, names)
|
||||
}
|
||||
}
|
||||
|
||||
spec := filepath.Join("..", "..", "tide", "testdata", "one-route-spec.yaml")
|
||||
outDir := t.TempDir()
|
||||
fixture := filepath.Join(outDir, "sample.yaml")
|
||||
|
||||
origJSON := httptest.NewServer(jsonHandler(`{"data":"ok"}`))
|
||||
t.Cleanup(origJSON.Close)
|
||||
changedJSON := httptest.NewServer(jsonHandler(`{"data":"no"}`))
|
||||
t.Cleanup(changedJSON.Close)
|
||||
origPlain := httptest.NewServer(plainHandler("hello"))
|
||||
t.Cleanup(origPlain.Close)
|
||||
changedPlain := httptest.NewServer(plainHandler("hallo"))
|
||||
t.Cleanup(changedPlain.Close)
|
||||
|
||||
if err := runParity("parity:record", "--spec", spec, "--target", origJSON.URL, "--output", fixture); err != nil {
|
||||
t.Fatalf("record: %v", err)
|
||||
}
|
||||
raw, err := os.ReadFile(fixture)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(raw, []byte(`{"data":"ok"}`)) {
|
||||
t.Fatalf("recorded fixture missing body:\n%s", raw)
|
||||
}
|
||||
|
||||
if err := runParity("parity:replay", "--fixtures", fixture, "--target", origJSON.URL); err != nil {
|
||||
t.Fatalf("replay identical JSON: %v", err)
|
||||
}
|
||||
|
||||
err = runParity("parity:replay", "--fixtures", fixture, "--target", changedJSON.URL)
|
||||
if err == nil {
|
||||
t.Fatal("changed JSON must fail")
|
||||
}
|
||||
msg := err.Error()
|
||||
if !strings.Contains(msg, "$.data") {
|
||||
t.Fatalf("JSON mismatch missing $.data: %s", msg)
|
||||
}
|
||||
if !strings.Contains(msg, "ok") || !strings.Contains(msg, "no") {
|
||||
t.Fatalf("JSON mismatch missing expected/actual: %s", msg)
|
||||
}
|
||||
|
||||
plainFixture := filepath.Join(outDir, "plain.yaml")
|
||||
if err := runParity("parity:record", "--spec", spec, "--target", origPlain.URL, "--output", plainFixture); err != nil {
|
||||
t.Fatalf("record plain: %v", err)
|
||||
}
|
||||
if err := runParity("parity:replay", "--fixtures", plainFixture, "--target", origPlain.URL); err != nil {
|
||||
t.Fatalf("replay identical plain: %v", err)
|
||||
}
|
||||
err = runParity("parity:replay", "--fixtures", plainFixture, "--target", changedPlain.URL)
|
||||
if err == nil {
|
||||
t.Fatal("changed bytes must fail")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "1") {
|
||||
t.Fatalf("byte mismatch missing offset: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func commandNames() []string {
|
||||
var names []string
|
||||
for _, c := range toolCommands() {
|
||||
names = append(names, c.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func containsName(names []string, want string) bool {
|
||||
for _, name := range names {
|
||||
if name == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func runParity(args ...string) error {
|
||||
var buf bytes.Buffer
|
||||
root, err := bonfire.NewRoot("summer", toolCommands(), &buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root.SetArgs(args)
|
||||
return root.Execute()
|
||||
}
|
||||
|
||||
func jsonHandler(body string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(body))
|
||||
})
|
||||
}
|
||||
|
||||
func plainHandler(body string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(body))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user