Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

Chore/876 server unit tests (#927)

Sanjeev Kumar17cd15656c57
diff --git a/server/src/lib/plan-guard.js b/server/src/lib/plan-guard.js
index 07e4a1a16..e911b1d2b 100644
--- a/server/src/lib/plan-guard.js
+++ b/server/src/lib/plan-guard.js
@@ -21,7 +21,12 @@ export class PlanLimitError extends Error {
* organizations.plan_overrides (jsonb) — mirrors web getOrgPlan().
*/
export function applyPlanOverrides(plan, org) {
- if (org?.plan === 'enterprise' && org.plan_overrides && typeof org.plan_overrides === 'object') {
+ if (
+ org?.plan === 'enterprise' &&
+ org.plan_overrides &&
+ typeof org.plan_overrides === 'object' &&
+ !Array.isArray(org.plan_overrides)
+ ) {
return { ...plan, limits: { ...plan.limits, ...org.plan_overrides } };
}
return plan;
diff --git a/server/src/lib/plan-guard.test.js b/server/src/lib/plan-guard.test.js
new file mode 100644
index 000000000..ce2912e04
--- /dev/null
+++ b/server/src/lib/plan-guard.test.js
@@ -0,0 +1,48 @@
+import { beforeAll, describe, expect, it } from 'vitest';
+
+let applyPlanOverrides;
+
+beforeAll(async () => {
+ process.env.SUPABASE_URL = 'https://example.supabase.co';
+ process.env.SUPABASE_SERVICE_ROLE_KEY = 'test-key';
+ ({ applyPlanOverrides } = await import('./plan-guard.js'));
+});
+
+const plan = {
+ id: 'enterprise',
+ name: 'Enterprise',
+ limits: { maxBrands: -1, maxPrompts: -1, allowedModels: [] },
+};
+
+describe('applyPlanOverrides', () => {
+ it('merges enterprise overrides without mutating the plan', () => {
+ const result = applyPlanOverrides(plan, {
+ plan: 'enterprise',
+ plan_overrides: { maxPrompts: 500, allowedModels: ['claude-sonnet-5'] },
+ });
+
+ expect(result).not.toBe(plan);
+ expect(result.limits).not.toBe(plan.limits);
+ expect(result.limits.maxPrompts).toBe(500);
+ expect(result.limits.allowedModels).toEqual(['claude-sonnet-5']);
+ expect(plan).toEqual({
+ id: 'enterprise',
+ name: 'Enterprise',
+ limits: { maxBrands: -1, maxPrompts: -1, allowedModels: [] },
+ });
+ });
+
+ it('ignores non-enterprise and invalid overrides', () => {
+ expect(
+ applyPlanOverrides(plan, {
+ plan: 'growth',
+ plan_overrides: { maxPrompts: 500 },
+ }),
+ ).toEqual(plan);
+ expect(applyPlanOverrides(plan, { plan: 'enterprise', plan_overrides: null })).toEqual(plan);
+ expect(applyPlanOverrides(plan, { plan: 'enterprise', plan_overrides: 'invalid' })).toEqual(
+ plan,
+ );
+ expect(applyPlanOverrides(plan, { plan: 'enterprise', plan_overrides: [] })).toEqual(plan);
+ });
+});
diff --git a/server/src/lib/pulse/email.js b/server/src/lib/pulse/email.js
index 578780bb2..7aef2381c 100644
--- a/server/src/lib/pulse/email.js
+++ b/server/src/lib/pulse/email.js
@@ -40,10 +40,11 @@ function signed(n, suffix = '') {
}
function kpiCell(label, value, change) {
+ const roundedChange = change === null ? null : Math.round(change * 10) / 10;
const changeHtml =
change === null
? ''
- : `<div style="font-size:12px;color:${change >= 0 ? '#059669' : '#dc2626'};">${escapeHtml(change >= 0 ? '▲' : '▼')} ${escapeHtml(signed(change))}</div>`;
+ : `<div style="font-size:12px;color:${roundedChange >= 0 ? '#059669' : '#dc2626'};">${escapeHtml(roundedChange >= 0 ? '▲' : '▼')} ${escapeHtml(signed(roundedChange))}</div>`;
return `<td style="padding:12px 16px;border:1px solid #e5e7eb;border-radius:8px;text-align:center;">
<div style="font-size:12px;color:#6b7280;text-transform:uppercase;letter-spacing:0.04em;">${escapeHtml(label)}</div>
<div style="font-size:22px;font-weight:600;color:#111827;">${escapeHtml(value)}</div>
diff --git a/server/src/lib/pulse/email.test.js b/server/src/lib/pulse/email.test.js
new file mode 100644
index 000000000..eada2d1a8
--- /dev/null
+++ b/server/src/lib/pulse/email.test.js
@@ -0,0 +1,84 @@
+import { describe, expect, it } from 'vitest';
+
+import { renderPulseEmail } from './email.js';
+
+const metrics = {
+ kpis: {
+ visibilityRate: 42,
+ weekRate: 43,
+ weekTrend: 1.2,
+ mentions: 20,
+ mentionsChange: 2,
+ citations: 8,
+ citationsChange: -1,
+ sentimentPct: 75,
+ sentimentChange: 0,
+ visiblePrompts: 4,
+ promptCount: 10,
+ },
+ highlights: [],
+ warnings: [],
+ degradedPlatforms: [],
+ windowDays: 7,
+};
+
+describe('renderPulseEmail', () => {
+ it('escapes user-controlled HTML values', () => {
+ const { html } = renderPulseEmail({
+ brandName: '<script>alert("x")</script>',
+ metrics: {
+ ...metrics,
+ highlights: [
+ {
+ type: 'prompt_gain',
+ promptText: '<script>alert("prompt")</script>',
+ gain: 1.5,
+ },
+ {
+ type: 'competitor_overtaken',
+ competitorName: 'Bad "Brand"',
+ brandRate: 12,
+ competitorRate: 8,
+ },
+ ],
+ },
+ insightsUrl: 'https://example.com/insights?a=1&b=2',
+ settingsUrl: 'https://example.com/settings',
+ });
+
+ expect(html).toContain('&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt;');
+ expect(html).toContain('&lt;script&gt;alert(&quot;prompt&quot;)&lt;/script&gt;');
+ expect(html).toContain('Bad &quot;Brand&quot;');
+ expect(html).not.toContain('<script>alert("prompt")</script>');
+ });
+
+ it('renders the degraded-platform notice only when needed', () => {
+ const healthy = renderPulseEmail({
+ brandName: 'Acme',
+ metrics,
+ insightsUrl: 'https://example.com/insights',
+ settingsUrl: 'https://example.com/settings',
+ });
+ expect(healthy.html).not.toContain('Data collection was degraded');
+
+ const degraded = renderPulseEmail({
+ brandName: 'Acme',
+ metrics: { ...metrics, degradedPlatforms: ['chatgpt-web', 'perplexity-web'] },
+ insightsUrl: 'https://example.com/insights',
+ settingsUrl: 'https://example.com/settings',
+ });
+ expect(degraded.html).toContain('Data collection was degraded on ChatGPT, Perplexity today');
+ });
+
+ it('uses the rounded change when selecting the KPI trend arrow', () => {
+ const { html } = renderPulseEmail({
+ brandName: 'Acme',
+ metrics: { ...metrics, kpis: { ...metrics.kpis, weekTrend: -0.04 } },
+ insightsUrl: 'https://example.com/insights',
+ settingsUrl: 'https://example.com/settings',
+ });
+
+ expect(html).toContain('color:#059669;">▲ 0</div>');
+ expect(html).not.toContain('color:#dc2626;">▼ 0</div>');
+ });
+});
diff --git a/server/src/lib/target-url-stats.test.js b/server/src/lib/target-url-stats.test.js
new file mode 100644
index 000000000..dca99e3dd
--- /dev/null
+++ b/server/src/lib/target-url-stats.test.js
@@ -0,0 +1,35 @@
+import { beforeAll, describe, expect, it } from 'vitest';
+
+let normalizeUrlForMatch;
+
+beforeAll(async () => {
+ process.env.SUPABASE_URL = 'https://example.supabase.co';
+ process.env.SUPABASE_SERVICE_ROLE_KEY = 'test-key';
+ ({ normalizeUrlForMatch } = await import('./target-url-stats.js'));
+});
+
+describe('normalizeUrlForMatch', () => {
+ it('normalizes protocol, www, query strings, and trailing slashes', () => {
+ expect(normalizeUrlForMatch('https://www.example.com/blog/x?a=1')).toBe('example.com/blog/x');
+ expect(normalizeUrlForMatch('http://example.com/blog/x/')).toBe('example.com/blog/x');
+ });
+
+ it('ignores fragments, lowercases the host, and strips repeated trailing slashes', () => {
+ expect(normalizeUrlForMatch('HTTPS://WWW.Example.COM/Blog/X///#section')).toBe(
+ 'example.com/Blog/X',
+ );
+ });
+
+ it('removes the root path slash', () => {
+ expect(normalizeUrlForMatch('https://example.com/')).toBe('example.com');
+ expect(normalizeUrlForMatch('https://example.com///')).toBe('example.com');
+ });
+
+ it.each([null, '', 123, {}, [], undefined])('returns null for invalid input: %j', (value) => {
+ expect(normalizeUrlForMatch(value)).toBeNull();
+ });
+
+ it('trims surrounding whitespace before parsing', () => {
+ expect(normalizeUrlForMatch(' https://www.example.com/path ')).toBe('example.com/path');
+ });
+});