Files
isphere-ai-bridge/cmd/uia-selector-report/main_test.go
2026-07-09 16:28:45 +08:00

64 lines
1.8 KiB
Go

package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRunWritesJSONAndMarkdownReports(t *testing.T) {
tmp := t.TempDir()
jsonPath := filepath.Join(tmp, "report.json")
mdPath := filepath.Join(tmp, "report.md")
var stdout bytes.Buffer
var stderr bytes.Buffer
code := Run([]string{
"-dump", "../../internal/uiaselector/testdata/n12r-2026-07-09-uia-redacted.json",
"-out-json", jsonPath,
"-out-md", mdPath,
"-strict",
}, &stdout, &stderr)
if code != 0 {
t.Fatalf("Run exit code = %d, stderr=%s stdout=%s", code, stderr.String(), stdout.String())
}
jsonBody, err := os.ReadFile(jsonPath)
if err != nil {
t.Fatalf("read JSON report: %v", err)
}
var parsed struct {
OK bool `json:"ok"`
CatalogSize int `json:"catalog_size"`
MatchedCount int `json:"matched_count"`
UnmatchedCount int `json:"unmatched_count"`
AmbiguousCount int `json:"ambiguous_count"`
}
if err := json.Unmarshal(jsonBody, &parsed); err != nil {
t.Fatalf("JSON report did not parse: %v", err)
}
if !parsed.OK || parsed.CatalogSize != 10 || parsed.MatchedCount != 10 || parsed.UnmatchedCount != 0 || parsed.AmbiguousCount != 0 {
t.Fatalf("unexpected JSON report summary: %#v", parsed)
}
markdown, err := os.ReadFile(mdPath)
if err != nil {
t.Fatalf("read Markdown report: %v", err)
}
if !strings.Contains(string(markdown), "## Summary") {
t.Fatalf("Markdown report missing Summary: %s", string(markdown))
}
}
func TestRunRequiresDumpPath(t *testing.T) {
var stdout bytes.Buffer
var stderr bytes.Buffer
code := Run([]string{"-strict"}, &stdout, &stderr)
if code != 1 {
t.Fatalf("Run exit code = %d, want 1", code)
}
if !strings.Contains(stderr.String(), "missing -dump") {
t.Fatalf("stderr missing dump message: %s", stderr.String())
}
}