Skip to content

Commit f773750

Browse files
committed
tests
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 9e187b7 commit f773750

23 files changed

+276
-208
lines changed

core/explorer/empty_db.json.lock

Whitespace-only changes.

core/explorer/test_db.json.lock

Whitespace-only changes.

core/http/endpoints/explorer/dashboard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/labstack/echo/v4"
1010
"github.com/mudler/LocalAI/core/explorer"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/internal"
1313
)
1414

@@ -17,7 +17,7 @@ func Dashboard() echo.HandlerFunc {
1717
summary := map[string]interface{}{
1818
"Title": "LocalAI API - " + internal.PrintableVersion(),
1919
"Version": internal.PrintableVersion(),
20-
"BaseURL": utils.BaseURL(c),
20+
"BaseURL": middleware.BaseURL(c),
2121
}
2222

2323
contentType := c.Request().Header.Get("Content-Type")

core/http/endpoints/localai/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/google/uuid"
99
"github.com/mudler/LocalAI/core/config"
1010
"github.com/mudler/LocalAI/core/gallery"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/core/schema"
1313
"github.com/mudler/LocalAI/core/services"
1414
"github.com/mudler/LocalAI/pkg/system"
@@ -82,7 +82,7 @@ func (mgs *BackendEndpointService) ApplyBackendEndpoint() echo.HandlerFunc {
8282
Galleries: mgs.galleries,
8383
}
8484

85-
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", utils.BaseURL(c), uuid.String())})
85+
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", middleware.BaseURL(c), uuid.String())})
8686
}
8787
}
8888

@@ -106,7 +106,7 @@ func (mgs *BackendEndpointService) DeleteBackendEndpoint() echo.HandlerFunc {
106106
return err
107107
}
108108

109-
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", utils.BaseURL(c), uuid.String())})
109+
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", middleware.BaseURL(c), uuid.String())})
110110
}
111111
}
112112

core/http/endpoints/localai/edit_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/labstack/echo/v4"
1010
"github.com/mudler/LocalAI/core/config"
11-
httpUtils "github.com/mudler/LocalAI/core/http/utils"
11+
httpUtils "github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/internal"
1313
"github.com/mudler/LocalAI/pkg/utils"
1414

core/http/endpoints/localai/edit_model_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package localai_test
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"net/http"
78
"net/http/httptest"
@@ -16,6 +17,14 @@ import (
1617
. "github.com/onsi/gomega"
1718
)
1819

20+
// testRenderer is a simple renderer for tests that returns JSON
21+
type testRenderer struct{}
22+
23+
func (t *testRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
24+
// For tests, just return the data as JSON
25+
return json.NewEncoder(w).Encode(data)
26+
}
27+
1928
var _ = Describe("Edit Model test", func() {
2029

2130
var tempDir string
@@ -41,9 +50,12 @@ var _ = Describe("Edit Model test", func() {
4150
//modelLoader := model.NewModelLoader(systemState, true)
4251
modelConfigLoader := config.NewModelConfigLoader(systemState.Model.ModelsPath)
4352

44-
// Define Echo app.
53+
// Define Echo app and register all routes upfront
4554
app := echo.New()
55+
// Set up a simple renderer for the test
56+
app.Renderer = &testRenderer{}
4657
app.POST("/import-model", ImportModelEndpoint(modelConfigLoader, applicationConfig))
58+
app.GET("/edit-model/:name", GetEditModelPage(modelConfigLoader, applicationConfig))
4759

4860
requestBody := bytes.NewBufferString(`{"name": "foo", "backend": "foo", "model": "foo"}`)
4961

@@ -57,16 +69,15 @@ var _ = Describe("Edit Model test", func() {
5769
Expect(string(body)).To(ContainSubstring("Model configuration created successfully"))
5870
Expect(rec.Code).To(Equal(http.StatusOK))
5971

60-
app.GET("/edit-model/:name", GetEditModelPage(modelConfigLoader, applicationConfig))
61-
requestBody = bytes.NewBufferString(`{"name": "foo", "parameters": { "model": "foo"}}`)
62-
63-
req = httptest.NewRequest("GET", "/edit-model/foo", requestBody)
72+
req = httptest.NewRequest("GET", "/edit-model/foo", nil)
6473
rec = httptest.NewRecorder()
6574
app.ServeHTTP(rec, req)
6675

6776
body, err = io.ReadAll(rec.Body)
6877
Expect(err).ToNot(HaveOccurred())
69-
Expect(string(body)).To(ContainSubstring(`"model":"foo"`))
78+
// The response contains the model configuration with backend field
79+
Expect(string(body)).To(ContainSubstring(`"backend":"foo"`))
80+
Expect(string(body)).To(ContainSubstring(`"name":"foo"`))
7081
Expect(rec.Code).To(Equal(http.StatusOK))
7182
})
7283
})

core/http/endpoints/localai/gallery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/google/uuid"
99
"github.com/mudler/LocalAI/core/config"
1010
"github.com/mudler/LocalAI/core/gallery"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/core/schema"
1313
"github.com/mudler/LocalAI/core/services"
1414
"github.com/mudler/LocalAI/pkg/system"
@@ -85,7 +85,7 @@ func (mgs *ModelGalleryEndpointService) ApplyModelGalleryEndpoint() echo.Handler
8585
BackendGalleries: mgs.backendGalleries,
8686
}
8787

88-
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", utils.BaseURL(c), uuid.String())})
88+
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", middleware.BaseURL(c), uuid.String())})
8989
}
9090
}
9191

