add tests for posthog feature flags
This commit is contained in:
parent
edb3b685b5
commit
a9b4d0017f
|
@ -1,14 +1,15 @@
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import * as context from "../context"
|
import * as context from "../context"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
import { PostHog } from "posthog-node"
|
import { PostHog, PostHogOptions } from "posthog-node"
|
||||||
import { IdentityType } from "@budibase/types"
|
import { IdentityType } from "@budibase/types"
|
||||||
|
|
||||||
let posthog: PostHog | undefined
|
let posthog: PostHog | undefined
|
||||||
export function init() {
|
export function init(opts?: PostHogOptions) {
|
||||||
if (env.POSTHOG_TOKEN) {
|
if (env.POSTHOG_TOKEN) {
|
||||||
posthog = new PostHog(env.POSTHOG_TOKEN, {
|
posthog = new PostHog(env.POSTHOG_TOKEN, {
|
||||||
host: "https://us.i.posthog.com",
|
host: "https://us.i.posthog.com",
|
||||||
|
...opts,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +19,14 @@ abstract class Flag<T> {
|
||||||
return new BooleanFlag(defaultValue)
|
return new BooleanFlag(defaultValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static string(defaultValue: string): Flag<string> {
|
||||||
|
return new StringFlag(defaultValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
static number(defaultValue: number): Flag<number> {
|
||||||
|
return new NumberFlag(defaultValue)
|
||||||
|
}
|
||||||
|
|
||||||
protected constructor(public defaultValue: T) {}
|
protected constructor(public defaultValue: T) {}
|
||||||
|
|
||||||
abstract parse(value: any): T
|
abstract parse(value: any): T
|
||||||
|
@ -37,6 +46,32 @@ class BooleanFlag extends Flag<boolean> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class StringFlag extends Flag<string> {
|
||||||
|
parse(value: any) {
|
||||||
|
if (typeof value === "string") {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
throw new Error(`could not parse value "${value}" as string`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NumberFlag extends Flag<number> {
|
||||||
|
parse(value: any) {
|
||||||
|
if (typeof value === "number") {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof value === "string") {
|
||||||
|
const parsed = parseFloat(value)
|
||||||
|
if (!isNaN(parsed)) {
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error(`could not parse value "${value}" as number`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This is the primary source of truth for feature flags. If you want to add a
|
// This is the primary source of truth for feature flags. If you want to add a
|
||||||
// new flag, add it here and use the `fetch` and `get` functions to access it.
|
// new flag, add it here and use the `fetch` and `get` functions to access it.
|
||||||
// All of the machinery in this file is to make sure that flags have their
|
// All of the machinery in this file is to make sure that flags have their
|
||||||
|
@ -46,6 +81,10 @@ const FLAGS = {
|
||||||
GOOGLE_SHEETS: Flag.boolean(false),
|
GOOGLE_SHEETS: Flag.boolean(false),
|
||||||
USER_GROUPS: Flag.boolean(false),
|
USER_GROUPS: Flag.boolean(false),
|
||||||
ONBOARDING_TOUR: Flag.boolean(false),
|
ONBOARDING_TOUR: Flag.boolean(false),
|
||||||
|
|
||||||
|
_TEST_BOOLEAN: Flag.boolean(false),
|
||||||
|
_TEST_STRING: Flag.string("default value"),
|
||||||
|
_TEST_NUMBER: Flag.number(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULTS = Object.keys(FLAGS).reduce((acc, key) => {
|
const DEFAULTS = Object.keys(FLAGS).reduce((acc, key) => {
|
||||||
|
@ -107,6 +146,7 @@ export async function fetch(): Promise<Flags> {
|
||||||
throw new Error(`Feature: ${feature} is not a boolean`)
|
throw new Error(`Feature: ${feature} is not a boolean`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
flags[feature] = value
|
flags[feature] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,7 +154,7 @@ export async function fetch(): Promise<Flags> {
|
||||||
const identity = context.getIdentity()
|
const identity = context.getIdentity()
|
||||||
if (posthog && identity?.type === IdentityType.USER) {
|
if (posthog && identity?.type === IdentityType.USER) {
|
||||||
const posthogFlags = await posthog.getAllFlagsAndPayloads(identity._id)
|
const posthogFlags = await posthog.getAllFlagsAndPayloads(identity._id)
|
||||||
for (const [name, value] of Object.entries(posthogFlags)) {
|
for (const [name, value] of Object.entries(posthogFlags.featureFlags)) {
|
||||||
const key = name as keyof typeof FLAGS
|
const key = name as keyof typeof FLAGS
|
||||||
const flag = FLAGS[key]
|
const flag = FLAGS[key]
|
||||||
if (!flag) {
|
if (!flag) {
|
||||||
|
@ -124,8 +164,11 @@ export async function fetch(): Promise<Flags> {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const payload = posthogFlags.featureFlagPayloads?.[name]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
flags[key] = flag.parse(value)
|
// @ts-ignore
|
||||||
|
flags[key] = flag.parse(payload || value)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// We don't want an invalid PostHog flag to break the app, so we just
|
// We don't want an invalid PostHog flag to break the app, so we just
|
||||||
// log it and continue.
|
// log it and continue.
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { IdentityContext, IdentityType } from "@budibase/types"
|
||||||
import { defaultFlags, fetch, get, Flags, init } from "../"
|
import { defaultFlags, fetch, get, Flags, init } from "../"
|
||||||
import { context } from "../.."
|
import { context } from "../.."
|
||||||
import { setEnv, withEnv } from "../../environment"
|
import { setEnv, withEnv } from "../../environment"
|
||||||
|
import nodeFetch from "node-fetch"
|
||||||
import nock from "nock"
|
import nock from "nock"
|
||||||
|
|
||||||
describe("feature flags", () => {
|
describe("feature flags", () => {
|
||||||
|
@ -14,23 +15,23 @@ describe("feature flags", () => {
|
||||||
it.each<TestCase>([
|
it.each<TestCase>([
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
flags: "tenant1:ONBOARDING_TOUR",
|
flags: "tenant1:_TEST_BOOLEAN",
|
||||||
expected: { ONBOARDING_TOUR: true },
|
expected: { _TEST_BOOLEAN: true },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
flags: "tenant1:!ONBOARDING_TOUR",
|
flags: "tenant1:!_TEST_BOOLEAN",
|
||||||
expected: { ONBOARDING_TOUR: false },
|
expected: { _TEST_BOOLEAN: false },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
flags: "*:ONBOARDING_TOUR",
|
flags: "*:_TEST_BOOLEAN",
|
||||||
expected: { ONBOARDING_TOUR: true },
|
expected: { _TEST_BOOLEAN: true },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
flags: "tenant2:ONBOARDING_TOUR",
|
flags: "tenant2:_TEST_BOOLEAN",
|
||||||
expected: { ONBOARDING_TOUR: false },
|
expected: { _TEST_BOOLEAN: false },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
|
@ -62,7 +63,7 @@ describe("feature flags", () => {
|
||||||
it.each<FailedTestCase>([
|
it.each<FailedTestCase>([
|
||||||
{
|
{
|
||||||
tenant: "tenant1",
|
tenant: "tenant1",
|
||||||
flags: "tenant1:ONBOARDING_TOUR,tenant1:FOO",
|
flags: "tenant1:_TEST_BOOLEAN,tenant1:FOO",
|
||||||
expected: "Feature: FOO is not an allowed option",
|
expected: "Feature: FOO is not an allowed option",
|
||||||
},
|
},
|
||||||
])(
|
])(
|
||||||
|
@ -75,43 +76,88 @@ describe("feature flags", () => {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// describe("posthog", () => {
|
describe("posthog", () => {
|
||||||
// const identity: IdentityContext = {
|
const identity: IdentityContext = {
|
||||||
// _id: "us_1234",
|
_id: "us_1234",
|
||||||
// tenantId: "budibase",
|
tenantId: "budibase",
|
||||||
// type: IdentityType.USER,
|
type: IdentityType.USER,
|
||||||
// email: "test@example.com",
|
email: "test@example.com",
|
||||||
// firstName: "Test",
|
firstName: "Test",
|
||||||
// lastName: "User",
|
lastName: "User",
|
||||||
// }
|
}
|
||||||
|
|
||||||
// let cleanup: () => void
|
let cleanup: () => void
|
||||||
|
|
||||||
// beforeAll(() => {
|
beforeAll(() => {
|
||||||
// cleanup = setEnv({ POSTHOG_TOKEN: "test" })
|
cleanup = setEnv({ POSTHOG_TOKEN: "test" })
|
||||||
// init()
|
})
|
||||||
// })
|
|
||||||
|
|
||||||
// afterAll(() => {
|
afterAll(() => {
|
||||||
// cleanup()
|
cleanup()
|
||||||
// })
|
})
|
||||||
|
|
||||||
// beforeEach(() => {
|
beforeEach(() => {
|
||||||
// nock.cleanAll()
|
nock.cleanAll()
|
||||||
// })
|
|
||||||
|
|
||||||
// it("should be able to read flags from posthog", () =>
|
// We need to pass in node-fetch here otherwise nock won't get used
|
||||||
// context.doInIdentityContext(identity, async () => {
|
// because posthog-node uses axios under the hood.
|
||||||
// nock("https://app.posthog.com")
|
init({ fetch: nodeFetch })
|
||||||
// .get("/api/feature_flags/tenant/budibase")
|
})
|
||||||
// .reply(200, {
|
|
||||||
// flags: {
|
|
||||||
// "budibase:onboardingTour": true,
|
|
||||||
// },
|
|
||||||
// })
|
|
||||||
|
|
||||||
// const flags = await fetch()
|
function mockFlags(flags: {
|
||||||
// expect(flags.ONBOARDING_TOUR).toBe(true)
|
featureFlags?: Record<string, boolean>
|
||||||
// }))
|
featureFlagPayloads?: Record<string, any>
|
||||||
// })
|
}) {
|
||||||
|
nock("https://us.i.posthog.com")
|
||||||
|
.post("/decide/?v=3", body => {
|
||||||
|
return body.token === "test" && body.distinct_id === "us_1234"
|
||||||
|
})
|
||||||
|
.reply(200, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should be able to read flags from posthog", async () => {
|
||||||
|
mockFlags({
|
||||||
|
featureFlags: {
|
||||||
|
_TEST_BOOLEAN: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await context.doInIdentityContext(identity, async () => {
|
||||||
|
const flags = await fetch()
|
||||||
|
expect(flags._TEST_BOOLEAN).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should be able to read flags from posthog with payloads", async () => {
|
||||||
|
mockFlags({
|
||||||
|
featureFlags: {
|
||||||
|
_TEST_STRING: true,
|
||||||
|
},
|
||||||
|
featureFlagPayloads: {
|
||||||
|
_TEST_STRING: "test payload",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await context.doInIdentityContext(identity, async () => {
|
||||||
|
const flags = await fetch()
|
||||||
|
expect(flags._TEST_STRING).toBe("test payload")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("should be able to read flags from posthog with numbers", async () => {
|
||||||
|
mockFlags({
|
||||||
|
featureFlags: {
|
||||||
|
_TEST_NUMBER: true,
|
||||||
|
},
|
||||||
|
featureFlagPayloads: {
|
||||||
|
_TEST_NUMBER: 123,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await context.doInIdentityContext(identity, async () => {
|
||||||
|
const flags = await fetch()
|
||||||
|
expect(flags._TEST_NUMBER).toBe(123)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue