diff --git a/cmd/fn/main.go b/cmd/fn/main.go index 63dc6346..79094ea5 100644 --- a/cmd/fn/main.go +++ b/cmd/fn/main.go @@ -123,7 +123,7 @@ func cmdIndex() { } } - fmt.Printf("Indexed %d functions, %d types, %d apps, %d analysis\n", result.Functions, result.Types, result.Apps, result.Analysis) + fmt.Printf("Indexed %d functions, %d types, %d apps, %d analysis, %d unit_tests\n", result.Functions, result.Types, result.Apps, result.Analysis, result.UnitTests) for _, e := range result.ValidationErrors { fmt.Fprintf(os.Stderr, " INVALID: %s\n", e) } diff --git a/registry/indexer.go b/registry/indexer.go index 12bddafc..a38422a4 100644 --- a/registry/indexer.go +++ b/registry/indexer.go @@ -14,6 +14,7 @@ type IndexResult struct { Types int Apps int Analysis int + UnitTests int ValidationErrors []string Warnings []string Errors []string @@ -203,6 +204,39 @@ func Index(db *DB, root string) (*IndexResult, error) { result.Analysis++ } + // Extract unit tests from test files of tested functions + if err := db.PurgeUnitTests(); err != nil { + result.Warnings = append(result.Warnings, fmt.Sprintf("purging unit_tests: %v", err)) + } + for _, f := range functions { + if !f.Tested || f.TestFilePath == "" { + continue + } + absTestPath := filepath.Join(root, f.TestFilePath) + cases, err := parseTestFile(absTestPath, f.Lang) + if err != nil { + result.Warnings = append(result.Warnings, fmt.Sprintf("%s: parsing tests: %v", f.ID, err)) + continue + } + for i, tc := range cases { + ut := &UnitTest{ + ID: fmt.Sprintf("%s_t%d", f.ID, i), + FunctionID: f.ID, + Name: tc.Name, + Code: tc.Code, + FilePath: f.TestFilePath, + Lang: f.Lang, + CreatedAt: now, + UpdatedAt: now, + } + if err := db.InsertUnitTest(ut); err != nil { + result.Warnings = append(result.Warnings, fmt.Sprintf("insert unit_test %s: %v", ut.ID, err)) + continue + } + result.UnitTests++ + } + } + // Post-insert: warn about file_path entries that don't exist on disk for _, f := range functions { if f.FilePath != "" {