Files
insights/services/mcp/tests/tools/logs.integration.test.ts
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

286 lines
10 KiB
TypeScript

import { afterEach, beforeAll, describe, expect, it } from 'vitest'
import {
type CreatedResources,
TEST_ORG_ID,
TEST_PROJECT_ID,
cleanupResources,
createTestClient,
createTestContext,
parseToolResponse,
setActiveProjectAndOrg,
validateEnvironmentVariables,
} from '@/shared/test-utils'
import { GENERATED_TOOLS } from '@/tools/generated/logs'
import type { Context } from '@/tools/types'
describe('Logs', { concurrent: false }, () => {
let context: Context
const createdResources: CreatedResources = {
featureFlags: [],
insights: [],
dashboards: [],
surveys: [],
actions: [],
cohorts: [],
}
beforeAll(async () => {
validateEnvironmentVariables()
const client = createTestClient()
context = createTestContext(client)
await setActiveProjectAndOrg(context, TEST_PROJECT_ID!, TEST_ORG_ID!)
})
afterEach(async () => {
await cleanupResources(context.api, TEST_PROJECT_ID!, createdResources)
})
describe('query-logs tool', () => {
const queryTool = GENERATED_TOOLS['query-logs']!()
it('should query logs with date range', async () => {
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const result = await queryTool.handler(context, {
query: { dateRange: { date_from: dateFrom, date_to: dateTo } },
})
const logsData = parseToolResponse(result)
expect(logsData).toHaveProperty('results')
expect(Array.isArray(logsData.results)).toBe(true)
})
it('should query logs with severity filter', async () => {
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const result = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
severityLevels: ['error', 'warn'],
},
})
const logsData = parseToolResponse(result)
expect(logsData).toHaveProperty('results')
expect(Array.isArray(logsData.results)).toBe(true)
})
it('should query logs with limit', async () => {
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const result = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
limit: 10,
},
})
const logsData = parseToolResponse(result)
expect(logsData).toHaveProperty('results')
expect(Array.isArray(logsData.results)).toBe(true)
expect(logsData.results.length).toBeLessThanOrEqual(10)
})
it('should filter logs matching a known value', async () => {
const dateFrom = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const seed = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
limit: 1,
},
})
const seedData = parseToolResponse(seed)
if (seedData.results.length === 0) {
return
}
const snippet = seedData.results[0].body.slice(0, 20)
const result = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
filterGroup: [{ key: 'message', operator: 'icontains', type: 'log', value: snippet }],
},
})
const logsData = parseToolResponse(result)
expect(logsData.results.length).toBeGreaterThan(0)
for (const log of logsData.results) {
expect(log.body.toLowerCase()).toContain(snippet.toLowerCase())
}
})
it('should return empty results for a non-matching filter', async () => {
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const result = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
filterGroup: [
{
key: 'message',
operator: 'exact',
type: 'log',
value: ['IMPOSSIBLE_f47ac10b-58cc-4372-a567-0e02b2c3d479'],
},
],
},
})
const logsData = parseToolResponse(result)
expect(logsData.results).toHaveLength(0)
})
it('should query logs with ordering', async () => {
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const result = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
orderBy: 'earliest',
},
})
const logsData = parseToolResponse(result)
expect(logsData).toHaveProperty('results')
expect(Array.isArray(logsData.results)).toBe(true)
})
})
describe('logs-attributes-list tool', () => {
const attributesTool = GENERATED_TOOLS['logs-attributes-list']!()
it('should list log attributes', async () => {
const result = await attributesTool.handler(context, {})
const attributesData = parseToolResponse(result)
expect(attributesData).toHaveProperty('results')
expect(attributesData).toHaveProperty('count')
expect(Array.isArray(attributesData.results)).toBe(true)
})
it('should list log attributes with search', async () => {
const result = await attributesTool.handler(context, {
search: 'service',
})
const attributesData = parseToolResponse(result)
expect(attributesData).toHaveProperty('results')
expect(Array.isArray(attributesData.results)).toBe(true)
})
it('should list resource attributes', async () => {
const result = await attributesTool.handler(context, {
attribute_type: 'resource',
})
const attributesData = parseToolResponse(result)
expect(attributesData).toHaveProperty('results')
expect(Array.isArray(attributesData.results)).toBe(true)
})
it('should support pagination', async () => {
const result = await attributesTool.handler(context, {
limit: 5,
offset: 0,
})
const attributesData = parseToolResponse(result)
expect(attributesData).toHaveProperty('results')
expect(attributesData).toHaveProperty('count')
expect(Array.isArray(attributesData.results)).toBe(true)
expect(attributesData.results.length).toBeLessThanOrEqual(5)
})
})
describe('logs-attribute-values-list tool', () => {
const valuesTool = GENERATED_TOOLS['logs-attribute-values-list']!()
it('should list attribute values for a key', async () => {
const result = await valuesTool.handler(context, {
key: 'service.name',
})
const valuesData = parseToolResponse(result)
expect(valuesData).toHaveProperty('results')
})
it('should list attribute values with search', async () => {
const result = await valuesTool.handler(context, {
key: 'level',
value: 'error',
})
const valuesData = parseToolResponse(result)
expect(valuesData).toHaveProperty('results')
})
it('should list resource attribute values', async () => {
const result = await valuesTool.handler(context, {
key: 'k8s.container.name',
attribute_type: 'resource',
})
const valuesData = parseToolResponse(result)
expect(valuesData).toHaveProperty('results')
})
})
describe('logs-facet-values-create tool', () => {
const facetTool = GENERATED_TOOLS['logs-facet-values-create']!()
it.each([
{ name: 'column facet', query: { facetField: 'service_name' as const } },
{ name: 'resource-attribute facet', query: { facetResourceAttribute: 'k8s.namespace.name' } },
])('should return per-value counts for a $name', async ({ query }) => {
const result = await facetTool.handler(context, {
query: { ...query, dateRange: { date_from: '-24h' } },
})
const facetData = parseToolResponse(result)
expect(facetData).toHaveProperty('results')
expect(Array.isArray(facetData.results)).toBe(true)
for (const facet of facetData.results) {
expect(facet.value).not.toBeUndefined()
expect(typeof facet.count).toBe('number')
}
})
})
describe('Logs workflow', () => {
it('should support attribute discovery and query workflow', async () => {
const attributesTool = GENERATED_TOOLS['logs-attributes-list']!()
const queryTool = GENERATED_TOOLS['query-logs']!()
const attributesResult = await attributesTool.handler(context, {})
const attributesData = parseToolResponse(attributesResult)
expect(attributesData).toHaveProperty('results')
expect(Array.isArray(attributesData.results)).toBe(true)
const dateFrom = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()
const dateTo = new Date().toISOString()
const queryResult = await queryTool.handler(context, {
query: {
dateRange: { date_from: dateFrom, date_to: dateTo },
limit: 10,
},
})
const queryData = parseToolResponse(queryResult)
expect(queryData).toHaveProperty('results')
expect(Array.isArray(queryData.results)).toBe(true)
})
})
})