Files
geo/server/internal/tenant/app/monitoring_service_date_test.go
T
root af35301d9b
Desktop Client Build / Resolve Build Metadata (push) Successful in 28s
Frontend CI / Frontend (push) Successful in 4m5s
Backend CI / Backend (push) Successful in 17m7s
Desktop Client Build / Publish Client Artifacts to NAS (push) Has been cancelled
Desktop Client Build / Build Desktop Client (push) Has been cancelled
test(desktop): cover deepseek account name extraction for wechat + phone logins
Refactor readDeepseekAccountIdentity so the JSON payload parser is a pure
ts function (extractDeepseekIdentityFromPayload). The webContents IIFE now
just polls for userToken and returns the raw API JSON; payload picking lives
in ts and is unit-tested.

Tests lock the contract for both login types we observed in the wild:
  - WeChat-bound account: id_profile.name = '阿白', picture present.
  - Phone-only login: id_profile = null, mobile_number = '177******08',
    email = '' (empty string, not null) — the case that originally regressed.

Plus regressions for the priority order (profile.name > mobile_number > email),
the all-empty fallback, and non-record payloads.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 20:02:57 +08:00

213 lines
7.4 KiB
Go

package app
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/geo-platform/tenant-api/internal/shared/response"
)
func TestResolveDashboardDateWindowAtUsesBusinessTimezone(t *testing.T) {
loc := time.FixedZone("CST", 8*60*60)
now := time.Date(2026, 4, 12, 0, 5, 0, 0, loc)
startDate, endDate, err := resolveDashboardDateWindowAt(7, "2026-04-12", now, loc)
require.NoError(t, err)
assert.Equal(t, time.Date(2026, 4, 6, 0, 0, 0, 0, loc), startDate)
assert.Equal(t, time.Date(2026, 4, 12, 0, 0, 0, 0, loc), endDate)
}
func TestResolveDashboardDateWindowAtRejectsFutureBusinessDay(t *testing.T) {
loc := time.FixedZone("CST", 8*60*60)
now := time.Date(2026, 4, 12, 0, 5, 0, 0, loc)
_, _, err := resolveDashboardDateWindowAt(7, "2026-04-13", now, loc)
require.Error(t, err)
appErr := response.Normalize(err)
assert.Equal(t, 40031, appErr.Code)
assert.Equal(t, "invalid_business_date", appErr.Message)
assert.Equal(t, "business_date must not be later than today", appErr.Detail)
}
func TestParseDetailDateRangeAtDefaultsToCurrentBusinessDay(t *testing.T) {
loc := time.FixedZone("CST", 8*60*60)
now := time.Date(2026, 4, 12, 0, 5, 0, 0, loc)
fromDate, toDate, err := parseDetailDateRangeAt("", "", now, loc)
require.NoError(t, err)
expected := time.Date(2026, 4, 12, 0, 0, 0, 0, loc)
assert.Equal(t, expected, fromDate)
assert.Equal(t, expected, toDate)
}
func TestMonitoringBusinessDayAndDateAtUsesShanghaiTimezone(t *testing.T) {
now := time.Date(2026, 4, 11, 17, 19, 0, 0, time.UTC)
businessDay, businessDate := monitoringBusinessDayAndDateAt(now)
assert.Equal(t, time.Date(2026, 4, 12, 0, 0, 0, 0, monitoringBusinessLocation()), businessDay)
assert.Equal(t, "2026-04-12", businessDate)
}
func TestEncodeMonitoringProjectionRebuildEnvelopePreservesBusinessDate(t *testing.T) {
businessDay := time.Date(2026, 4, 12, 0, 0, 0, 0, monitoringBusinessLocation())
payload, err := encodeMonitoringProjectionRebuildEnvelope(1, 2, businessDay, "test")
require.NoError(t, err)
envelope, err := decodeMonitoringProjectionRebuildEnvelope(payload)
require.NoError(t, err)
assert.Equal(t, "2026-04-12", envelope.BusinessDate)
}
func TestFilterMonitoringPlatformsReturnsSelectedPlatform(t *testing.T) {
platforms := []monitoringPlatformMetadata{
{ID: "deepseek", Name: "DeepSeek"},
{ID: "qwen", Name: "通义千问"},
{ID: "doubao", Name: "豆包"},
}
selected := "qwen"
filtered := filterMonitoringPlatforms(platforms, &selected)
assert.Equal(t, []monitoringPlatformMetadata{
{ID: "qwen", Name: "通义千问"},
}, filtered)
}
func TestFilterMonitoringPlatformsDropsUnauthorizedSelection(t *testing.T) {
platforms := []monitoringPlatformMetadata{
{ID: "deepseek", Name: "DeepSeek"},
}
selected := "doubao"
filtered := filterMonitoringPlatforms(platforms, &selected)
assert.Empty(t, filtered)
}
func TestNormalizeMonitoringPlatformIDCanonicalizesLatestIDs(t *testing.T) {
assert.Equal(t, "qwen", normalizeMonitoringPlatformID(" QWEN "))
assert.Equal(t, "wenxin", normalizeMonitoringPlatformID("WenXin"))
assert.Equal(t, "yuanbao", normalizeMonitoringPlatformID("yuanbao"))
}
func TestFilterMonitoringPlatformsMatchesCanonicalPlatform(t *testing.T) {
platforms := []monitoringPlatformMetadata{
{ID: "qwen", Name: "通义千问"},
}
selected := "qwen"
filtered := filterMonitoringPlatforms(platforms, &selected)
assert.Equal(t, []monitoringPlatformMetadata{
{ID: "qwen", Name: "通义千问"},
}, filtered)
}
func TestIntersectMonitoringPlatformsAcceptsCanonicalIDs(t *testing.T) {
enabled := []monitoringPlatformMetadata{
{ID: "qwen", Name: "通义千问"},
{ID: "wenxin", Name: "文心一言"},
}
filtered, err := intersectMonitoringPlatforms(enabled, []string{"qwen", "wenxin"})
require.NoError(t, err)
assert.Equal(t, []monitoringPlatformMetadata{
{ID: "qwen", Name: "通义千问"},
{ID: "wenxin", Name: "文心一言"},
}, filtered)
}
func TestReconcileEnabledMonitoringPlatformsKeepsAuthorizedSubset(t *testing.T) {
assert.Equal(t, []string{"deepseek", "doubao", "qwen"}, reconcileEnabledMonitoringPlatforms([]string{"deepseek", "qwen", "doubao"}))
assert.Empty(t, reconcileEnabledMonitoringPlatforms(nil))
assert.Empty(t, resolveMonitoringPlatforms(nil))
}
func TestFilterMonitoringPlatformsByIDSetKeepsCatalogOrder(t *testing.T) {
platforms := defaultMonitoringPlatformMetadata()
filtered := filterMonitoringPlatformsByIDSet(platforms, []string{"qwen", "yuanbao"})
assert.Equal(t, []string{"yuanbao", "qwen"}, monitoringPlatformIDs(filtered))
}
func TestCollectNowDispatchTargetClientIDsDeduplicatesByPlatformOrder(t *testing.T) {
sharedClientID := mustParseDailyMonitoringUUID(t, "00000000-0000-0000-0000-000000000501")
qwenClientID := mustParseDailyMonitoringUUID(t, "00000000-0000-0000-0000-000000000502")
targets := collectNowDispatchTargetClientIDs(map[string]uuid.UUID{
"yuanbao": sharedClientID,
"qwen": qwenClientID,
"doubao": sharedClientID,
}, &sharedClientID)
assert.Equal(t, []uuid.UUID{sharedClientID, qwenClientID}, targets)
}
func TestMonitoringPlatformAuthorizationStatus(t *testing.T) {
clientID := uuid.New()
assert.Equal(t, monitoringPlatformAuthorizationNoDesktopClient, monitoringPlatformAuthorizationStatus(monitoringQuotaConfig{}))
assert.Equal(t, monitoringPlatformAuthorizationNoAuthorizedPlatforms, monitoringPlatformAuthorizationStatus(monitoringQuotaConfig{
PrimaryClientID: &clientID,
}))
assert.Equal(t, monitoringPlatformAuthorizationAuthorized, monitoringPlatformAuthorizationStatus(monitoringQuotaConfig{
PrimaryClientID: &clientID,
EnabledPlatforms: []string{"qwen"},
}))
}
func TestDefaultMonitoringPlatformMetadataReturnsFullCatalogCopy(t *testing.T) {
platforms := defaultMonitoringPlatformMetadata()
require.Len(t, platforms, 6)
assert.Equal(t, []string{"yuanbao", "kimi", "wenxin", "deepseek", "doubao", "qwen"}, monitoringPlatformIDs(platforms))
platforms[0].ID = "mutated"
assert.Equal(t, "yuanbao", defaultMonitoringPlatformMetadata()[0].ID)
}
func TestLoadMonitoringProjectionPlatformsReturnsFullCatalog(t *testing.T) {
platforms, err := loadMonitoringProjectionPlatforms(nil, nil, 0, 0, time.Time{})
require.NoError(t, err)
assert.Equal(t, []string{"yuanbao", "kimi", "wenxin", "deepseek", "doubao", "qwen"}, monitoringPlatformIDs(platforms))
}
func TestApplyDerivedRatesToOverviewSnapshotUsesAllSamplesForMentionRate(t *testing.T) {
day := time.Date(2026, 4, 22, 0, 0, 0, 0, monitoringBusinessLocation())
snapshot := monitoringOverviewSnapshot{Date: day}
derived := newMonitoringDerivedMetrics()
derived.ByDate["2026-04-22"] = monitoringDerivedRateStats{
Total: 4,
MentionedCount: 1,
Top1Count: 1,
FirstRecommend: 1,
PositiveMentions: 1,
}
derived.ByDatePlatform["2026-04-22"] = map[string]monitoringDerivedRateStats{
"qwen": {Total: 1, MentionedCount: 1},
"deepseek": {Total: 3, MentionedCount: 0},
}
applyDerivedRatesToOverviewSnapshot(&snapshot, derived)
require.True(t, snapshot.MentionRate.Valid)
require.True(t, snapshot.Top1MentionRate.Valid)
require.True(t, snapshot.FirstRecommendRate.Valid)
require.True(t, snapshot.PositiveMentionRate.Valid)
assert.InDelta(t, 0.25, snapshot.MentionRate.Float64, 0.0001)
assert.InDelta(t, 0.25, snapshot.Top1MentionRate.Float64, 0.0001)
assert.InDelta(t, 0.25, snapshot.FirstRecommendRate.Float64, 0.0001)
assert.InDelta(t, 0.25, snapshot.PositiveMentionRate.Float64, 0.0001)
}