Drive ordered route cases through RecordFlow, resume in batches of 15, and print recorded/passing/failing/unrecorded coverage. Co-authored-by: Cursor <cursoragent@cursor.com>
179 lines
6.1 KiB
Go
179 lines
6.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)
|
|
}
|
|
|
|
manDir := t.TempDir()
|
|
fx := filepath.Join(manDir, "fx")
|
|
man := filepath.Join(manDir, "manifest.yaml")
|
|
if err := os.WriteFile(man, []byte("version: 1\nauth_groups:\n - public\nroutes:\n - id: GET /cli public\n method: GET\n path: /sample\n auth_group: public\n status: ported\n cases:\n - id: ok\n status: 200\n fixture: routes/cli.yaml\n request:\n method: GET\n path: /sample\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := runParity("parity:record", "--manifest", man, "--target", origJSON.URL, "--fixtures", fx, "--resume", "true", "--next-batch", "15"); err != nil {
|
|
t.Fatalf("manifest record: %v", err)
|
|
}
|
|
if err := runParity("parity:record", "--manifest", man, "--target", origJSON.URL, "--fixtures", fx, "--next-batch", "16"); err == nil {
|
|
t.Fatal("batch 16 must fail")
|
|
}
|
|
if err := runParity("parity:replay", "--manifest", man, "--fixtures", fx, "--target", origJSON.URL); err != nil {
|
|
t.Fatalf("manifest replay: %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))
|
|
})
|
|
}
|