Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

3.7 KB
Raw Blame
import { FeatureKeys } from 'constants/features';
import { ORG_PREFERENCES } from 'constants/orgPreferences';
import { IAppContext } from 'providers/App/types';
import {
	LicenseEvent,
	LicensePlatform,
	LicenseState,
	LicenseStatus,
} from 'types/api/licensesV3/getActive';
import { ROLES, USER_ROLES } from 'types/roles';

/**
 * Factory for the spies assigned to the callable members of `IAppContext`.
 * Jest passes `jest.fn`, Storybook passes `fn` from `storybook/test`; the
 * fixture itself stays free of any test-runner import so both can consume it.
 */
export type SpyFactory = () => (...args: unknown[]) => void;

const noopSpyFactory: SpyFactory = () => (): void => {};

export const defaultFeatureFlags = [
	{ name: FeatureKeys.SSO, active: true, usage: 0, usage_limit: -1, route: '' },
	{
		name: FeatureKeys.USE_SPAN_METRICS,
		active: false,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
	{
		name: FeatureKeys.GATEWAY,
		active: true,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
	{
		name: FeatureKeys.PREMIUM_SUPPORT,
		active: true,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
	{
		name: FeatureKeys.ANOMALY_DETECTION,
		active: true,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
	{
		name: FeatureKeys.ONBOARDING,
		active: true,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
	{
		name: FeatureKeys.CHAT_SUPPORT,
		active: true,
		usage: 0,
		usage_limit: -1,
		route: '',
	},
];

export function createAppContextMock(
	role: string,
	appContextOverrides?: Partial<IAppContext>,
	createSpy: SpyFactory = noopSpyFactory,
): IAppContext {
	return {
		activeLicense: {
			id: 'test-license-id',
			eventQueue: {
				createdAt: '0',
				event: LicenseEvent.NO_EVENT,
				scheduledAt: '0',
				status: '',
				updatedAt: '0',
			},
			state: LicenseState.ACTIVATED,
			status: LicenseStatus.VALID,
			platform: LicensePlatform.CLOUD,
			createdAt: '0',
			plan: {
				id: '0',
				createdAt: '0',
				description: '',
				isActive: true,
				name: '',
				updatedAt: '0',
			},
			freeUntil: '0',
			updatedAt: '0',
			validFrom: 0,
			validUntil: 0,
		},
		trialInfo: {
			trialStart: -1,
			trialEnd: -1,
			onTrial: false,
			workSpaceBlock: false,
			trialConvertedToSubscription: false,
			gracePeriodEnd: -1,
		},
		isFetchingActiveLicense: false,
		activeLicenseFetchError: null,
		changelog: null,
		user: {
			accessJwt: 'some-token',
			refreshJwt: 'some-refresh-token',
			id: 'some-user-id',
			email: '[email protected]',
			displayName: 'John Doe',
			createdAt: 1732544623,
			organization: 'Nightswatch',
			orgId: 'does-not-matter-id',
			role: role as ROLES,
		},
		org: [
			{
				createdAt: 0,
				id: 'does-not-matter-id',
				displayName: 'Pentagon',
			},
		],
		hasEditPermission: role === USER_ROLES.ADMIN || role === USER_ROLES.EDITOR,
		isFetchingUser: false,
		userFetchError: null,
		featureFlags: defaultFeatureFlags,
		isFetchingFeatureFlags: false,
		featureFlagsFetchError: null,
		hostsData: null,
		isFetchingHosts: false,
		hostsFetchError: null,
		orgPreferences: [
			{
				name: ORG_PREFERENCES.ORG_ONBOARDING,
				description: 'Organisation Onboarding',
				valueType: 'boolean',
				defaultValue: false,
				allowedValues: ['true', 'false'],
				allowedScopes: ['org'],
				value: false,
			},
		],
		userPreferences: [],
		updateUserPreferenceInContext: createSpy(),
		isFetchingOrgPreferences: false,
		isFetchingUserPreferences: false,
		orgPreferencesFetchError: null,
		isLoggedIn: true,
		isPreflightLoading: false,
		showChangelogModal: false,
		updateUser: createSpy(),
		updateOrg: createSpy(),
		updateOrgPreferences: createSpy(),
		activeLicenseRefetch: createSpy(),
		updateChangelog: createSpy(),
		toggleChangelogModal: createSpy(),
		versionData: {
			version: '1.0.0',
			ee: 'Y',
			setupCompleted: true,
		},

		...appContextOverrides,
	};
}