posthog-node 3.3.0 → 3.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  // import { PostHog, PostHogOptions } from '../'
2
2
  // Uncomment below line while developing to not compile code everytime
3
3
  import { PostHog as PostHog, PostHogOptions } from '../src/posthog-node'
4
- import { matchProperty, InconclusiveMatchError } from '../src/feature-flags'
4
+ import { matchProperty, InconclusiveMatchError, relativeDateParseForFeatureFlagMatching } from '../src/feature-flags'
5
5
  jest.mock('../src/fetch')
6
6
  import fetch from '../src/fetch'
7
7
 
@@ -50,6 +50,17 @@ export const apiImplementation = ({
50
50
  }) as any
51
51
  }
52
52
 
53
+ if ((url as any).includes('batch/')) {
54
+ return Promise.resolve({
55
+ status: 200,
56
+ text: () => Promise.resolve('ok'),
57
+ json: () =>
58
+ Promise.resolve({
59
+ status: 'ok',
60
+ }),
61
+ }) as any
62
+ }
63
+
53
64
  return Promise.resolve({
54
65
  status: 400,
55
66
  text: () => Promise.resolve('ok'),
@@ -342,7 +353,11 @@ describe('local evaluation', () => {
342
353
  token: 'TEST_API_KEY',
343
354
  distinct_id: 'some-distinct-id_outside_rollout?',
344
355
  groups: {},
345
- person_properties: { region: 'USA', email: 'a@b.com' },
356
+ person_properties: {
357
+ $current_distinct_id: 'some-distinct-id_outside_rollout?',
358
+ region: 'USA',
359
+ email: 'a@b.com',
360
+ },
346
361
  group_properties: {},
347
362
  geoip_disable: true,
348
363
  }),
@@ -361,7 +376,7 @@ describe('local evaluation', () => {
361
376
  token: 'TEST_API_KEY',
362
377
  distinct_id: 'some-distinct-id',
363
378
  groups: {},
364
- person_properties: { doesnt_matter: '1' },
379
+ person_properties: { $current_distinct_id: 'some-distinct-id', doesnt_matter: '1' },
365
380
  group_properties: {},
366
381
  geoip_disable: true,
367
382
  }),
@@ -1814,6 +1829,8 @@ describe('local evaluation', () => {
1814
1829
  })
1815
1830
 
1816
1831
  describe('match properties', () => {
1832
+ jest.useFakeTimers()
1833
+
1817
1834
  it('with operator exact', () => {
1818
1835
  const property_a = { key: 'key', value: 'value' }
1819
1836
 
@@ -1953,7 +1970,8 @@ describe('match properties', () => {
1953
1970
 
1954
1971
  expect(matchProperty(property_a, { key: 0 })).toBe(false)
1955
1972
  expect(matchProperty(property_a, { key: -1 })).toBe(false)
1956
- expect(matchProperty(property_a, { key: '23' })).toBe(false)
1973
+ // # now we handle type mismatches so this should be true
1974
+ expect(matchProperty(property_a, { key: '23' })).toBe(true)
1957
1975
 
1958
1976
  const property_b = { key: 'key', value: 1, operator: 'lt' }
1959
1977
  expect(matchProperty(property_b, { key: 0 })).toBe(true)
@@ -1971,7 +1989,8 @@ describe('match properties', () => {
1971
1989
  expect(matchProperty(property_c, { key: 0 })).toBe(false)
1972
1990
  expect(matchProperty(property_c, { key: -1 })).toBe(false)
1973
1991
  expect(matchProperty(property_c, { key: -3 })).toBe(false)
1974
- expect(matchProperty(property_c, { key: '3' })).toBe(false)
1992
+ // # now we handle type mismatches so this should be true
1993
+ expect(matchProperty(property_c, { key: '3' })).toBe(true)
1975
1994
 
1976
1995
  const property_d = { key: 'key', value: '43', operator: 'lte' }
1977
1996
  expect(matchProperty(property_d, { key: '43' })).toBe(true)
@@ -1979,6 +1998,21 @@ describe('match properties', () => {
1979
1998
 
1980
1999
  expect(matchProperty(property_d, { key: '44' })).toBe(false)
1981
2000
  expect(matchProperty(property_d, { key: 44 })).toBe(false)
2001
+ expect(matchProperty(property_d, { key: 42 })).toBe(true)
2002
+
2003
+ const property_e = { key: 'key', value: '30', operator: 'lt' }
2004
+ expect(matchProperty(property_e, { key: '29' })).toBe(true)
2005
+
2006
+ // # depending on the type of override, we adjust type comparison
2007
+ expect(matchProperty(property_e, { key: '100' })).toBe(true)
2008
+ expect(matchProperty(property_e, { key: 100 })).toBe(false)
2009
+
2010
+ const property_f = { key: 'key', value: '123aloha', operator: 'gt' }
2011
+ expect(matchProperty(property_f, { key: '123' })).toBe(false)
2012
+ expect(matchProperty(property_f, { key: 122 })).toBe(false)
2013
+
2014
+ // # this turns into a string comparison
2015
+ expect(matchProperty(property_f, { key: 129 })).toBe(true)
1982
2016
  })
1983
2017
 
1984
2018
  it('with date operators', () => {
@@ -2016,6 +2050,240 @@ describe('match properties', () => {
2016
2050
  expect(matchProperty(property_d, { key: '2022-04-05 11:34:11 +00:00' })).toBe(true)
2017
2051
  expect(matchProperty(property_d, { key: '2022-04-05 11:34:13 +00:00' })).toBe(false)
2018
2052
  })
2053
+
2054
+ it('with relative date operators', () => {
2055
+ jest.setSystemTime(new Date('2022-05-01'))
2056
+
2057
+ const property_a = { key: 'key', value: '6h', operator: 'is_relative_date_before' }
2058
+ expect(matchProperty(property_a, { key: '2022-03-01' })).toBe(true)
2059
+ expect(matchProperty(property_a, { key: '2022-04-30' })).toBe(true)
2060
+
2061
+ // :TRICKY: MonthIndex is 0 indexed, so 3 is actually the 4th month, April.
2062
+ expect(matchProperty(property_a, { key: new Date(Date.UTC(2022, 3, 30, 1, 2, 3)) })).toBe(true)
2063
+ // false because date comparison, instead of datetime, so reduces to same date
2064
+ expect(matchProperty(property_a, { key: new Date(2022, 3, 30, 19, 2, 3) })).toBe(false)
2065
+ expect(matchProperty(property_a, { key: new Date('2022-04-30T01:02:03+02:00') })).toBe(true) // europe/madrid
2066
+ expect(matchProperty(property_a, { key: new Date('2022-04-30T20:02:03+02:00') })).toBe(false) // europe/madrid
2067
+ expect(matchProperty(property_a, { key: new Date('2022-04-30T19:59:03+02:00') })).toBe(true) // europe/madrid
2068
+ expect(matchProperty(property_a, { key: new Date('2022-04-30') })).toBe(true)
2069
+ expect(matchProperty(property_a, { key: '2022-05-30' })).toBe(false)
2070
+
2071
+ // # can't be an invalid string
2072
+ expect(() => matchProperty(property_a, { key: 'abcdef' })).toThrow(InconclusiveMatchError)
2073
+ // however js understands numbers as date offsets from utc epoch
2074
+ expect(() => matchProperty(property_a, { key: 1 })).not.toThrow(InconclusiveMatchError)
2075
+
2076
+ const property_b = { key: 'key', value: '1h', operator: 'is_relative_date_after' }
2077
+ expect(matchProperty(property_b, { key: '2022-05-02' })).toBe(true)
2078
+ expect(matchProperty(property_b, { key: '2022-05-30' })).toBe(true)
2079
+ expect(matchProperty(property_b, { key: new Date(2022, 4, 30) })).toBe(true)
2080
+ expect(matchProperty(property_b, { key: new Date('2022-05-30') })).toBe(true)
2081
+ expect(matchProperty(property_b, { key: '2022-04-30' })).toBe(false)
2082
+
2083
+ // # Invalid flag property
2084
+ const property_c = { key: 'key', value: 1234, operator: 'is_relative_date_after' }
2085
+ expect(() => matchProperty(property_c, { key: '2022-05-30' })).toThrow(InconclusiveMatchError)
2086
+ expect(() => matchProperty(property_c, { key: 1 })).toThrow(InconclusiveMatchError)
2087
+
2088
+ // # Try all possible relative dates
2089
+ const property_e = { key: 'key', value: '1h', operator: 'is_relative_date_before' }
2090
+ expect(matchProperty(property_e, { key: '2022-05-01 00:00:00' })).toBe(false)
2091
+ expect(matchProperty(property_e, { key: '2022-04-30 22:00:00' })).toBe(true)
2092
+
2093
+ const property_f = { key: 'key', value: '1d', operator: 'is_relative_date_before' }
2094
+ expect(matchProperty(property_f, { key: '2022-04-29 23:59:00 GMT' })).toBe(true)
2095
+ expect(matchProperty(property_f, { key: '2022-04-30 00:00:01 GMT' })).toBe(false)
2096
+
2097
+ const property_g = { key: 'key', value: '1w', operator: 'is_relative_date_before' }
2098
+ expect(matchProperty(property_g, { key: '2022-04-23 00:00:00 GMT' })).toBe(true)
2099
+ expect(matchProperty(property_g, { key: '2022-04-24 00:00:00 GMT' })).toBe(false)
2100
+ expect(matchProperty(property_g, { key: '2022-04-24 00:00:01 GMT' })).toBe(false)
2101
+
2102
+ const property_h = { key: 'key', value: '1m', operator: 'is_relative_date_before' }
2103
+ expect(matchProperty(property_h, { key: '2022-03-01 00:00:00 GMT' })).toBe(true)
2104
+ expect(matchProperty(property_h, { key: '2022-04-05 00:00:00 GMT' })).toBe(false)
2105
+
2106
+ const property_i = { key: 'key', value: '1y', operator: 'is_relative_date_before' }
2107
+ expect(matchProperty(property_i, { key: '2021-04-28 00:00:00 GMT' })).toBe(true)
2108
+ expect(matchProperty(property_i, { key: '2021-05-01 00:00:01 GMT' })).toBe(false)
2109
+
2110
+ const property_j = { key: 'key', value: '122h', operator: 'is_relative_date_after' }
2111
+ expect(matchProperty(property_j, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
2112
+ expect(matchProperty(property_j, { key: '2022-04-23 01:00:00 GMT' })).toBe(false)
2113
+
2114
+ const property_k = { key: 'key', value: '2d', operator: 'is_relative_date_after' }
2115
+ expect(matchProperty(property_k, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
2116
+ expect(matchProperty(property_k, { key: '2022-04-29 00:00:01 GMT' })).toBe(true)
2117
+ expect(matchProperty(property_k, { key: '2022-04-29 00:00:00 GMT' })).toBe(false)
2118
+
2119
+ const property_l = { key: 'key', value: '02w', operator: 'is_relative_date_after' }
2120
+ expect(matchProperty(property_l, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
2121
+ expect(matchProperty(property_l, { key: '2022-04-16 00:00:00 GMT' })).toBe(false)
2122
+
2123
+ const property_m = { key: 'key', value: '1m', operator: 'is_relative_date_after' }
2124
+ expect(matchProperty(property_m, { key: '2022-04-01 00:00:01 GMT' })).toBe(true)
2125
+ expect(matchProperty(property_m, { key: '2022-04-01 00:00:00 GMT' })).toBe(false)
2126
+
2127
+ const property_n = { key: 'key', value: '1y', operator: 'is_relative_date_after' }
2128
+ expect(matchProperty(property_n, { key: '2022-05-01 00:00:00 GMT' })).toBe(true)
2129
+ expect(matchProperty(property_n, { key: '2021-05-01 00:00:01 GMT' })).toBe(true)
2130
+ expect(matchProperty(property_n, { key: '2021-05-01 00:00:00 GMT' })).toBe(false)
2131
+ expect(matchProperty(property_n, { key: '2021-04-30 00:00:00 GMT' })).toBe(false)
2132
+ expect(matchProperty(property_n, { key: '2021-03-01 12:13:00 GMT' })).toBe(false)
2133
+ })
2134
+
2135
+ it('null or undefined property value', () => {
2136
+ const property_a = { key: 'key', value: 'null', operator: 'is_not' }
2137
+ expect(matchProperty(property_a, { key: null })).toBe(false)
2138
+ expect(matchProperty(property_a, { key: undefined })).toBe(true)
2139
+ expect(matchProperty(property_a, { key: 'null' })).toBe(false)
2140
+ expect(matchProperty(property_a, { key: 'nul' })).toBe(true)
2141
+
2142
+ const property_b = { key: 'key', value: 'null', operator: 'is_set' }
2143
+ expect(matchProperty(property_b, { key: null })).toBe(true)
2144
+ expect(matchProperty(property_b, { key: undefined })).toBe(true)
2145
+ expect(matchProperty(property_b, { key: 'null' })).toBe(true)
2146
+
2147
+ const property_c = { key: 'key', value: 'undefined', operator: 'icontains' }
2148
+ expect(matchProperty(property_c, { key: null })).toBe(false)
2149
+ expect(matchProperty(property_c, { key: undefined })).toBe(true)
2150
+ expect(matchProperty(property_c, { key: 'lol' })).toBe(false)
2151
+
2152
+ const property_d = { key: 'key', value: 'undefined', operator: 'regex' }
2153
+ expect(matchProperty(property_d, { key: null })).toBe(false)
2154
+ expect(matchProperty(property_d, { key: undefined })).toBe(true)
2155
+
2156
+ const property_e = { key: 'key', value: 1, operator: 'gt' }
2157
+ expect(matchProperty(property_e, { key: null })).toBe(true)
2158
+ expect(matchProperty(property_e, { key: undefined })).toBe(true)
2159
+
2160
+ const property_f = { key: 'key', value: 1, operator: 'lt' }
2161
+ expect(matchProperty(property_f, { key: null })).toBe(false)
2162
+ expect(matchProperty(property_f, { key: undefined })).toBe(false)
2163
+
2164
+ const property_g = { key: 'key', value: 'xyz', operator: 'gte' }
2165
+ expect(matchProperty(property_g, { key: null })).toBe(false)
2166
+ expect(matchProperty(property_g, { key: undefined })).toBe(false)
2167
+
2168
+ const property_h = { key: 'key', value: 'Oo', operator: 'lte' }
2169
+ expect(matchProperty(property_h, { key: null })).toBe(false)
2170
+ expect(matchProperty(property_h, { key: undefined })).toBe(false)
2171
+
2172
+ const property_h_lower = { key: 'key', value: 'oo', operator: 'lte' }
2173
+ expect(matchProperty(property_h_lower, { key: null })).toBe(true)
2174
+ expect(matchProperty(property_h_lower, { key: undefined })).toBe(false)
2175
+
2176
+ const property_i = { key: 'key', value: '2022-05-01', operator: 'is_date_before' }
2177
+ expect(() => matchProperty(property_i, { key: null })).toThrow(InconclusiveMatchError)
2178
+ expect(() => matchProperty(property_i, { key: undefined })).toThrow(InconclusiveMatchError)
2179
+
2180
+ const property_j = { key: 'key', value: '2022-05-01', operator: 'is_date_after' }
2181
+ expect(() => matchProperty(property_j, { key: null })).toThrow(InconclusiveMatchError)
2182
+
2183
+ const property_k = { key: 'key', value: '2022-05-01', operator: 'is_date_before' }
2184
+ expect(() => matchProperty(property_k, { key: null })).toThrow(InconclusiveMatchError)
2185
+ })
2186
+
2187
+ it('with invalid operator', () => {
2188
+ const property_a = { key: 'key', value: '2022-05-01', operator: 'is_unknown' }
2189
+
2190
+ expect(() => matchProperty(property_a, { key: 'random' })).toThrow(
2191
+ new InconclusiveMatchError('Unknown operator: is_unknown')
2192
+ )
2193
+ })
2194
+ })
2195
+
2196
+ describe('relative date parsing', () => {
2197
+ jest.useFakeTimers()
2198
+ beforeEach(() => {
2199
+ jest.setSystemTime(new Date('2020-01-01T12:01:20.134Z'))
2200
+ })
2201
+
2202
+ it('invalid input', () => {
2203
+ expect(relativeDateParseForFeatureFlagMatching('1')).toBe(null)
2204
+ expect(relativeDateParseForFeatureFlagMatching('1x')).toBe(null)
2205
+ expect(relativeDateParseForFeatureFlagMatching('1.2y')).toBe(null)
2206
+ expect(relativeDateParseForFeatureFlagMatching('1z')).toBe(null)
2207
+ expect(relativeDateParseForFeatureFlagMatching('1s')).toBe(null)
2208
+ expect(relativeDateParseForFeatureFlagMatching('123344000.134m')).toBe(null)
2209
+ expect(relativeDateParseForFeatureFlagMatching('bazinga')).toBe(null)
2210
+ expect(relativeDateParseForFeatureFlagMatching('000bello')).toBe(null)
2211
+ expect(relativeDateParseForFeatureFlagMatching('000hello')).toBe(null)
2212
+
2213
+ expect(relativeDateParseForFeatureFlagMatching('000h')).not.toBe(null)
2214
+ expect(relativeDateParseForFeatureFlagMatching('1000h')).not.toBe(null)
2215
+ })
2216
+
2217
+ it('overflow', () => {
2218
+ expect(relativeDateParseForFeatureFlagMatching('1000000h')).toBe(null)
2219
+ expect(relativeDateParseForFeatureFlagMatching('100000000000000000y')).toBe(null)
2220
+ })
2221
+
2222
+ it('hour parsing', () => {
2223
+ expect(relativeDateParseForFeatureFlagMatching('1h')).toEqual(new Date('2020-01-01T11:01:20.134Z'))
2224
+ expect(relativeDateParseForFeatureFlagMatching('2h')).toEqual(new Date('2020-01-01T10:01:20.134Z'))
2225
+ expect(relativeDateParseForFeatureFlagMatching('24h')).toEqual(new Date('2019-12-31T12:01:20.134Z'))
2226
+ expect(relativeDateParseForFeatureFlagMatching('30h')).toEqual(new Date('2019-12-31T06:01:20.134Z'))
2227
+ expect(relativeDateParseForFeatureFlagMatching('48h')).toEqual(new Date('2019-12-30T12:01:20.134Z'))
2228
+
2229
+ expect(relativeDateParseForFeatureFlagMatching('24h')).toEqual(relativeDateParseForFeatureFlagMatching('1d'))
2230
+ expect(relativeDateParseForFeatureFlagMatching('48h')).toEqual(relativeDateParseForFeatureFlagMatching('2d'))
2231
+ })
2232
+
2233
+ it('day parsing', () => {
2234
+ expect(relativeDateParseForFeatureFlagMatching('1d')).toEqual(new Date('2019-12-31T12:01:20.134Z'))
2235
+ expect(relativeDateParseForFeatureFlagMatching('2d')).toEqual(new Date('2019-12-30T12:01:20.134Z'))
2236
+ expect(relativeDateParseForFeatureFlagMatching('7d')).toEqual(new Date('2019-12-25T12:01:20.134Z'))
2237
+ expect(relativeDateParseForFeatureFlagMatching('14d')).toEqual(new Date('2019-12-18T12:01:20.134Z'))
2238
+ expect(relativeDateParseForFeatureFlagMatching('30d')).toEqual(new Date('2019-12-02T12:01:20.134Z'))
2239
+
2240
+ expect(relativeDateParseForFeatureFlagMatching('7d')).toEqual(relativeDateParseForFeatureFlagMatching('1w'))
2241
+ })
2242
+
2243
+ it('week parsing', () => {
2244
+ expect(relativeDateParseForFeatureFlagMatching('1w')).toEqual(new Date('2019-12-25T12:01:20.134Z'))
2245
+ expect(relativeDateParseForFeatureFlagMatching('2w')).toEqual(new Date('2019-12-18T12:01:20.134Z'))
2246
+ expect(relativeDateParseForFeatureFlagMatching('4w')).toEqual(new Date('2019-12-04T12:01:20.134Z'))
2247
+ expect(relativeDateParseForFeatureFlagMatching('8w')).toEqual(new Date('2019-11-06T12:01:20.134Z'))
2248
+
2249
+ expect(relativeDateParseForFeatureFlagMatching('1m')).toEqual(new Date('2019-12-01T12:01:20.134Z'))
2250
+ expect(relativeDateParseForFeatureFlagMatching('4w')).not.toEqual(relativeDateParseForFeatureFlagMatching('1m'))
2251
+ })
2252
+
2253
+ it('month parsing', () => {
2254
+ expect(relativeDateParseForFeatureFlagMatching('1m')).toEqual(new Date('2019-12-01T12:01:20.134Z'))
2255
+ expect(relativeDateParseForFeatureFlagMatching('2m')).toEqual(new Date('2019-11-01T12:01:20.134Z'))
2256
+
2257
+ expect(relativeDateParseForFeatureFlagMatching('4m')).toEqual(new Date('2019-09-01T12:01:20.134Z'))
2258
+ expect(relativeDateParseForFeatureFlagMatching('5m')).toEqual(new Date('2019-08-01T12:01:20.134Z'))
2259
+ expect(relativeDateParseForFeatureFlagMatching('6m')).toEqual(new Date('2019-07-01T12:01:20.134Z'))
2260
+ expect(relativeDateParseForFeatureFlagMatching('8m')).toEqual(new Date('2019-05-01T12:01:20.134Z'))
2261
+ expect(relativeDateParseForFeatureFlagMatching('10m')).toEqual(new Date('2019-03-01T12:01:20.134Z'))
2262
+
2263
+ expect(relativeDateParseForFeatureFlagMatching('24m')).toEqual(new Date('2018-01-01T12:01:20.134Z'))
2264
+
2265
+ expect(relativeDateParseForFeatureFlagMatching('1y')).toEqual(new Date('2019-01-01T12:01:20.134Z'))
2266
+ expect(relativeDateParseForFeatureFlagMatching('12m')).toEqual(relativeDateParseForFeatureFlagMatching('1y'))
2267
+
2268
+ jest.setSystemTime(new Date('2020-04-03T00:00:00Z'))
2269
+ expect(relativeDateParseForFeatureFlagMatching('1m')).toEqual(new Date('2020-03-03T00:00:00Z'))
2270
+ expect(relativeDateParseForFeatureFlagMatching('2m')).toEqual(new Date('2020-02-03T00:00:00Z'))
2271
+ expect(relativeDateParseForFeatureFlagMatching('4m')).toEqual(new Date('2019-12-03T00:00:00Z'))
2272
+ expect(relativeDateParseForFeatureFlagMatching('8m')).toEqual(new Date('2019-08-03T00:00:00Z'))
2273
+
2274
+ expect(relativeDateParseForFeatureFlagMatching('1y')).toEqual(new Date('2019-04-03T00:00:00Z'))
2275
+ expect(relativeDateParseForFeatureFlagMatching('12m')).toEqual(relativeDateParseForFeatureFlagMatching('1y'))
2276
+ })
2277
+
2278
+ it('year parsing', () => {
2279
+ expect(relativeDateParseForFeatureFlagMatching('1y')).toEqual(new Date('2019-01-01T12:01:20.134Z'))
2280
+ expect(relativeDateParseForFeatureFlagMatching('2y')).toEqual(new Date('2018-01-01T12:01:20.134Z'))
2281
+ expect(relativeDateParseForFeatureFlagMatching('4y')).toEqual(new Date('2016-01-01T12:01:20.134Z'))
2282
+ expect(relativeDateParseForFeatureFlagMatching('8y')).toEqual(new Date('2012-01-01T12:01:20.134Z'))
2283
+
2284
+ expect(relativeDateParseForFeatureFlagMatching('1y')).toEqual(new Date('2019-01-01T12:01:20.134Z'))
2285
+ expect(relativeDateParseForFeatureFlagMatching('12m')).toEqual(relativeDateParseForFeatureFlagMatching('1y'))
2286
+ })
2019
2287
  })
2020
2288
 
2021
2289
  describe('consistency tests', () => {