mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The rule table mapped HOG_ -> FN_ ahead of any POSTHOG rule, so POSTHOG_FOO split into POST + HOG_ and came out POSTFN_FOO. Nothing sets that. The live pods set INSIGHTS_SKIP_MIGRATION_CHECKS, which bin/migrate-check reads on the container entrypoint, so the misspelling does not degrade a feature -- it runs migrate --check against a database whose history this tree does not share and the container never starts. Both replacements are measured against the shipped tree rather than chosen: every HOG_ name with a counterpart there is spelled INSIGHTS_ (HOG_QL -> INSIGHTS_QL, HOG_FUNCTION_TEMPLATES -> INSIGHTS_FUNCTION_TEMPLATES, 12 of 12 determinate cases), and FN_ appears in it nowhere. Upstream carries 95 HOG_ tokens and zero FN_ ones, so repairing FN_ back out is exactly the inverse of the bad rule and cannot collide with an upstream name. The rule is fixed for the next convergence, and the tree it already produced is repaired in the same commit -- the codemod is idempotent against its own output, so it cannot undo this itself.
77 lines
3.0 KiB
TypeScript
77 lines
3.0 KiB
TypeScript
import { createServer } from 'node:net'
|
|
import { unstable_dev, type Unstable_DevWorker } from 'wrangler'
|
|
|
|
import { startHonoHarness } from './hono'
|
|
import type { IntegrationEnv, IntegrationHarness } from './types'
|
|
|
|
// Bind a Node TCP server to port 0 to get a free port, then release it. There's
|
|
// a small race vs hard-coded ports, but it keeps the harness parallel-safe.
|
|
async function reserveFreePort(): Promise<number> {
|
|
return new Promise((resolve, reject) => {
|
|
const server = createServer()
|
|
server.unref()
|
|
server.once('error', reject)
|
|
server.listen(0, '127.0.0.1', () => {
|
|
const address = server.address()
|
|
if (typeof address === 'object' && address && 'port' in address) {
|
|
const port = address.port
|
|
server.close(() => resolve(port))
|
|
} else {
|
|
server.close(() => reject(new Error('Failed to read assigned port')))
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
// Boots the full proxy stack:
|
|
// 1. A Hono listener (with a real Redis on `TEST_REDIS_URL` / db `TEST_REDIS_DB`)
|
|
// pointed at `env.apiBaseUrl` (the local Insights stack).
|
|
// 2. A Cloudflare Worker via `wrangler unstable_dev` running this service's
|
|
// `src/index.ts`, with `MCP_HONO_URL` set to the Hono URL so every
|
|
// proxied request lands on the in-process Hono.
|
|
//
|
|
// Tests receive the Worker's URL — every assertion travels through the proxy
|
|
// → Hono → Insights backend chain end-to-end. Nothing is mocked.
|
|
export async function startCfProxyHarness(env: IntegrationEnv): Promise<IntegrationHarness & { honoUrl: URL }> {
|
|
const hono = await startHonoHarness(env)
|
|
const port = await reserveFreePort()
|
|
|
|
let worker: Unstable_DevWorker
|
|
try {
|
|
worker = await unstable_dev('src/index.ts', {
|
|
config: 'wrangler.jsonc',
|
|
local: true,
|
|
ip: '127.0.0.1',
|
|
port,
|
|
// `vars` are baked in at boot — workerd reads them via
|
|
// `cloudflare:workers`'s `env` export. `MCP_HONO_URL` collapses
|
|
// both per-region targets onto the local Hono. `INSIGHTS_API_BASE_URL`
|
|
// points the worker's token-probe path at the same backend Hono
|
|
// talks to.
|
|
vars: {
|
|
MCP_HONO_URL: hono.baseUrl.toString().replace(/\/$/, ''),
|
|
INSIGHTS_API_BASE_URL: env.apiBaseUrl,
|
|
INSIGHTS_ANALYTICS_API_KEY: '',
|
|
INSIGHTS_ANALYTICS_HOST: '',
|
|
},
|
|
experimental: {
|
|
disableExperimentalWarning: true,
|
|
disableDevRegistry: true,
|
|
},
|
|
logLevel: 'warn',
|
|
})
|
|
} catch (err) {
|
|
await hono.stop().catch(() => undefined)
|
|
throw err
|
|
}
|
|
|
|
return {
|
|
baseUrl: new URL(`http://${worker.address}:${worker.port}`),
|
|
honoUrl: hono.baseUrl,
|
|
stop: async () => {
|
|
await worker.stop().catch(() => undefined)
|
|
await hono.stop().catch(() => undefined)
|
|
},
|
|
}
|
|
}
|