From e6228ea8c03ac1d604400cb7d388e38d082d5be1 Mon Sep 17 00:00:00 2001 From: Egutierrez Date: Sun, 5 Apr 2026 18:19:21 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20fn=20index=20extrae=20unit=5Ftests=20au?= =?UTF-8?q?tom=C3=A1ticamente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit El indexer lee test_file_path de funciones testeadas, parsea los test cases y los inserta en unit_tests. El output de fn index ahora muestra el conteo de unit_tests extraídos. --- cmd/fn/main.go | 2 +- registry/indexer.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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 != "" {