package report import ( "encoding/json" "fmt" "os" "path/filepath" "strings" "github.com/fraktal/mev-beta/tools/math-audit/internal/audit" ) // WriteJSON writes the audit result as pretty JSON to the specified directory. func WriteJSON(dir string, res audit.Result) (string, error) { if err := os.MkdirAll(dir, 0o755); err != nil { return "", fmt.Errorf("create report dir: %w", err) } path := filepath.Join(dir, "report.json") payload, err := json.MarshalIndent(res, "", " ") if err != nil { return "", fmt.Errorf("marshal report: %w", err) } if err := os.WriteFile(path, payload, 0o644); err != nil { return "", fmt.Errorf("write report: %w", err) } return path, nil } // WriteMarkdown renders a human-readable summary and writes it next to the JSON. func WriteMarkdown(dir string, res audit.Result) (string, error) { if err := os.MkdirAll(dir, 0o755); err != nil { return "", fmt.Errorf("create report dir: %w", err) } path := filepath.Join(dir, "report.md") content := GenerateMarkdown(res) if err := os.WriteFile(path, []byte(content), 0o644); err != nil { return "", fmt.Errorf("write markdown: %w", err) } return path, nil } // GenerateMarkdown returns a markdown representation of the audit result. func GenerateMarkdown(res audit.Result) string { var b strings.Builder b.WriteString("# Math Audit Report\n\n") b.WriteString(fmt.Sprintf("- Generated: %s UTC\n", res.Summary.GeneratedAt.Format("2006-01-02 15:04:05"))) b.WriteString(fmt.Sprintf("- Vectors: %d/%d passed\n", res.Summary.VectorsPassed, res.Summary.TotalVectors)) b.WriteString(fmt.Sprintf("- Assertions: %d/%d passed\n", res.Summary.AssertionsPassed, res.Summary.TotalAssertions)) b.WriteString(fmt.Sprintf("- Property checks: %d/%d passed\n\n", res.Summary.PropertySucceeded, res.Summary.PropertyChecks)) if len(res.Vectors) > 0 { b.WriteString("## Vector Results\n\n") b.WriteString("| Vector | Exchange | Status | Notes |\n") b.WriteString("| --- | --- | --- | --- |\n") for _, vec := range res.Vectors { status := "✅ PASS" if !vec.Passed { status = "❌ FAIL" } var notes []string for _, test := range vec.Tests { if !test.Passed { notes = append(notes, fmt.Sprintf("%s (%.4f bps)", test.Name, test.DeltaBPS)) } } if len(vec.Errors) > 0 { notes = append(notes, vec.Errors...) } noteStr := "" if len(notes) > 0 { noteStr = strings.Join(notes, "; ") } b.WriteString(fmt.Sprintf("| %s | %s | %s | %s |\n", vec.Name, vec.Exchange, status, noteStr)) } b.WriteString("\n") } if len(res.PropertyChecks) > 0 { b.WriteString("## Property Checks\n\n") for _, check := range res.PropertyChecks { status := "✅" if !check.Passed { status = "❌" } if check.Details != "" { b.WriteString(fmt.Sprintf("- %s %s — %s\n", status, check.Name, check.Details)) } else { b.WriteString(fmt.Sprintf("- %s %s\n", status, check.Name)) } } b.WriteString("\n") } return b.String() }