-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathauto-inject.test.ts
More file actions
196 lines (143 loc) · 6.99 KB
/
auto-inject.test.ts
File metadata and controls
196 lines (143 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { describe, expect, it } from 'vitest'
import { getAllProxyConfigs } from '../../src/first-party/proxy-configs'
import { applyAutoInject } from '../../src/first-party/setup'
import { normalizeRegistryConfig } from '../../src/normalize'
function makeRuntimeConfig(scripts: Record<string, any> = {}) {
return { public: { scripts } }
}
/**
* Apply auto-inject for all proxy configs that have autoInject defined,
* mimicking what finalizeFirstParty does (normalize → inject).
*/
function autoInjectAll(registry: any, rt: any, proxyPrefix: string) {
normalizeRegistryConfig(registry)
const configs = getAllProxyConfigs(proxyPrefix)
for (const [key, config] of Object.entries(configs)) {
if (config.autoInject && registry[key] !== undefined) {
applyAutoInject(registry, rt, proxyPrefix, key, config.autoInject)
}
}
}
describe('autoInject via proxy configs', () => {
describe('object entries', () => {
it('injects apiHost for posthog config object', () => {
const registry: any = { posthog: { apiKey: 'phc_test' } }
const rt = makeRuntimeConfig({ posthog: { apiKey: 'phc_test' } })
autoInjectAll(registry, rt, '/_proxy')
// After normalization, object entries become [input]
expect(registry.posthog[0].apiHost).toBe('/_proxy/ph')
expect(rt.public.scripts.posthog.apiHost).toBe('/_proxy/ph')
})
it('injects endpoint for plausible config object', () => {
const registry: any = { plausibleAnalytics: { domain: 'example.com' } }
const rt = makeRuntimeConfig({ plausibleAnalytics: { domain: 'example.com' } })
autoInjectAll(registry, rt, '/_proxy')
expect(registry.plausibleAnalytics[0].endpoint).toBe('/_proxy/plausible/api/event')
expect(rt.public.scripts.plausibleAnalytics.endpoint).toBe('/_proxy/plausible/api/event')
})
it('does not override existing config field', () => {
const registry: any = { posthog: { apiKey: 'phc_test', apiHost: 'https://custom.host' } }
const rt = makeRuntimeConfig({ posthog: { apiKey: 'phc_test', apiHost: 'https://custom.host' } })
autoInjectAll(registry, rt, '/_proxy')
expect(registry.posthog[0].apiHost).toBe('https://custom.host')
expect(rt.public.scripts.posthog.apiHost).toBe('https://custom.host')
})
it('uses EU prefix when posthog region is eu', () => {
const registry: any = { posthog: { apiKey: 'phc_test', region: 'eu' } }
const rt = makeRuntimeConfig({ posthog: { apiKey: 'phc_test', region: 'eu' } })
autoInjectAll(registry, rt, '/_proxy')
expect(registry.posthog[0].apiHost).toBe('/_proxy/ph-eu')
})
})
describe('array entries', () => {
it('injects into first element of array entry', () => {
const registry: any = { posthog: [{ apiKey: 'phc_test' }, { partytown: true }] }
const rt = makeRuntimeConfig({ posthog: { apiKey: 'phc_test' } })
autoInjectAll(registry, rt, '/_proxy')
expect(registry.posthog[0].apiHost).toBe('/_proxy/ph')
expect(rt.public.scripts.posthog.apiHost).toBe('/_proxy/ph')
})
it('skips empty array entries', () => {
const registry: any = { posthog: [] }
const rt = makeRuntimeConfig({ posthog: { apiKey: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.posthog.apiHost).toBeUndefined()
})
})
describe('empty object entries (env var driven)', () => {
it('injects into runtimeConfig for posthog: {}', () => {
const registry: any = { posthog: {} }
const rt = makeRuntimeConfig({ posthog: { apiKey: '' } })
autoInjectAll(registry, rt, '/_proxy')
// After normalization, {} becomes [{}] — both input and runtimeConfig get the value
expect(registry.posthog[0].apiHost).toBe('/_proxy/ph')
expect(rt.public.scripts.posthog.apiHost).toBe('/_proxy/ph')
})
it('uses EU prefix for posthog: {} when runtime region is eu', () => {
const registry: any = { posthog: {} }
const rt = makeRuntimeConfig({ posthog: { apiKey: '', region: 'eu' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.posthog.apiHost).toBe('/_proxy/ph-eu')
})
it('injects into runtimeConfig for plausibleAnalytics: {}', () => {
const registry: any = { plausibleAnalytics: {} }
const rt = makeRuntimeConfig({ plausibleAnalytics: { domain: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.plausibleAnalytics.endpoint).toBe('/_proxy/plausible/api/event')
})
it('injects into runtimeConfig for umamiAnalytics: {}', () => {
const registry: any = { umamiAnalytics: {} }
const rt = makeRuntimeConfig({ umamiAnalytics: { websiteId: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.umamiAnalytics.hostUrl).toBe('/_proxy/umami')
})
it('injects into runtimeConfig for rybbitAnalytics: {}', () => {
const registry: any = { rybbitAnalytics: {} }
const rt = makeRuntimeConfig({ rybbitAnalytics: { siteId: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.rybbitAnalytics.analyticsHost).toBe('/_proxy/rybbit/api')
})
it('injects into runtimeConfig for databuddyAnalytics: {}', () => {
const registry: any = { databuddyAnalytics: {} }
const rt = makeRuntimeConfig({ databuddyAnalytics: { clientId: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.databuddyAnalytics.apiUrl).toBe('/_proxy/databuddy-api')
})
})
describe('mock entries', () => {
it('injects into runtimeConfig for posthog: mock', () => {
const registry: any = { posthog: 'mock' }
const rt = makeRuntimeConfig({ posthog: { apiKey: '' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.posthog.apiHost).toBe('/_proxy/ph')
})
})
describe('skipped entries', () => {
it('skips scripts without autoInject config', () => {
const registry: any = { googleAnalytics: { id: 'G-TEST' } }
const rt = makeRuntimeConfig({ googleAnalytics: { id: 'G-TEST' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.googleAnalytics).toEqual({ id: 'G-TEST' })
})
it('skips falsy entries', () => {
const registry: any = { posthog: false }
const rt = makeRuntimeConfig()
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.posthog).toBeUndefined()
})
it('does not modify existing runtimeConfig for falsy entry', () => {
const registry: any = { posthog: false }
const rt = makeRuntimeConfig({ posthog: { existing: 'value' } })
autoInjectAll(registry, rt, '/_proxy')
expect(rt.public.scripts.posthog).toEqual({ existing: 'value' })
})
})
describe('custom proxyPrefix', () => {
it('uses custom prefix in computed values', () => {
const registry: any = { posthog: {} }
const rt = makeRuntimeConfig({ posthog: { apiKey: '' } })
autoInjectAll(registry, rt, '/_analytics')
expect(rt.public.scripts.posthog.apiHost).toBe('/_analytics/ph')
})
})
})