Skip to content

Commit

Permalink
fix: Silence several TS errors from plugin-server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeelaudibert committed Feb 25, 2025
1 parent 27db5f7 commit 94dcf1c
Show file tree
Hide file tree
Showing 42 changed files with 358 additions and 224 deletions.
1 change: 1 addition & 0 deletions plugin-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
"@types/generic-pool": "^3.1.9",
"@types/ioredis": "^4.26.4",
"@types/jest": "^29.5.14",
"@types/kafkajs-snappy": "^1.0.0",
"@types/long": "4.x.x",
"@types/luxon": "^3.4.2",
"@types/node-fetch": "^2.5.10",
Expand Down
2 changes: 1 addition & 1 deletion plugin-server/src/worker/ingestion/process-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class EventsProcessor {
teamId: number,
timestamp: DateTime,
eventUuid: string,
processPerson: boolean
processPerson: boolean = false
): Promise<PreIngestionEvent> {
const singleSaveTimer = new Date()
const timeout = timeoutGuard(
Expand Down
4 changes: 4 additions & 0 deletions plugin-server/tests/cdp/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ describe('Utils', () => {
index: 0,
properties: { name: 'Acme Corp' },
})

// @ts-expect-error this is correct TS
expect(response['organization']).toBe(response['group_0'])

// Verify that group_1 and project are set correctly
Expand All @@ -150,6 +152,8 @@ describe('Utils', () => {
index: 1,
properties: { name: 'Project X' },
})

// @ts-expect-error this is correct TS
expect(response['project']).toBe(response['group_1'])
})
})
Expand Down
2 changes: 1 addition & 1 deletion plugin-server/tests/e2e.timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('e2e ingestion timeout', () => {
`)
await resetTestDatabaseClickhouse(extraServerConfig)
const startResponse = await startPluginsServer(extraServerConfig, makePiscina, { ingestion: true })
hub = startResponse.hub
hub = startResponse.hub!
stopServer = startResponse.stop
posthog = createPosthog(hub, pluginConfig39)
})
Expand Down
4 changes: 3 additions & 1 deletion plugin-server/tests/helpers/clickhouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export async function delayUntilEventIngested<T extends any[] | number>(
maxDelayCount = 100
): Promise<T> {
const timer = performance.now()
let data: T
let data: T | null = null
let dataLength = 0

for (let i = 0; i < maxDelayCount; i++) {
data = await fetchData()
dataLength = typeof data === 'number' ? data : data.length
Expand All @@ -54,5 +55,6 @@ export async function delayUntilEventIngested<T extends any[] | number>(
}
await delay(delayMs)
}

throw Error(`Failed to get data in time, got ${JSON.stringify(data)}`)
}
19 changes: 0 additions & 19 deletions plugin-server/tests/helpers/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,3 @@ export function createPromise<T = void>(): MockPromise<T> {

return result as MockPromise<T>
}

export class WaitEvent {
private promise: Promise<void>
private resolve: () => void

constructor() {
this.promise = new Promise((resolve) => {
this.resolve = resolve
})
}

public set() {
this.resolve()
}

public async wait() {
return this.promise
}
}
72 changes: 38 additions & 34 deletions plugin-server/tests/main/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DateTime } from 'luxon'
import { Pool } from 'pg'

import { defaultConfig } from '../../src/config/config'
import { Hub, Person, PropertyOperator, PropertyUpdateOperation, RawAction, Team } from '../../src/types'
import { BasePerson, Hub, Person, PropertyOperator, PropertyUpdateOperation, RawAction, Team } from '../../src/types'
import { DB } from '../../src/utils/db/db'
import { DependencyUnavailableError, RedisOperationError } from '../../src/utils/db/error'
import { closeHub, createHub } from '../../src/utils/db/hub'
Expand Down Expand Up @@ -35,9 +35,10 @@ describe('DB', () => {
})

const TIMESTAMP = DateTime.fromISO('2000-10-14T11:42:06.502Z').toUTC()
const ISO_TIMESTAMP = ISO_TIMESTAMP!

function runPGQuery(queryString: string, values: any[] = null) {
return db.postgres.query(PostgresUse.COMMON_WRITE, queryString, values, 'testQuery')
function runPGQuery(queryString: string) {
return db.postgres.query(PostgresUse.COMMON_WRITE, queryString, [], 'testQuery')
}

describe('fetchAllActionsGroupedByTeam() and fetchAction()', () => {
Expand Down Expand Up @@ -298,40 +299,43 @@ describe('DB', () => {

test('without properties', async () => {
const person = await db.createPerson(TIMESTAMP, {}, {}, {}, team.id, null, false, uuid, [{ distinctId }])
const fetched_person = await fetchPersonByPersonId(team.id, person.id)

expect(fetched_person!.is_identified).toEqual(false)
expect(fetched_person!.properties).toEqual({})
expect(fetched_person!.properties_last_operation).toEqual({})
expect(fetched_person!.properties_last_updated_at).toEqual({})
expect(fetched_person!.uuid).toEqual(uuid)
expect(fetched_person!.team_id).toEqual(team.id)
const fetched_person = (await fetchPersonByPersonId(team.id, person.id))! as unknown as BasePerson

expect(fetched_person.is_identified).toEqual(false)
expect(fetched_person.properties).toEqual({})
expect(fetched_person.properties_last_operation).toEqual({})
expect(fetched_person.properties_last_updated_at).toEqual({})
expect(fetched_person.uuid).toEqual(uuid)
expect(fetched_person.team_id).toEqual(team.id)
})

test('without properties indentified true', async () => {
const person = await db.createPerson(TIMESTAMP, {}, {}, {}, team.id, null, true, uuid, [{ distinctId }])
const fetched_person = await fetchPersonByPersonId(team.id, person.id)
expect(fetched_person!.is_identified).toEqual(true)
expect(fetched_person!.properties).toEqual({})
expect(fetched_person!.properties_last_operation).toEqual({})
expect(fetched_person!.properties_last_updated_at).toEqual({})
expect(fetched_person!.uuid).toEqual(uuid)
expect(fetched_person!.team_id).toEqual(team.id)
const fetched_person = (await fetchPersonByPersonId(team.id, person.id))! as unknown as BasePerson

expect(fetched_person.is_identified).toEqual(true)
expect(fetched_person.properties).toEqual({})
expect(fetched_person.properties_last_operation).toEqual({})
expect(fetched_person.properties_last_updated_at).toEqual({})
expect(fetched_person.uuid).toEqual(uuid)
expect(fetched_person.team_id).toEqual(team.id)
})

test('with properties', async () => {
const person = await db.createPerson(
TIMESTAMP,
{ a: 123, b: false, c: 'bbb' },
{ a: TIMESTAMP.toISO(), b: TIMESTAMP.toISO(), c: TIMESTAMP.toISO() },
{ a: ISO_TIMESTAMP, b: ISO_TIMESTAMP, c: ISO_TIMESTAMP },
{ a: PropertyUpdateOperation.Set, b: PropertyUpdateOperation.Set, c: PropertyUpdateOperation.SetOnce },
team.id,
null,
false,
uuid,
[{ distinctId }]
)
const fetched_person = await fetchPersonByPersonId(team.id, person.id)

const fetched_person = (await fetchPersonByPersonId(team.id, person.id))! as unknown as BasePerson

expect(fetched_person!.is_identified).toEqual(false)
expect(fetched_person!.properties).toEqual({ a: 123, b: false, c: 'bbb' })
expect(fetched_person!.properties_last_operation).toEqual({
Expand All @@ -340,9 +344,9 @@ describe('DB', () => {
c: PropertyUpdateOperation.SetOnce,
})
expect(fetched_person!.properties_last_updated_at).toEqual({
a: TIMESTAMP.toISO(),
b: TIMESTAMP.toISO(),
c: TIMESTAMP.toISO(),
a: ISO_TIMESTAMP,
b: ISO_TIMESTAMP,
c: ISO_TIMESTAMP,
})
expect(fetched_person!.uuid).toEqual(uuid)
expect(fetched_person!.team_id).toEqual(team.id)
Expand Down Expand Up @@ -509,7 +513,7 @@ describe('DB', () => {
'group_key',
{ prop: 'val' },
TIMESTAMP,
{ prop: TIMESTAMP.toISO() },
{ prop: ISO_TIMESTAMP },
{ prop: PropertyUpdateOperation.Set },
1
)
Expand All @@ -526,7 +530,7 @@ describe('DB', () => {
'group_key',
{ prop: 'val' },
TIMESTAMP,
{ prop: TIMESTAMP.toISO()! },
{ prop: ISO_TIMESTAMP },
{ prop: PropertyUpdateOperation.Set },
1
)
Expand All @@ -538,7 +542,7 @@ describe('DB', () => {
group_key: 'group_key',
group_properties: { prop: 'val' },
created_at: TIMESTAMP,
properties_last_updated_at: { prop: TIMESTAMP.toISO() },
properties_last_updated_at: { prop: ISO_TIMESTAMP },
properties_last_operation: { prop: PropertyUpdateOperation.Set },
version: 1,
})
Expand All @@ -551,7 +555,7 @@ describe('DB', () => {
'group_key',
{ prop: 'val' },
TIMESTAMP,
{ prop: TIMESTAMP.toISO() },
{ prop: ISO_TIMESTAMP },
{ prop: PropertyUpdateOperation.Set },
1
)
Expand All @@ -563,7 +567,7 @@ describe('DB', () => {
'group_key',
{ prop: 'newval' },
TIMESTAMP,
{ prop: TIMESTAMP.toISO() },
{ prop: ISO_TIMESTAMP },
{ prop: PropertyUpdateOperation.Set },
1
)
Expand All @@ -577,7 +581,7 @@ describe('DB', () => {
'group_key',
{ prop: 'val' },
TIMESTAMP,
{ prop: TIMESTAMP.toISO() },
{ prop: ISO_TIMESTAMP },
{ prop: PropertyUpdateOperation.Set },
1
)
Expand All @@ -591,7 +595,7 @@ describe('DB', () => {
'group_key',
{ prop: 'newVal', prop2: 2 },
TIMESTAMP,
{ prop: timestamp2.toISO(), prop2: timestamp2.toISO() },
{ prop: timestamp2.toISO()!, prop2: timestamp2.toISO()! },
{ prop: PropertyUpdateOperation.Set, prop2: PropertyUpdateOperation.Set },
2
)
Expand Down Expand Up @@ -681,8 +685,8 @@ describe('DB', () => {

describe('updateCohortsAndFeatureFlagsForMerge()', () => {
let team: Team
let sourcePersonID: Person['id']
let targetPersonID: Person['id']
let sourcePersonID: BasePerson['id']
let targetPersonID: BasePerson['id']

async function getAllHashKeyOverrides(): Promise<any> {
const result = await db.postgres.query(
Expand Down Expand Up @@ -1047,8 +1051,8 @@ describe('PostgresRouter()', () => {
return Promise.reject(new Error(errorMessage))
})

const router = new PostgresRouter(defaultConfig, null)
await expect(router.query(PostgresUse.COMMON_WRITE, 'SELECT 1;', null, 'testing')).rejects.toEqual(
const router = new PostgresRouter(defaultConfig)
await expect(router.query(PostgresUse.COMMON_WRITE, 'SELECT 1;', [], 'testing')).rejects.toEqual(
new DependencyUnavailableError(errorMessage, 'Postgres', new Error(errorMessage))
)
pgQueryMock.mockRestore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('eachMessageWebhooksHandlers', () => {
hub.EXTERNAL_REQUEST_TIMEOUT_MS
)
const groupTypeManager = new GroupTypeManager(hub.postgres, hub.teamManager)
groupTypeManager['groupTypesCache'].set(2, [
groupTypeManager['groupTypesCache'].set(2 as ProjectId, [
{
organization: 0,
},
Expand All @@ -121,6 +121,7 @@ describe('eachMessageWebhooksHandlers', () => {
is_calculating: false,
steps: [
{
// @ts-expect-error TODO revisit this, but fixing TS for now
id: 913,
action_id: 69,
tag_name: null,
Expand Down
15 changes: 11 additions & 4 deletions plugin-server/tests/main/ingestion-queues/each-batch.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PostgresRouter } from '~/src/utils/db/postgres'

import { buildStringMatcher } from '../../../src/config/config'
import { KAFKA_EVENTS_PLUGIN_INGESTION } from '../../../src/config/kafka-topics'
import {
Expand All @@ -16,6 +18,7 @@ import {
ClickHouseTimestampSecondPrecision,
ISOTimestamp,
PostIngestionEvent,
ProjectId,
RawKafkaEvent,
} from '../../../src/types'
import { ActionManager } from '../../../src/worker/ingestion/action-manager'
Expand Down Expand Up @@ -50,7 +53,7 @@ const event: PostIngestionEvent = {
eventUuid: 'uuid1',
distinctId: 'my_id',
teamId: 2,
projectId: 1,
projectId: 1 as ProjectId,
timestamp: '2020-02-23T02:15:00.000Z' as ISOTimestamp,
event: '$pageview',
properties: {},
Expand All @@ -60,6 +63,7 @@ const event: PostIngestionEvent = {
person_properties: {},
}

// @ts-expect-error TODO: Fix add `person_mode` to this
const kafkaEvent: RawKafkaEvent = {
event: '$pageview',
properties: JSON.stringify({
Expand All @@ -69,7 +73,7 @@ const kafkaEvent: RawKafkaEvent = {
elements_chain: '',
timestamp: '2020-02-23 02:15:00.00' as ClickHouseTimestamp,
team_id: 2,
project_id: 1,
project_id: 1 as ProjectId,
distinct_id: 'my_id',
created_at: '2020-02-23 02:15:00.00' as ClickHouseTimestamp,
person_id: 'F99FA0A1-E0C2-4CFE-A09A-4C3C4327A4CC',
Expand Down Expand Up @@ -186,7 +190,7 @@ describe('eachBatchX', () => {
} as unknown as GroupTypeManager
const organizatonManager: OrganizationManager = {
hasAvailableFeature: jest.fn(() => Promise.resolve(true)),
} as unknown as GroupTypeManager
} as unknown as OrganizationManager

const matchSpy = jest.spyOn(actionMatcher, 'match')
// mock hasWebhooks to return true
Expand All @@ -197,7 +201,10 @@ describe('eachBatchX', () => {
hookCannon,
10,
groupTypeManager,
organizatonManager
organizatonManager,

// @ts-expect-error this is not being used in the function, so just passing null here
null as PostgresRouter
)

// NOTE: really it would be nice to verify that fire has been called
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe.skip('IngestionConsumer', () => {
await resetTestDatabase()
await resetTestDatabaseClickhouse(extraServerConfig)
pluginServer = await startPluginsServer(extraServerConfig, makePiscina)
hub = pluginServer.hub
hub = pluginServer.hub!
stopServer = pluginServer.stop
posthog = createPosthog(hub, pluginConfig39)
})
Expand Down
Loading

0 comments on commit 94dcf1c

Please sign in to comment.