CI / test (push) Failing after 6m25s
GLBs carried the licence's name, URL and notice in asset.extras.license. A pipeline that sorts assets by licence needs what a machine matches on: the id, whether commercial use is allowed, the regions it excludes, the revenue cap and the attribution. The whole licence is written, as /v1/models gives it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
435 lines
16 KiB
Go
435 lines
16 KiB
Go
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"image"
|
|
"image/png"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/comfy"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/control/catalog"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint"
|
|
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
|
)
|
|
|
|
// testGLB is the smallest glTF binary: an asset, and a binary chunk after
|
|
// its JSON that must come through untouched.
|
|
func testGLB(t *testing.T) []byte {
|
|
t.Helper()
|
|
js := []byte(`{"asset":{"version":"2.0","generator":"test"},"buffers":[{"byteLength":4}]}`)
|
|
for len(js)%4 != 0 {
|
|
js = append(js, ' ')
|
|
}
|
|
bin := []byte{1, 2, 3, 4}
|
|
var b []byte
|
|
b = append(b, "glTF"...)
|
|
b = binary.LittleEndian.AppendUint32(b, 2)
|
|
b = binary.LittleEndian.AppendUint32(b, uint32(12+8+len(js)+8+len(bin)))
|
|
b = binary.LittleEndian.AppendUint32(b, uint32(len(js)))
|
|
b = append(b, "JSON"...)
|
|
b = append(b, js...)
|
|
b = binary.LittleEndian.AppendUint32(b, uint32(len(bin)))
|
|
b = append(b, "BIN\x00"...)
|
|
return append(b, bin...)
|
|
}
|
|
|
|
// meshComfy is a ComfyUI that takes a picture and saves a mesh, as a 3D
|
|
// node's SaveGLB does: under "3d", not "images".
|
|
type meshComfy struct {
|
|
srv *httptest.Server
|
|
mu sync.Mutex
|
|
uploaded []string
|
|
workflow map[string]any
|
|
}
|
|
|
|
func newMeshComfy(t *testing.T) *meshComfy {
|
|
glb := testGLB(t)
|
|
c := &meshComfy{}
|
|
c.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case "/upload/image":
|
|
f, h, err := r.FormFile("image")
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
b, _ := io.ReadAll(f)
|
|
c.mu.Lock()
|
|
c.uploaded = append(c.uploaded, h.Filename+":"+http.DetectContentType(b))
|
|
c.mu.Unlock()
|
|
fmt.Fprintf(w, `{"name":%q,"subfolder":"","type":"input"}`, h.Filename)
|
|
case "/prompt":
|
|
var b struct {
|
|
Prompt map[string]any `json:"prompt"`
|
|
}
|
|
_ = json.NewDecoder(r.Body).Decode(&b)
|
|
c.mu.Lock()
|
|
c.workflow = b.Prompt
|
|
c.mu.Unlock()
|
|
fmt.Fprint(w, `{"prompt_id":"m1"}`)
|
|
case "/history/m1":
|
|
fmt.Fprint(w, `{"m1":{"status":{"status_str":"success","completed":true},"outputs":{
|
|
"3":{"text":["a preview"]},"4":{"3d":[{"filename":"mesh_00001_.glb","subfolder":"3d","type":"output"}]}}}}`)
|
|
case "/view":
|
|
// Served as ComfyUI serves a file it has no type for.
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
_, _ = w.Write(glb)
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
}))
|
|
t.Cleanup(c.srv.Close)
|
|
return c
|
|
}
|
|
|
|
const meshWorkflow = `{
|
|
"1": {"class_type": "LoadImage", "inputs": {"image": "{{image}}"}},
|
|
"2": {"class_type": "Trellis2ImageToMesh", "inputs": {"image": ["1", 0], "seed": "{{seed}}", "resolution": "{{resolution|512}}"}},
|
|
"4": {"class_type": "SaveGLB", "inputs": {"mesh": ["2", 0], "filename_prefix": "3d/mesh"}}
|
|
}`
|
|
|
|
func testPicture(t *testing.T) string {
|
|
var b bytes.Buffer
|
|
_ = png.Encode(&b, image.NewRGBA(image.Rect(0, 0, 8, 8)))
|
|
return base64.StdEncoding.EncodeToString(b.Bytes())
|
|
}
|
|
|
|
func meshRouter(t *testing.T, c *meshComfy) (*Router, string) {
|
|
t.Helper()
|
|
cfg := baseConfig()
|
|
mit := &catalog.License{Name: "MIT", URL: "https://github.com/microsoft/TRELLIS.2/blob/main/LICENSE",
|
|
Notice: "Made with TRELLIS.2 (MIT)."}
|
|
cfg.Models = []catalog.Model{
|
|
{Name: "trellis2", Source: "external", License: mit, External: &catalog.External{URL: c.srv.URL,
|
|
Model: "trellis2", API: catalog.APIComfyUI, Output: catalog.OutputMeshes, Workflow: "w.json"}},
|
|
{Name: "flux", Source: "external", External: &catalog.External{URL: c.srv.URL,
|
|
Model: "flux1-schnell-fp8.safetensors", API: catalog.APIComfyUI}},
|
|
}
|
|
cfg.Auth.Enabled = true
|
|
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
|
|
cfg.Auth.Pricing = map[string]config.Price{"trellis2": {PerMesh: 250000}}
|
|
cfg.Auth.FlushInterval = 20 * time.Millisecond
|
|
cfg.Auth.BatchSize = 1
|
|
rt := newTestRouter(t, cfg)
|
|
rt.comfy = &comfy.Client{Poll: time.Millisecond}
|
|
rt.SetEndpoints(t.Context(), []endpoint.Info{
|
|
{ID: "external:trellis2", URL: c.srv.URL, Models: []string{"trellis2"}, Engine: endpoint.EngineComfyUI,
|
|
Pool: "external", Weight: 1, UpstreamModel: "trellis2", Workflow: meshWorkflow},
|
|
{ID: "external:flux", URL: c.srv.URL, Models: []string{"flux"}, Engine: endpoint.EngineComfyUI,
|
|
Pool: "external", Weight: 1, UpstreamModel: "flux1-schnell-fp8.safetensors"},
|
|
})
|
|
org, _ := rt.store.CreateOrg(t.Context(), "studio")
|
|
team, _ := rt.store.CreateTeam(t.Context(), org, "art")
|
|
project, err := rt.store.CreateProject(t.Context(), team, "props")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secret := store.KeyPrefix + strings.Repeat("m", 43)
|
|
sum := sha256.Sum256([]byte(secret))
|
|
if err := rt.store.CreateKey(t.Context(), store.Key{ID: "key-m", Hash: sum[:], Prefix: "mmmm", Name: "art",
|
|
ProjectID: project, CreatedAt: time.Now()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return rt, secret
|
|
}
|
|
|
|
func TestA3DModelIsMadeFromAPictureWithItsLicenceInside(t *testing.T) {
|
|
c := newMeshComfy(t)
|
|
rt, secret := meshRouter(t, c)
|
|
h := rt.Handler()
|
|
|
|
code, j, raw := call(t, h, secret, http.MethodPost, meshesPath,
|
|
map[string]any{"model": "trellis2", "image": "data:image/png;base64," + testPicture(t), "seed": 11, "resolution": 1024})
|
|
if code != http.StatusAccepted {
|
|
t.Fatalf("%d %s", code, raw)
|
|
}
|
|
got := waitJob(t, h, secret, j["id"].(string), jobDone)
|
|
if got["status"] != "completed" || got["content_type"] != "model/gltf-binary" {
|
|
t.Fatalf("job %v", got)
|
|
}
|
|
if p, _ := got["progress"].(map[string]any); p["state"] != "done" {
|
|
t.Errorf("a finished job says %v", got["progress"])
|
|
}
|
|
if lic, _ := got["license"].(map[string]any); lic["name"] != "MIT" {
|
|
t.Errorf("the job does not say the licence: %v", got["license"])
|
|
}
|
|
code, _, file := call(t, h, secret, http.MethodGet, "/v1/files/"+got["output_file_id"].(string)+"/content", nil)
|
|
if code != http.StatusOK || !strings.HasPrefix(file, "glTF") {
|
|
t.Fatalf("the result is not the mesh: %d %q", code, file[:min(len(file), 40)])
|
|
}
|
|
b := []byte(file)
|
|
n := binary.LittleEndian.Uint32(b[12:])
|
|
var doc struct {
|
|
Asset struct {
|
|
Generator string `json:"generator"`
|
|
Extras struct {
|
|
License catalog.License `json:"license"`
|
|
} `json:"extras"`
|
|
} `json:"asset"`
|
|
}
|
|
if err := json.Unmarshal(b[20:20+n], &doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if doc.Asset.Extras.License.Notice != "Made with TRELLIS.2 (MIT)." || doc.Asset.Generator != "test" {
|
|
t.Errorf("asset %+v: the notice is not in the mesh, or what was there is lost", doc.Asset)
|
|
}
|
|
if int(binary.LittleEndian.Uint32(b[8:])) != len(b) || !bytes.HasSuffix(b, []byte{1, 2, 3, 4}) {
|
|
t.Error("the mesh's length or its binary chunk did not survive")
|
|
}
|
|
|
|
c.mu.Lock()
|
|
wf, _ := json.Marshal(c.workflow)
|
|
uploaded := strings.Join(c.uploaded, ",")
|
|
c.mu.Unlock()
|
|
if !strings.HasSuffix(uploaded, ".png:image/png") || !strings.HasPrefix(uploaded, "llmgw-") {
|
|
t.Errorf("uploaded %q", uploaded)
|
|
}
|
|
name := strings.TrimSuffix(uploaded, ":image/png")
|
|
for _, want := range []string{`"image":"` + name + `"`, `"seed":11`, `"resolution":1024`} {
|
|
if !strings.Contains(string(wf), want) {
|
|
t.Errorf("%s not in the workflow sent: %s", want, wf)
|
|
}
|
|
}
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
u, ok, _ := rt.store.UsageByRequest(t.Context(), j["id"].(string))
|
|
if ok {
|
|
if u.Meshes != 1 || u.CostMicros < 250000 {
|
|
t.Errorf("recorded %d meshes, %d micros; want one at its price", u.Meshes, u.CostMicros)
|
|
}
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("no usage recorded")
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func TestA3DModelIsAskedOfAModelThatMakesThem(t *testing.T) {
|
|
c := newMeshComfy(t)
|
|
rt, secret := meshRouter(t, c)
|
|
h := rt.Handler()
|
|
for name, c := range map[string]struct {
|
|
path string
|
|
body map[string]any
|
|
want string
|
|
}{
|
|
"a mesh of flux": {meshesPath, map[string]any{"model": "flux", "image": testPicture(t)}, "makes pictures, not 3D models"},
|
|
"no picture": {meshesPath, map[string]any{"model": "trellis2"}, "image is required"},
|
|
"not a picture": {meshesPath, map[string]any{"model": "trellis2", "image": base64.StdEncoding.EncodeToString([]byte("hello"))}, "not a PNG"},
|
|
"an odd resolution": {meshesPath, map[string]any{"model": "trellis2", "image": testPicture(t), "resolution": 700}, "multiple of 32"},
|
|
"a picture of trellis2": {imagesPath, map[string]any{"model": "trellis2", "prompt": "a cube"}, "makes 3D models, not pictures"},
|
|
"a chat with trellis2": {"/v1/chat/completions", map[string]any{"model": "trellis2", "messages": []any{}}, "makes 3D models"},
|
|
} {
|
|
code, _, raw := call(t, h, secret, http.MethodPost, c.path, c.body)
|
|
if code != http.StatusBadRequest || !strings.Contains(raw, c.want) {
|
|
t.Errorf("%s: %d %s", name, code, raw)
|
|
}
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/models", nil)
|
|
req.Header.Set("Authorization", "Bearer "+secret)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
if !strings.Contains(rec.Body.String(), `"meshes":true`) || !strings.Contains(rec.Body.String(), `"license":{"name":"MIT"`) {
|
|
t.Errorf("/v1/models: %s", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAnOBJLeavesTheGatewayAsAGLB(t *testing.T) {
|
|
// TripoSR saves an OBJ with a colour after each position, and a quad
|
|
// here stands for the polygons other nodes write.
|
|
obj := "# made by a test\nv 0 0 0 1 0 0\nv 1 0 0 0 1 0\nv 1 1 0 0 0 1\nv 0 1 0 1 1 1\nf 1 2 3 4\nf -4 -2 -1\n"
|
|
glb, err := objToGLB([]byte(obj))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(glb[:4]) != "glTF" || int(binary.LittleEndian.Uint32(glb[8:])) != len(glb) {
|
|
t.Fatal("not a glTF binary of its own length")
|
|
}
|
|
n := binary.LittleEndian.Uint32(glb[12:])
|
|
var doc struct {
|
|
Accessors []struct {
|
|
Count int `json:"count"`
|
|
Max []float64 `json:"max"`
|
|
} `json:"accessors"`
|
|
Meshes []struct {
|
|
Primitives []struct {
|
|
Attributes map[string]int `json:"attributes"`
|
|
} `json:"primitives"`
|
|
} `json:"meshes"`
|
|
}
|
|
if err := json.Unmarshal(glb[20:20+n], &doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if doc.Accessors[0].Count != 4 || doc.Accessors[1].Count != 9 || doc.Accessors[0].Max[1] != 1 {
|
|
t.Errorf("accessors %+v: four vertices and three triangles wanted", doc.Accessors)
|
|
}
|
|
if _, ok := doc.Meshes[0].Primitives[0].Attributes["COLOR_0"]; !ok {
|
|
t.Error("the vertices' colours were lost")
|
|
}
|
|
if _, err := glbWithLicense(glb, catalog.License{Name: "MIT"}); err != nil {
|
|
t.Errorf("the licence cannot be written into it: %v", err)
|
|
}
|
|
for _, bad := range []string{"v 0 0 0\n", "v 0 0 0\nf 1 2 3\n", "f x y z\n"} {
|
|
if _, err := objToGLB([]byte(bad)); err == nil {
|
|
t.Errorf("%q made a GLB", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestThe3DPageMakesAJobAndShowsItsMeshToItsOwnerOnly(t *testing.T) {
|
|
c := newMeshComfy(t)
|
|
rt, _ := meshRouter(t, c)
|
|
project := mustProjectOf(t, rt, "key-m")
|
|
user := store.User{ID: "usr-1", Email: "ann@studio.example", State: store.UserActive, CreatedAt: time.Now()}
|
|
if err := rt.store.CreateUser(t.Context(), user); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := rt.store.AddMembership(t.Context(), user.ID, store.ScopeProject, project, "member", "test", time.Now()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
org, _ := rt.store.CreateOrg(t.Context(), "other")
|
|
team, _ := rt.store.CreateTeam(t.Context(), org, "t")
|
|
otherProject, _ := rt.store.CreateProject(t.Context(), team, "p")
|
|
stranger := store.User{ID: "usr-2", Email: "bob@else.example", State: store.UserActive, CreatedAt: time.Now()}
|
|
_ = rt.store.CreateUser(t.Context(), stranger)
|
|
_ = rt.store.AddMembership(t.Context(), stranger.ID, store.ScopeProject, otherProject, "member", "test", time.Now())
|
|
|
|
body := `{"model":"trellis2","image":"` + testPicture(t) + `","resolution":256,"seed":5}`
|
|
rec := httptest.NewRecorder()
|
|
rt.meshesSend(rec, httptest.NewRequest("POST", "/3d/generate", strings.NewReader(body)), user)
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body.String())
|
|
}
|
|
var j map[string]any
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &j)
|
|
id := j["id"].(string)
|
|
rt.wakeJobs()
|
|
|
|
get := func(u store.User, path string) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest("GET", path, nil)
|
|
req.SetPathValue("id", id)
|
|
rec := httptest.NewRecorder()
|
|
next, ok := rt.asPerson(rec, req, u, "")
|
|
if !ok {
|
|
return rec
|
|
}
|
|
if strings.HasSuffix(path, "/file") {
|
|
p, _ := auth.FromContext(next.Context())
|
|
jj, ok := rt.ownedJob(rec, next, ownerOf(p))
|
|
if ok {
|
|
b, _ := rt.store.FileContent(next.Context(), jj.OutputFileID)
|
|
rec.Header().Set("Content-Type", jj.ContentType)
|
|
_, _ = rec.Write(b)
|
|
}
|
|
return rec
|
|
}
|
|
rt.handleGetJob(rec, next)
|
|
return rec
|
|
}
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
rec := get(user, "/3d/jobs/"+id)
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &j)
|
|
if j["status"] == "completed" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("not made: %s", rec.Body.String())
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
if rec := get(user, "/3d/jobs/"+id+"/file"); !strings.HasPrefix(rec.Body.String(), "glTF") {
|
|
t.Errorf("the mesh: %q", rec.Body.String()[:min(20, rec.Body.Len())])
|
|
}
|
|
if rec := get(stranger, "/3d/jobs/"+id); rec.Code != http.StatusNotFound {
|
|
t.Errorf("another project's person read the job: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestAModelAsleepUntilAskedIsListedAndSaidAsleep(t *testing.T) {
|
|
// triposr and flux-schnell load when asked and stop when idle; a client
|
|
// asking /v1/models which model makes 3D models found neither while
|
|
// they slept, and so never asked.
|
|
cfg := baseConfig()
|
|
asleep := catModel("triposr", nil)
|
|
asleep.Scaling = catalog.Scaling{Min: 0, Max: 1}
|
|
asleep.ComfyUI = &catalog.ComfyUI{Output: catalog.OutputMeshes, Workflow: "w.json"}
|
|
asleep.Profiles = []catalog.Profile{{Name: "gpu", Engine: catalog.EngineComfyUI, GPUClass: "6000-24g", TP: 1}}
|
|
cfg.Models = []catalog.Model{asleep, catModel("chat", nil)}
|
|
rt := newTestRouter(t, cfg)
|
|
e := newFakeEngine(t, "chat")
|
|
rt.SetEndpoints(t.Context(), []endpoint.Info{{ID: "c", URL: e.srv.URL, Models: []string{"chat"}}})
|
|
rec := httptest.NewRecorder()
|
|
rt.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/v1/models", nil))
|
|
var list struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
Loaded bool `json:"loaded"`
|
|
Capabilities map[string]bool `json:"capabilities"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &list); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]bool{}
|
|
for _, m := range list.Data {
|
|
got[m.ID] = m.Loaded
|
|
if m.ID == "triposr" && !m.Capabilities["meshes"] {
|
|
t.Error("triposr is not said to make meshes")
|
|
}
|
|
}
|
|
if loaded, ok := got["triposr"]; !ok || loaded {
|
|
t.Errorf("triposr listed %v, loaded %v: wanted listed and asleep", ok, loaded)
|
|
}
|
|
if !got["chat"] {
|
|
t.Error("a model serving is not said loaded")
|
|
}
|
|
}
|
|
|
|
func TestAMeshCarriesItsWholeLicence(t *testing.T) {
|
|
// The name alone was written: a pipeline that sorts assets by licence
|
|
// reads the id, whether commercial use is allowed and where it is not.
|
|
glb, err := objToGLB([]byte("v 0 0 0\nv 1 0 0\nv 1 1 0\nf 1 2 3\n"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
yes := true
|
|
out, err := glbWithLicense(glb, catalog.License{ID: "stability-community", Name: "Stability AI Community License",
|
|
Commercial: &yes, RevenueCapUSD: 1000000, Attribution: "Powered by Stability AI", ExcludedRegions: []string{"XX"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
n := binary.LittleEndian.Uint32(out[12:])
|
|
var doc struct {
|
|
Asset struct {
|
|
Extras struct {
|
|
License map[string]any `json:"license"`
|
|
} `json:"extras"`
|
|
} `json:"asset"`
|
|
}
|
|
if err := json.Unmarshal(out[20:20+n], &doc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
l := doc.Asset.Extras.License
|
|
if l["id"] != "stability-community" || l["commercial"] != true || l["revenue_cap_usd"] != 1000000.0 ||
|
|
l["attribution"] != "Powered by Stability AI" || l["name"] == nil {
|
|
t.Errorf("licence %v", l)
|
|
}
|
|
}
|