Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ai/ai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"
"time"

"dappco.re/go/core"
"dappco.re/go"
coreio "dappco.re/go/io"
)

Expand Down
137 changes: 137 additions & 0 deletions ai/ax7_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package ai

import (
"context"
"time"

. "dappco.re/go"
rag "dappco.re/go/rag"
)

func TestAI_Record_Good(t *T) {
withTempMetricsHome(t)
err := Record(Event{Type: "security.scan", Repo: "core/go-ai"})
events, readErr := ReadEvents(time.Now().Add(-time.Minute))

AssertNoError(t, err)
AssertNoError(t, readErr)
AssertLen(t, events, 1)
}

func TestAI_Record_Bad(t *T) {
withTempMetricsHome(t)
err := Record(Event{Type: "security.scan", Data: map[string]any{"bad": make(chan int)}})
got := ErrorMessage(err)

AssertError(t, err)
AssertContains(t, got, "record event")
}

func TestAI_Record_Ugly(t *T) {
withTempMetricsHome(t)
err := Record(Event{})
events, readErr := ReadEvents(time.Now().Add(-time.Minute))

AssertNoError(t, err)
AssertNoError(t, readErr)
AssertLen(t, events, 1)
}

func TestAI_ReadEvents_Good(t *T) {
withTempMetricsHome(t)
recordErr := Record(Event{Type: "scan", Timestamp: time.Now().Add(-time.Second)})
events, err := ReadEvents(time.Now().Add(-time.Minute))

AssertNoError(t, recordErr)
AssertNoError(t, err)
AssertLen(t, events, 1)
}

func TestAI_ReadEvents_Bad(t *T) {
withTempMetricsHome(t)
events, err := ReadEvents(time.Now().Add(-time.Minute))
got := len(events)

AssertNoError(t, err)
AssertEqual(t, 0, got)
}

func TestAI_ReadEvents_Ugly(t *T) {
withTempMetricsHome(t)
recordErr := Record(Event{Type: "scan", Timestamp: time.Now().Add(-time.Hour)})
events, err := ReadEvents(time.Now().Add(time.Hour))

AssertNoError(t, recordErr)
AssertNoError(t, err)
AssertLen(t, events, 0)
}

func TestAI_Summary_Good(t *T) {
events := []Event{{Type: "scan", Repo: "core/go-ai", AgentID: "agent-1"}}
summary := Summary(events)
byType := summary["by_type"].(map[string]int)

AssertEqual(t, 1, byType["scan"])
AssertLen(t, summary["recent"].([]Event), 1)
}

func TestAI_Summary_Bad(t *T) {
summary := Summary(nil)
byType := summary["by_type"].(map[string]int)
recent := summary["recent"].([]Event)

AssertEmpty(t, byType)
AssertEmpty(t, recent)
}

func TestAI_Summary_Ugly(t *T) {
events := []Event{{Type: "scan", Data: map[string]any{"nested": []any{"x"}}}}
summary := Summary(events)
recent := summary["recent"].([]Event)

recent[0].Data["nested"].([]any)[0] = "changed"
AssertEqual(t, "x", events[0].Data["nested"].([]any)[0])
}

func TestAI_QueryRAGForTask_Good(t *T) {
origNewQdrantClient := newQdrantClient
origNewOllamaClient := newOllamaClient
origRunRAGQuery := runRAGQuery
t.Cleanup(func() {
newQdrantClient = origNewQdrantClient
newOllamaClient = origNewOllamaClient
runRAGQuery = origRunRAGQuery
})

newQdrantClient = func(rag.QdrantConfig) (*rag.QdrantClient, error) { return nil, nil }
newOllamaClient = func(rag.OllamaConfig) (*rag.OllamaClient, error) { return nil, nil }
runRAGQuery = func(_ context.Context, _ rag.VectorStore, _ rag.Embedder, _ string, _ rag.QueryConfig) ([]rag.QueryResult, error) {
return []rag.QueryResult{{Text: "Runbook", Source: "docs/build.md", Score: 0.9}}, nil
}

got, err := QueryRAGForTask(TaskInfo{Title: "Investigate", Description: "failure"})
AssertNoError(t, err)
AssertContains(t, got, "Runbook")
}

