mirror of
https://github.com/hanzoai/insights-js.git
synced 2026-10-30 18:34:19 +00:00
Bulk rename across 989 files: - Package names: posthog-js → @hanzo/insights, posthog-node → @hanzo/insights-node, etc. - Repository URLs: posthog/posthog-js → hanzoai/insights-js - Brand names: PostHog → Insights - File renames: 259 reference JSON files, source files, workflow files - Directory rename: eslint-plugin-posthog-js → eslint-plugin-insights Remaining: 8 refs to @posthog/rrweb-* (real npm deps, need fork+publish)
211 lines
7.0 KiB
TypeScript
211 lines
7.0 KiB
TypeScript
/* eslint-disable @hanzo/insights/no-direct-function-check, no-console, @typescript-eslint/no-unused-vars */
|
|
import { expect, test } from './utils/insights-playwright-test-base'
|
|
import { start } from './utils/setup'
|
|
|
|
test.describe('logs extension', () => {
|
|
test('should load logs extension when enabled in remote config', async ({ page, context }) => {
|
|
// Start Insights
|
|
await start(
|
|
{
|
|
options: {
|
|
api_host: 'https://localhost:1234',
|
|
debug: true,
|
|
},
|
|
},
|
|
page,
|
|
context
|
|
)
|
|
|
|
// Wait for Insights to initialize
|
|
await page.waitForTimeout(100)
|
|
|
|
// Check that Insights logs is available
|
|
const logsAvailable = await page.evaluate(() => {
|
|
const insights = (window as any).insights
|
|
return !!(insights && insights.logs)
|
|
})
|
|
|
|
expect(logsAvailable).toBe(true)
|
|
})
|
|
|
|
test('should call onRemoteConfig when logs are enabled', async ({ page, context }) => {
|
|
await start(
|
|
{
|
|
options: {
|
|
api_host: 'https://localhost:1234',
|
|
debug: true,
|
|
},
|
|
},
|
|
page,
|
|
context
|
|
)
|
|
|
|
// Wait for Insights to initialize
|
|
await page.waitForTimeout(100)
|
|
|
|
// Test that we can call onRemoteConfig with logs enabled
|
|
const result = await page.evaluate(() => {
|
|
const insights = (window as any).insights
|
|
let configCalled = false
|
|
|
|
if (insights && insights.logs && typeof insights.logs.onRemoteConfig === 'function') {
|
|
try {
|
|
insights.logs.onRemoteConfig({
|
|
logs: {
|
|
captureConsoleLogs: true,
|
|
},
|
|
})
|
|
configCalled = true
|
|
} catch (error) {
|
|
console.log('Error in onRemoteConfig:', error)
|
|
configCalled = false
|
|
}
|
|
}
|
|
|
|
return {
|
|
hasInsights: !!insights,
|
|
hasLogs: !!(insights && insights.logs),
|
|
hasOnRemoteConfig: !!(insights && insights.logs && typeof insights.logs.onRemoteConfig === 'function'),
|
|
configCalled: configCalled,
|
|
}
|
|
})
|
|
|
|
expect(result.hasInsights).toBe(true)
|
|
expect(result.hasLogs).toBe(true)
|
|
expect(result.hasOnRemoteConfig).toBe(true)
|
|
expect(result.configCalled).toBe(true)
|
|
})
|
|
|
|
test('should handle disabled logs in remote config', async ({ page, context }) => {
|
|
await start(
|
|
{
|
|
options: {
|
|
api_host: 'https://localhost:1234',
|
|
debug: true,
|
|
},
|
|
},
|
|
page,
|
|
context
|
|
)
|
|
|
|
// Wait for Insights to initialize
|
|
await page.waitForTimeout(100)
|
|
|
|
// Test that we can call onRemoteConfig with logs disabled
|
|
const result = await page.evaluate(() => {
|
|
const insights = (window as any).insights
|
|
let configCalled = false
|
|
|
|
if (insights && insights.logs && typeof insights.logs.onRemoteConfig === 'function') {
|
|
try {
|
|
insights.logs.onRemoteConfig({
|
|
logs: {
|
|
captureConsoleLogs: false,
|
|
},
|
|
})
|
|
configCalled = true
|
|
} catch (error) {
|
|
console.log('Error in onRemoteConfig:', error)
|
|
configCalled = false
|
|
}
|
|
}
|
|
|
|
return {
|
|
hasInsights: !!insights,
|
|
hasLogs: !!(insights && insights.logs),
|
|
configCalled: configCalled,
|
|
}
|
|
})
|
|
|
|
expect(result.hasInsights).toBe(true)
|
|
expect(result.hasLogs).toBe(true)
|
|
expect(result.configCalled).toBe(true)
|
|
})
|
|
|
|
test('should intercept console methods when logs extension is manually initialized', async ({ page, context }) => {
|
|
await start(
|
|
{
|
|
options: {
|
|
api_host: 'https://localhost:1234',
|
|
debug: true,
|
|
},
|
|
},
|
|
page,
|
|
context
|
|
)
|
|
|
|
// Wait for Insights to initialize
|
|
await page.waitForTimeout(100)
|
|
|
|
// Set up the logs extension and initialize it in the same context
|
|
const result = await page.evaluate(() => {
|
|
// Set up the logs extension directly
|
|
;(window as any).__InsightsExtensions__ = {
|
|
initializeLogs: (insights: any) => {
|
|
// Simple console interception
|
|
const originalConsole = {
|
|
log: console.log,
|
|
warn: console.warn,
|
|
error: console.error,
|
|
}
|
|
|
|
;(window as any).__intercepted_logs = []
|
|
|
|
console.log = (...args: any[]) => {
|
|
;(window as any).__intercepted_logs.push({
|
|
level: 'log',
|
|
args: args,
|
|
})
|
|
originalConsole.log.apply(console, args)
|
|
}
|
|
|
|
console.warn = (...args: any[]) => {
|
|
;(window as any).__intercepted_logs.push({
|
|
level: 'warn',
|
|
args: args,
|
|
})
|
|
originalConsole.warn.apply(console, args)
|
|
}
|
|
|
|
console.error = (...args: any[]) => {
|
|
;(window as any).__intercepted_logs.push({
|
|
level: 'error',
|
|
args: args,
|
|
})
|
|
originalConsole.error.apply(console, args)
|
|
}
|
|
},
|
|
}
|
|
|
|
// Initialize the logs extension
|
|
const insights = (window as any).insights
|
|
const extensions = (window as any).__InsightsExtensions__
|
|
if (extensions && extensions.initializeLogs && insights) {
|
|
extensions.initializeLogs(insights)
|
|
}
|
|
|
|
// Test console methods immediately after initialization
|
|
console.log('Test message 1')
|
|
console.warn('Warning message')
|
|
console.error('Error message')
|
|
|
|
// Return the intercepted logs
|
|
return (window as any).__intercepted_logs || []
|
|
})
|
|
|
|
expect(result).toHaveLength(3)
|
|
expect(result[0]).toMatchObject({
|
|
level: 'log',
|
|
args: ['Test message 1'],
|
|
})
|
|
expect(result[1]).toMatchObject({
|
|
level: 'warn',
|
|
args: ['Warning message'],
|
|
})
|
|
expect(result[2]).toMatchObject({
|
|
level: 'error',
|
|
args: ['Error message'],
|
|
})
|
|
})
|
|
})
|