mirror of
https://github.com/langgenius/dify.git
synced 2026-04-24 17:16:37 +08:00
Signed-off-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Signed-off-by: -LAN- <laipz8200@outlook.com> Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: majiayu000 <1835304752@qq.com> Co-authored-by: Poojan <poojan@infocusp.com> Co-authored-by: sahil-infocusp <73810410+sahil-infocusp@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Pandaaaa906 <ye.pandaaaa906@gmail.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: heyszt <270985384@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ijas <ijas.ahmd.ap@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: 木之本澪 <kinomotomiovo@gmail.com> Co-authored-by: KinomotoMio <200703522+KinomotoMio@users.noreply.github.com> Co-authored-by: 不做了睡大觉 <64798754+stakeswky@users.noreply.github.com> Co-authored-by: User <user@example.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: edvatar <88481784+toroleapinc@users.noreply.github.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Leilei <138381132+Inlei@users.noreply.github.com> Co-authored-by: HaKu <104669497+haku-ink@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: wangxiaolei <fatelei@gmail.com> Co-authored-by: Varun Chawla <34209028+veeceey@users.noreply.github.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: tda <95275462+tda1017@users.noreply.github.com> Co-authored-by: root <root@DESKTOP-KQLO90N> Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai> Co-authored-by: Niels Kaspers <153818647+nielskaspers@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: Tyson Cung <45380903+tysoncung@users.noreply.github.com> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: FFXN <31929997+FFXN@users.noreply.github.com> Co-authored-by: slegarraga <64795732+slegarraga@users.noreply.github.com> Co-authored-by: 99 <wh2099@pm.me> Co-authored-by: Br1an <932039080@qq.com> Co-authored-by: L1nSn0w <l1nsn0w@qq.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: akkoaya <151345394+akkoaya@users.noreply.github.com> Co-authored-by: 盐粒 Yanli <yanli@dify.ai> Co-authored-by: lif <1835304752@qq.com> Co-authored-by: weiguang li <codingpunk@gmail.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com> Co-authored-by: HanWenbo <124024253+hwb96@users.noreply.github.com> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: Stable Genius <stablegenius043@gmail.com> Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com> Co-authored-by: ふるい <46769295+Echo0ff@users.noreply.github.com> Co-authored-by: Xiyuan Chen <52963600+GareArc@users.noreply.github.com>
140 lines
4.8 KiB
TypeScript
140 lines
4.8 KiB
TypeScript
import * as amplitude from '@amplitude/analytics-browser'
|
|
import { sessionReplayPlugin } from '@amplitude/plugin-session-replay-browser'
|
|
import { render } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import AmplitudeProvider, { isAmplitudeEnabled } from './AmplitudeProvider'
|
|
|
|
const mockConfig = vi.hoisted(() => ({
|
|
AMPLITUDE_API_KEY: 'test-api-key',
|
|
IS_CLOUD_EDITION: true,
|
|
}))
|
|
|
|
vi.mock('@/config', () => mockConfig)
|
|
|
|
vi.mock('@amplitude/analytics-browser', () => ({
|
|
init: vi.fn(),
|
|
add: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@amplitude/plugin-session-replay-browser', () => ({
|
|
sessionReplayPlugin: vi.fn(() => ({ name: 'session-replay' })),
|
|
}))
|
|
|
|
describe('AmplitudeProvider', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockConfig.AMPLITUDE_API_KEY = 'test-api-key'
|
|
mockConfig.IS_CLOUD_EDITION = true
|
|
})
|
|
|
|
describe('isAmplitudeEnabled', () => {
|
|
it('returns true when cloud edition and api key present', () => {
|
|
expect(isAmplitudeEnabled()).toBe(true)
|
|
})
|
|
|
|
it('returns false when cloud edition but no api key', () => {
|
|
mockConfig.AMPLITUDE_API_KEY = ''
|
|
expect(isAmplitudeEnabled()).toBe(false)
|
|
})
|
|
|
|
it('returns false when not cloud edition', () => {
|
|
mockConfig.IS_CLOUD_EDITION = false
|
|
expect(isAmplitudeEnabled()).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('Component', () => {
|
|
it('initializes amplitude when enabled', () => {
|
|
render(<AmplitudeProvider sessionReplaySampleRate={0.8} />)
|
|
|
|
expect(amplitude.init).toHaveBeenCalledWith('test-api-key', expect.any(Object))
|
|
expect(sessionReplayPlugin).toHaveBeenCalledWith({ sampleRate: 0.8 })
|
|
expect(amplitude.add).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('does not initialize amplitude when disabled', () => {
|
|
mockConfig.AMPLITUDE_API_KEY = ''
|
|
render(<AmplitudeProvider />)
|
|
|
|
expect(amplitude.init).not.toHaveBeenCalled()
|
|
expect(amplitude.add).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('pageNameEnrichmentPlugin logic works as expected', async () => {
|
|
render(<AmplitudeProvider />)
|
|
const plugin = vi.mocked(amplitude.add).mock.calls[0]?.[0] as amplitude.Types.EnrichmentPlugin | undefined
|
|
expect(plugin).toBeDefined()
|
|
if (!plugin?.execute || !plugin.setup)
|
|
throw new Error('Expected page-name-enrichment plugin with setup/execute')
|
|
|
|
expect(plugin.name).toBe('page-name-enrichment')
|
|
|
|
const execute = plugin.execute
|
|
const setup = plugin.setup
|
|
type SetupFn = NonNullable<amplitude.Types.EnrichmentPlugin['setup']>
|
|
const getPageTitle = (evt: amplitude.Types.Event | null | undefined) =>
|
|
(evt?.event_properties as Record<string, unknown> | undefined)?.['[Amplitude] Page Title']
|
|
|
|
await setup(
|
|
{} as Parameters<SetupFn>[0],
|
|
{} as Parameters<SetupFn>[1],
|
|
)
|
|
|
|
const originalWindowLocation = window.location
|
|
try {
|
|
Object.defineProperty(window, 'location', {
|
|
value: { pathname: '/datasets' },
|
|
writable: true,
|
|
})
|
|
const event: amplitude.Types.Event = {
|
|
event_type: '[Amplitude] Page Viewed',
|
|
event_properties: {},
|
|
}
|
|
const result = await execute(event)
|
|
expect(getPageTitle(result)).toBe('Knowledge')
|
|
window.location.pathname = '/'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Home')
|
|
window.location.pathname = '/apps'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Studio')
|
|
window.location.pathname = '/explore'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Explore')
|
|
window.location.pathname = '/tools'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Tools')
|
|
window.location.pathname = '/account'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Account')
|
|
window.location.pathname = '/signin'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Sign In')
|
|
window.location.pathname = '/signup'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Sign Up')
|
|
window.location.pathname = '/unknown'
|
|
await execute(event)
|
|
expect(getPageTitle(event)).toBe('Unknown')
|
|
const otherEvent = {
|
|
event_type: 'Button Clicked',
|
|
event_properties: {},
|
|
} as amplitude.Types.Event
|
|
const otherResult = await execute(otherEvent)
|
|
expect(getPageTitle(otherResult)).toBeUndefined()
|
|
const noPropsEvent = {
|
|
event_type: '[Amplitude] Page Viewed',
|
|
} as amplitude.Types.Event
|
|
const noPropsResult = await execute(noPropsEvent)
|
|
expect(noPropsResult?.event_properties).toBeUndefined()
|
|
}
|
|
finally {
|
|
Object.defineProperty(window, 'location', {
|
|
value: originalWindowLocation,
|
|
writable: true,
|
|
})
|
|
}
|
|
})
|
|
})
|
|
})
|