Files
summercms/cmd/summer/parity_test.go
Jakub Zych f669d056c9 feat(02-01): lock the one-route tide record and replay contract
- Reject unknown YAML fields, empty names, duplicate steps and unsafe sidecars
- Treat JSON key order as insignificant and fail missing keys and token types at $.path
- Bound request bodies and refuse oversized or malformed input before writing a fixture

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-17 12:21:34 +02:00

146 lines
4.3 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)
}
badSpec := filepath.Join(outDir, "bad.yaml")
if err := os.WriteFile(badSpec, []byte("version: [\n"), 0o644); err != nil {
t.Fatal(err)
}
leftover := filepath.Join(outDir, "should-not-exist.yaml")
err = runParity("parity:record", "--spec", badSpec, "--target", origJSON.URL, "--output", leftover)
if err == nil {
t.Fatal("malformed YAML must fail")
}
if _, statErr := os.Stat(leftover); !os.IsNotExist(statErr) {
t.Fatalf("malformed spec committed a fixture: %v", statErr)
}
keyOrder := httptest.NewServer(jsonHandler(`{"a":"x","z":1}`))
t.Cleanup(keyOrder.Close)
origKeys := httptest.NewServer(jsonHandler(`{"z":1,"a":"x"}`))
t.Cleanup(origKeys.Close)
keyFixture := filepath.Join(outDir, "keys.yaml")
if err := runParity("parity:record", "--spec", spec, "--target", origKeys.URL, "--output", keyFixture); err != nil {
t.Fatalf("record key order: %v", err)
}
if err := runParity("parity:replay", "--fixtures", keyFixture, "--target", keyOrder.URL); err != nil {
t.Fatalf("reordered JSON keys must pass: %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))
})
}