naco-api/internal/server/coverage_test.go
Nacorid 0dcb74675e fix(analytics,x402): fix coverage gaps, engagement drivers, and Twitter cost passing
Analytics review fixes:
- Store Twitter analytics coverage as discrete intervals instead of a single
  contiguous window so disjoint fetch ranges no longer mask inner gaps as
  covered (fetch per uncovered range, add gap regression tests)
- Compute real AvgEngagement/Correlation in getEngagementDrivers instead of
  all-zero placeholders
- Make parseAnalyticsTimeRange surface invalid timestamps, cap merged analytics
  posts at limit, and normalize sentiment by timestamped posts only
- gofmt: fix misalignment in config.go and types.go
- Include Twitter in the default aggregate analytics scope; its API costs are
  passed to the caller through paid tool pricing

x402 cost passing:
- computeTwitterStyleProfileCost now charges base cost ($0.10) + user lookup
  ($0.01) + $0.005 per Twitter post, guaranteeing the charge always clears the
  underlying API cost (never break-even or loss)
- Return a $-prefixed dollar string for the settlement override, fixing the
  previously off-by-factor-100 raw-atomic-unit amount
- Raise getStyleProfile base registration to $0.36 to authorize the max Twitter
  charge
- Add tests for the cost formula, profitable floor, and dollar formatting
2026-09-02 22:44:02 +00:00

100 lines
3.5 KiB
Go

package server
import (
"testing"
"time"
)
func rangeOf(t *testing.T, s, e string) timeRange {
t.Helper()
layout := "2006-01-02 15:04"
start, err := time.Parse(layout, s)
if err != nil {
t.Fatalf("bad start %q: %v", s, err)
}
end, err := time.Parse(layout, e)
if err != nil {
t.Fatalf("bad end %q: %v", e, err)
}
return timeRange{start: start, end: end}
}
func assertRanges(t *testing.T, got []timeRange, want ...timeRange) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %d ranges, want %d: %+v", len(got), len(want), got)
}
for i := range want {
if !got[i].start.Equal(want[i].start) || !got[i].end.Equal(want[i].end) {
t.Fatalf("range %d: got [%v, %v], want [%v, %v]",
i, got[i].start, got[i].end, want[i].start, want[i].end)
}
}
}
func TestUncoveredRangesNoCoverage(t *testing.T) {
r := rangeOf(t, "2026-01-01 00:00", "2026-01-31 00:00")
got := uncoveredRanges(r.start, r.end, nil)
assertRanges(t, got, r)
}
func TestUncoveredRangesFullyCovered(t *testing.T) {
req := rangeOf(t, "2026-01-10 00:00", "2026-01-20 00:00")
cov := rangeOf(t, "2026-01-01 00:00", "2026-01-31 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{cov})
assertRanges(t, got)
}
func TestUncoveredRangesLeftOnly(t *testing.T) {
req := rangeOf(t, "2026-01-01 00:00", "2026-01-31 00:00")
cov := rangeOf(t, "2026-01-15 00:00", "2026-01-31 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{cov})
want := rangeOf(t, "2026-01-01 00:00", "2026-01-15 00:00")
assertRanges(t, got, want)
}
func TestUncoveredRangesRightOnly(t *testing.T) {
req := rangeOf(t, "2026-01-01 00:00", "2026-01-31 00:00")
cov := rangeOf(t, "2026-01-01 00:00", "2026-01-15 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{cov})
want := rangeOf(t, "2026-01-15 00:00", "2026-01-31 00:00")
assertRanges(t, got, want)
}
func TestUncoveredRangesBothSides(t *testing.T) {
req := rangeOf(t, "2026-01-01 00:00", "2026-01-31 00:00")
cov := rangeOf(t, "2026-01-10 00:00", "2026-01-20 00:00")
wantLeft := rangeOf(t, "2026-01-01 00:00", "2026-01-10 00:00")
wantRight := rangeOf(t, "2026-01-20 00:00", "2026-01-31 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{cov})
assertRanges(t, got, wantLeft, wantRight)
}
// Regression: disjoint covered intervals must not be merged into a single
// contiguous window. A request landing in the gap between two covered ranges
// must be reported as uncovered so it is fetched rather than served from an
// incomplete cache.
func TestUncoveredRangesDisjointGap(t *testing.T) {
req := rangeOf(t, "2026-01-15 00:00", "2026-01-20 00:00")
first := rangeOf(t, "2026-01-01 00:00", "2026-01-15 00:00")
second := rangeOf(t, "2026-01-20 00:00", "2026-01-31 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{first, second})
assertRanges(t, got, req)
}
func TestUncoveredRangesGapSurroundedByCoverage(t *testing.T) {
req := rangeOf(t, "2026-01-10 00:00", "2026-01-25 00:00")
first := rangeOf(t, "2026-01-01 00:00", "2026-01-12 00:00")
second := rangeOf(t, "2026-01-20 00:00", "2026-01-31 00:00")
want := rangeOf(t, "2026-01-12 00:00", "2026-01-20 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{first, second})
assertRanges(t, got, want)
}
func TestUncoveredRangesOverlappingCoverage(t *testing.T) {
req := rangeOf(t, "2026-01-10 00:00", "2026-01-20 00:00")
first := rangeOf(t, "2026-01-01 00:00", "2026-01-15 00:00")
second := rangeOf(t, "2026-01-12 00:00", "2026-01-31 00:00")
got := uncoveredRanges(req.start, req.end, []timeRange{first, second})
assertRanges(t, got)
}