Files
geo/server/internal/tenant/app/monitoring_service_date_test.go
T
root ff86933c24 feat(monitoring): generate daily tasks and optimize heartbeat writes
Adds a MonitoringDailyTaskWorker that materializes and dispatches
daily collect tasks from active subscription/desktop-client state,
with per-run atomic metrics and a Prometheus collector. Dashboard
and question-detail APIs now surface a platform_authorization_status
so the UI can distinguish no-desktop-client, no-authorized-platforms,
and authorized states; quota/access-state lookups resolve by request
workspace instead of tenant.

Hardens heartbeat: a desktop_client_primary_leases table gives
per-(tenant, workspace) sticky primary selection, read-mostly lease
resolution avoids a per-heartbeat transaction, and unchanged platform
access reports skip snapshot upserts outside a 10-minute refresh
window. Adds heartbeat primary/snapshot metrics exposed via
token-protected tenant-api /api/internal/metrics endpoints plus
Prometheus. Monitoring quota seed is removed from dev-seed since
daily plans now derive from desktop bindings, and the projection
uses the canonical six-platform catalog so platforms with zero
samples still render.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 22:19:57 +08:00

192 lines
6.6 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 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)
}