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.
104 lines
3.7 KiB
TypeScript
104 lines
3.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const mockIsFeatureEnabled = vi.fn()
|
|
const mockGetAllFlags = vi.fn()
|
|
// Mock the client `flags.ts` consumes, not insights-node directly: `getInsightsClient()`
|
|
// returns a `InsightsMCP` (a insights-node subclass) from the externalized
|
|
// `@hanzo/mcp-analytics`, whose internal insights-node import a `vi.mock('insights-node')`
|
|
// here would not intercept.
|
|
vi.mock('@/lib/insights/client', () => ({
|
|
getInsightsClient: () => ({
|
|
isFeatureEnabled: mockIsFeatureEnabled,
|
|
getAllFlags: mockGetAllFlags,
|
|
}),
|
|
}))
|
|
|
|
// Must import after vi.mock
|
|
import { evaluateFeatureFlags, isFeatureFlagEnabled } from '@/lib/insights/flags'
|
|
|
|
describe('isFeatureFlagEnabled', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('should return true when the flag is enabled', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(true)
|
|
|
|
const result = await isFeatureFlagEnabled('mcp-version-2', 'user-123')
|
|
expect(result).toBe(true)
|
|
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('mcp-version-2', 'user-123', undefined)
|
|
})
|
|
|
|
it('should return false when the flag is disabled', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(false)
|
|
|
|
const result = await isFeatureFlagEnabled('mcp-version-2', 'user-123')
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should return false when the flag returns undefined', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(undefined)
|
|
|
|
const result = await isFeatureFlagEnabled('mcp-version-2', 'user-123')
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should return false when the client throws', async () => {
|
|
mockIsFeatureEnabled.mockRejectedValue(new Error('network error'))
|
|
|
|
const result = await isFeatureFlagEnabled('mcp-version-2', 'user-123')
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should forward groups as options when provided', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(true)
|
|
|
|
await isFeatureFlagEnabled('notebooks-collaboration', 'user-123', { organization: 'org-abc' })
|
|
|
|
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('notebooks-collaboration', 'user-123', {
|
|
groups: { organization: 'org-abc' },
|
|
})
|
|
})
|
|
|
|
it('should omit the options arg when groups is an empty object', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(true)
|
|
|
|
await isFeatureFlagEnabled('flag-x', 'user-123', {})
|
|
|
|
expect(mockIsFeatureEnabled).toHaveBeenCalledWith('flag-x', 'user-123', undefined)
|
|
})
|
|
})
|
|
|
|
describe('evaluateFeatureFlags', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('should evaluate multiple flags via getAllFlags and forward groups', async () => {
|
|
mockGetAllFlags.mockResolvedValue({ 'flag-on': true, 'flag-off': false, unrelated: true })
|
|
|
|
const result = await evaluateFeatureFlags(['flag-on', 'flag-off'], 'user-123', { organization: 'org-abc' })
|
|
|
|
expect(result).toEqual({ 'flag-on': true, 'flag-off': false })
|
|
expect(mockGetAllFlags).toHaveBeenCalledWith('user-123', {
|
|
flagKeys: ['flag-on', 'flag-off'],
|
|
groups: { organization: 'org-abc' },
|
|
})
|
|
})
|
|
|
|
it('should omit groups when not provided', async () => {
|
|
mockGetAllFlags.mockResolvedValue({ 'flag-a': true })
|
|
|
|
await evaluateFeatureFlags(['flag-a'], 'user-123')
|
|
|
|
expect(mockGetAllFlags).toHaveBeenCalledWith('user-123', { flagKeys: ['flag-a'] })
|
|
})
|
|
|
|
it('should short-circuit when no flag keys are requested', async () => {
|
|
const result = await evaluateFeatureFlags([], 'user-123', { organization: 'org-abc' })
|
|
|
|
expect(result).toEqual({})
|
|
expect(mockGetAllFlags).not.toHaveBeenCalled()
|
|
})
|
|
})
|