Files
summercms/cmd/summer/parity_test.go
Jakub Zych bb6a5a91c9 feat(02-02): capture named sessions through a loopback proxy
Record Nuxt/MCP traffic as ordered flows via parity:proxy, pin
loopback upstream, and refuse oversized or credential-shaped fixtures.

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

163 lines
5.1 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", "parity:proxy"} {
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)
}
err = runParity("parity:proxy")
if err == nil || !strings.Contains(err.Error(), "--rules") {
t.Fatalf("proxy without rules: %v", err)
}
rulesPath := filepath.Join(outDir, "rules.yaml")
if err := os.WriteFile(rulesPath, []byte("client: nuxt\nkeep_request_headers: []\nkeep_response_headers: []\n"), 0o644); err != nil {
t.Fatal(err)
}
err = runParity("parity:proxy", "--rules", rulesPath, "--fixtures", outDir, "--upstream", "http://example.com")
if err == nil || !strings.Contains(err.Error(), "loopback") {
t.Fatalf("proxy non-loopback upstream: %v", err)
}
err = runParity("parity:proxy", "--rules", rulesPath, "--fixtures", outDir, "--listen", "0.0.0.0:8422")
if err == nil || !strings.Contains(err.Error(), "loopback") {
t.Fatalf("proxy non-loopback listen: %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))
})
}