mirror of
https://github.com/hanzoai/insights-js.git
synced 2026-10-30 18:34:19 +00:00
We own the ingestion server (hanzoai/insights: Django + rust/capture + nodejs plugin) AND this SDK, so there is no external wire contract to preserve. The server is already 100% posthog-free; this SDK was the last laggard. - $last_posthog_reset -> $last_insights_reset. This FIXES A LIVE MISMATCH: the server taxonomy (insights/taxonomy/taxonomy.py, insights/insightsql/ai.py) already expected the insights form while the SDK emitted the posthog form. - __PosthogExtensions__ -> __InsightsExtensions__; all PostHog/posthog/POSTHOG identifiers -> Insights/insights/INSIGHTS (src, tests, playground, playwright, scripts, rrweb subpackages, package.json keywords). - Hosts: DEFAULT_PLUGIN_HOST + DEFAULT_NUXT_HOST + the core host default pointed at PostHog's cloud (us.i.posthog.com) — a real bug. All -> insights.hanzo.ai. - Removed the us-assets/eu-assets CDN split in getRemoteConfig (PostHog-specific; Hanzo serves remote config from the same host) — it had degenerated to a no-op. - Fixed generate-docs writing to a stray references/@hanzo/ subdir. - Renamed the 3 posthog-named test files; dropped 58MB of stale reference snapshots named for upstream posthog versions we never shipped (they are generated output — references/ is now gitignored). Verified: typecheck errors 42 (HEAD) -> 18 (all pre-existing test-mock looseness); types package 0; every package.json valid with @hanzo/* names. Guard: grep -rioE 'posthog' packages ... | wc -l => 0
84 lines
3.0 KiB
TypeScript
84 lines
3.0 KiB
TypeScript
// An MSW server that handles requests to https://insights.hanzo.ai/e/ and stores
|
|
|
|
import { ResponseComposition, rest } from 'msw'
|
|
import { setupServer } from 'msw/node'
|
|
import { RestContext } from 'msw'
|
|
import { RestRequest } from 'msw'
|
|
import { decompressSync, strFromU8 } from 'fflate'
|
|
|
|
// the request bodies in a store that we can inspect within tests.
|
|
const capturedRequests: { '/e/': any[]; '/engage/': any[]; '/flags/': any[] } = {
|
|
'/e/': [],
|
|
'/engage/': [],
|
|
'/flags/': [],
|
|
}
|
|
|
|
const handleRequest = (group: string) => (req: RestRequest, res: ResponseComposition, ctx: RestContext) => {
|
|
let body = req.body
|
|
|
|
if (typeof body === 'string') {
|
|
try {
|
|
const b64Encoded = req.url.href.includes('compression=base64')
|
|
const gzipCompressed = req.url.href.includes('compression=gzip-js')
|
|
if (b64Encoded) {
|
|
body = JSON.parse(Buffer.from(decodeURIComponent(body.split('=')[1]), 'base64').toString())
|
|
} else if (gzipCompressed) {
|
|
const data = new Uint8Array(req._body)
|
|
const decoded = strFromU8(decompressSync(data))
|
|
body = JSON.parse(decoded)
|
|
} else {
|
|
body = JSON.parse(decodeURIComponent(body.split('=')[1]))
|
|
}
|
|
} catch {
|
|
return res(ctx.status(500))
|
|
}
|
|
}
|
|
|
|
capturedRequests[group] = [...(capturedRequests[group] || []), body]
|
|
|
|
return res(ctx.json({}))
|
|
}
|
|
|
|
const server = setupServer(
|
|
rest.post('http://localhost/e/', (req, res, ctx) => {
|
|
return handleRequest('/e/')(req, res, ctx)
|
|
}),
|
|
rest.post('http://localhost/engage/', (req, res, ctx) => {
|
|
return handleRequest('/engage/')(req, res, ctx)
|
|
}),
|
|
rest.post('http://localhost/flags/', (req, res, ctx) => {
|
|
return handleRequest('/flags/')(req, res, ctx)
|
|
})
|
|
)
|
|
|
|
// Start the server before all the tests run, and close it afterwards.
|
|
beforeAll(() =>
|
|
server.listen({
|
|
onUnhandledRequest: 'error',
|
|
})
|
|
)
|
|
afterAll(() => server.close())
|
|
|
|
export const getRequests = (token: string) => {
|
|
// Filter the captured requests by the given token.
|
|
return {
|
|
'/e/': capturedRequests['/e/'].filter((request) => request.properties.token === token),
|
|
'/engage/': capturedRequests['/engage/'].filter((request) => request.properties.token === token),
|
|
'/flags/': capturedRequests['/flags/'].filter((request) => request.token === token),
|
|
}
|
|
}
|
|
|
|
export const resetRequests = (token: string) => {
|
|
Object.assign(capturedRequests, {
|
|
'/e/': (capturedRequests['/e/'] = capturedRequests['/e/'].filter(
|
|
(request) => request.properties.token !== token
|
|
)),
|
|
'/engage/': (capturedRequests['/engage/'] = capturedRequests['/engage/'].filter(
|
|
(request) => request.properties.token !== token
|
|
)),
|
|
'/flags/': (capturedRequests['/flags/'] = capturedRequests['/flags/'].filter(
|
|
(request) => request.token !== token
|
|
)),
|
|
})
|
|
}
|