Files
llmgw/internal/router/images_test.go
T
Ilker ManapandClaude Opus 5.5 2f06c0e513 Pictures from ComfyUI at /v1/images/generations
An external model with api: comfyui is a ComfyUI server. A request for
pictures goes through the same key, allowlist, limit, admission and guard
checks as any other; at the replica it becomes a workflow — a built-in
(flux-schnell, flux-dev) or a JSON file in API format, with {{prompt}},
{{width}}, {{seed}} and the like filled in — queued, waited on, and its
pictures sent back as b64_json. A client that leaves takes its workflow
off the queue or interrupts it. ComfyUI is never scraped for metrics.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 21:15:50 +02:00

154 lines
5.5 KiB
Go

package router
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/comfy"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
"gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint"
)
// imageRouter has a ComfyUI model, flux, beside a text model, and returns
// what the ComfyUI server was sent.
func imageRouter(t *testing.T) (*Router, *map[string]any, *sync.Mutex) {
t.Helper()
var mu sync.Mutex
var queued map[string]any
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/metrics":
t.Error("ComfyUI was scraped for metrics")
case "/prompt":
var b struct {
Prompt map[string]any `json:"prompt"`
}
_ = json.NewDecoder(r.Body).Decode(&b)
mu.Lock()
queued = b.Prompt
mu.Unlock()
fmt.Fprint(w, `{"prompt_id":"p1"}`)
case "/history/p1":
fmt.Fprint(w, `{"p1":{"status":{"status_str":"success","completed":true},"outputs":{"7":{"images":[{"filename":"a.png","type":"temp"}]}}}}`)
case "/view":
w.Header().Set("Content-Type", "image/png")
fmt.Fprint(w, "png-bytes")
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
cfg := baseConfig()
cfg.Models = []catalog.Model{catModel("chat", nil), {Name: "flux", Source: "external",
External: &catalog.External{URL: srv.URL, Model: "flux1-schnell-fp8.safetensors", API: catalog.APIComfyUI}}}
rt := newTestRouter(t, cfg)
rt.comfy = &comfy.Client{Poll: time.Millisecond}
rt.SetEndpoints(t.Context(), []endpoint.Info{
{ID: "external:flux", URL: srv.URL, Models: []string{"flux"}, Engine: endpoint.EngineComfyUI,
Pool: "external", Weight: 1, UpstreamModel: "flux1-schnell-fp8.safetensors"},
})
return rt, &queued, &mu
}
func postImages(t *testing.T, h http.Handler, path string, body any) *httptest.ResponseRecorder {
t.Helper()
b, _ := json.Marshal(body)
req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(b))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec
}
func TestAPictureIsMadeByComfyUIAndComesBackAsBase64(t *testing.T) {
rt, queued, mu := imageRouter(t)
rec := postImages(t, rt.Handler(), imagesPath, map[string]any{
"model": "flux", "prompt": "a lighthouse at dusk", "size": "768x512", "seed": 7})
if rec.Code != http.StatusOK {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
var out struct {
Data []struct {
B64 string `json:"b64_json"`
} `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || len(out.Data) != 1 {
t.Fatalf("body %s: %v", rec.Body.String(), err)
}
if b, _ := base64.StdEncoding.DecodeString(out.Data[0].B64); string(b) != "png-bytes" {
t.Errorf("picture = %q", b)
}
if rec.Header().Get("x-llmgw-seed") != "7" {
t.Errorf("seed header = %q", rec.Header().Get("x-llmgw-seed"))
}
mu.Lock()
defer mu.Unlock()
wf, _ := json.Marshal(*queued)
for _, want := range []string{`"ckpt_name":"flux1-schnell-fp8.safetensors"`, `"text":"a lighthouse at dusk"`,
`"width":768`, `"height":512`, `"seed":7`} {
if !strings.Contains(string(wf), want) {
t.Errorf("%s not in the workflow sent: %s", want, wf)
}
}
}
func TestPicturesAndTextAreAskedForAtTheirOwnPaths(t *testing.T) {
rt, _, _ := imageRouter(t)
h := rt.Handler()
for name, c := range map[string]struct {
path string
body map[string]any
want string
}{
"text of flux": {"/v1/chat/completions", map[string]any{"model": "flux", "messages": []any{}}, "ask for them at " + imagesPath},
"picture of chat": {imagesPath, map[string]any{"model": "chat", "prompt": "x"}, "does not make pictures"},
"no prompt": {imagesPath, map[string]any{"model": "flux"}, "prompt is required"},
"odd size": {imagesPath, map[string]any{"model": "flux", "prompt": "x", "size": "1000x1000"}, "multiple of 16"},
"url wanted": {imagesPath, map[string]any{"model": "flux", "prompt": "x", "response_format": "url"}, "b64_json"},
"too many at once": {imagesPath, map[string]any{"model": "flux", "prompt": "x", "n": 9}, "n is between"},
} {
rec := postImages(t, h, c.path, c.body)
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), c.want) {
t.Errorf("%s: %d %s", name, rec.Code, rec.Body.String())
}
}
}
func TestAComfyUIModelSaysItMakesPictures(t *testing.T) {
rt, _, _ := imageRouter(t)
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
rec := httptest.NewRecorder()
rt.Handler().ServeHTTP(rec, req)
if !strings.Contains(rec.Body.String(), `"images":true`) {
t.Errorf("/v1/models: %s", rec.Body.String())
}
}
func TestAComfyUIModelIsCheckedInTheCatalog(t *testing.T) {
ok := catalog.Model{Name: "f", Source: "external", External: &catalog.External{
URL: "http://10.0.0.147:8188", Model: "flux1-dev.safetensors", API: catalog.APIComfyUI, Workflow: "flux-dev"}}
if _, err := catalog.New([]catalog.Model{ok}); err != nil {
t.Fatalf("refused: %v", err)
}
for name, e := range map[string]catalog.External{
"unknown api": {URL: "http://a:1", Model: "m", API: "a1111"},
"unknown workflow": {URL: "http://a:1", Model: "m", API: catalog.APIComfyUI, Workflow: "sdxl"},
"workflow, openai": {URL: "http://a:1", Model: "m", Workflow: "flux-dev"},
"comfyui from peer": {URL: "http://a:1", Model: "m", API: catalog.APIComfyUI, Peer: "other"},
} {
e := e
if _, err := catalog.New([]catalog.Model{{Name: "f", Source: "external", External: &e}}); err == nil {
t.Errorf("%s: accepted", name)
}
}
}