51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
type fakeOCRRecognizer struct {
|
|
image []byte
|
|
text string
|
|
}
|
|
|
|
func (f *fakeOCRRecognizer) RecognizeText(_ context.Context, pngBytes []byte) (string, error) {
|
|
f.image = append([]byte(nil), pngBytes...)
|
|
if f.text == "" {
|
|
return "extracted text", nil
|
|
}
|
|
return f.text, nil
|
|
}
|
|
|
|
func TestCaptureOCRServiceRecognizesAndCopies(t *testing.T) {
|
|
ctx := context.Background()
|
|
recognizer := &fakeOCRRecognizer{}
|
|
clip := &fakeClipboard{}
|
|
svc := &CaptureOCRService{Recognizer: recognizer, Clipboard: clip}
|
|
|
|
text, err := svc.Recognize(ctx, []byte("png"))
|
|
if err != nil {
|
|
t.Fatalf("recognize: %v", err)
|
|
}
|
|
if string(recognizer.image) != "png" {
|
|
t.Fatalf("expected screenshot bytes forwarded, got %q", string(recognizer.image))
|
|
}
|
|
if text != "extracted text" {
|
|
t.Fatalf("expected extracted text, got %q", text)
|
|
}
|
|
if err := svc.CopyText(ctx, text); err != nil {
|
|
t.Fatalf("copy text: %v", err)
|
|
}
|
|
if clip.text != "extracted text" {
|
|
t.Fatalf("expected ocr text copied, got %q", clip.text)
|
|
}
|
|
}
|
|
|
|
func TestCaptureOCRServiceRejectsEmptyResult(t *testing.T) {
|
|
svc := &CaptureOCRService{Recognizer: &fakeOCRRecognizer{text: " \n "}}
|
|
if _, err := svc.Recognize(context.Background(), []byte("png")); err == nil {
|
|
t.Fatalf("expected empty OCR result to fail")
|
|
}
|
|
}
|