// 图片生成服务测试，验证参考图文件名映射和对象键去重规则。
package productimage

import "testing"

// TestBuildProviderPrompt 验证文件名提及会与参考图数组顺序建立稳定对应关系。
func TestBuildProviderPrompt(t *testing.T) {
	t.Parallel()
	prompt := "让@正面图.png中的商品使用@包装图.webp的包装"
	got := buildProviderPrompt(prompt, []string{"正面图.png", "包装图.webp"})
	want := "参考图对应关系：第1张参考图名称为“正面图.png”；第2张参考图名称为“包装图.webp”。\n" + prompt
	if got != want {
		t.Fatalf("buildProviderPrompt() = %q, want %q", got, want)
	}
	if withoutReferences := buildProviderPrompt(prompt, nil); withoutReferences != prompt {
		t.Fatalf("buildProviderPrompt() without references = %q, want %q", withoutReferences, prompt)
	}
}

// TestUniqueStrings 验证重复或空对象键不会触发重复删除请求。
func TestUniqueStrings(t *testing.T) {
	t.Parallel()
	got := uniqueStrings([]string{"a", "", "b", "a", "b", "c"})
	want := []string{"a", "b", "c"}
	if len(got) != len(want) {
		t.Fatalf("uniqueStrings() length = %d, want %d", len(got), len(want))
	}
	for index := range want {
		if got[index] != want[index] {
			t.Fatalf("uniqueStrings()[%d] = %q, want %q", index, got[index], want[index])
		}
	}
}

// TestCanDeleteGeneration 验证失败停滞的活动任务可删除，而正常生成中的任务仍受保护。
func TestCanDeleteGeneration(t *testing.T) {
	t.Parallel()
	tests := []struct {
		name         string
		status       string
		errorMessage string
		want         bool
	}{
		{name: "正常生成", status: "processing", want: false},
		{name: "连接超时", status: "pending_submission", errorMessage: "dial tcp: i/o timeout", want: true},
		{name: "生成失败", status: "failed", want: true},
		{name: "生成成功", status: "succeeded", want: true},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			if got := canDeleteGeneration(test.status, test.errorMessage); got != test.want {
				t.Fatalf("canDeleteGeneration(%q, %q) = %t, want %t", test.status, test.errorMessage, got, test.want)
			}
		})
	}
}
