0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-30 19:41:46 +01:00
posthog/plugin-server/functional_tests/exports-v1.test.ts

160 lines
4.8 KiB
TypeScript
Raw Normal View History

import { createServer, Server } from 'http'
import { UUIDT } from '../src/utils/utils'
import { capture, createAndReloadPluginConfig, createOrganization, createPlugin, createTeam } from './api'
import { waitForExpect } from './expectations'
let organizationId: string
let server: Server
const webHookCalledWith: any = {}
beforeAll(async () => {
organizationId = await createOrganization()
server = createServer((req, res) => {
let body = ''
req.on('data', (chunk) => {
body += chunk
})
req.on('end', () => {
webHookCalledWith[req.url!] = webHookCalledWith[req.url!] ?? []
webHookCalledWith[req.url!].push(JSON.parse(body))
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end()
})
})
await new Promise((resolve) => {
server.on('listening', resolve)
server.listen()
})
})
afterAll(() => {
server.close()
})
test.concurrent(`exports: exporting events on ingestion`, async () => {
const teamId = await createTeam(organizationId)
const plugin = await createPlugin({
organization_id: organizationId,
name: 'export plugin',
plugin_type: 'source',
is_global: false,
source__index_ts: `
2023-12-05 14:40:05 +01:00
export const onEvent = async (event, { global, config }) => {
await fetch(
"http://localhost:${server.address()?.port}/${teamId}",
2023-12-05 14:40:05 +01:00
{method: "POST", body: JSON.stringify(event)}
)
}
`,
})
await createAndReloadPluginConfig(teamId, plugin.id)
const distinctId = new UUIDT().toString()
const uuid = new UUIDT().toString()
// First let's ingest an event
await capture({
teamId,
distinctId,
uuid,
event: 'custom event',
properties: {
name: 'hehe',
uuid: new UUIDT().toString(),
},
})
2023-12-05 14:40:05 +01:00
// Then check that the onEvent function was called
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
await waitForExpect(
() => {
2023-12-05 14:40:05 +01:00
const onEvents = webHookCalledWith[`/${teamId}`]
expect(onEvents).toEqual([
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
expect.objectContaining({
distinct_id: distinctId,
team_id: teamId,
event: 'custom event',
properties: expect.objectContaining({
name: 'hehe',
uuid: uuid,
}),
timestamp: expect.any(String),
uuid: uuid,
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
elements: [],
}),
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
])
},
60_000,
1_000
)
})
test.concurrent.skip(`exports: exporting $autocapture events on ingestion`, async () => {
const teamId = await createTeam(organizationId)
const plugin = await createPlugin({
organization_id: organizationId,
name: 'export plugin',
plugin_type: 'source',
is_global: false,
source__index_ts: `
2023-12-05 14:40:05 +01:00
export const onEvent = async (event, { global, config }) => {
await fetch(
"http://localhost:${server.address()?.port}/${teamId}",
2023-12-05 14:40:05 +01:00
{method: "POST", body: JSON.stringify(event)}
)
}
`,
})
await createAndReloadPluginConfig(teamId, plugin.id)
const distinctId = new UUIDT().toString()
const uuid = new UUIDT().toString()
// First let's ingest an event
await capture({
teamId,
distinctId,
uuid,
event: '$autocapture',
properties: {
name: 'hehe',
uuid: new UUIDT().toString(),
$elements: [{ tag_name: 'div', nth_child: 1, nth_of_type: 2, $el_text: '💻' }],
},
})
2023-12-05 14:40:05 +01:00
// Then check that the onEvent function was called
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
await waitForExpect(
() => {
2023-12-05 14:40:05 +01:00
const onEvents = webHookCalledWith[`/${teamId}`]
expect(onEvents).toEqual([
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
expect.objectContaining({
distinct_id: distinctId,
team_id: teamId,
event: '$autocapture',
properties: expect.objectContaining({
name: 'hehe',
uuid: uuid,
}),
timestamp: expect.any(String),
uuid: uuid,
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
elements: [
{
tag_name: 'div',
nth_child: 1,
nth_of_type: 2,
order: 0,
$el_text: '💻',
text: '💻',
attributes: {},
},
],
}),
feat(person-on-events): add option to delay all events (#13505) * feat(person-on-events): add option to delay all events This change implements the option outlined in https://github.com/PostHog/product-internal/pull/405 Here I do not try to do any large structural changes to the code, I'll leave that for later although it does mean the code has a few loose couplings between pipeline steps that probably should be strongly coupled. I've tried to comment these to try to make it clear about the couplings. I've also added a workflow to run the functional tests against both configurations, which we can remove once we're happy with the new implementation. Things of note: 1. We can't enable this for all users yet, not without the live events view and not without verifying that the buffer size is sufficiently large. We can however enable this for the test team and verify that it functions as expected. 2. I have not handled the case mentioned in the above PR regarding guarding against processing the delayed events before all events in the delay window have been processed. wip test(person-on-events): add currently failing test for person on events This test doesn't work with the previous behaviour of the person-on-events implementation, but should pass with the new delay all events behaviour. * add test for KafkaJSError behaviour * add comment re delay * add test for create_alias * chore: increase exports timeout It seems to fail in CI, but only for the delayed events enabled tests. I'm not sure why, but I'm guessing it's because the events are further delayed by the new implementation. * chore: fix test * add test for ordering of person properties * use ubuntu-latest-8-cores runner * add tests for plugin processEvent * chore: ensure plugin processEvent isn't run multiple times * expand on person properties ordering test * wip * wip * add additional test * change fullyProcessEvent to onlyUpdatePersonIdAssociations * update test * add test to ensure person properties do not propagate backwards in time * simplicfy person property tests * weaken guarantee in test * chore: make sure we don't update properties on the first parse We should only be updating person_id and asociated distinct_ids on first parse. * add tests for dropping events * increase export timeout * increase historical exports timeout * increase default waitForExpect interval to 1 second
2023-01-05 17:38:43 +01:00
])
},
60_000,
1_000
)
})