fix(ide): replace fmt.Errorf with coreerr.E(), add unit tests
Replace all fmt.Errorf calls in runtime.go with structured errors via
coreerr.E() from go-log, ensuring every error carries operation context
for structured logging and tracing.
Add unit tests for runtime utilities (findFreePort, waitForHealth,
defaultProvidersDir), RuntimeManager (List, StopAll, StartAll),
ProvidersAPI (Name, BasePath, list endpoint), guiEnabled, and
staticAssetGroup. Coverage: 27.1%.
No os.ReadFile/os.WriteFile violations found. CLAUDE.md reviewed —
no outdated commands.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 09:00:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-03-22 01:43:01 +00:00
|
|
|
"dappco.re/go/core/scm/manifest"
|
fix(ide): replace fmt.Errorf with coreerr.E(), add unit tests
Replace all fmt.Errorf calls in runtime.go with structured errors via
coreerr.E() from go-log, ensuring every error carries operation context
for structured logging and tracing.
Add unit tests for runtime utilities (findFreePort, waitForHealth,
defaultProvidersDir), RuntimeManager (List, StopAll, StartAll),
ProvidersAPI (Name, BasePath, list endpoint), guiEnabled, and
staticAssetGroup. Coverage: 27.1%.
No os.ReadFile/os.WriteFile violations found. CLAUDE.md reviewed —
no outdated commands.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 09:00:18 +00:00
|
|
|
"forge.lthn.ai/core/api/pkg/provider"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestProvidersAPI_Name(t *testing.T) {
|
|
|
|
|
api := NewProvidersAPI(nil, nil)
|
|
|
|
|
assert.Equal(t, "providers-api", api.Name())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProvidersAPI_BasePath(t *testing.T) {
|
|
|
|
|
api := NewProvidersAPI(nil, nil)
|
|
|
|
|
assert.Equal(t, "/api/v1/providers", api.BasePath())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProvidersAPI_List_Good_Empty(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
|
|
|
|
reg := provider.NewRegistry()
|
|
|
|
|
rm := NewRuntimeManager(nil)
|
|
|
|
|
api := NewProvidersAPI(reg, rm)
|
|
|
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
|
rg := router.Group(api.BasePath())
|
|
|
|
|
api.RegisterRoutes(rg)
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/providers", nil)
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
|
|
var resp providersResponse
|
|
|
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Empty(t, resp.Providers)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProvidersAPI_List_Good_WithRuntimeProviders(t *testing.T) {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
|
|
|
|
|
reg := provider.NewRegistry()
|
|
|
|
|
rm := NewRuntimeManager(nil)
|
|
|
|
|
|
|
|
|
|
// Simulate a runtime provider.
|
|
|
|
|
rm.providers = append(rm.providers, &RuntimeProvider{
|
|
|
|
|
Dir: "/tmp/test",
|
|
|
|
|
Port: 9999,
|
|
|
|
|
Manifest: &manifest.Manifest{
|
|
|
|
|
Code: "test-provider",
|
|
|
|
|
Name: "Test Provider",
|
|
|
|
|
Version: "0.1.0",
|
|
|
|
|
Namespace: "test",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
api := NewProvidersAPI(reg, rm)
|
|
|
|
|
|
|
|
|
|
router := gin.New()
|
|
|
|
|
rg := router.Group(api.BasePath())
|
|
|
|
|
api.RegisterRoutes(rg)
|
|
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
req, _ := http.NewRequest("GET", "/api/v1/providers", nil)
|
|
|
|
|
router.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
|
|
|
|
|
var resp providersResponse
|
|
|
|
|
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, resp.Providers, 1)
|
|
|
|
|
assert.Equal(t, "test-provider", resp.Providers[0].Name)
|
|
|
|
|
assert.Equal(t, "test", resp.Providers[0].BasePath)
|
2026-03-31 18:52:45 +00:00
|
|
|
assert.Equal(t, "running", resp.Providers[0].Status)
|
fix(ide): replace fmt.Errorf with coreerr.E(), add unit tests
Replace all fmt.Errorf calls in runtime.go with structured errors via
coreerr.E() from go-log, ensuring every error carries operation context
for structured logging and tracing.
Add unit tests for runtime utilities (findFreePort, waitForHealth,
defaultProvidersDir), RuntimeManager (List, StopAll, StartAll),
ProvidersAPI (Name, BasePath, list endpoint), guiEnabled, and
staticAssetGroup. Coverage: 27.1%.
No os.ReadFile/os.WriteFile violations found. CLAUDE.md reviewed —
no outdated commands.
Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 09:00:18 +00:00
|
|
|
}
|