package main import ( "bytes" "context" "image" "image/png" "strings" "testing" "time" "github.com/kami/maven/internal/config" "github.com/kami/maven/internal/ipc" "github.com/kami/maven/internal/media" "github.com/kami/maven/internal/vision" ) func testIntake(t *testing.T) *visionIntake { t.Helper() st := newTestStore(t) blobs, err := media.Open(t.TempDir(), 0, 0) if err != nil { t.Fatal(err) } return &visionIntake{ in: vision.NewIntake(blobs, vision.Disabled{}, 0), st: st, now: time.Now, } } // The contract says exactly one of Data or ID. Taking the ID branch and // dropping the bytes silently is the worst of the three possible answers: the // caller believes it sent a new image and nothing says otherwise. func TestDescribeRefusesBothDataAndID(t *testing.T) { v := testIntake(t) _, err := v.describe(context.Background(), ipc.DescribeImageReq{ Data: []byte("bytes"), ID: strings.Repeat("a", 64), }) if err == nil { t.Fatal("both data and id must be refused") } if !strings.Contains(err.Error(), "send one") { t.Fatalf("err = %v, want it to name the contract", err) } } // Vision being off does not remove the method: the bytes are stored and the // answer says she cannot read the picture yet, which is re-runnable by id. That // is the state this box is in today, and three doc comments used to claim the // opposite. func TestVisionOffStillStores(t *testing.T) { v := testIntake(t) var buf bytes.Buffer if err := png.Encode(&buf, image.NewRGBA(image.Rect(0, 0, 4, 4))); err != nil { t.Fatal(err) } resp, err := v.describe(context.Background(), ipc.DescribeImageReq{Data: buf.Bytes(), Source: "web:upload"}) if err != nil { t.Fatalf("storing must succeed even with no vision model: %v", err) } if len(resp.ID) != 64 { t.Fatalf("no blob id came back: %+v", resp) } if resp.Description != "" { t.Errorf("description = %q, want none", resp.Description) } // And with no media block at all the method does not exist. if vi := newVisionIntake(nil, nil, nil, &config.Config{}); vi != nil { t.Fatal("no media block must leave the method nonexistent") } }