Files
summercms/cmd/summer/parity_test.go
Jakub Zych f6e1b892bc 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>
2026-09-17 12:20:22 +02:00

121 lines
3.2 KiB
Go

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))
})
}