Leon4gr45 commited on
Commit
292f9ce
·
verified ·
1 Parent(s): 6cb2295

Delete test/plausible_web/controllers/api/stats_controller/exploration_test.exs with huggingface_hub

Browse files
test/plausible_web/controllers/api/stats_controller/exploration_test.exs DELETED
@@ -1,508 +0,0 @@
1
- defmodule PlausibleWeb.Api.StatsController.ExplorationTest do
2
- use PlausibleWeb.ConnCase, async: false
3
- use Plausible
4
-
5
- on_ee do
6
- alias Plausible.Stats.Exploration.Journey
7
-
8
- setup [:create_user, :log_in, :create_site]
9
-
10
- setup %{user: user, site: site} do
11
- patch_env(:super_admin_user_ids, [user.id])
12
-
13
- now = DateTime.utc_now()
14
-
15
- populate_stats(site, [
16
- build(:pageview,
17
- user_id: 123,
18
- pathname: "/home",
19
- timestamp: DateTime.shift(now, minute: -300)
20
- ),
21
- build(:pageview,
22
- user_id: 123,
23
- pathname: "/login",
24
- timestamp: DateTime.shift(now, minute: -270)
25
- ),
26
- build(:pageview,
27
- user_id: 123,
28
- pathname: "/home",
29
- timestamp: DateTime.shift(now, minute: -30)
30
- ),
31
- build(:pageview,
32
- user_id: 123,
33
- pathname: "/login",
34
- timestamp: DateTime.shift(now, minute: -25)
35
- ),
36
- build(:pageview,
37
- user_id: 123,
38
- pathname: "/logout",
39
- timestamp: DateTime.shift(now, minute: -20)
40
- ),
41
- build(:pageview,
42
- user_id: 124,
43
- pathname: "/home",
44
- timestamp: DateTime.shift(now, minute: -30)
45
- ),
46
- build(:pageview,
47
- user_id: 124,
48
- pathname: "/login",
49
- timestamp: DateTime.shift(now, minute: -25)
50
- ),
51
- build(:pageview,
52
- user_id: 124,
53
- pathname: "/docs",
54
- timestamp: DateTime.shift(now, minute: -20)
55
- ),
56
- build(:pageview,
57
- user_id: 124,
58
- pathname: "/logout",
59
- timestamp: DateTime.shift(now, minute: -15)
60
- )
61
- ])
62
-
63
- {:ok, site: site}
64
- end
65
-
66
- describe "exploration_next/2" do
67
- test "returns suggestions for the next step", %{conn: conn, site: site} do
68
- journey =
69
- Jason.encode!([
70
- Journey.Step.new("pageview", "/home"),
71
- Journey.Step.new("pageview", "/login")
72
- ])
73
-
74
- resp =
75
- conn
76
- |> post("/api/stats/#{site.domain}/exploration/next/", %{
77
- "journey" => journey,
78
- "period" => "24h"
79
- })
80
- |> json_response(200)
81
-
82
- assert [next_step1, next_step2, next_step3] = resp
83
- assert next_step1["step"]["label"] == "/docs"
84
- assert next_step1["step"]["pathname"] == "/docs"
85
- assert next_step1["visitors"] == 1
86
- assert next_step2["step"]["label"] == "/home"
87
- assert next_step2["step"]["pathname"] == "/home"
88
- assert next_step2["visitors"] == 1
89
- assert next_step3["step"]["label"] == "/logout"
90
- assert next_step3["step"]["pathname"] == "/logout"
91
- assert next_step3["visitors"] == 1
92
- end
93
-
94
- test "returns error on too long journey", %{conn: conn, site: site} do
95
- journey =
96
- Jason.encode!(
97
- Enum.map(1..20, fn idx -> Journey.Step.new("pageview", "/page#{idx}") end)
98
- )
99
-
100
- resp =
101
- conn
102
- |> post("/api/stats/#{site.domain}/exploration/next/", %{
103
- "journey" => journey,
104
- "period" => "24h"
105
- })
106
- |> json_response(400)
107
-
108
- assert resp["error"] == "The journey is too long"
109
- end
110
-
111
- test "allows filtering suggestions", %{conn: conn, site: site} do
112
- journey =
113
- Jason.encode!([
114
- Journey.Step.new("pageview", "/home"),
115
- Journey.Step.new("pageview", "/login")
116
- ])
117
-
118
- resp =
119
- conn
120
- |> post("/api/stats/#{site.domain}/exploration/next/", %{
121
- "journey" => journey,
122
- "search_term" => "doc",
123
- "period" => "24h"
124
- })
125
- |> json_response(200)
126
-
127
- assert [next_step] = resp
128
- assert next_step["step"]["pathname"] == "/docs"
129
- assert next_step["visitors"] == 1
130
- end
131
-
132
- test "supports backward direction", %{conn: conn, site: site} do
133
- journey = Jason.encode!([Journey.Step.new("pageview", "/logout")])
134
-
135
- resp =
136
- conn
137
- |> post("/api/stats/#{site.domain}/exploration/next/", %{
138
- "journey" => journey,
139
- "direction" => "backward",
140
- "period" => "24h"
141
- })
142
- |> json_response(200)
143
-
144
- assert [next_step1, next_step2] = resp
145
- assert next_step1["step"]["pathname"] == "/docs"
146
- assert next_step1["visitors"] == 1
147
- assert next_step2["step"]["pathname"] == "/login"
148
- assert next_step2["visitors"] == 1
149
- end
150
- end
151
-
152
- describe "exploration_funnel/2" do
153
- test "returns a funnel", %{conn: conn, site: site} do
154
- journey =
155
- Jason.encode!([
156
- Journey.Step.new("pageview", "/home"),
157
- Journey.Step.new("pageview", "/login"),
158
- Journey.Step.new("pageview", "/logout")
159
- ])
160
-
161
- resp =
162
- conn
163
- |> post("/api/stats/#{site.domain}/exploration/funnel/", %{
164
- "journey" => journey,
165
- "period" => "24h"
166
- })
167
- |> json_response(200)
168
-
169
- assert [step1, step2, step3] = resp
170
-
171
- assert step1["step"]["label"] == "/home"
172
- assert step1["step"]["pathname"] == "/home"
173
- assert step1["visitors"] == 2
174
- assert step1["dropoff"] == 0
175
- assert step1["dropoff_percentage"] == "0"
176
- assert step1["conversion_rate"] == "100"
177
- assert step1["conversion_rate_step"] == "0"
178
- assert step2["step"]["label"] == "/login"
179
- assert step2["step"]["pathname"] == "/login"
180
- assert step2["visitors"] == 2
181
- assert step2["dropoff"] == 0
182
- assert step2["dropoff_percentage"] == "0"
183
- assert step2["conversion_rate"] == "100"
184
- assert step2["conversion_rate_step"] == "100"
185
- assert step3["step"]["label"] == "/logout"
186
- assert step3["step"]["pathname"] == "/logout"
187
- assert step3["visitors"] == 1
188
- assert step3["dropoff"] == 1
189
- assert step3["dropoff_percentage"] == "50"
190
- assert step3["conversion_rate"] == "50"
191
- assert step3["conversion_rate_step"] == "50"
192
- end
193
-
194
- test "returns error on too long journey", %{conn: conn, site: site} do
195
- journey =
196
- Jason.encode!(
197
- Enum.map(1..21, fn idx -> Journey.Step.new("pageview", "/page#{idx}") end)
198
- )
199
-
200
- resp =
201
- conn
202
- |> post("/api/stats/#{site.domain}/exploration/funnel/", %{
203
- "journey" => journey,
204
- "period" => "24h"
205
- })
206
- |> json_response(400)
207
-
208
- assert resp["error"] == "The journey is too long"
209
- end
210
-
211
- test "returns error on empty journey", %{conn: conn, site: site} do
212
- resp =
213
- conn
214
- |> post("/api/stats/#{site.domain}/exploration/funnel/", %{
215
- "journey" => Jason.encode!([]),
216
- "period" => "24h"
217
- })
218
- |> json_response(400)
219
-
220
- assert resp["error"] == "We are unable to show funnels when journey is empty"
221
- end
222
-
223
- test "supports backward direction", %{conn: conn, site: site} do
224
- journey =
225
- Jason.encode!([
226
- Journey.Step.new("pageview", "/logout"),
227
- Journey.Step.new("pageview", "/login"),
228
- Journey.Step.new("pageview", "/home")
229
- ])
230
-
231
- resp =
232
- conn
233
- |> post("/api/stats/#{site.domain}/exploration/funnel/", %{
234
- "journey" => journey,
235
- "direction" => "backward",
236
- "period" => "24h"
237
- })
238
- |> json_response(200)
239
-
240
- assert [step1, step2, step3] = resp
241
-
242
- assert step1["step"]["pathname"] == "/logout"
243
- assert step1["visitors"] == 2
244
- assert step1["dropoff"] == 0
245
- assert step1["dropoff_percentage"] == "0"
246
- assert step1["conversion_rate"] == "100"
247
- assert step1["conversion_rate_step"] == "0"
248
- assert step2["step"]["pathname"] == "/login"
249
- assert step2["visitors"] == 1
250
- assert step2["dropoff"] == 1
251
- assert step2["dropoff_percentage"] == "50"
252
- assert step2["conversion_rate"] == "50"
253
- assert step2["conversion_rate_step"] == "50"
254
- assert step3["step"]["pathname"] == "/home"
255
- assert step3["visitors"] == 1
256
- assert step3["dropoff"] == 0
257
- assert step3["dropoff_percentage"] == "0"
258
- assert step3["conversion_rate"] == "50"
259
- assert step3["conversion_rate_step"] == "100"
260
- end
261
- end
262
-
263
- describe "exploration_next_with_funnel/2" do
264
- test "returns next steps without funnel by default", %{conn: conn, site: site} do
265
- journey =
266
- Jason.encode!([
267
- Journey.Step.new("pageview", "/home"),
268
- Journey.Step.new("pageview", "/login")
269
- ])
270
-
271
- resp =
272
- conn
273
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
274
- "journey" => journey,
275
- "period" => "24h"
276
- })
277
- |> json_response(200)
278
-
279
- assert [next_step1, next_step2, next_step3] = resp["next"]
280
- assert next_step1["step"]["label"] == "/docs"
281
- assert next_step1["step"]["pathname"] == "/docs"
282
- assert next_step1["visitors"] == 1
283
- assert next_step2["step"]["label"] == "/home"
284
- assert next_step2["step"]["pathname"] == "/home"
285
- assert next_step2["visitors"] == 1
286
- assert next_step3["step"]["label"] == "/logout"
287
- assert next_step3["step"]["pathname"] == "/logout"
288
- assert next_step3["visitors"] == 1
289
-
290
- assert resp["funnel"] == []
291
- end
292
-
293
- test "filters next steps by search_term", %{conn: conn, site: site} do
294
- journey =
295
- Jason.encode!([
296
- Journey.Step.new("pageview", "/home"),
297
- Journey.Step.new("pageview", "/login")
298
- ])
299
-
300
- resp =
301
- conn
302
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
303
- "journey" => journey,
304
- "search_term" => "doc",
305
- "period" => "24h"
306
- })
307
- |> json_response(200)
308
-
309
- assert [next_step] = resp["next"]
310
- assert next_step["step"]["pathname"] == "/docs"
311
- assert next_step["visitors"] == 1
312
- assert resp["funnel"] == []
313
- end
314
-
315
- test "supports backward direction for next steps", %{conn: conn, site: site} do
316
- journey = Jason.encode!([Journey.Step.new("pageview", "/logout")])
317
-
318
- resp =
319
- conn
320
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
321
- "journey" => journey,
322
- "direction" => "backward",
323
- "period" => "24h"
324
- })
325
- |> json_response(200)
326
-
327
- assert [next_step1, next_step2] = resp["next"]
328
- assert next_step1["step"]["pathname"] == "/docs"
329
- assert next_step1["visitors"] == 1
330
- assert next_step2["step"]["pathname"] == "/login"
331
- assert next_step2["visitors"] == 1
332
- assert resp["funnel"] == []
333
- end
334
-
335
- test "includes funnel when include_funnel is true", %{conn: conn, site: site} do
336
- journey =
337
- Jason.encode!([
338
- Journey.Step.new("pageview", "/home"),
339
- Journey.Step.new("pageview", "/login"),
340
- Journey.Step.new("pageview", "/logout")
341
- ])
342
-
343
- resp =
344
- conn
345
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
346
- "journey" => journey,
347
- "include_funnel" => true,
348
- "period" => "24h"
349
- })
350
- |> json_response(200)
351
-
352
- assert [step1, step2, step3] = resp["funnel"]
353
-
354
- assert step1["step"]["label"] == "/home"
355
- assert step1["step"]["pathname"] == "/home"
356
- assert step1["visitors"] == 2
357
- assert step1["dropoff"] == 0
358
- assert step1["dropoff_percentage"] == "0"
359
- assert step1["conversion_rate"] == "100"
360
- assert step1["conversion_rate_step"] == "0"
361
- assert step2["step"]["label"] == "/login"
362
- assert step2["step"]["pathname"] == "/login"
363
- assert step2["visitors"] == 2
364
- assert step2["dropoff"] == 0
365
- assert step2["dropoff_percentage"] == "0"
366
- assert step2["conversion_rate"] == "100"
367
- assert step2["conversion_rate_step"] == "100"
368
- assert step3["step"]["label"] == "/logout"
369
- assert step3["step"]["pathname"] == "/logout"
370
- assert step3["visitors"] == 1
371
- assert step3["dropoff"] == 1
372
- assert step3["dropoff_percentage"] == "50"
373
- assert step3["conversion_rate"] == "50"
374
- assert step3["conversion_rate_step"] == "50"
375
- end
376
-
377
- test "includes funnel with backward direction when include_funnel is true", %{
378
- conn: conn,
379
- site: site
380
- } do
381
- journey =
382
- Jason.encode!([
383
- Journey.Step.new("pageview", "/logout"),
384
- Journey.Step.new("pageview", "/login"),
385
- Journey.Step.new("pageview", "/home")
386
- ])
387
-
388
- resp =
389
- conn
390
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
391
- "journey" => journey,
392
- "include_funnel" => true,
393
- "direction" => "backward",
394
- "period" => "24h"
395
- })
396
- |> json_response(200)
397
-
398
- assert [step1, step2, step3] = resp["funnel"]
399
-
400
- assert step1["step"]["pathname"] == "/logout"
401
- assert step1["visitors"] == 2
402
- assert step1["dropoff"] == 0
403
- assert step1["dropoff_percentage"] == "0"
404
- assert step1["conversion_rate"] == "100"
405
- assert step1["conversion_rate_step"] == "0"
406
- assert step2["step"]["pathname"] == "/login"
407
- assert step2["visitors"] == 1
408
- assert step2["dropoff"] == 1
409
- assert step2["dropoff_percentage"] == "50"
410
- assert step2["conversion_rate"] == "50"
411
- assert step2["conversion_rate_step"] == "50"
412
- assert step3["step"]["pathname"] == "/home"
413
- assert step3["visitors"] == 1
414
- assert step3["dropoff"] == 0
415
- assert step3["dropoff_percentage"] == "0"
416
- assert step3["conversion_rate"] == "50"
417
- assert step3["conversion_rate_step"] == "100"
418
- end
419
-
420
- test "returns empty funnel when include_funnel is true but journey is empty", %{
421
- conn: conn,
422
- site: site
423
- } do
424
- journey = Jason.encode!([])
425
-
426
- resp =
427
- conn
428
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel", %{
429
- "journey" => journey,
430
- "include_funnel" => true,
431
- "period" => "24h"
432
- })
433
- |> json_response(200)
434
-
435
- # An empty journey still returns starting-point candidates for `next`
436
- assert is_list(resp["next"])
437
- # Funnel requires at least one step; empty journey yields empty funnel
438
- assert resp["funnel"] == []
439
- end
440
- end
441
-
442
- describe "rate limiting" do
443
- defp exhaust_exploration_rate_limit(site) do
444
- Plausible.RateLimit.check_rate(
445
- Plausible.RateLimit,
446
- "exploration:#{site.id}",
447
- :timer.hours(1),
448
- 600,
449
- 601
450
- )
451
- end
452
-
453
- test "returns 429 for exploration_next when rate limit exceeded", %{
454
- conn: conn,
455
- site: site
456
- } do
457
- exhaust_exploration_rate_limit(site)
458
-
459
- journey = Jason.encode!([Journey.Step.new("pageview", "/home")])
460
-
461
- resp =
462
- conn
463
- |> post("/api/stats/#{site.domain}/exploration/next/", %{
464
- "journey" => journey,
465
- "period" => "all"
466
- })
467
-
468
- assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}
469
- end
470
-
471
- test "returns 429 for exploration_funnel when rate limit exceeded", %{
472
- conn: conn,
473
- site: site
474
- } do
475
- exhaust_exploration_rate_limit(site)
476
-
477
- journey = Jason.encode!([Journey.Step.new("pageview", "/home")])
478
-
479
- resp =
480
- conn
481
- |> post("/api/stats/#{site.domain}/exploration/funnel/", %{
482
- "journey" => journey,
483
- "period" => "all"
484
- })
485
-
486
- assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}
487
- end
488
-
489
- test "returns 429 for exploration_next_with_funnel when rate limit exceeded", %{
490
- conn: conn,
491
- site: site
492
- } do
493
- exhaust_exploration_rate_limit(site)
494
-
495
- journey = Jason.encode!([Journey.Step.new("pageview", "/home")])
496
-
497
- resp =
498
- conn
499
- |> post("/api/stats/#{site.domain}/exploration/next-with-funnel/", %{
500
- "journey" => journey,
501
- "period" => "all"
502
- })
503
-
504
- assert json_response(resp, 429) == %{"error" => "Too many exploration requests"}
505
- end
506
- end
507
- end
508
- end