func TestAI_QueryRAGForTask_Bad(t *T) {
got, err := QueryRAGForTask(TaskInfo{})
want := ""

AssertNoError(t, err)
AssertEqual(t, want, got)
}

func TestAI_QueryRAGForTask_Ugly(t *T) {
origNewQdrantClient := newQdrantClient
t.Cleanup(func() {
newQdrantClient = origNewQdrantClient
})
newQdrantClient = func(rag.QdrantConfig) (*rag.QdrantClient, error) {
return nil, NewError("qdrant unavailable")
}

got, err := QueryRAGForTask(TaskInfo{Title: "Investigate"})
AssertNoError(t, err)
AssertEqual(t, "", got)
}
2 changes: 1 addition & 1 deletion ai/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"
"time"

"dappco.re/go/core"
"dappco.re/go"
coreio "dappco.re/go/io"
coreerr "dappco.re/go/log"
)
Expand Down
2 changes: 1 addition & 1 deletion ai/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing" // Note: intrinsic — Go test entry points and assertions
"time" // Note: test-only — fixes timestamps and read windows for metrics behavior

"dappco.re/go/core"
"dappco.re/go"
coreio "dappco.re/go/io"
)

Expand Down
2 changes: 1 addition & 1 deletion ai/rag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"time"

"dappco.re/go/core"
"dappco.re/go"
rag "dappco.re/go/rag"
)

Expand Down
33 changes: 33 additions & 0 deletions cmd/ai/ax7_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ai

import (
. "dappco.re/go"
"dappco.re/go/cli/pkg/cli"
)

func TestAI_AddAICommands_Good(t *T) {
root := &cli.Command{Use: "core"}
AddAICommands(root)
cmd, _, err := root.Find([]string{"ai"})

AssertNoError(t, err)
AssertEqual(t, "ai", cmd.Name())
}

func TestAI_AddAICommands_Bad(t *T) {
root := &cli.Command{Use: "core"}
AddAICommands(root)
AddAICommands(root)

AssertLen(t, root.Commands(), 1)
AssertEqual(t, "ai", root.Commands()[0].Name())
}

func TestAI_AddAICommands_Ugly(t *T) {
root := &cli.Command{Use: "core"}
root.AddCommand(&cli.Command{Use: "ai"})
AddAICommands(root)

AssertLen(t, root.Commands(), 1)
AssertEqual(t, "ai", root.Commands()[0].Name())
}
16 changes: 12 additions & 4 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
return runWithContext(ctx, cfg)
}

func runWithContext(ctx context.Context, cfg Config) error {

Check failure on line 42 in cmd/daemon/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-ai&issues=AZ3ViKF0eGS5yYnfR_sG&open=AZ3ViKF0eGS5yYnfR_sG&pullRequest=3
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand All @@ -48,7 +48,9 @@
}
defer func() {
if cfg.PIDFile != "" {
_ = os.Remove(cfg.PIDFile)
if err := os.Remove(cfg.PIDFile); err != nil && !errors.Is(err, os.ErrNotExist) {
fmt.Fprintln(os.Stderr, "daemon pid cleanup:", err)
}
}
}()

Expand All @@ -59,7 +61,9 @@
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = svc.Shutdown(shutdownCtx)
if err := svc.Shutdown(shutdownCtx); err != nil {
fmt.Fprintln(os.Stderr, "daemon mcp shutdown:", err)
}
}()

errCh := make(chan error, 4)
Expand Down Expand Up @@ -95,7 +99,9 @@
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = healthServer.Shutdown(shutdownCtx)
if err := healthServer.Shutdown(shutdownCtx); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Fprintln(os.Stderr, "daemon health shutdown:", err)
}
}()
}

Expand Down Expand Up @@ -176,7 +182,9 @@
server := &http.Server{Handler: mux}
go func() {
<-ctx.Done()
_ = server.Shutdown(context.Background())
if err := server.Shutdown(context.Background()); err != nil && !errors.Is(err, http.ErrServerClosed) {
fmt.Fprintln(os.Stderr, "daemon health shutdown:", err)
}
}()
go func() {
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/embed-bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"slices"
"time"

"dappco.re/go/core"
"dappco.re/go"
coreerr "dappco.re/go/log"
)

Expand Down
Loading