Files
ocis/services/llm/pkg/config/parser/parse_test.go
Lukas Hirt 14ca73fa83 feat: [OCISDEV-1061] add llm proxy service
Add a new llm service that proxies chat-completion requests from AI web extensions to a configured OpenAI-compatible endpoint, replacing the standalone Node.js sidecar. It is part of the oCIS single binary, opt-in via OCIS_ADD_RUN_SERVICES=llm, and exposed through the proxy at POST /graph/v1beta1/extensions/org.libregraph/llm/chat/completions.

Authentication is delegated to oCIS. Requests are rate-limited per user with a sliding window backed by the shared store, and the request body is sanitized before forwarding. Streaming requests are rejected.

Signed-off-by: Lukas Hirt <info@hirt.cz>
2026-10-15 14:31:07 +02:00

69 lines
1.8 KiB
Go

package parser
import (
"testing"
"time"
"github.com/owncloud/ocis/v2/services/llm/pkg/config"
)
func TestValidate_RequiresEndpoint(t *testing.T) {
cfg := &config.Config{}
if err := Validate(cfg); err == nil {
t.Fatal("expected an error when LLM.Endpoint is empty")
}
}
func TestValidate_PassesWhenEndpointSet(t *testing.T) {
cfg := validConfig()
if err := Validate(cfg); err != nil {
t.Fatalf("expected no error when LLM.Endpoint is set, got: %v", err)
}
}
func validConfig() *config.Config {
return &config.Config{
LLM: config.LLM{
Endpoint: "https://api.example.com/v1",
MaxTokensLimit: 4096,
},
}
}
func TestValidate_RejectsInvalidValues(t *testing.T) {
tests := map[string]func(*config.Config){
"endpoint without scheme": func(c *config.Config) { c.LLM.Endpoint = "api.example.com/v1" },
"endpoint wrong scheme": func(c *config.Config) { c.LLM.Endpoint = "ftp://api.example.com" },
"zero max tokens": func(c *config.Config) { c.LLM.MaxTokensLimit = 0 },
"negative timeout": func(c *config.Config) { c.LLM.Timeout = -time.Second },
"negative window": func(c *config.Config) { c.RateLimit.Window = -time.Second },
"store ttl below window": func(c *config.Config) {
c.RateLimit.Window = time.Hour
c.RateLimit.MaxRequests = 10
c.Store.TTL = 10 * time.Minute
},
}
for name, mutate := range tests {
t.Run(name, func(t *testing.T) {
cfg := validConfig()
mutate(cfg)
if err := Validate(cfg); err == nil {
t.Fatal("expected a validation error")
}
})
}
}
func TestValidate_AcceptsStoreTTLCoveringWindow(t *testing.T) {
cfg := validConfig()
cfg.RateLimit.Window = time.Minute
cfg.RateLimit.MaxRequests = 20
cfg.Store.TTL = 10 * time.Minute
if err := Validate(cfg); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}