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.

2.7 KB
Raw Blame
import {
	DEFAULT_DASHBOARD_ICON_PATH,
	isValidDashboardImage,
	resolveDashboardImage,
	SYSTEM_ICON_PATHS,
} from './dashboardIcons';

// The glob-backed icon URL map is Vite-only; jest redirects it to a stub via
// moduleNameMapper (see jest.config.ts), so path lookups return a stable value
// and the resolver's fallback is deterministic under test.
describe('resolveDashboardImage', () => {
	const fallback = resolveDashboardImage(undefined);

	it('passes through valid base64 image data URIs unchanged', () => {
		const svg = 'data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=';
		const png = 'data:image/png;base64,iVBORw0KGgo=';
		expect(resolveDashboardImage(svg)).toBe(svg);
		expect(resolveDashboardImage(png)).toBe(png);
	});

	it.each([
		['non-image data URI', 'data:text/html;base64,PHNjcmlwdD4='],
		['javascript URI', 'javascript:alert(1)'],
		['remote URL', 'http://evil.com/x.svg'],
		['raw markup', '<svg onload=alert(1)>'],
		['empty string', ''],
		['unknown icon path', '/assets/Icons/does-not-exist'],
		['unknown logo path', '/assets/Logos/does-not-exist'],
	])('falls back for %s', (_label, value) => {
		const resolved = resolveDashboardImage(value);
		expect(resolved).not.toBe(value);
		expect(resolved).toBe(fallback);
	});

	it('resolves a known system icon path to a truthy src', () => {
		expect(resolveDashboardImage('/assets/Icons/eight-ball')).toBeTruthy();
	});

	it('resolves a known logo path to a truthy src', () => {
		expect(resolveDashboardImage('/assets/Logos/aws-dark')).toBe('mock-logo-url');
	});
});

describe('system icon catalogue', () => {
	it('exposes 20 icons, all under the /assets/Icons/ prefix', () => {
		expect(SYSTEM_ICON_PATHS).toHaveLength(20);
		expect(SYSTEM_ICON_PATHS.every((p) => p.startsWith('/assets/Icons/'))).toBe(
			true,
		);
	});

	it('defaults to the eight-ball icon', () => {
		expect(DEFAULT_DASHBOARD_ICON_PATH).toBe('/assets/Icons/eight-ball');
		expect(SYSTEM_ICON_PATHS).toContain(DEFAULT_DASHBOARD_ICON_PATH);
	});
});

describe('isValidDashboardImage', () => {
	it.each([
		['icon path', '/assets/Icons/eight-ball'],
		['unknown-but-well-formed icon path', '/assets/Icons/whatever'],
		['logo path', '/assets/Logos/aws-dark'],
		['base64 image', 'data:image/svg+xml;base64,PHN2Zz48L3N2Zz4='],
		['empty (no image)', ''],
	])('accepts %s', (_label, value) => {
		expect(isValidDashboardImage(value)).toBe(true);
	});

	it.each([
		['remote URL', 'http://evil.com/x.svg'],
		['javascript URI', 'javascript:alert(1)'],
		['non-image data URI', 'data:text/html;base64,PHNjcmlwdD4='],
		['raw markup', '<svg onload=alert(1)>'],
		['prefix with no name', '/assets/Icons/'],
		['plain string', 'not-a-path'],
	])('rejects %s', (_label, value) => {
		expect(isValidDashboardImage(value)).toBe(false);
	});
});