237 lines
8.1 KiB
Go
237 lines
8.1 KiB
Go
package app
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"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 TestEnqueueCollectNowOutboxDoesNotInterruptActiveTasks(t *testing.T) {
|
|
ctx := t.Context()
|
|
targetClientID := mustParseDailyMonitoringUUID(t, "00000000-0000-0000-0000-000000000503")
|
|
tx := &captureDailyMonitorTaskTx{commandTag: pgconn.NewCommandTag("INSERT 0 1")}
|
|
|
|
err := (&MonitoringService{}).enqueueCollectNowOutbox(
|
|
ctx,
|
|
tx,
|
|
uuid.New(),
|
|
11,
|
|
[]uuid.UUID{targetClientID},
|
|
3,
|
|
9,
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
require.Len(t, tx.execSQLs, 1)
|
|
normalized := normalizeSQLWhitespace(tx.execSQLs[0])
|
|
assert.Contains(t, normalized, "'dispatch_high'")
|
|
assert.NotContains(t, normalized, "'interrupt_requested'")
|
|
assert.NotContains(t, normalized, "task_control")
|
|
}
|
|
|
|
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)
|
|
}
|