|
| 1 | +package posthog |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +/* |
| 12 | +Tests of the feature flag api against the decide endpoint, no local evaluation. |
| 13 | +This is primarily here to ensure we handle the different versions of the decide |
| 14 | +endpoint correctly. |
| 15 | +*/ |
| 16 | +func TestDecide(t *testing.T) { |
| 17 | + validateCapturedEvent := func(t *testing.T, client Client) { |
| 18 | + lastEvent := client.GetLastCapturedEvent() |
| 19 | + |
| 20 | + if lastEvent == nil { |
| 21 | + return |
| 22 | + } |
| 23 | + if lastEvent.Event != "$feature_flag_called" { |
| 24 | + t.Errorf("Expected a $feature_flag_called event, got: %v", lastEvent) |
| 25 | + } |
| 26 | + |
| 27 | + if lastEvent.Properties["$feature_flag_request_id"] != "42853c54-1431-4861-996e-3a548989fa2c" { |
| 28 | + t.Errorf("Expected $feature_flag_request_id property to be 42853c54-1431-4861-996e-3a548989fa2c, got: %v", lastEvent.Properties["$feature_flag_request_id"]) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + fixture string |
| 35 | + }{ |
| 36 | + {name: "v3", fixture: "test-decide-v3.json"}, |
| 37 | + {name: "v4", fixture: "test-decide-v4.json"}, |
| 38 | + } |
| 39 | + |
| 40 | + for _, test := range tests { |
| 41 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 42 | + if strings.HasPrefix(r.URL.Path, "/decide") { |
| 43 | + w.Write([]byte(fixture(test.fixture))) |
| 44 | + } |
| 45 | + })) |
| 46 | + defer server.Close() |
| 47 | + |
| 48 | + t.Run(test.name, func(t *testing.T) { |
| 49 | + subTests := []struct { |
| 50 | + name string |
| 51 | + flagKey string |
| 52 | + expected interface{} |
| 53 | + }{ |
| 54 | + {name: "IsFeatureEnabled", flagKey: "enabled-flag", expected: true}, |
| 55 | + {name: "IsFeatureEnabled", flagKey: "disabled-flag", expected: false}, |
| 56 | + {name: "IsFeatureEnabled", flagKey: "non-existent-flag", expected: false}, // Note: This differs from posthog-node which returns undefined for non-existent flags |
| 57 | + } |
| 58 | + |
| 59 | + for _, subTest := range subTests { |
| 60 | + t.Run(subTest.name+" "+subTest.flagKey, func(t *testing.T) { |
| 61 | + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ |
| 62 | + Endpoint: server.URL, |
| 63 | + }) |
| 64 | + defer client.Close() |
| 65 | + |
| 66 | + isMatch, _ := client.IsFeatureEnabled( |
| 67 | + FeatureFlagPayload{ |
| 68 | + Key: subTest.flagKey, |
| 69 | + DistinctId: "some-distinct-id", |
| 70 | + PersonProperties: NewProperties().Set("region", "USA"), |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + if isMatch != subTest.expected { |
| 75 | + t.Errorf("Expected IsFeatureEnabled to return %v but was %v", subTest.expected, isMatch) |
| 76 | + } |
| 77 | + |
| 78 | + validateCapturedEvent(t, client) |
| 79 | + }) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run(test.name, func(t *testing.T) { |
| 84 | + subTests := []struct { |
| 85 | + name string |
| 86 | + flagKey string |
| 87 | + expected interface{} |
| 88 | + }{ |
| 89 | + {name: "GetFeatureFlag", flagKey: "enabled-flag", expected: true}, |
| 90 | + {name: "GetFeatureFlag", flagKey: "disabled-flag", expected: false}, |
| 91 | + {name: "GetFeatureFlag", flagKey: "multi-variate-flag", expected: "hello"}, |
| 92 | + {name: "GetFeatureFlag", flagKey: "non-existent-flag", expected: false}, // Note: This differs from posthog-node which returns undefined for non-existent flags |
| 93 | + } |
| 94 | + |
| 95 | + for _, subTest := range subTests { |
| 96 | + t.Run(subTest.name+" "+subTest.flagKey, func(t *testing.T) { |
| 97 | + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ |
| 98 | + Endpoint: server.URL, |
| 99 | + }) |
| 100 | + defer client.Close() |
| 101 | + |
| 102 | + flag, _ := client.GetFeatureFlag( |
| 103 | + FeatureFlagPayload{ |
| 104 | + Key: subTest.flagKey, |
| 105 | + DistinctId: "some-distinct-id", |
| 106 | + PersonProperties: NewProperties().Set("region", "USA"), |
| 107 | + }, |
| 108 | + ) |
| 109 | + |
| 110 | + if flag != subTest.expected { |
| 111 | + t.Errorf("Expected GetFeatureFlag to return %v but was %v", subTest.expected, flag) |
| 112 | + } |
| 113 | + |
| 114 | + validateCapturedEvent(t, client) |
| 115 | + }) |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + t.Run(test.name, func(t *testing.T) { |
| 120 | + subTests := []struct { |
| 121 | + name string |
| 122 | + flagKey string |
| 123 | + expected interface{} |
| 124 | + }{ |
| 125 | + {name: "GetFeatureFlagPayload", flagKey: "enabled-flag", expected: "{\"foo\": 1}"}, |
| 126 | + {name: "GetFeatureFlagPayload", flagKey: "disabled-flag", expected: ""}, // Incorrectly returns "" for disabled flags |
| 127 | + {name: "GetFeatureFlagPayload", flagKey: "multi-variate-flag", expected: "this is the payload"}, |
| 128 | + {name: "GetFeatureFlagPayload", flagKey: "non-existent-flag", expected: ""}, // Incorrectly returns "" for non-existent flags |
| 129 | + } |
| 130 | + |
| 131 | + for _, subTest := range subTests { |
| 132 | + t.Run(subTest.name+" "+subTest.flagKey, func(t *testing.T) { |
| 133 | + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ |
| 134 | + Endpoint: server.URL, |
| 135 | + }) |
| 136 | + defer client.Close() |
| 137 | + |
| 138 | + payload, _ := client.GetFeatureFlagPayload( |
| 139 | + FeatureFlagPayload{ |
| 140 | + Key: subTest.flagKey, |
| 141 | + DistinctId: "some-distinct-id", |
| 142 | + PersonProperties: NewProperties().Set("region", "USA"), |
| 143 | + }, |
| 144 | + ) |
| 145 | + |
| 146 | + if payload != subTest.expected { |
| 147 | + t.Errorf("Expected GetFeatureFlagPayload to return %v but was %v", subTest.expected, payload) |
| 148 | + } |
| 149 | + |
| 150 | + validateCapturedEvent(t, client) |
| 151 | + }) |
| 152 | + } |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestDecideV4(t *testing.T) { |
| 158 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 159 | + if strings.HasPrefix(r.URL.Path, "/decide") { |
| 160 | + w.Write([]byte(fixture("test-decide-v4.json"))) |
| 161 | + } |
| 162 | + })) |
| 163 | + defer server.Close() |
| 164 | + |
| 165 | + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ |
| 166 | + Endpoint: server.URL, |
| 167 | + }) |
| 168 | + defer client.Close() |
| 169 | + |
| 170 | + tests := []struct { |
| 171 | + name string |
| 172 | + flagKey string |
| 173 | + expected interface{} |
| 174 | + expectedVersion int |
| 175 | + expectedReason string |
| 176 | + expectedId int |
| 177 | + }{ |
| 178 | + {name: "GetFeatureFlag", flagKey: "enabled-flag", expected: true, expectedVersion: 23, expectedReason: "Matched conditions set 3", expectedId: 1}, |
| 179 | + {name: "GetFeatureFlag", flagKey: "disabled-flag", expected: false, expectedVersion: 12, expectedReason: "No matching condition set", expectedId: 3}, |
| 180 | + {name: "GetFeatureFlag", flagKey: "multi-variate-flag", expected: "hello", expectedVersion: 42, expectedReason: "Matched conditions set 2", expectedId: 4}, |
| 181 | + } |
| 182 | + |
| 183 | + for _, test := range tests { |
| 184 | + t.Run(test.name, func(t *testing.T) { |
| 185 | + flag, _ := client.GetFeatureFlag( |
| 186 | + FeatureFlagPayload{ |
| 187 | + Key: test.flagKey, |
| 188 | + DistinctId: "some-distinct-id", |
| 189 | + }, |
| 190 | + ) |
| 191 | + |
| 192 | + if flag != test.expected { |
| 193 | + t.Errorf("Expected GetFeatureFlag(%s) to return %v but was %v", test.flagKey, test.expected, flag) |
| 194 | + } |
| 195 | + |
| 196 | + capturedEvent := client.GetLastCapturedEvent() |
| 197 | + if capturedEvent == nil { |
| 198 | + t.Errorf("Expected a $feature_flag_called for %s event, got: %v", test.flagKey, capturedEvent) |
| 199 | + } |
| 200 | + |
| 201 | + if capturedEvent.Properties["$feature_flag"] != test.flagKey { |
| 202 | + t.Errorf("Expected $feature_flag property to be %v, got: %v", test.flagKey, capturedEvent.Properties["$feature_flag"]) |
| 203 | + } |
| 204 | + |
| 205 | + if capturedEvent.Properties["$feature_flag_request_id"] != "42853c54-1431-4861-996e-3a548989fa2c" { |
| 206 | + t.Errorf("Expected $feature_flag_request_id property to be 42853c54-1431-4861-996e-3a548989fa2c, got: %v", capturedEvent.Properties["$feature_flag_request_id"]) |
| 207 | + } |
| 208 | + |
| 209 | + if capturedEvent.Properties["$feature_flag_response"] != test.expected { |
| 210 | + t.Errorf("Expected $feature_flag_response property for %s to be %v, got: %v", test.flagKey, test.expected, capturedEvent.Properties["$feature_flag_response"]) |
| 211 | + } |
| 212 | + |
| 213 | + if version, ok := capturedEvent.Properties["$feature_flag_version"].(int); !ok || version != test.expectedVersion { |
| 214 | + t.Errorf("Expected $feature_flag_version property for %s to be %v, got: %v", test.flagKey, test.expectedVersion, capturedEvent.Properties["$feature_flag_version"]) |
| 215 | + } |
| 216 | + |
| 217 | + if reason, ok := capturedEvent.Properties["$feature_flag_reason"].(string); !ok || reason != test.expectedReason { |
| 218 | + t.Errorf("Expected $feature_flag_reason property for %s to be %v, got: %v", test.flagKey, test.expectedReason, capturedEvent.Properties["$feature_flag_reason"]) |
| 219 | + } |
| 220 | + |
| 221 | + if id, ok := capturedEvent.Properties["$feature_flag_id"].(int); !ok || int(id) != test.expectedId { |
| 222 | + t.Errorf("Expected $feature_flag_id for %s to be %v, got: %v", test.flagKey, test.expectedId, capturedEvent.Properties["$feature_flag_id"]) |
| 223 | + } |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments