Files
insights/services/mcp/tests/unit/tool-schema-snapshots.test.ts
zeekay 10e6fec4c0 debrand: the brand token is INSIGHTS_, and a wrong one stops the container
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.
2026-08-01 17:42:28 -07:00

135 lines
5.2 KiB
TypeScript

import { mkdir, readdir, rm } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { format } from 'oxfmt'
import { describe, expect, it } from 'vitest'
import { z } from 'zod'
import { OAUTH_SCOPES_HIDDEN } from '@/lib/constants'
import { SessionManager } from '@/lib/SessionManager'
import { getToolsFromContext } from '@/tools'
import type { Context } from '@/tools/types'
function createMockContext(): Context {
return {
api: {} as any,
cache: {} as any,
env: {
MCP_APPS_BASE_URL: undefined,
INSIGHTS_ANALYTICS_API_KEY: undefined,
INSIGHTS_ANALYTICS_HOST: undefined,
INSIGHTS_API_BASE_URL: undefined,
INSIGHTS_PUBLIC_URL: undefined,
INSIGHTS_MCP_APPS_ANALYTICS_BASE_URL: undefined,
INSIGHTS_UI_APPS_TOKEN: undefined,
},
stateManager: {
// Staff-only tools require their OAuth-hidden scope explicitly on the key
// (`*` alone does not match) plus a staff user, so grant both here to keep
// their schemas in the snapshot surface.
getApiKey: async () => ({ scopes: ['*', ...OAUTH_SCOPES_HIDDEN] }),
getAiConsentGiven: async () => true,
getUser: async () => ({ is_staff: true }),
} as any,
sessionManager: new SessionManager({} as any),
getDistinctId: async () => 'test-distinct-id',
trackEvent: async () => {},
}
}
function deepSortKeys(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map((item) => deepSortKeys(item))
}
if (value && typeof value === 'object') {
const obj = value as Record<string, unknown>
const sortedEntries = Object.entries(obj)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, nestedValue]) => [key, deepSortKeys(nestedValue)] as const)
return Object.fromEntries(sortedEntries)
}
return value
}
async function listSnapshotFiles(root: string): Promise<string[]> {
try {
const files = await readdir(root)
return files.filter((file) => file.endsWith('.json')).map((file) => path.join(root, file))
} catch {
// Directory might not exist yet.
return []
}
}
function isSnapshotUpdateAll(): boolean {
// Vitest strips CLI flags from process.argv in worker threads, so we read
// the snapshot-update mode from vitest's internal state instead.
const state = expect.getState() as unknown as { snapshotState?: { _updateSnapshot?: string } }
return state.snapshotState?._updateSnapshot === 'all'
}
async function formatSnapshotJson(snapshotPath: string, schema: unknown): Promise<string> {
const content = `${JSON.stringify(schema, null, 4)}\n`
const result = await format(snapshotPath, content, { tabWidth: 4, printWidth: 120 })
if (result.errors.length > 0) {
const errorMessage = result.errors.map((error) => error.message ?? 'unknown formatting error').join('; ')
throw new Error(`Failed formatting snapshot ${snapshotPath}: ${errorMessage}`)
}
return result.code
}
describe('Tool schema snapshots', () => {
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const context = createMockContext()
it('snapshots runtime tool schemas', async () => {
const shouldUpdateSnapshots = isSnapshotUpdateAll()
const root = path.resolve(__dirname, '__snapshots__', 'tool-schemas')
// Enable flag-gated tools we snapshot here: tracing (APM spans), tasks, loops,
// dashboard-widgets. Other flag-gated tools (logs-alerts, visual-review, etc.) stay off to keep the surface stable.
// agent-feedback is always_available and no longer flag-gated, so it appears regardless.
const featureFlags = {
tracing: true,
tasks: true,
loops: true,
'dashboard-widgets': true,
}
const tools = [...(await getToolsFromContext(context, { featureFlags }))].sort((a, b) =>
a.name.localeCompare(b.name)
)
expect(tools.length).toBeGreaterThan(0)
await mkdir(root, { recursive: true })
const expectedPaths = new Set<string>()
for (const tool of tools) {
const jsonSchema = deepSortKeys(z.toJSONSchema(tool.schema, { io: 'input', reused: 'inline' }))
const snapshotPath = path.join(root, `${tool.name}.json`)
expectedPaths.add(snapshotPath)
const content = await formatSnapshotJson(snapshotPath, jsonSchema)
await expect(content).toMatchFileSnapshot(snapshotPath)
}
const existingPaths = await listSnapshotFiles(root)
const stalePaths = existingPaths.filter((existingPath) => !expectedPaths.has(existingPath)).sort()
if (shouldUpdateSnapshots) {
for (const stalePath of stalePaths) {
await rm(stalePath)
}
} else if (stalePaths.length > 0) {
throw new Error(
`Found stale snapshot files (run vitest -u to clean them):\n${stalePaths
.map((file) => `- ${file}`)
.join('\n')}`
)
}
})
})