Files
insights/services/mcp/tests/tools/cohorts.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

287 lines
11 KiB
TypeScript

import { afterEach, beforeAll, describe, expect, it } from 'vitest'
import {
type CreatedResources,
TEST_ORG_ID,
TEST_PROJECT_ID,
cleanupResources,
createTestClient,
createTestContext,
generateUniqueKey,
parseToolResponse,
setActiveProjectAndOrg,
validateEnvironmentVariables,
} from '@/shared/test-utils'
import { GENERATED_TOOLS } from '@/tools/generated/cohorts'
import type { Context } from '@/tools/types'
describe('Cohorts', { 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('cohorts-list tool', () => {
const listTool = GENERATED_TOOLS['cohorts-list']!()
const createTool = GENERATED_TOOLS['cohorts-create']!()
it('should list cohorts including a newly created one', async () => {
const name = `List Test ${generateUniqueKey('list')}`
const createResult = await createTool.handler(context, {
name,
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const result = await listTool.handler(context, {})
const response = parseToolResponse(result)
expect(Array.isArray(response.results)).toBe(true)
const found = response.results.find((c: { id: number }) => c.id === created.id)
expect(found).toBeTruthy()
expect(found.name).toBe(name)
expect(found._insightsUrl).toContain('/cohorts/')
})
it('should support pagination', async () => {
const result = await listTool.handler(context, { limit: 5, offset: 0 })
const response = parseToolResponse(result)
expect(Array.isArray(response.results)).toBe(true)
expect(response.results.length).toBeLessThanOrEqual(5)
})
})
describe('cohorts-create tool', () => {
const createTool = GENERATED_TOOLS['cohorts-create']!()
it('should create a dynamic cohort with filters', async () => {
const name = `Dynamic Cohort ${generateUniqueKey('dynamic')}`
const result = await createTool.handler(context, {
name,
description: 'Integration test dynamic cohort',
filters: {
properties: {
type: 'OR',
values: [
{
type: 'person',
key: '$browser',
value: 'Chrome',
operator: 'exact',
},
],
},
},
})
const cohort = parseToolResponse(result)
expect(cohort.id).toBeTruthy()
expect(cohort.name).toBe(name)
expect(cohort.description).toBe('Integration test dynamic cohort')
expect(cohort._insightsUrl).toContain('/cohorts/')
createdResources.cohorts.push(cohort.id)
})
it('should create a static cohort', async () => {
const name = `Static Cohort ${generateUniqueKey('static')}`
const result = await createTool.handler(context, {
name,
is_static: true,
})
const cohort = parseToolResponse(result)
expect(cohort.id).toBeTruthy()
expect(cohort.name).toBe(name)
expect(cohort.is_static).toBe(true)
expect(cohort._insightsUrl).toContain('/cohorts/')
createdResources.cohorts.push(cohort.id)
})
})
describe('cohorts-retrieve tool', () => {
const createTool = GENERATED_TOOLS['cohorts-create']!()
const retrieveTool = GENERATED_TOOLS['cohorts-retrieve']!()
it('should retrieve a specific cohort by ID', async () => {
const name = `Retrieve Test ${generateUniqueKey('retrieve')}`
const createResult = await createTool.handler(context, {
name,
description: 'For retrieve test',
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const result = await retrieveTool.handler(context, { id: created.id })
const cohort = parseToolResponse(result)
expect(cohort.id).toBe(created.id)
expect(cohort.name).toBe(name)
expect(cohort.description).toBe('For retrieve test')
expect(cohort._insightsUrl).toContain('/cohorts/')
})
})
describe('cohorts-partial-update tool', () => {
const createTool = GENERATED_TOOLS['cohorts-create']!()
const updateTool = GENERATED_TOOLS['cohorts-partial-update']!()
it('should update cohort name and description', async () => {
const createResult = await createTool.handler(context, {
name: `Original ${generateUniqueKey('orig')}`,
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const updatedName = `Updated ${generateUniqueKey('upd')}`
const result = await updateTool.handler(context, {
id: created.id,
name: updatedName,
description: 'Updated description',
})
const updated = parseToolResponse(result)
expect(updated.id).toBe(created.id)
expect(updated.name).toBe(updatedName)
expect(updated.description).toBe('Updated description')
})
it('should soft-delete a cohort', async () => {
const createResult = await createTool.handler(context, {
name: `Delete Test ${generateUniqueKey('delete')}`,
is_static: true,
})
const created = parseToolResponse(createResult)
const result = await updateTool.handler(context, {
id: created.id,
deleted: true,
})
const deleted = parseToolResponse(result)
expect(deleted.id).toBe(created.id)
const listTool = GENERATED_TOOLS['cohorts-list']!()
const listResult = await listTool.handler(context, {})
const listResponse = parseToolResponse(listResult)
expect(listResponse.results.some((c: { id: number }) => c.id === created.id)).toBe(false)
})
})
describe('static cohort person management', () => {
const createTool = GENERATED_TOOLS['cohorts-create']!()
const addPersonsTool = GENERATED_TOOLS['cohorts-add-persons-to-static-cohort-partial-update']!()
const removePersonTool = GENERATED_TOOLS['cohorts-rm-person-from-static-cohort-partial-update']!()
it('should reject adding persons to a dynamic cohort', async () => {
const createResult = await createTool.handler(context, {
name: `Dynamic Reject Test ${generateUniqueKey('dyn-reject')}`,
filters: {
properties: {
type: 'OR',
values: [{ type: 'person', key: '$browser', value: 'Chrome', operator: 'exact' }],
},
},
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const fakeUuid = '00000000-0000-4000-8000-000000000003'
await expect(
addPersonsTool.handler(context, {
id: created.id,
person_ids: [fakeUuid],
})
).rejects.toThrow()
})
it('should reject non-existent person UUIDs', async () => {
const createResult = await createTool.handler(context, {
name: `Add Persons Test ${generateUniqueKey('add')}`,
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const fakeUuid = '00000000-0000-4000-8000-000000000001'
await expect(
addPersonsTool.handler(context, {
id: created.id,
person_ids: [fakeUuid],
})
).rejects.toThrow('Validation error')
})
it('should reject removing a person UUID that does not exist in the team', async () => {
const createResult = await createTool.handler(context, {
name: `Remove Person Test ${generateUniqueKey('remove')}`,
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const fakeUuid = '00000000-0000-4000-8000-000000000002'
await expect(
removePersonTool.handler(context, {
id: created.id,
person_id: fakeUuid,
})
).rejects.toThrow()
})
})
describe('cohort workflow', () => {
it('should support create, list, retrieve, update workflow', async () => {
const createTool = GENERATED_TOOLS['cohorts-create']!()
const listTool = GENERATED_TOOLS['cohorts-list']!()
const retrieveTool = GENERATED_TOOLS['cohorts-retrieve']!()
const updateTool = GENERATED_TOOLS['cohorts-partial-update']!()
const name = `Workflow Test ${generateUniqueKey('workflow')}`
const createResult = await createTool.handler(context, {
name,
is_static: true,
})
const created = parseToolResponse(createResult)
createdResources.cohorts.push(created.id)
const listResult = await listTool.handler(context, {})
const listResponse = parseToolResponse(listResult)
expect(listResponse.results.some((c: { id: number }) => c.id === created.id)).toBe(true)
const retrieveResult = await retrieveTool.handler(context, { id: created.id })
const retrieved = parseToolResponse(retrieveResult)
expect(retrieved.name).toBe(name)
const updatedName = `Updated Workflow ${generateUniqueKey('upd-wf')}`
const updateResult = await updateTool.handler(context, {
id: created.id,
name: updatedName,
})
const updated = parseToolResponse(updateResult)
expect(updated.name).toBe(updatedName)
})
})
})