mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
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.
403 lines
16 KiB
TypeScript
403 lines
16 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 featureFlagGetDefinitionByKey from '@/tools/featureFlags/getDefinitionByKey'
|
|
import { GENERATED_TOOLS } from '@/tools/generated/feature_flags'
|
|
import { GENERATED_TOOLS as PERSONS_GENERATED_TOOLS } from '@/tools/generated/persons'
|
|
import type { Context } from '@/tools/types'
|
|
|
|
describe('Feature flags', { 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('create-feature-flag tool', () => {
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
|
|
it('should create a feature flag with minimal required fields', async () => {
|
|
const params = {
|
|
name: 'Test feature flag',
|
|
key: generateUniqueKey('test-flag'),
|
|
filters: {
|
|
groups: [
|
|
{
|
|
properties: [],
|
|
rollout_percentage: 100,
|
|
},
|
|
],
|
|
},
|
|
active: true,
|
|
}
|
|
|
|
const result = await createTool.handler(context, params)
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.id).toBeTruthy()
|
|
expect(flagData.key).toBe(params.key)
|
|
expect(flagData.name).toBe(params.name)
|
|
expect(flagData.active).toBe(params.active)
|
|
expect(flagData._insightsUrl).toContain('/feature_flags/')
|
|
expect(flagData.updated_at).toBeTruthy()
|
|
|
|
createdResources.featureFlags.push(flagData.id)
|
|
})
|
|
|
|
it('should create a feature flag with tags and evaluation tags', async () => {
|
|
const params = {
|
|
name: 'Tagged feature flag',
|
|
key: generateUniqueKey('tagged-flag'),
|
|
filters: {
|
|
groups: [
|
|
{
|
|
properties: [],
|
|
rollout_percentage: 100,
|
|
},
|
|
],
|
|
},
|
|
active: true,
|
|
tags: ['test', 'integration'],
|
|
evaluation_tags: ['test'],
|
|
}
|
|
|
|
const result = await createTool.handler(context, params)
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.id).toBeTruthy()
|
|
expect(flagData.key).toBe(params.key)
|
|
expect(flagData.name).toBe(params.name)
|
|
|
|
createdResources.featureFlags.push(flagData.id)
|
|
})
|
|
|
|
it('should create a multivariate feature flag', async () => {
|
|
const params = {
|
|
name: 'Multivariate flag',
|
|
key: generateUniqueKey('multi-flag'),
|
|
active: true,
|
|
filters: {
|
|
groups: [
|
|
{
|
|
properties: [],
|
|
rollout_percentage: 100,
|
|
},
|
|
],
|
|
multivariate: {
|
|
variants: [
|
|
{ key: 'control', rollout_percentage: 50 },
|
|
{ key: 'test', rollout_percentage: 50 },
|
|
],
|
|
},
|
|
},
|
|
}
|
|
|
|
const result = await createTool.handler(context, params)
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.id).toBeTruthy()
|
|
expect(flagData.filters.multivariate.variants).toHaveLength(2)
|
|
|
|
createdResources.featureFlags.push(flagData.id)
|
|
})
|
|
})
|
|
|
|
describe('feature-flag-get-all tool', () => {
|
|
const getAllTool = GENERATED_TOOLS['feature-flag-get-all']!()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
const getTool = GENERATED_TOOLS['feature-flag-get-definition']!()
|
|
|
|
it('should list feature flags', async () => {
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'List test flag',
|
|
key: generateUniqueKey('list-test'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const result = await getAllTool.handler(context, { limit: 10, offset: 0 })
|
|
const response = parseToolResponse(result)
|
|
|
|
expect(Array.isArray(response.results)).toBe(true)
|
|
expect(response.results.some((flag: { id: number }) => flag.id === createdFlag.id)).toBe(true)
|
|
expect(response._insightsUrl).toContain('/feature_flags')
|
|
})
|
|
|
|
it('should support search by key and use returned ID for retrieval', async () => {
|
|
const uniqueKey = generateUniqueKey('search-key')
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Search test flag',
|
|
key: uniqueKey,
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const searchResult = await getAllTool.handler(context, { search: uniqueKey, limit: 20, offset: 0 })
|
|
const listResponse = parseToolResponse(searchResult)
|
|
const found = listResponse.results.find((flag: { id: number; key: string }) => flag.key === uniqueKey)
|
|
|
|
expect(found).toBeTruthy()
|
|
if (!found) {
|
|
throw new Error(`Expected to find feature flag with key ${uniqueKey}`)
|
|
}
|
|
expect(found.id).toBe(createdFlag.id)
|
|
|
|
const getResult = await getTool.handler(context, { id: found.id })
|
|
const flagData = parseToolResponse(getResult)
|
|
expect(flagData.id).toBe(createdFlag.id)
|
|
expect(flagData.key).toBe(uniqueKey)
|
|
})
|
|
})
|
|
|
|
describe('feature-flag-get-definition tool', () => {
|
|
const getTool = GENERATED_TOOLS['feature-flag-get-definition']!()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
|
|
it('should get a feature flag by ID', async () => {
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Definition test flag',
|
|
key: generateUniqueKey('definition-test'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const result = await getTool.handler(context, { id: createdFlag.id })
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.id).toBe(createdFlag.id)
|
|
expect(flagData.key).toBe(createdFlag.key)
|
|
expect(flagData._insightsUrl).toContain('/feature_flags/')
|
|
})
|
|
})
|
|
|
|
describe('feature-flag-get-definition-by-key tool', () => {
|
|
const getByKeyTool = featureFlagGetDefinitionByKey()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
|
|
it('should get a feature flag by its exact key', async () => {
|
|
const uniqueKey = generateUniqueKey('by-key-test')
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'By-key test flag',
|
|
key: uniqueKey,
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const result = await getByKeyTool.handler(context, { key: uniqueKey })
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.id).toBe(createdFlag.id)
|
|
expect(flagData.key).toBe(uniqueKey)
|
|
expect(flagData._insightsUrl).toContain('/feature_flags/')
|
|
expect(flagData.found).toBe(true)
|
|
})
|
|
|
|
it('should return a non-error found:false result for a flag that does not exist', async () => {
|
|
const missingKey = generateUniqueKey('does-not-exist')
|
|
|
|
const result = await getByKeyTool.handler(context, { key: missingKey })
|
|
const flagData = parseToolResponse(result)
|
|
|
|
expect(flagData.found).toBe(false)
|
|
expect(flagData.key).toBe(missingKey)
|
|
})
|
|
})
|
|
|
|
describe('update-feature-flag tool', () => {
|
|
const updateTool = GENERATED_TOOLS['update-feature-flag']!()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
|
|
it('should update a feature flag by ID', async () => {
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Original name',
|
|
key: generateUniqueKey('update-test'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const updateResult = await updateTool.handler(context, {
|
|
id: createdFlag.id,
|
|
name: 'Updated name',
|
|
active: false,
|
|
})
|
|
const updatedFlag = parseToolResponse(updateResult)
|
|
|
|
expect(updatedFlag.id).toBe(createdFlag.id)
|
|
expect(updatedFlag.name).toBe('Updated name')
|
|
expect(updatedFlag.active).toBe(false)
|
|
expect(updatedFlag._insightsUrl).toContain('/feature_flags/')
|
|
})
|
|
|
|
it('should update feature flag filters', async () => {
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Filter update flag',
|
|
key: generateUniqueKey('filter-update'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const updateResult = await updateTool.handler(context, {
|
|
id: createdFlag.id,
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 50 }],
|
|
},
|
|
})
|
|
const updatedFlag = parseToolResponse(updateResult)
|
|
|
|
expect(updatedFlag.id).toBe(createdFlag.id)
|
|
})
|
|
})
|
|
|
|
describe('delete-feature-flag tool', () => {
|
|
const deleteTool = GENERATED_TOOLS['delete-feature-flag']!()
|
|
const getAllTool = GENERATED_TOOLS['feature-flag-get-all']!()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
|
|
it('should delete a feature flag by ID', async () => {
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Delete test flag',
|
|
key: generateUniqueKey('delete-test'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
|
|
await deleteTool.handler(context, { id: createdFlag.id })
|
|
|
|
const listResult = await getAllTool.handler(context, { limit: 100, offset: 0 })
|
|
const listResponse = parseToolResponse(listResult)
|
|
expect(listResponse.results.some((flag: { id: number }) => flag.id === createdFlag.id)).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('feature-flags-test-evaluation-create tool', () => {
|
|
const testEvaluationTool = GENERATED_TOOLS['feature-flags-test-evaluation-create']!()
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
const personsListTool = PERSONS_GENERATED_TOOLS['persons-list']!()
|
|
|
|
it('should evaluate a feature flag for a real distinct_id', async () => {
|
|
const personsResult = await personsListTool.handler(context, { limit: 100 })
|
|
const personsResponse = parseToolResponse(personsResult)
|
|
const personWithDistinctId = personsResponse.results.find(
|
|
(p: { distinct_ids?: string[] }) => p.distinct_ids && p.distinct_ids.length > 0
|
|
)
|
|
|
|
if (!personWithDistinctId) {
|
|
console.warn('No persons with distinct_ids found in project, skipping test')
|
|
return
|
|
}
|
|
|
|
const distinctId: string = personWithDistinctId.distinct_ids[0]
|
|
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Test evaluation flag',
|
|
key: generateUniqueKey('test-eval'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: true,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
createdResources.featureFlags.push(createdFlag.id)
|
|
|
|
const result = await testEvaluationTool.handler(context, {
|
|
id: createdFlag.id,
|
|
distinct_id: distinctId,
|
|
})
|
|
const evaluation = parseToolResponse(result)
|
|
|
|
expect(evaluation.flag_key).toBe(createdFlag.key)
|
|
expect(evaluation.result).toBe(true)
|
|
expect(typeof evaluation.reason).toBe('string')
|
|
expect(Array.isArray(evaluation.conditions)).toBe(true)
|
|
expect(evaluation.person_properties).toBeTruthy()
|
|
})
|
|
})
|
|
|
|
describe('Feature flag workflow', () => {
|
|
it('should support full CRUD workflow', async () => {
|
|
const createTool = GENERATED_TOOLS['create-feature-flag']!()
|
|
const updateTool = GENERATED_TOOLS['update-feature-flag']!()
|
|
const getTool = GENERATED_TOOLS['feature-flag-get-definition']!()
|
|
const deleteTool = GENERATED_TOOLS['delete-feature-flag']!()
|
|
|
|
const createResult = await createTool.handler(context, {
|
|
name: 'Workflow test flag',
|
|
key: generateUniqueKey('workflow-test'),
|
|
filters: {
|
|
groups: [{ properties: [], rollout_percentage: 100 }],
|
|
},
|
|
active: false,
|
|
})
|
|
const createdFlag = parseToolResponse(createResult)
|
|
|
|
const getResult = await getTool.handler(context, { id: createdFlag.id })
|
|
const retrievedFlag = parseToolResponse(getResult)
|
|
expect(retrievedFlag.id).toBe(createdFlag.id)
|
|
|
|
const updateResult = await updateTool.handler(context, {
|
|
id: createdFlag.id,
|
|
active: true,
|
|
name: 'Updated workflow flag',
|
|
})
|
|
const updatedFlag = parseToolResponse(updateResult)
|
|
expect(updatedFlag.active).toBe(true)
|
|
expect(updatedFlag.name).toBe('Updated workflow flag')
|
|
|
|
await deleteTool.handler(context, { id: createdFlag.id })
|
|
})
|
|
})
|
|
})
|