diff --git a/cmd/uia-selector-report/main.go b/cmd/uia-selector-report/main.go new file mode 100644 index 0000000..e03dc5b --- /dev/null +++ b/cmd/uia-selector-report/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "flag" + "fmt" + "io" + "os" + "time" + + "isphere-ai-bridge/internal/uiaselector" +) + +func main() { + os.Exit(Run(os.Args[1:], os.Stdout, os.Stderr)) +} + +func Run(args []string, stdout io.Writer, stderr io.Writer) int { + flags := flag.NewFlagSet("uia-selector-report", flag.ContinueOnError) + flags.SetOutput(stderr) + dumpPath := flags.String("dump", "", "UIA dump JSON path") + outJSON := flags.String("out-json", "", "JSON report output path") + outMD := flags.String("out-md", "", "Markdown report output path") + strict := flags.Bool("strict", false, "return non-zero for unmatched, ambiguous, or invalid selector reports") + if err := flags.Parse(args); err != nil { + return uiaselector.StrictExitReportGenerationFailure + } + if *dumpPath == "" { + fmt.Fprintln(stderr, "missing -dump") + return uiaselector.StrictExitReportGenerationFailure + } + + root, err := uiaselector.LoadDumpFile(*dumpPath) + if err != nil { + fmt.Fprintf(stderr, "load dump: %v\n", err) + return uiaselector.StrictExitReportGenerationFailure + } + report := uiaselector.RunCatalogReport(*dumpPath, root, uiaselector.DefaultCatalog(), time.Now().UTC()) + if *outJSON != "" { + if err := uiaselector.WriteReportJSON(report, *outJSON); err != nil { + fmt.Fprintf(stderr, "write JSON report: %v\n", err) + return uiaselector.StrictExitReportGenerationFailure + } + } + if *outMD != "" { + if err := uiaselector.WriteReportMarkdown(report, *outMD); err != nil { + fmt.Fprintf(stderr, "write Markdown report: %v\n", err) + return uiaselector.StrictExitReportGenerationFailure + } + } + + code := uiaselector.StrictExitCode(report) + if *strict && code != uiaselector.StrictExitOK { + fmt.Fprintf(stderr, "strict report check failed with code %d\n", code) + return code + } + if !report.OK { + fmt.Fprintf(stderr, "report generation failed: %#v\n", report.Error) + return uiaselector.StrictExitReportGenerationFailure + } + fmt.Fprintf(stdout, "N14 selector report generated: catalog=%d matched=%d unmatched=%d ambiguous=%d\n", report.CatalogSize, report.MatchedCount, report.UnmatchedCount, report.AmbiguousCount) + return uiaselector.StrictExitOK +} diff --git a/cmd/uia-selector-report/main_test.go b/cmd/uia-selector-report/main_test.go new file mode 100644 index 0000000..98d2750 --- /dev/null +++ b/cmd/uia-selector-report/main_test.go @@ -0,0 +1,63 @@ +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()) + } +}