90 lines
3.5 KiB
Go
90 lines
3.5 KiB
Go
package insight
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.amiirkhl.ir/starcoach-hackathon/zarinpal-analytics-backend/internal/peercomparison"
|
|
)
|
|
|
|
func TestGeneratedInsightTextMustBeOnePlainParagraph(t *testing.T) {
|
|
for _, value := range []string{"# عنوان", "**مهم**", "متن _تأکیدی_", "خط اول\nخط دوم", "- مورد اول"} {
|
|
if isPlainParagraph(value) {
|
|
t.Fatalf("isPlainParagraph(%q) = true, want false", value)
|
|
}
|
|
}
|
|
if !isPlainParagraph("فروش این بازه بهتر شده و بهتر است روند آن را حفظ کنید.") {
|
|
t.Fatal("simple Persian paragraph was rejected")
|
|
}
|
|
}
|
|
|
|
func TestInsightInstructionsRequireExactPersianPeriodNames(t *testing.T) {
|
|
instructions := insightInstructions(ModuleOverview)
|
|
for _, required := range []string{
|
|
"selectedPeriod.jalaliLabelFa",
|
|
"precedingPeriod.jalaliLabelFa",
|
|
"در بازه انتخابشده",
|
|
"بازه بلافاصله قبل",
|
|
} {
|
|
if !strings.Contains(instructions, required) {
|
|
t.Fatalf("instructions do not contain %q", required)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGeneratedInsightAllowsOnlyDigitsInsideExactPeriodLabels(t *testing.T) {
|
|
labels := []string{"مرداد ۱۴۰۵", "تیر ۱۴۰۵"}
|
|
if containsUnexpectedDigit("در مرداد ۱۴۰۵ وضعیت پرداخت بهتر از تیر ۱۴۰۵ بود.", labels) {
|
|
t.Fatal("digits inside exact allowlisted period labels were rejected")
|
|
}
|
|
if !containsUnexpectedDigit("در مرداد ۱۴۰۶ وضعیت پرداخت بهتر بود.", labels) {
|
|
t.Fatal("a number outside the exact period labels was accepted")
|
|
}
|
|
}
|
|
|
|
func TestGeneratedInsightMustNameSelectedAndPrecedingPeriods(t *testing.T) {
|
|
labels := []string{"مرداد ۱۴۰۵", "تیر ۱۴۰۵"}
|
|
if !hasExactPeriodReferences(generatedInsight{
|
|
LatestObservedBody: "در مرداد ۱۴۰۵ وضعیت پرداخت بهتر بود.",
|
|
PastBody: "در تیر ۱۴۰۵ پرداخت موفق کمتری ثبت شده بود.",
|
|
}, labels) {
|
|
t.Fatal("exact selected and preceding period labels were rejected")
|
|
}
|
|
if hasExactPeriodReferences(generatedInsight{
|
|
LatestObservedBody: "در بازه انتخابشده وضعیت پرداخت بهتر بود.",
|
|
PastBody: "نسبت به بازه بلافاصله قبل پرداخت موفق بیشتر شد.",
|
|
}, labels) {
|
|
t.Fatal("generic period wording was accepted")
|
|
}
|
|
}
|
|
|
|
func TestPeerInsightAllowsOnlyGroundedAggregateNumbers(t *testing.T) {
|
|
evidence, err := json.Marshal(map[string]any{
|
|
"peerComparison": peercomparison.Result{
|
|
State: peercomparison.StateReady,
|
|
EligiblePeerCount: 10,
|
|
CombinedPeerSessions: 1000,
|
|
Metrics: []peercomparison.Metric{{
|
|
MetricID: "payment_success_rate",
|
|
CohortMedian: "72.50",
|
|
MerchantPerformancePercentile: "85.00",
|
|
Direction: "HIGHER_IS_BETTER",
|
|
}},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal evidence: %v", err)
|
|
}
|
|
labels := []string{"مرداد ۱۴۰۵"}
|
|
if containsUnexpectedPeerNumber("در مرداد ۱۴۰۵، جایگاه شما صدک ۸۵٫۰۰ است.", labels, evidence) {
|
|
t.Fatal("grounded Persian percentile was rejected")
|
|
}
|
|
if containsUnexpectedPeerNumber("این مقایسه بر پایه ۱٬۰۰۰ نشست است.", labels, evidence) {
|
|
t.Fatal("grounded Persian session count was rejected")
|
|
}
|
|
if !containsUnexpectedPeerNumber("جایگاه شما صدک ۹۱ است.", labels, evidence) {
|
|
t.Fatal("invented percentile was accepted")
|
|
}
|
|
}
|