增强AI个性化画像分层并补回归测试

This commit is contained in:
hello-dd-code
2026-02-28 16:24:11 +08:00
parent c735b791cb
commit 28690e2477
2 changed files with 59 additions and 15 deletions
@@ -79,6 +79,7 @@ type adviceUserProfile struct {
SleepTime string `json:"sleep_time,omitempty"`
AwakeMinutes int `json:"awake_minutes,omitempty"`
BaselineIntervalMinutes int `json:"baseline_interval_minutes,omitempty"`
UserSegment string `json:"user_segment,omitempty"`
OnboardingCompletedAtISO string `json:"onboarding_completed_at,omitempty"`
}
@@ -334,6 +335,7 @@ func loadAdviceUserProfile(ctx context.Context, db *gorm.DB, uid int) *adviceUse
SleepTime: sleep,
AwakeMinutes: awake,
BaselineIntervalMinutes: baselineIntervalMinutes(awake, profile.BaselineCigsPerDay),
UserSegment: deriveUserSegment(profile.BaselineCigsPerDay, profile.SmokingYears),
}
if profile.OnboardingCompletedAt != nil {
out.OnboardingCompletedAtISO = profile.OnboardingCompletedAt.In(time.Local).Format(time.RFC3339)
@@ -467,3 +469,13 @@ func truncateString(s string, max int) string {
}
return s[:max]
}
func deriveUserSegment(baselineCigsPerDay int, smokingYears float64) string {
if baselineCigsPerDay >= 20 || smokingYears >= 10 {
return "heavy"
}
if baselineCigsPerDay >= 10 || smokingYears >= 3 {
return "moderate"
}
return "newbie"
}
@@ -0,0 +1,32 @@
package service
import "testing"
func TestDeriveUserSegment(t *testing.T) {
t.Parallel()
tests := []struct {
name string
baselinePerDay int
smokingYears float64
expectedSegment string
}{
{name: "新手用户", baselinePerDay: 6, smokingYears: 1.5, expectedSegment: "newbie"},
{name: "中度用户_按日均", baselinePerDay: 12, smokingYears: 1, expectedSegment: "moderate"},
{name: "中度用户_按烟龄", baselinePerDay: 8, smokingYears: 3.2, expectedSegment: "moderate"},
{name: "重度用户_按日均", baselinePerDay: 20, smokingYears: 2, expectedSegment: "heavy"},
{name: "重度用户_按烟龄", baselinePerDay: 9, smokingYears: 10, expectedSegment: "heavy"},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := deriveUserSegment(tc.baselinePerDay, tc.smokingYears)
if got != tc.expectedSegment {
t.Fatalf("deriveUserSegment(%d, %.1f)=%s, want=%s", tc.baselinePerDay, tc.smokingYears, got, tc.expectedSegment)
}
})
}
}