Phase 0: turn Supermarket into a Dockerized local web app
Replace the old Go TUI with a web app (port 8470) that runs in a container as a CoreDesktop sibling: - cmd/supermarket server with htmx web UI: dashboard, company profile, inventory (partners/competitors), AI and credential settings, and a Core & Hermes page - SQLite storage and a secrets store under ~/.local/share/supermarket - AI providers (Anthropic, OpenAI-compatible) behind one interface - MCP server and a Hermes bridge to the Federated Core MCP hub - Import of the old Coremarketing TUI's state - install.sh / update.sh / lib.sh, Dockerfile, compose.yaml, the bin/supermarket launcher and hermes/setup.sh Posting is not wired back up yet; that comes in Phase 1. See PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/secrets"
|
||||
)
|
||||
|
||||
func TestNormalizeBaseURL(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"llm.lan:11434": "http://llm.lan:11434/v1",
|
||||
"http://llm.lan:11434/": "http://llm.lan:11434/v1",
|
||||
"https://openrouter.ai/api/v1": "https://openrouter.ai/api/v1",
|
||||
" ": "",
|
||||
} {
|
||||
if got := NormalizeBaseURL(in); got != want {
|
||||
t.Errorf("NormalizeBaseURL(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONToleratesFences(t *testing.T) {
|
||||
var out struct{ Name string }
|
||||
if err := decodeJSON("Here you go:\n```json\n{\"Name\": \"Acme\"}\n```", &out); err != nil || out.Name != "Acme" {
|
||||
t.Errorf("%+v %v", out, err)
|
||||
}
|
||||
if err := decodeJSON("no json here", &out); err == nil {
|
||||
t.Error("expected an error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenAICompatible(t *testing.T) {
|
||||
var got map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/v1/models":
|
||||
w.Write([]byte(`{"data":[{"id":"qwen3"},{"id":"llama3.3"}]}`))
|
||||
case "/v1/chat/completions":
|
||||
json.NewDecoder(r.Body).Decode(&got)
|
||||
w.Write([]byte(`{"choices":[{"message":{"content":" {\"ok\": true} "},"finish_reason":"stop"}]}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
models, err := ListModels(context.Background(), srv.URL, "")
|
||||
if err != nil || len(models) != 2 || models[0] != "llama3.3" {
|
||||
t.Fatalf("models: %v %v", models, err)
|
||||
}
|
||||
|
||||
cfg := Config{Provider: ProviderOpenAI, Model: "qwen3", BaseURL: srv.URL, TaskModels: map[string]string{TaskProfile: "llama3.3"}}
|
||||
c, err := New(cfg, mustSecrets(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out struct{ OK bool }
|
||||
err = GenerateJSON(context.Background(), c, Request{Task: TaskProfile, System: "s", Prompt: "p", Schema: map[string]any{"type": "object"}}, &out)
|
||||
if err != nil || !out.OK {
|
||||
t.Fatalf("%+v %v", out, err)
|
||||
}
|
||||
if got["model"] != "llama3.3" {
|
||||
t.Errorf("task model not used: %v", got["model"])
|
||||
}
|
||||
if msgs := got["messages"].([]any); len(msgs) != 2 {
|
||||
t.Errorf("messages: %v", msgs)
|
||||
}
|
||||
if got["response_format"] == nil {
|
||||
t.Error("schema not sent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewNeedsCredentials(t *testing.T) {
|
||||
sec := mustSecrets(t)
|
||||
if _, err := New(Config{Provider: ProviderAnthropic, Model: DefaultAnthropicModel}, sec); err == nil {
|
||||
t.Error("anthropic without a key should fail")
|
||||
}
|
||||
if _, err := New(Config{Provider: ProviderOpenAI, Model: "m"}, sec); err == nil {
|
||||
t.Error("openai without an address should fail")
|
||||
}
|
||||
sec.Set(map[string]string{secrets.AnthropicAPIKey: "k"})
|
||||
if _, err := New(Config{Provider: ProviderAnthropic, Model: DefaultAnthropicModel}, sec); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigDefaults(t *testing.T) {
|
||||
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
c, err := LoadConfig(d)
|
||||
if err != nil || c.Provider != ProviderAnthropic || c.Model != DefaultAnthropicModel || c.Effort != "medium" {
|
||||
t.Errorf("%+v %v", c, err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustSecrets(t *testing.T) *secrets.Store {
|
||||
t.Helper()
|
||||
s, err := secrets.Open(filepath.Join(t.TempDir(), "secrets.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user