2021-04-22 14:23:22 +02:00
|
|
|
import { ConsoleExtension } from '@posthog/plugin-scaffold'
|
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
import { Hub, PluginLogEntryType } from '../../src/types'
|
|
|
|
import { createHub } from '../../src/utils/db/hub'
|
2021-05-03 13:14:30 +02:00
|
|
|
import { getPluginConfigRows } from '../../src/utils/db/sql'
|
2021-04-22 14:23:22 +02:00
|
|
|
import { createConsole } from '../../src/worker/vm/extensions/console'
|
|
|
|
import { resetTestDatabase } from '../helpers/sql'
|
|
|
|
|
|
|
|
describe('console extension', () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
let hub: Hub
|
|
|
|
let closeHub: () => Promise<void>
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
2022-05-19 19:18:15 +02:00
|
|
|
;[hub, closeHub] = await createHub({ KAFKA_ENABLED: false })
|
2021-04-22 14:23:22 +02:00
|
|
|
await resetTestDatabase()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
await closeHub()
|
2021-04-22 14:23:22 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
Object.values(PluginLogEntryType).map((type) => {
|
|
|
|
const method = type.toLowerCase() as keyof ConsoleExtension
|
|
|
|
describe(`console#${method}`, () => {
|
|
|
|
it('leaves an empty entry in the database', async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginConfig = (await getPluginConfigRows(hub))[0]
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const console = createConsole(hub, pluginConfig)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-06-14 14:53:55 +02:00
|
|
|
await (console[method]() as unknown as Promise<void>)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginLogEntries = await hub.db.fetchPluginLogEntries()
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
expect(pluginLogEntries.length).toBe(1)
|
|
|
|
expect(pluginLogEntries[0].type).toEqual(type)
|
|
|
|
expect(pluginLogEntries[0].message).toEqual('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('leaves a string + number entry in the database', async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginConfig = (await getPluginConfigRows(hub))[0]
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const console = createConsole(hub, pluginConfig)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-06-14 14:53:55 +02:00
|
|
|
await (console[method]('number =', 987) as unknown as Promise<void>)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginLogEntries = await hub.db.fetchPluginLogEntries()
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
expect(pluginLogEntries.length).toBe(1)
|
|
|
|
expect(pluginLogEntries[0].type).toEqual(type)
|
|
|
|
expect(pluginLogEntries[0].message).toEqual('number = 987')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('leaves an error entry in the database', async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginConfig = (await getPluginConfigRows(hub))[0]
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const console = createConsole(hub, pluginConfig)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-06-14 14:53:55 +02:00
|
|
|
await (console[method](new Error('something')) as unknown as Promise<void>)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginLogEntries = await hub.db.fetchPluginLogEntries()
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
expect(pluginLogEntries.length).toBe(1)
|
|
|
|
expect(pluginLogEntries[0].type).toEqual(type)
|
|
|
|
expect(pluginLogEntries[0].message).toEqual('Error: something')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('leaves an object entry in the database', async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginConfig = (await getPluginConfigRows(hub))[0]
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const console = createConsole(hub, pluginConfig)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-06-14 14:53:55 +02:00
|
|
|
await (console[method]({ 1: 'ein', 2: 'zwei' }) as unknown as Promise<void>)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginLogEntries = await hub.db.fetchPluginLogEntries()
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
expect(pluginLogEntries.length).toBe(1)
|
|
|
|
expect(pluginLogEntries[0].type).toEqual(type)
|
|
|
|
expect(pluginLogEntries[0].message).toEqual(`{"1":"ein","2":"zwei"}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('leaves an object entry in the database', async () => {
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginConfig = (await getPluginConfigRows(hub))[0]
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const console = createConsole(hub, pluginConfig)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-06-14 14:53:55 +02:00
|
|
|
await (console[method]([99, 79]) as unknown as Promise<void>)
|
2021-04-22 14:23:22 +02:00
|
|
|
|
2021-05-25 13:49:41 +02:00
|
|
|
const pluginLogEntries = await hub.db.fetchPluginLogEntries()
|
2021-04-22 14:23:22 +02:00
|
|
|
|
|
|
|
expect(pluginLogEntries.length).toBe(1)
|
|
|
|
expect(pluginLogEntries[0].type).toEqual(type)
|
|
|
|
expect(pluginLogEntries[0].message).toEqual(`[99,79]`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|