carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "bytes"
5 "testing"
6
7 "github.com/carlosframework/platform/internal/analytics"
8)
9
10func TestPrintAnalytics(t *testing.T) {
11 res := apiAnalyticsResponse{
12 Range: "7d",
13 PV: 5,
14 Visitors: 4,
15 Bytes: 100,
16 Bots: 0,
17 Pages: map[string]int64{
18 "/pricing": 3,
19 "/": 2,
20 },
21 Referers: map[string]int64{
22 "news.ycombinator.com": 5,
23 },
24 Events: map[string]int64{
25 "signup": 1,
26 },
27 Days: []analytics.DaySummary{
28 {
29 Date: "2026-09-07",
30 PV: 3,
31 Bytes: 50,
32 Bots: 0,
33 Visitors: 2,
34 Hours: [24]int64{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
35 },
36 {
37 Date: "2026-09-06",
38 PV: 2,
39 Bytes: 50,
40 Bots: 0,
41 Visitors: 2,
42 Hours: [24]int64{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
43 },
44 },
45 On: true,
46 }
47
48 var buf bytes.Buffer
49 printAnalytics(&buf, res)
50 output := buf.String()
51
52 checks := []string{
53 "5", // PV count
54 "4", // Visitors count
55 "daily visitors, summed", // text from headline
56 "/pricing", // page
57 "news.ycombinator.com", // referer
58 "signup", // event
59 }
60
61 for _, check := range checks {
62 if !bytes.Contains(buf.Bytes(), []byte(check)) {
63 t.Errorf("output missing %q:\n%s", check, output)
64 }
65 }
66}
67
68func TestAnalyticsSetFlags(t *testing.T) {
69 tests := []struct {
70 name string
71 count string
72 door string
73 wantErr bool
74 want map[string]string
75 }{
76 {
77 name: "both flags",
78 count: "edge",
79 door: "on",
80 want: map[string]string{"count": "edge", "door": "on"},
81 },
82 {
83 name: "count only",
84 count: "off",
85 want: map[string]string{"count": "off"},
86 },
87 {
88 name: "door only",
89 door: "off",
90 want: map[string]string{"door": "off"},
91 },
92 {
93 name: "neither",
94 want: map[string]string{},
95 },
96 {
97 name: "invalid count",
98 count: "invalid",
99 wantErr: true,
100 },
101 {
102 name: "invalid door",
103 door: "maybe",
104 wantErr: true,
105 },
106 }
107
108 for _, tt := range tests {
109 t.Run(tt.name, func(t *testing.T) {
110 got, err := analyticsSetBody(tt.count, tt.door)
111 if (err != nil) != tt.wantErr {
112 t.Errorf("analyticsSetBody() error = %v, wantErr %v", err, tt.wantErr)
113 return
114 }
115 if err != nil {
116 return
117 }
118 if len(got) != len(tt.want) {
119 t.Errorf("analyticsSetBody() got %v, want %v", got, tt.want)
120 return
121 }
122 for k, v := range tt.want {
123 if got[k] != v {
124 t.Errorf("analyticsSetBody() got %v, want %v", got, tt.want)
125 return
126 }
127 }
128 })
129 }
130}
131