summaryrefslogtreecommitdiff
path: root/internal/image/prompt_test.go
blob: 957c84c472742b273849297af7a8f4dd65462a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package image

import (
	"fmt"
	"strings"
	"testing"
	"unicode/utf8"
)

func TestPromptSubjectUsesTranslationFirst(t *testing.T) {
	t.Parallel()

	if got := promptSubject(" apple ", "ябълка"); got != "apple" {
		t.Fatalf("promptSubject() = %q, want %q", got, "apple")
	}
}

func TestPromptSubjectFallsBackToOriginalWord(t *testing.T) {
	t.Parallel()

	if got := promptSubject("   ", "ябълка"); got != "ябълка" {
		t.Fatalf("promptSubject() = %q, want %q", got, "ябълка")
	}
}

func TestSanitizeSceneDescriptionRemovesLabelsAndFences(t *testing.T) {
	t.Parallel()

	scene := "```text\nScene: A bright apple sits centered on a wooden table.\n```"
	if got := sanitizeSceneDescription(scene); got != "A bright apple sits centered on a wooden table" {
		t.Fatalf("sanitizeSceneDescription() = %q", got)
	}
}

func TestUsableSceneDescriptionRejectsTrivialContent(t *testing.T) {
	t.Parallel()

	if usableSceneDescription("A") {
		t.Fatal("usableSceneDescription() unexpectedly accepted trivial scene")
	}
	if usableSceneDescription("A single, perfectly") {
		t.Fatal("usableSceneDescription() unexpectedly accepted incomplete scene fragment")
	}
}

func TestFallbackVisualDirectionForPhrase(t *testing.T) {
	t.Parallel()

	got := fallbackVisualDirection("to indulge someone")
	if got == "" || !usableSceneDescription(got) {
		t.Fatalf("fallbackVisualDirection() returned unusable phrase direction: %q", got)
	}
}

func TestFallbackVisualDirectionForSingleWord(t *testing.T) {
	t.Parallel()

	got := fallbackVisualDirection("apple")
	if got == "" || !strings.Contains(got, "single apple") {
		t.Fatalf("fallbackVisualDirection() = %q", got)
	}
}

func TestBuildEducationalPromptTruncatesLongSceneWithoutBreakingUTF8(t *testing.T) {
	t.Parallel()

	style := "Photorealism"
	subject := "ябълка"
	for {
		template := fmt.Sprintf(
			"Generate a %s flashcard image illustrating \"%s\". Scene:  "+
				"The image should be educational and suitable for language learning flashcards. "+
				"Requirements: The main subject or concept must be clearly visible, centered, well lit, and easy to identify.",
			style, subject,
		)
		if (maxImagePromptChars-len(template))%2 == 1 {
			break
		}
		subject += "x"
	}

	scene := strings.Repeat("Ярка сцена с ябълки и хора, ", 80)
	got := buildEducationalPrompt(style, scene, subject)

	if !utf8.ValidString(got) {
		t.Fatalf("buildEducationalPrompt() returned invalid UTF-8: %q", got)
	}
	if len(got) > maxImagePromptChars {
		t.Fatalf("buildEducationalPrompt() length = %d, want <= %d", len(got), maxImagePromptChars)
	}
	if !strings.Contains(got, "...") {
		t.Fatalf("buildEducationalPrompt() = %q, want truncated scene marker", got)
	}
}