2021-04-22 14:23:22 +02:00
|
|
|
import { ConsoleExtension } from '@posthog/plugin-scaffold'
|
|
|
|
|
2022-05-30 12:09:13 +02:00
|
|
|
import { KAFKA_PLUGIN_LOG_ENTRIES } from '../../src/config/kafka-topics'
|
|
|
|
import { Hub, PluginLogEntrySource, PluginLogEntryType } from '../../src/types'
|
2024-09-03 11:00:50 +02:00
|
|
|
import { closeHub, createHub } from '../../src/utils/db/hub'
|
2021-04-22 14:23:22 +02:00
|
|
|
import { createConsole } from '../../src/worker/vm/extensions/console'
|
2022-05-30 12:09:13 +02:00
|
|
|
import { pluginConfig39 } from '../../tests/helpers/plugins'
|
|
|
|
|
|
|
|
jest.setTimeout(60000) // 60 sec timeout
|
|
|
|
jest.mock('../../src/utils/status')
|
|
|
|
jest.mock('../../src/utils/db/kafka-producer-wrapper')
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
describe('console extension', () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
let hub: Hub
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2022-05-30 12:09:13 +02:00
|
|
|
beforeAll(async () => {
|
2024-09-03 11:00:50 +02:00
|
|
|
hub = await createHub()
|
2021-04-22 14:23:22 +02:00
|
|
|
})
|
|
|
|
|
2022-05-30 12:09:13 +02:00
|
|
|
afterAll(async () => {
|
2024-09-03 11:00:50 +02:00
|
|
|
await closeHub(hub)
|
2021-04-22 14:23:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Object.values(PluginLogEntryType).map((type) => {
|
2022-05-30 12:09:13 +02:00
|
|
|
const typeMethod = type.toLowerCase() as keyof ConsoleExtension
|
|
|
|
|
|
|
|
describe(`console#${typeMethod}`, () => {
|
|
|
|
const testCases: [string, any[], string][] = [
|
|
|
|
['empty', [], ''],
|
|
|
|
['string + number', ['number =', 987], 'number = 987'],
|
|
|
|
['Error', [new Error('something')], 'Error: something'],
|
|
|
|
['object', [{ 1: 'ein', 2: 'zwei' }], `{"1":"ein","2":"zwei"}`],
|
|
|
|
['array', [[99, 79]], `[99,79]`],
|
|
|
|
]
|
|
|
|
|
|
|
|
testCases.forEach(([description, args, expectedFinalMessage]) => {
|
|
|
|
it(`leaves a well-formed ${description} entry in the database`, async () => {
|
|
|
|
const queueSingleJsonMessageSpy = jest.spyOn(hub.kafkaProducer, 'queueSingleJsonMessage')
|
|
|
|
const console = createConsole(hub, pluginConfig39)
|
|
|
|
|
|
|
|
await (console[typeMethod](...args) as unknown as Promise<void>)
|
|
|
|
|
|
|
|
expect(queueSingleJsonMessageSpy).toHaveBeenCalledTimes(1)
|
2024-03-25 14:01:15 +01:00
|
|
|
expect(queueSingleJsonMessageSpy).toHaveBeenCalledWith({
|
|
|
|
topic: KAFKA_PLUGIN_LOG_ENTRIES,
|
|
|
|
key: expect.any(String),
|
|
|
|
object: {
|
2022-05-30 12:09:13 +02:00
|
|
|
source: PluginLogEntrySource.Console,
|
|
|
|
type,
|
|
|
|
id: expect.any(String),
|
|
|
|
team_id: pluginConfig39.team_id,
|
|
|
|
plugin_id: pluginConfig39.plugin_id,
|
|
|
|
plugin_config_id: pluginConfig39.id,
|
|
|
|
timestamp: expect.any(String),
|
|
|
|
message: expectedFinalMessage,
|
|
|
|
instance_id: hub.instanceId.toString(),
|
2023-05-17 12:21:26 +02:00
|
|
|
},
|
2024-03-25 14:01:15 +01:00
|
|
|
waitForAck: false,
|
|
|
|
})
|
2022-05-30 12:09:13 +02:00
|
|
|
})
|
2021-04-22 14:23:22 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|