Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Feb 7, 2025
1 parent 2cce3ed commit 4c335d7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async function syncCustomerMetadata(meta: CustomerIoMeta, event: ProcessedPlugin
const customerExistsAlready = customerStatus.has('seen')
const email = getEmailFromEvent(event)

logger.debug(email)
logger.debug('Detected email', email)

// Update customer status
customerStatus.add('seen')
Expand Down
80 changes: 44 additions & 36 deletions plugin-server/src/cdp/services/legacy-plugin-executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { forSnapshot } from '~/tests/helpers/snapshots'
import { getFirstTeam, resetTestDatabase } from '~/tests/helpers/sql'

import { createPlugin, createPluginConfig } from '../../../tests/helpers/sql'
import { Hub, Team } from '../../types'
import { Hub, PluginConfig, Team } from '../../types'
import { closeHub, createHub } from '../../utils/db/hub'
import { DESTINATION_PLUGINS_BY_ID, TRANSFORMATION_PLUGINS_BY_ID } from '../legacy-plugins'
import { LegacyDestinationPlugin, LegacyTransformationPlugin } from '../legacy-plugins/types'
Expand All @@ -29,6 +29,7 @@ describe('LegacyPluginExecutorService', () => {
let globals: HogFunctionInvocationGlobalsWithInputs
let fn: HogFunctionType
let mockFetch: jest.Mock
let pluginConfig: PluginConfig

const customerIoPlugin = DESTINATION_PLUGINS_BY_ID['plugin-customerio-plugin']

Expand All @@ -41,11 +42,29 @@ describe('LegacyPluginExecutorService', () => {
fn = createHogFunction({
name: 'Plugin test',
template_id: customerIoPlugin.template.id,
team_id: team.id,
})

const fixedTime = DateTime.fromObject({ year: 2025, month: 1, day: 1 }, { zone: 'UTC' })
jest.spyOn(Date, 'now').mockReturnValue(fixedTime.toMillis())

const plugin = await createPlugin(hub.postgres, {
organization_id: team.organization_id,
name: 'first-time-event-tracker',
plugin_type: 'source',
is_global: false,
source__index_ts: `
export async function runEveryMinute() {
console.info(JSON.stringify(['runEveryMinute']))
}
`,
})
pluginConfig = await createPluginConfig(hub.postgres, {
name: 'first-time-event-tracker',
team_id: team.id,
plugin_id: plugin.id,
} as any)

mockFetch = jest.fn(() =>
Promise.resolve({
status: 200,
Expand Down Expand Up @@ -80,6 +99,7 @@ describe('LegacyPluginExecutorService', () => {
customerioSiteId: '1234567890',
customerioToken: 'cio-token',
email: '[email protected]',
legacy_plugin_config_id: pluginConfig.id,
},
}
})
Expand Down Expand Up @@ -151,6 +171,9 @@ describe('LegacyPluginExecutorService', () => {

const res = await service.execute(invocation)

expect(res.finished).toBe(true)
expect(res.error).toBeUndefined()

expect(customerIoPlugin.onEvent).toHaveBeenCalledTimes(1)
expect(forSnapshot(jest.mocked(customerIoPlugin.onEvent!).mock.calls[0][0])).toMatchInlineSnapshot(`
{
Expand All @@ -162,13 +185,13 @@ describe('LegacyPluginExecutorService', () => {
"email": "[email protected]",
"first_name": "Pumpkin",
},
"team_id": 1,
"team_id": 2,
"uuid": "uuid",
},
"properties": {
"email": "[email protected]",
},
"team_id": 1,
"team_id": 2,
"timestamp": "2025-01-01T00:00:00.000Z",
"uuid": "<REPLACED-UUID-0>",
}
Expand All @@ -191,7 +214,7 @@ describe('LegacyPluginExecutorService', () => {
[
"https://track.customer.io/api/v1/customers/distinct_id",
{
"body": "{"identifier":"distinct_id","email":"[email protected]"}",
"body": "{"_update":false,"identifier":"distinct_id","email":"[email protected]"}",
"headers": {
"Authorization": "Basic MTIzNDU2Nzg5MDpjaW8tdG9rZW4=",
"Content-Type": "application/json",
Expand Down Expand Up @@ -220,9 +243,9 @@ describe('LegacyPluginExecutorService', () => {
[
"Executing plugin plugin-customerio-plugin",
"Successfully authenticated with Customer.io. Completing setupPlugin.",
"Detected email:, [email protected]",
"{"status":{},"email":"[email protected]"}",
"Should customer be tracked:, true",
"Detected email, [email protected]",
"{"status":{},"existsAlready":false,"email":"[email protected]"}",
"true",
"Execution successful",
]
`)
Expand All @@ -249,11 +272,12 @@ describe('LegacyPluginExecutorService', () => {
[
"Executing plugin plugin-customerio-plugin",
"Successfully authenticated with Customer.io. Completing setupPlugin.",
"Detected email:, [email protected]",
"{"status":{},"email":"[email protected]"}",
"Should customer be tracked:, true",
"Detected email, [email protected]",
"{"status":{},"existsAlready":false,"email":"[email protected]"}",
"true",
"Fetch called but mocked due to test function",
"Plugin execution failed: Received a potentially intermittent error from the Customer.io API. Response 500: {"message":"Test function"}",
"Fetch called but mocked due to test function",
"Execution successful",
]
`)
})
Expand Down Expand Up @@ -287,9 +311,9 @@ describe('LegacyPluginExecutorService', () => {
[
"Executing plugin plugin-customerio-plugin",
"Successfully authenticated with Customer.io. Completing setupPlugin.",
"Detected email:, [email protected]",
"{"status":{},"email":"[email protected]"}",
"Should customer be tracked:, true",
"Detected email, [email protected]",
"{"status":{},"existsAlready":false,"email":"[email protected]"}",
"true",
"Plugin execution failed: Received a potentially intermittent error from the Customer.io API. Response 500: {}",
]
`)
Expand Down Expand Up @@ -342,7 +366,7 @@ describe('LegacyPluginExecutorService', () => {
"properties": {
"email": "[email protected]",
},
"team_id": 1,
"team_id": 2,
"timestamp": "2025-01-01T00:00:00.000Z",
"uuid": "<REPLACED-UUID-0>",
}
Expand Down Expand Up @@ -394,7 +418,7 @@ describe('LegacyPluginExecutorService', () => {
"version__minor": 12,
"version__patch": 20,
},
"team_id": 1,
"team_id": 2,
"timestamp": "2025-01-01T00:00:00.000Z",
"uuid": "<REPLACED-UUID-0>",
}
Expand Down Expand Up @@ -441,6 +465,10 @@ describe('LegacyPluginExecutorService', () => {
const invocation = buildInvocation(plugin)
invocation.hogFunction.name = name
invocation.globals.event.event = '$identify' // Many plugins filter for this

if (plugin.template.id === 'plugin-customerio-plugin') {
invocation.globals.inputs.legacy_plugin_config_id = pluginConfig.id
}
const res = await service.execute(invocation)
expect(res.logs.map((l) => l.message)).toMatchSnapshot()
})
Expand Down Expand Up @@ -485,26 +513,6 @@ describe('LegacyPluginExecutorService', () => {
})

it('should succeed if legacy plugin config id is provided', async () => {
console.log(team.id, team.organization_id)
const plugin = await createPlugin(hub.postgres, {
organization_id: team.organization_id,
name: 'first-time-event-tracker',
plugin_type: 'source',
is_global: false,
source__index_ts: `
export async function runEveryMinute() {
console.info(JSON.stringify(['runEveryMinute']))
}
`,
})
const pluginConfig = await createPluginConfig(hub.postgres, {
name: 'first-time-event-tracker',
team_id: team.id,
plugin_id: plugin.id,
} as any)

console.log(pluginConfig)

invocation.globals.inputs.legacy_plugin_config_id = pluginConfig.id

const res = await service.execute(invocation)
Expand Down

0 comments on commit 4c335d7

Please sign in to comment.