Files
Pulse/pkg/server/telemetry_licensed_features_test.go
rcourtman c5b6d05107 feat(telemetry): add schema v26 map topology API/CLI/MCP telemetry
I added Schema v26 content-free Infrastructure Map topology read telemetry for external live GET /api/agent/topology requests (total read days, CLI days, MCP days, anonymized days, and an explicit availability flag) alongside hardened opt-out zero-write boundaries, credential-binding checks, and read-only SQLite/config guards.

Key changes:
- Added a dedicated root-scoped daily tally store (infrastructure_map_topology_tally.json) with strict schema validation, fail-closed degraded sidecar accounting, and strict zero-write guarantees when telemetry is disabled by settings or PULSE_TELEMETRY=false.
- Added X-Pulse-Agent-Surface attribution for pulse-cli and MCP clients while filtering out first-party browser traffic, mock-mode snapshots, and unverified monitor states.
- Updated Settings payload preview, localization copy, shipped PRIVACY.md disclosure, integration specs, and schema parity tooling for Schema v26.
- Hardened host/agent token hash binding, self-uninstall live authority and durable hash-scoped tombstones, Patrol investigation replay-floor provenance, bounded non-blocking regular-file config reads, and cold read-only legacy SQLite history queries.
2026-12-02 01:41:25 +00:00

115 lines
5.0 KiB
Go

package server
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/telemetry"
)
func TestApplyLicensedFeatureConfigSnapshot_CountsScheduledReportingAndProfiles(t *testing.T) {
now := time.Date(2026, 8, 5, 12, 0, 0, 0, time.UTC)
recent := now.Add(-48 * time.Hour)
old := now.Add(-(telemetry.PulseIntelligenceTelemetryWindow + time.Hour))
persistence := config.NewConfigPersistence(t.TempDir())
if err := persistence.SaveReportScheduleStore(config.ReportScheduleStore{
Schedules: []config.ReportSchedule{
{ID: "a", Name: "weekly-capacity", Enabled: true, LastRunAt: &recent},
{ID: "b", Name: "monthly-uptime", Enabled: true, LastRunAt: &old},
{ID: "c", Name: "paused", Enabled: false, LastRunAt: &recent},
{ID: "d", Name: "never-run", Enabled: true},
},
}); err != nil {
t.Fatalf("SaveReportScheduleStore: %v", err)
}
if err := persistence.SaveAgentProfiles([]models.AgentProfile{
{ID: "profile-1", Name: "edge"},
{ID: "profile-2", Name: "core"},
}); err != nil {
t.Fatalf("SaveAgentProfiles: %v", err)
}
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshot(&snap, persistence, now)
if snap.ReportSchedules != 4 {
t.Fatalf("report schedules = %d, want 4", snap.ReportSchedules)
}
if snap.ReportSchedulesEnabled != 3 {
t.Fatalf("enabled report schedules = %d, want 3", snap.ReportSchedulesEnabled)
}
// Only the two schedules whose last run falls inside the window count, and
// a disabled schedule that still ran recently is one of them.
if snap.ReportSchedulesRun30d != 2 {
t.Fatalf("report schedules run in window = %d, want 2", snap.ReportSchedulesRun30d)
}
if snap.AgentProfiles != 2 {
t.Fatalf("agent profiles = %d, want 2", snap.AgentProfiles)
}
}
func TestApplyLicensedFeatureConfigSnapshot_LeavesCountsZeroWhenNothingConfigured(t *testing.T) {
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshot(&snap, config.NewConfigPersistence(t.TempDir()), time.Now().UTC())
if snap.ReportSchedules != 0 || snap.ReportSchedulesEnabled != 0 || snap.ReportSchedulesRun30d != 0 || snap.AgentProfiles != 0 {
t.Fatalf("unconfigured install must report zeroes: %#v", snap)
}
}
// A nil persistence must not panic: the telemetry snapshot runs on a timer and
// a failed persistence init cannot be allowed to take the process down.
func TestApplyLicensedFeatureConfigSnapshot_ToleratesNilInputs(t *testing.T) {
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshot(&snap, nil, time.Now().UTC())
applyLicensedFeatureConfigSnapshot(nil, config.NewConfigPersistence(t.TempDir()), time.Now().UTC())
}
func TestApplyLicensedFeatureConfigSnapshotIncludesSuccessfulExtendedHistoryReads(t *testing.T) {
persistence := config.NewConfigPersistence(t.TempDir())
now := time.Now().UTC()
if err := persistence.RecordSuccessfulExtendedHistoryRead(now); err != nil {
t.Fatal(err)
}
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshot(&snap, persistence, now)
if snap.SuccessfulHistoryReadsOver7d30d != 1 {
t.Fatalf("extended history reads = %d, want 1", snap.SuccessfulHistoryReadsOver7d30d)
}
}
func TestApplyLicensedFeatureConfigSnapshotReportingOutcomes(t *testing.T) {
p := config.NewConfigPersistence(t.TempDir())
now := time.Now().UTC()
for _, event := range []string{"generated", "saved", "email_accepted", "delivery_failed", "delivery_unconfirmed"} {
if err := p.RecordReportingActivity(config.ReportingActivityEvent{At: now, Kind: config.ReportScheduleKindBackupHealth, Event: event, DataState: "partial"}); err != nil {
t.Fatal(err)
}
}
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshot(&snap, p, now)
if !snap.ReportingTelemetryAvailable || snap.ReportingObservationDays != 1 || snap.ReportingGeneratedBackupHealth30d != 1 || snap.ReportingBackupHealthActiveDays30d != 1 || snap.ReportingSaved30d != 1 || snap.ReportingEmailAccepted30d != 1 || snap.ReportingDeliveryFailed30d != 1 || snap.ReportingDeliveryUnconfirmed30d != 1 || snap.ReportingDataIncomplete30d != 1 {
t.Fatalf("reporting snapshot: %+v", snap)
}
}
// High #7 regression: read-only preview/opted-out licensed feature snapshot
// must never create reporting_activity.json when absent.
func TestApplyLicensedFeatureConfigSnapshotReadOnlyWritesZeroBytes(t *testing.T) {
dir := t.TempDir()
p := config.NewConfigPersistence(dir)
var snap telemetry.Snapshot
applyLicensedFeatureConfigSnapshotWithMode(&snap, p, time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC), true, false, 0)
if snap.ReportingTelemetryAvailable || snap.ReportingObservationDays != 0 {
t.Fatalf("expected unavailable zero reporting snapshot when file absent, got avail=%v days=%d", snap.ReportingTelemetryAvailable, snap.ReportingObservationDays)
}
if _, err := os.Stat(filepath.Join(dir, "reporting_activity.json")); !os.IsNotExist(err) {
t.Fatalf("read-only licensed feature snapshot created reporting_activity.json (stat err=%v)", err)
}
}