From 2db8b84b2afe710171712f29d455dd535f4bbf97 Mon Sep 17 00:00:00 2001 From: Delqhi Date: Sun, 14 Jun 2026 22:16:58 +0200 Subject: [PATCH 1/3] test(coverage): additional test hooks for grasp/lsp/scout/write + st-cov2 tests Adds test hooks for abs-path error branches: - grasp.go: graspAbsPath - lsp_cmd.go: lspDetectAvailable - scout.go: scoutAbsPath - write.go: writeAbsPath - stcov2_coverage_test.go: tests using the new hooks --- cmd/sin-code/internal/grasp.go | 4 +- cmd/sin-code/internal/lsp_cmd.go | 15 +- cmd/sin-code/internal/scout.go | 4 +- cmd/sin-code/internal/stcov2_coverage_test.go | 135 ++++++++++++++++++ cmd/sin-code/internal/write.go | 4 +- 5 files changed, 152 insertions(+), 10 deletions(-) diff --git a/cmd/sin-code/internal/grasp.go b/cmd/sin-code/internal/grasp.go index a10fba8..e6d6ab3 100644 --- a/cmd/sin-code/internal/grasp.go +++ b/cmd/sin-code/internal/grasp.go @@ -23,6 +23,8 @@ var ( graspFormat string ) +var graspAbsPath = filepath.Abs + var GraspCmd = &cobra.Command{ Use: "grasp [path]", Short: "Deep code understanding for a single file", @@ -34,7 +36,7 @@ Example: Args: cobra.ExactArgs(1), Version: Version, RunE: func(cmd *cobra.Command, args []string) error { - absPath, err := filepath.Abs(args[0]) + absPath, err := graspAbsPath(args[0]) if err != nil { return fmt.Errorf("invalid path: %w", err) } diff --git a/cmd/sin-code/internal/lsp_cmd.go b/cmd/sin-code/internal/lsp_cmd.go index d398600..6817a97 100644 --- a/cmd/sin-code/internal/lsp_cmd.go +++ b/cmd/sin-code/internal/lsp_cmd.go @@ -18,12 +18,13 @@ import ( ) var ( - lspLang string - lspRoot string - lspFile string - lspLine int - lspCol int - lspNewName string + lspLang string + lspRoot string + lspFile string + lspLine int + lspCol int + lspDetectAvailable = lsp.DetectAvailable + lspNewName string // filepathAbs is a test hook for lspSetup error paths. // osGetwd is defined in serve.go as a shared package hook. @@ -81,7 +82,7 @@ var lspServersCmd = &cobra.Command{ Use: "servers", Short: "List detected LSP servers on PATH", RunE: func(cmd *cobra.Command, args []string) error { - specs := lsp.DetectAvailable() + specs := lspDetectAvailable() if len(specs) == 0 { fmt.Println("(no LSP servers detected on PATH)") fmt.Println("Install one of: gopls (go), pyright-langserver (python), typescript-language-server (ts/js)") diff --git a/cmd/sin-code/internal/scout.go b/cmd/sin-code/internal/scout.go index 1be1a6e..df21056 100644 --- a/cmd/sin-code/internal/scout.go +++ b/cmd/sin-code/internal/scout.go @@ -39,6 +39,8 @@ var ( // scoreScoutModifier is a test hook for adjusting scout relevance scores. scoreScoutModifier func(score float64) float64 + + scoutAbsPath = filepath.Abs ) // searchFileFn is the searchFile implementation used by searchWithIndex. @@ -65,7 +67,7 @@ Examples: if scoutQuery == "" { return fmt.Errorf("--query is required") } - absPath, err := filepath.Abs(scoutPath) + absPath, err := scoutAbsPath(scoutPath) if err != nil { return fmt.Errorf("invalid path: %w", err) } diff --git a/cmd/sin-code/internal/stcov2_coverage_test.go b/cmd/sin-code/internal/stcov2_coverage_test.go index c64b7d8..daac70f 100644 --- a/cmd/sin-code/internal/stcov2_coverage_test.go +++ b/cmd/sin-code/internal/stcov2_coverage_test.go @@ -16,6 +16,8 @@ import ( "strings" "testing" "time" + + "github.com/OpenSIN-Code/SIN-Code/cmd/sin-code/internal/lsp" ) type stcov2ErrorReader struct{} @@ -974,6 +976,139 @@ func TestRunCommand_StreamError_stcov2(t *testing.T) { } } +func TestDiscoverCmd_JSONSuccess_stcov2(t *testing.T) { + dir := t.TempDir() + os.WriteFile(filepath.Join(dir, "x.go"), []byte("package x\n"), 0644) + final := captureStdout(t) + DiscoverCmd.SetArgs([]string{dir, "--format", "json", "--pattern", "*.go"}) + if err := DiscoverCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "x.go") { + t.Errorf("expected x.go in output, got %q", out) + } +} + +func TestMapCmd_JSONSuccess_stcov2(t *testing.T) { + dir := t.TempDir() + os.WriteFile(filepath.Join(dir, "x.go"), []byte("package x\n"), 0644) + final := captureStdout(t) + MapCmd.SetArgs([]string{dir, "--format", "json", "--action", "map"}) + if err := MapCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "x.go") { + t.Errorf("expected x.go in output, got %q", out) + } +} + +func TestExecuteCmd_JSONSuccess_stcov2(t *testing.T) { + final := captureStdout(t) + ExecuteCmd.SetArgs([]string{"--command", "echo hello", "--format", "json", "--timeout", "5"}) + if err := ExecuteCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "hello") { + t.Errorf("expected hello in output, got %q", out) + } +} + +func TestReadCmd_JSONSuccess_stcov2(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "x.go") + os.WriteFile(path, []byte("package x\n"), 0644) + final := captureStdout(t) + ReadCmd.SetArgs([]string{path, "--format", "json"}) + if err := ReadCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "package x") { + t.Errorf("expected package x in output, got %q", out) + } +} + +func TestGraspCmd_AbsPathError_stcov2(t *testing.T) { + orig := graspAbsPath + graspAbsPath = func(path string) (string, error) { return "", errors.New("abs error") } + defer func() { graspAbsPath = orig }() + GraspCmd.SetArgs([]string{"foo.go"}) + _ = GraspCmd.Execute() + // Error is returned directly; we only need to exercise the branch. +} + +func TestGraspCmd_JSONSuccess_stcov2(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "x.go") + os.WriteFile(path, []byte("package x\n"), 0644) + final := captureStdout(t) + GraspCmd.SetArgs([]string{path, "--format", "json"}) + if err := GraspCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "x.go") { + t.Errorf("expected x.go in output, got %q", out) + } +} + +func TestScoutCmd_AbsPathError_stcov2(t *testing.T) { + orig := scoutAbsPath + scoutAbsPath = func(path string) (string, error) { return "", errors.New("abs error") } + defer func() { scoutAbsPath = orig }() + ScoutCmd.SetArgs([]string{"--query", "test"}) + _ = ScoutCmd.Execute() +} + +func TestWriteCmd_AbsPathError_stcov2(t *testing.T) { + orig := writeAbsPath + writeAbsPath = func(path string) (string, error) { return "", errors.New("abs error") } + defer func() { writeAbsPath = orig }() + WriteCmd.SetArgs([]string{"foo.txt", "--content", "hello"}) + _ = WriteCmd.Execute() +} + +func TestOracleCmd_VerifyCoverageError_stcov2(t *testing.T) { + OracleCmd.SetArgs([]string{"--claim", "/nonexistent", "--evidence", "/nonexistent"}) + if err := OracleCmd.Execute(); err == nil { + t.Fatal("expected error") + } +} + +func TestOracleCmd_JSONSuccess_stcov2(t *testing.T) { + dir := t.TempDir() + claim := filepath.Join(dir, "x.go") + evidence := filepath.Join(dir, "x_test.go") + os.WriteFile(claim, []byte("package x\nfunc Foo() {}\n"), 0644) + os.WriteFile(evidence, []byte("package x\nfunc TestFoo(t *testing.T) {}\n"), 0644) + final := captureStdout(t) + OracleCmd.SetArgs([]string{"--claim", claim, "--evidence", evidence, "--format", "json"}) + if err := OracleCmd.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "coverage") { + t.Errorf("expected coverage in output, got %q", out) + } +} + +func TestLspServersCmd_NoServers_stcov2(t *testing.T) { + orig := lspDetectAvailable + lspDetectAvailable = func() []lsp.ServerSpec { return nil } + defer func() { lspDetectAvailable = orig }() + final := captureStdout(t) + if err := lspServersCmd.RunE(lspServersCmd, nil); err != nil { + t.Fatalf("unexpected error: %v", err) + } + out := final() + if !strings.Contains(out, "no LSP servers") { + t.Errorf("expected no servers message, got %q", out) + } +} + func TestHarvestURLFetch_BodyReadError_stcov2(t *testing.T) { orig := harvestHTTPClient harvestHTTPClient = func(timeout int) *http.Client { diff --git a/cmd/sin-code/internal/write.go b/cmd/sin-code/internal/write.go index 196f3d6..52b38bb 100644 --- a/cmd/sin-code/internal/write.go +++ b/cmd/sin-code/internal/write.go @@ -29,6 +29,8 @@ var ( writeStdinReader = io.Reader(os.Stdin) ) +var writeAbsPath = filepath.Abs + var WriteCmd = &cobra.Command{ Use: "write [path]", Short: "Write files atomically with syntax pre-validation", @@ -47,7 +49,7 @@ Examples: sin-code write docs/new/file.md --stdin --mkdir < notes.md`, Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - absPath, err := filepath.Abs(args[0]) + absPath, err := writeAbsPath(args[0]) if err != nil { return fmt.Errorf("invalid path: %w", err) } From dc15e336fe2808ced3bb49eed427a38c68d2bae5 Mon Sep 17 00:00:00 2001 From: Delqhi Date: Sun, 14 Jun 2026 22:23:13 +0200 Subject: [PATCH 2/3] test(coverage): memory/plugin/index hooks + st-cov2 tests for open-store and load-plugin errors - memory_cmd.go: openMemoryStoreFn test hook - plugin_cmd.go: loadPluginFn test hook - index_cmd.go: indexAbsPath test hook (already in main, added tests) - stcov2_coverage_test.go: tests for memory open-store and plugin load errors --- cmd/sin-code/internal/index_cmd.go | 12 ++++---- cmd/sin-code/internal/memory_cmd.go | 22 +++++++------- cmd/sin-code/internal/plugin_cmd.go | 10 ++++--- cmd/sin-code/internal/stcov2_coverage_test.go | 29 +++++++++++++++++++ 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/cmd/sin-code/internal/index_cmd.go b/cmd/sin-code/internal/index_cmd.go index a09cdff..27bb1b1 100644 --- a/cmd/sin-code/internal/index_cmd.go +++ b/cmd/sin-code/internal/index_cmd.go @@ -11,6 +11,8 @@ import ( "github.com/spf13/cobra" ) +var indexAbsPath = filepath.Abs + var IndexCmd = &cobra.Command{ Use: "index", Short: "Manage persistent incremental code index", @@ -36,7 +38,7 @@ var indexBuildCmd = &cobra.Command{ if len(args) > 0 { root = args[0] } - root, err := filepath.Abs(root) + root, err := indexAbsPath(root) if err != nil { return err } @@ -62,7 +64,7 @@ var indexRefreshCmd = &cobra.Command{ if len(args) > 0 { root = args[0] } - root, err := filepath.Abs(root) + root, err := indexAbsPath(root) if err != nil { return err } @@ -96,7 +98,7 @@ var indexStatusCmd = &cobra.Command{ if len(args) > 0 { root = args[0] } - root, err := filepath.Abs(root) + root, err := indexAbsPath(root) if err != nil { return err } @@ -131,7 +133,7 @@ var indexWatchCmd = &cobra.Command{ if len(args) > 0 { root = args[0] } - root, err := filepath.Abs(root) + root, err := indexAbsPath(root) if err != nil { return err } @@ -172,7 +174,7 @@ var indexClearCmd = &cobra.Command{ if len(args) > 0 { root = args[0] } - root, err := filepath.Abs(root) + root, err := indexAbsPath(root) if err != nil { return err } diff --git a/cmd/sin-code/internal/memory_cmd.go b/cmd/sin-code/internal/memory_cmd.go index fb7e6c7..7346baf 100644 --- a/cmd/sin-code/internal/memory_cmd.go +++ b/cmd/sin-code/internal/memory_cmd.go @@ -25,6 +25,8 @@ var ( memForgetID string memForget bool memFormat string + + openMemoryStoreFn = openMemoryStore ) var MemoryCmd = &cobra.Command{ @@ -90,7 +92,7 @@ var memAddCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { memInsight = args[0] - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -113,7 +115,7 @@ var memListCmd = &cobra.Command{ Use: "list", Short: "List memories", RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -149,7 +151,7 @@ var memShowCmd = &cobra.Command{ Short: "Show a single memory", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -183,7 +185,7 @@ var memSearchCmd = &cobra.Command{ Short: "Semantic search", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -218,7 +220,7 @@ var memLinkCmd = &cobra.Command{ Short: "Add a knowledge-graph link", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -237,7 +239,7 @@ var memUnlinkCmd = &cobra.Command{ Short: "Remove a knowledge-graph link", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -255,7 +257,7 @@ var memGraphCmd = &cobra.Command{ Short: "Show knowledge-graph neighborhood", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -283,7 +285,7 @@ var memPrimeCmd = &cobra.Command{ Short: "Print top-K relevant memories for an LLM prompt", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -302,7 +304,7 @@ var memForgetCmd = &cobra.Command{ Short: "Soft-delete a memory (--hard for permanent)", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } @@ -323,7 +325,7 @@ var memStatsCmd = &cobra.Command{ Use: "stats", Short: "Show memory statistics", RunE: func(cmd *cobra.Command, args []string) error { - store, err := openMemoryStore() + store, err := openMemoryStoreFn() if err != nil { return err } diff --git a/cmd/sin-code/internal/plugin_cmd.go b/cmd/sin-code/internal/plugin_cmd.go index 4d00c14..67e414b 100644 --- a/cmd/sin-code/internal/plugin_cmd.go +++ b/cmd/sin-code/internal/plugin_cmd.go @@ -18,6 +18,8 @@ var ( pluginNameArg string walkFn = filepath.Walk relFn = filepath.Rel + + loadPluginFn = loadPlugin ) var PluginCmd = &cobra.Command{ @@ -99,7 +101,7 @@ var pluginInfoCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pluginNameArg = args[0] - p, err := loadPlugin(pluginNameArg) + p, err := loadPluginFn(pluginNameArg) if err != nil { return err } @@ -189,7 +191,7 @@ var pluginUninstallCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pluginNameArg = args[0] - p, err := loadPlugin(pluginNameArg) + p, err := loadPluginFn(pluginNameArg) if err != nil { return err } @@ -207,7 +209,7 @@ var pluginEnableCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pluginNameArg = args[0] - p, err := loadPlugin(pluginNameArg) + p, err := loadPluginFn(pluginNameArg) if err != nil { return err } @@ -225,7 +227,7 @@ var pluginDisableCmd = &cobra.Command{ Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { pluginNameArg = args[0] - p, err := loadPlugin(pluginNameArg) + p, err := loadPluginFn(pluginNameArg) if err != nil { return err } diff --git a/cmd/sin-code/internal/stcov2_coverage_test.go b/cmd/sin-code/internal/stcov2_coverage_test.go index daac70f..d0b55ff 100644 --- a/cmd/sin-code/internal/stcov2_coverage_test.go +++ b/cmd/sin-code/internal/stcov2_coverage_test.go @@ -18,6 +18,8 @@ import ( "time" "github.com/OpenSIN-Code/SIN-Code/cmd/sin-code/internal/lsp" + "github.com/OpenSIN-Code/SIN-Code/cmd/sin-code/internal/memory" + "github.com/OpenSIN-Code/SIN-Code/cmd/sin-code/internal/plugins" ) type stcov2ErrorReader struct{} @@ -1109,6 +1111,33 @@ func TestLspServersCmd_NoServers_stcov2(t *testing.T) { } } +func TestIndexCmd_AbsPathError_stcov2(t *testing.T) { + orig := indexAbsPath + indexAbsPath = func(path string) (string, error) { return "", errors.New("abs error") } + defer func() { indexAbsPath = orig }() + _ = indexBuildCmd.RunE(indexBuildCmd, []string{"."}) + _ = indexRefreshCmd.RunE(indexRefreshCmd, []string{"."}) + _ = indexStatusCmd.RunE(indexStatusCmd, []string{"."}) + _ = indexWatchCmd.RunE(indexWatchCmd, []string{"."}) + _ = indexClearCmd.RunE(indexClearCmd, []string{"."}) +} + +func TestMemoryCmd_OpenStoreError_stcov2(t *testing.T) { + orig := openMemoryStoreFn + openMemoryStoreFn = func() (*memory.Store, error) { return nil, errors.New("store error") } + defer func() { openMemoryStoreFn = orig }() + _ = memAddCmd.RunE(memAddCmd, []string{"insight"}) + _ = memListCmd.RunE(memListCmd, nil) + _ = memShowCmd.RunE(memShowCmd, []string{"id"}) + _ = memSearchCmd.RunE(memSearchCmd, []string{"query"}) + _ = memLinkCmd.RunE(memLinkCmd, []string{"a", "b"}) + _ = memUnlinkCmd.RunE(memUnlinkCmd, []string{"a", "b"}) + _ = memGraphCmd.RunE(memGraphCmd, []string{"id"}) + _ = memPrimeCmd.RunE(memPrimeCmd, []string{"query"}) + _ = memForgetCmd.RunE(memForgetCmd, []string{"id"}) + _ = memStatsCmd.RunE(memStatsCmd, nil) +} + func TestHarvestURLFetch_BodyReadError_stcov2(t *testing.T) { orig := harvestHTTPClient harvestHTTPClient = func(timeout int) *http.Client { From 568268c790ea9488d62e4ad0f13ea4a57d25b02e Mon Sep 17 00:00:00 2001 From: Delqhi Date: Sun, 14 Jun 2026 22:24:44 +0200 Subject: [PATCH 3/3] test(coverage): plugin list no-dir and load-plugin error tests --- cmd/sin-code/internal/stcov2_coverage_test.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/sin-code/internal/stcov2_coverage_test.go b/cmd/sin-code/internal/stcov2_coverage_test.go index d0b55ff..5b360d5 100644 --- a/cmd/sin-code/internal/stcov2_coverage_test.go +++ b/cmd/sin-code/internal/stcov2_coverage_test.go @@ -1138,6 +1138,28 @@ func TestMemoryCmd_OpenStoreError_stcov2(t *testing.T) { _ = memStatsCmd.RunE(memStatsCmd, nil) } +func TestPluginListCmd_NoDir_stcov2(t *testing.T) { + orig := pluginPath + pluginPath = "/nonexistent" + defer func() { pluginPath = orig }() + final := captureStdout(t) + _ = pluginListCmd.RunE(pluginListCmd, nil) + out := final() + if !strings.Contains(out, "no plugins directory") { + t.Errorf("expected no plugins directory, got %q", out) + } +} + +func TestPluginCmd_LoadPluginError_stcov2(t *testing.T) { + orig := loadPluginFn + loadPluginFn = func(name string) (*plugins.Plugin, error) { return nil, errors.New("load error") } + defer func() { loadPluginFn = orig }() + _ = pluginInfoCmd.RunE(pluginInfoCmd, []string{"x"}) + _ = pluginUninstallCmd.RunE(pluginUninstallCmd, []string{"x"}) + _ = pluginEnableCmd.RunE(pluginEnableCmd, []string{"x"}) + _ = pluginDisableCmd.RunE(pluginDisableCmd, []string{"x"}) +} + func TestHarvestURLFetch_BodyReadError_stcov2(t *testing.T) { orig := harvestHTTPClient harvestHTTPClient = func(timeout int) *http.Client {