@@ -108,7 +108,7 @@ func (mgs *ModelGalleryEndpointService) DeleteModelGalleryEndpoint() echo.Handle
108108
return err
109109
}
110110

111-
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", utils.BaseURL(c), uuid.String())})
111+
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", middleware.BaseURL(c), uuid.String())})
112112
}
113113
}
114114

core/http/endpoints/localai/import_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/mudler/LocalAI/core/config"
1515
"github.com/mudler/LocalAI/core/gallery"
1616
"github.com/mudler/LocalAI/core/gallery/importers"
17-
httpUtils "github.com/mudler/LocalAI/core/http/utils"
17+
httpUtils "github.com/mudler/LocalAI/core/http/middleware"
1818
"github.com/mudler/LocalAI/core/schema"
1919
"github.com/mudler/LocalAI/core/services"
2020
"github.com/mudler/LocalAI/pkg/utils"

core/http/endpoints/localai/video.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/google/uuid"
1717
"github.com/mudler/LocalAI/core/config"
1818
"github.com/mudler/LocalAI/core/http/middleware"
19-
"github.com/mudler/LocalAI/core/http/utils"
2019
"github.com/mudler/LocalAI/core/schema"
2120

2221
"github.com/mudler/LocalAI/core/backend"
@@ -165,7 +164,7 @@ func VideoEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
165164
return err
166165
}
167166

168-
baseURL := utils.BaseURL(c)
167+
baseURL := middleware.BaseURL(c)
169168

170169
fn, err := backend.VideoGeneration(
171170
height,

core/http/endpoints/localai/welcome.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/mudler/LocalAI/core/config"
88
"github.com/mudler/LocalAI/core/gallery"
9-
"github.com/mudler/LocalAI/core/http/utils"
9+
"github.com/mudler/LocalAI/core/http/middleware"
1010
"github.com/mudler/LocalAI/core/services"
1111
"github.com/mudler/LocalAI/internal"
1212
"github.com/mudler/LocalAI/pkg/model"
@@ -45,7 +45,7 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
4545
summary := map[string]interface{}{
4646
"Title": "LocalAI API - " + internal.PrintableVersion(),
4747
"Version": internal.PrintableVersion(),
48-
"BaseURL": utils.BaseURL(c),
48+
"BaseURL": middleware.BaseURL(c),
4949
"Models": modelsWithoutConfig,
5050
"ModelsConfig": modelConfigs,
5151
"GalleryConfig": galleryConfigs,
@@ -58,7 +58,9 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
5858

5959
contentType := c.Request().Header.Get("Content-Type")
6060
accept := c.Request().Header.Get("Accept")
61-
if strings.Contains(contentType, "application/json") || !strings.Contains(accept, "text/html") {
61+
// Default to HTML if Accept header is empty (browser behavior)
62+
// Only return JSON if explicitly requested or Content-Type is application/json
63+
if strings.Contains(contentType, "application/json") || (accept != "" && !strings.Contains(accept, "text/html")) {
6264
// The client expects a JSON response
6365
return c.JSON(200, summary)
6466
} else {

0 commit comments

Comments
 (0)