Delete assets/js/dashboard/stats/graph/fetch-top-stats.test.ts with huggingface_hub
Browse files
assets/js/dashboard/stats/graph/fetch-top-stats.test.ts
DELETED
|
@@ -1,304 +0,0 @@
|
|
| 1 |
-
import { Metric } from '../metrics'
|
| 2 |
-
import {
|
| 3 |
-
DashboardState,
|
| 4 |
-
dashboardStateDefaultValue,
|
| 5 |
-
Filter
|
| 6 |
-
} from '../../dashboard-state'
|
| 7 |
-
import { ComparisonMode, DashboardPeriod } from '../../dashboard-time-periods'
|
| 8 |
-
import { PlausibleSite, siteContextDefaultValue } from '../../site-context'
|
| 9 |
-
import { StatsQuery } from '../../stats-query'
|
| 10 |
-
import { remapToApiFilters } from '../../util/filters'
|
| 11 |
-
import { StatsReportQueryKey } from '../../hooks/use-query-api'
|
| 12 |
-
import {
|
| 13 |
-
chooseMetrics,
|
| 14 |
-
getTopStatsQuery,
|
| 15 |
-
getPartialDayTimeRange,
|
| 16 |
-
formatTopStatsData
|
| 17 |
-
} from './fetch-top-stats'
|
| 18 |
-
|
| 19 |
-
const aGoalFilter = ['is', 'goal', ['any goal']] as Filter
|
| 20 |
-
const aPageFilter = ['is', 'page', ['/any/page']] as Filter
|
| 21 |
-
const aPeriodNotRealtime = DashboardPeriod['28d']
|
| 22 |
-
|
| 23 |
-
const expectedBaseInclude: StatsQuery['include'] = {
|
| 24 |
-
compare: ComparisonMode.previous_period,
|
| 25 |
-
compare_match_day_of_week: true,
|
| 26 |
-
imports: true,
|
| 27 |
-
imports_meta: true,
|
| 28 |
-
time_labels: false,
|
| 29 |
-
partial_time_labels: false,
|
| 30 |
-
empty_metrics: false,
|
| 31 |
-
present_index: false
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
const expectedBaseQuery = {
|
| 35 |
-
dimensions: [],
|
| 36 |
-
filters: [],
|
| 37 |
-
include: expectedBaseInclude,
|
| 38 |
-
relative_date: null,
|
| 39 |
-
order_by: null,
|
| 40 |
-
pagination: null
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
type TestCase = [
|
| 44 |
-
/** situation */
|
| 45 |
-
string,
|
| 46 |
-
/** input dashboard state & site state */
|
| 47 |
-
Pick<DashboardState, 'filters' | 'period'> &
|
| 48 |
-
Partial<{ site?: Pick<PlausibleSite, 'revenueGoals'> }>,
|
| 49 |
-
/** expected metrics */
|
| 50 |
-
Metric[],
|
| 51 |
-
/** expected top stats query */
|
| 52 |
-
StatsQuery
|
| 53 |
-
]
|
| 54 |
-
|
| 55 |
-
const cases: TestCase[] = [
|
| 56 |
-
[
|
| 57 |
-
'realtime and goal filter',
|
| 58 |
-
{ period: DashboardPeriod.realtime, filters: [aGoalFilter] },
|
| 59 |
-
['visitors', 'events'],
|
| 60 |
-
{
|
| 61 |
-
...expectedBaseQuery,
|
| 62 |
-
date_range: DashboardPeriod.realtime_30m,
|
| 63 |
-
filters: remapToApiFilters([aGoalFilter]),
|
| 64 |
-
include: { ...expectedBaseInclude, compare: null },
|
| 65 |
-
metrics: ['visitors', 'events']
|
| 66 |
-
}
|
| 67 |
-
],
|
| 68 |
-
|
| 69 |
-
[
|
| 70 |
-
'realtime',
|
| 71 |
-
{ period: DashboardPeriod.realtime, filters: [] },
|
| 72 |
-
['visitors', 'pageviews'],
|
| 73 |
-
{
|
| 74 |
-
...expectedBaseQuery,
|
| 75 |
-
date_range: DashboardPeriod.realtime_30m,
|
| 76 |
-
include: {
|
| 77 |
-
...expectedBaseInclude,
|
| 78 |
-
compare: null
|
| 79 |
-
},
|
| 80 |
-
metrics: ['visitors', 'pageviews']
|
| 81 |
-
}
|
| 82 |
-
],
|
| 83 |
-
|
| 84 |
-
[
|
| 85 |
-
'goal filter with revenue metrics',
|
| 86 |
-
{
|
| 87 |
-
period: aPeriodNotRealtime,
|
| 88 |
-
filters: [aGoalFilter],
|
| 89 |
-
site: {
|
| 90 |
-
revenueGoals: [{ display_name: 'a revenue goal', currency: 'USD' }]
|
| 91 |
-
}
|
| 92 |
-
},
|
| 93 |
-
[
|
| 94 |
-
'visitors',
|
| 95 |
-
'events',
|
| 96 |
-
'total_revenue',
|
| 97 |
-
'average_revenue',
|
| 98 |
-
'conversion_rate'
|
| 99 |
-
],
|
| 100 |
-
{
|
| 101 |
-
...expectedBaseQuery,
|
| 102 |
-
date_range: aPeriodNotRealtime,
|
| 103 |
-
filters: remapToApiFilters([aGoalFilter]),
|
| 104 |
-
metrics: [
|
| 105 |
-
'visitors',
|
| 106 |
-
'events',
|
| 107 |
-
'total_revenue',
|
| 108 |
-
'average_revenue',
|
| 109 |
-
'conversion_rate'
|
| 110 |
-
]
|
| 111 |
-
}
|
| 112 |
-
],
|
| 113 |
-
|
| 114 |
-
[
|
| 115 |
-
'goal filter',
|
| 116 |
-
{ period: aPeriodNotRealtime, filters: [aGoalFilter] },
|
| 117 |
-
['visitors', 'events', 'conversion_rate'],
|
| 118 |
-
{
|
| 119 |
-
...expectedBaseQuery,
|
| 120 |
-
date_range: aPeriodNotRealtime,
|
| 121 |
-
filters: remapToApiFilters([aGoalFilter]),
|
| 122 |
-
metrics: ['visitors', 'events', 'conversion_rate']
|
| 123 |
-
}
|
| 124 |
-
],
|
| 125 |
-
|
| 126 |
-
[
|
| 127 |
-
'page filter',
|
| 128 |
-
{
|
| 129 |
-
period: aPeriodNotRealtime,
|
| 130 |
-
filters: [aPageFilter]
|
| 131 |
-
},
|
| 132 |
-
[
|
| 133 |
-
'visitors',
|
| 134 |
-
'visits',
|
| 135 |
-
'pageviews',
|
| 136 |
-
'bounce_rate',
|
| 137 |
-
'scroll_depth',
|
| 138 |
-
'time_on_page'
|
| 139 |
-
],
|
| 140 |
-
{
|
| 141 |
-
...expectedBaseQuery,
|
| 142 |
-
date_range: aPeriodNotRealtime,
|
| 143 |
-
filters: remapToApiFilters([aPageFilter]),
|
| 144 |
-
metrics: [
|
| 145 |
-
'visitors',
|
| 146 |
-
'visits',
|
| 147 |
-
'pageviews',
|
| 148 |
-
'bounce_rate',
|
| 149 |
-
'scroll_depth',
|
| 150 |
-
'time_on_page'
|
| 151 |
-
]
|
| 152 |
-
}
|
| 153 |
-
],
|
| 154 |
-
|
| 155 |
-
[
|
| 156 |
-
'default',
|
| 157 |
-
{ period: aPeriodNotRealtime, filters: [] },
|
| 158 |
-
[
|
| 159 |
-
'visitors',
|
| 160 |
-
'visits',
|
| 161 |
-
'pageviews',
|
| 162 |
-
'views_per_visit',
|
| 163 |
-
'bounce_rate',
|
| 164 |
-
'visit_duration'
|
| 165 |
-
],
|
| 166 |
-
{
|
| 167 |
-
...expectedBaseQuery,
|
| 168 |
-
date_range: aPeriodNotRealtime,
|
| 169 |
-
metrics: [
|
| 170 |
-
'visitors',
|
| 171 |
-
'visits',
|
| 172 |
-
'pageviews',
|
| 173 |
-
'views_per_visit',
|
| 174 |
-
'bounce_rate',
|
| 175 |
-
'visit_duration'
|
| 176 |
-
]
|
| 177 |
-
}
|
| 178 |
-
]
|
| 179 |
-
]
|
| 180 |
-
|
| 181 |
-
describe(`${chooseMetrics.name}`, () => {
|
| 182 |
-
test.each(
|
| 183 |
-
cases.map(([name, inputDashboardState, expectedMetrics]) => [
|
| 184 |
-
name,
|
| 185 |
-
inputDashboardState,
|
| 186 |
-
expectedMetrics
|
| 187 |
-
])
|
| 188 |
-
)(
|
| 189 |
-
'for %s dashboard, top stats metrics are as expected',
|
| 190 |
-
(_, { site, ...inputDashboardState }, expectedMetrics) => {
|
| 191 |
-
const dashboardState = {
|
| 192 |
-
...dashboardStateDefaultValue,
|
| 193 |
-
resolvedFilters: inputDashboardState.filters,
|
| 194 |
-
...inputDashboardState
|
| 195 |
-
}
|
| 196 |
-
expect(
|
| 197 |
-
chooseMetrics({ ...siteContextDefaultValue, ...site }, dashboardState)
|
| 198 |
-
).toEqual(expectedMetrics)
|
| 199 |
-
}
|
| 200 |
-
)
|
| 201 |
-
})
|
| 202 |
-
|
| 203 |
-
describe(`${getPartialDayTimeRange.name}`, () => {
|
| 204 |
-
it('returns "until HH:MM" for today (partial day with current time as end)', () => {
|
| 205 |
-
expect(
|
| 206 |
-
getPartialDayTimeRange(['2024-01-13T00:00:00', '2024-01-13T14:32:00'])
|
| 207 |
-
).toBe('until 14:32')
|
| 208 |
-
})
|
| 209 |
-
|
| 210 |
-
it('returns "until HH:MM" when date strings include a timezone offset', () => {
|
| 211 |
-
expect(
|
| 212 |
-
getPartialDayTimeRange([
|
| 213 |
-
'2026-01-19T00:00:00+00:00',
|
| 214 |
-
'2026-01-19T14:32:00+00:00'
|
| 215 |
-
])
|
| 216 |
-
).toBe('until 14:32')
|
| 217 |
-
})
|
| 218 |
-
|
| 219 |
-
it('returns null for a full past day ending at 23:59:59', () => {
|
| 220 |
-
expect(
|
| 221 |
-
getPartialDayTimeRange(['2024-01-12T00:00:00', '2024-01-12T23:59:59'])
|
| 222 |
-
).toBeNull()
|
| 223 |
-
})
|
| 224 |
-
|
| 225 |
-
it('returns null when the date range spans multiple days', () => {
|
| 226 |
-
expect(
|
| 227 |
-
getPartialDayTimeRange(['2024-01-12T00:00:00', '2024-01-13T14:32:00'])
|
| 228 |
-
).toBeNull()
|
| 229 |
-
})
|
| 230 |
-
|
| 231 |
-
it('returns null when the end ISO has no time component', () => {
|
| 232 |
-
expect(getPartialDayTimeRange(['2024-01-13', '2024-01-13'])).toBeNull()
|
| 233 |
-
})
|
| 234 |
-
})
|
| 235 |
-
|
| 236 |
-
function makeTopStatsResponse(
|
| 237 |
-
dateRange: [string, string],
|
| 238 |
-
comparisonDateRange: [string, string] | null
|
| 239 |
-
) {
|
| 240 |
-
return {
|
| 241 |
-
query: {
|
| 242 |
-
metrics: ['visitors'] as ['visitors'],
|
| 243 |
-
dimensions: [],
|
| 244 |
-
date_range: dateRange,
|
| 245 |
-
comparison_date_range: comparisonDateRange as [string, string]
|
| 246 |
-
},
|
| 247 |
-
meta: {},
|
| 248 |
-
results: [
|
| 249 |
-
{
|
| 250 |
-
metrics: [100],
|
| 251 |
-
dimensions: [],
|
| 252 |
-
comparison: { metrics: [80], change: [25] }
|
| 253 |
-
}
|
| 254 |
-
],
|
| 255 |
-
extraContext: { isRealtime: false, hasConversionGoalFilter: false }
|
| 256 |
-
}
|
| 257 |
-
}
|
| 258 |
-
|
| 259 |
-
describe(`${formatTopStatsData.name}`, () => {
|
| 260 |
-
it('sets comparisonTimeRange to "until HH:MM" when comparison period is also a partial day (Today vs Previous period)', () => {
|
| 261 |
-
const response = makeTopStatsResponse(
|
| 262 |
-
['2026-04-21T00:00:00', '2026-04-21T10:25:00'],
|
| 263 |
-
['2026-04-20T00:00:00', '2026-04-20T10:25:00']
|
| 264 |
-
)
|
| 265 |
-
const { timeRange, comparisonTimeRange } = formatTopStatsData(response)
|
| 266 |
-
expect(timeRange).toBe('until 10:25')
|
| 267 |
-
expect(comparisonTimeRange).toBe('until 10:25')
|
| 268 |
-
})
|
| 269 |
-
|
| 270 |
-
it('sets comparisonTimeRange to null when comparison period is a full day (Today vs Custom period)', () => {
|
| 271 |
-
const response = makeTopStatsResponse(
|
| 272 |
-
['2026-04-21T00:00:00', '2026-04-21T10:25:00'],
|
| 273 |
-
['2026-04-20T00:00:00', '2026-04-20T23:59:59']
|
| 274 |
-
)
|
| 275 |
-
const { timeRange, comparisonTimeRange } = formatTopStatsData(response)
|
| 276 |
-
expect(timeRange).toBe('until 10:25')
|
| 277 |
-
expect(comparisonTimeRange).toBeNull()
|
| 278 |
-
})
|
| 279 |
-
})
|
| 280 |
-
|
| 281 |
-
describe(`${getTopStatsQuery.name}`, () => {
|
| 282 |
-
test.each(cases)(
|
| 283 |
-
'for %s dashboard, top stats query is as expected',
|
| 284 |
-
(_, { site: _site, ...inputDashboardState }, metrics, expectedQuery) => {
|
| 285 |
-
const dashboardState = {
|
| 286 |
-
...dashboardStateDefaultValue,
|
| 287 |
-
resolvedFilters: inputDashboardState.filters,
|
| 288 |
-
...inputDashboardState
|
| 289 |
-
}
|
| 290 |
-
const queryKey: StatsReportQueryKey = [
|
| 291 |
-
'top-stats',
|
| 292 |
-
{
|
| 293 |
-
dashboardState,
|
| 294 |
-
reportParams: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