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
95
96
97
98
99
100
101
102
103
104
105
106
|
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 := "ябълка"
var maxSceneLen int
for i := 0; i < 3; i++ {
candidateSubject := subject + strings.Repeat("x", i)
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, candidateSubject,
)
maxSceneLen = maxImagePromptChars - len(template)
if maxSceneLen%3 != 0 {
subject = candidateSubject
break
}
}
scene := "abc" + strings.Repeat("界", maxSceneLen+32)
if len(scene) <= maxSceneLen {
t.Fatalf("test setup failed: scene length = %d, want > %d", len(scene), maxSceneLen)
}
if utf8.ValidString(scene[:maxSceneLen]) {
t.Fatalf("test setup failed: raw byte slice unexpectedly remained valid UTF-8 at cut position %d", maxSceneLen)
}
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)
}
if !strings.Contains(got, "abc") {
t.Fatalf("buildEducationalPrompt() = %q, want ASCII scene prefix to survive truncation", got)
}
}
|