87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
type fakeOSSProvider struct {
|
|
key string
|
|
data []byte
|
|
contentType string
|
|
url string
|
|
}
|
|
|
|
func (f *fakeOSSProvider) Upload(_ context.Context, key string, data []byte, contentType string) (string, error) {
|
|
f.key = key
|
|
f.data = append([]byte(nil), data...)
|
|
f.contentType = contentType
|
|
if f.url == "" {
|
|
return "https://cdn.example.com/" + key, nil
|
|
}
|
|
return f.url, nil
|
|
}
|
|
|
|
func (f *fakeOSSProvider) Name() string { return "fake" }
|
|
|
|
type fakeSummarizer struct {
|
|
prompt string
|
|
url string
|
|
text string
|
|
}
|
|
|
|
func (f *fakeSummarizer) SummarizeImage(_ context.Context, prompt string, imageURL string) (string, error) {
|
|
f.prompt = prompt
|
|
f.url = imageURL
|
|
if f.text == "" {
|
|
return "summary text", nil
|
|
}
|
|
return f.text, nil
|
|
}
|
|
|
|
func TestCaptureSummaryServiceUploadsSummarizesAndCopies(t *testing.T) {
|
|
ctx := context.Background()
|
|
provider := &fakeOSSProvider{}
|
|
summarizer := &fakeSummarizer{}
|
|
clip := &fakeClipboard{}
|
|
svc := &CaptureSummaryService{
|
|
Provider: provider,
|
|
Summarizer: summarizer,
|
|
Clipboard: clip,
|
|
Prompt: "describe screenshot",
|
|
PathPrefix: "snapgo/",
|
|
}
|
|
|
|
uploaded, err := svc.UploadImage(ctx, []byte("png"))
|
|
if err != nil {
|
|
t.Fatalf("upload image: %v", err)
|
|
}
|
|
if provider.contentType != "image/png" {
|
|
t.Fatalf("expected image/png content type, got %q", provider.contentType)
|
|
}
|
|
if string(provider.data) != "png" {
|
|
t.Fatalf("expected uploaded png bytes, got %q", string(provider.data))
|
|
}
|
|
|
|
summary, err := svc.Summarize(ctx, uploaded.URL)
|
|
if err != nil {
|
|
t.Fatalf("summarize: %v", err)
|
|
}
|
|
if summarizer.prompt != "describe screenshot" {
|
|
t.Fatalf("expected prompt forwarded, got %q", summarizer.prompt)
|
|
}
|
|
if summarizer.url != uploaded.URL {
|
|
t.Fatalf("expected uploaded url forwarded, got %q", summarizer.url)
|
|
}
|
|
if summary != "summary text" {
|
|
t.Fatalf("expected summary text, got %q", summary)
|
|
}
|
|
|
|
if err := svc.CopySummary(ctx, summary); err != nil {
|
|
t.Fatalf("copy summary: %v", err)
|
|
}
|
|
if clip.text != "summary text" {
|
|
t.Fatalf("expected summary copied, got %q", clip.text)
|
|
}
|
|
}
|