Add coverage for send route and core permission and query logic
This commit is contained in:
1 parent
3a8f268d21
commit
8148d775b0
19 files changed
+3355
No files matched your search
@@ -0,0 +1,164 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import {
|
||||
EMAIL_DOMAINS,
|
||||
LLM_DOMAINS,
|
||||
PAID_AD_PARAMS,
|
||||
SEARCH_DOMAINS,
|
||||
SHOPPING_DOMAINS,
|
||||
SOCIAL_DOMAINS,
|
||||
VIDEO_DOMAINS,
|
||||
} from '@/lib/constants';
|
||||
import { getChannelExpandedMetrics } from './getChannelExpandedMetrics';
|
||||
|
||||
const {
|
||||
state,
|
||||
prismaRawQuery,
|
||||
prismaParseFilters,
|
||||
getTimestampDiffSQL,
|
||||
clickhouseRawQuery,
|
||||
clickhouseParseFilters,
|
||||
} = vi.hoisted(() => ({
|
||||
state: { mode: 'prisma' as 'prisma' | 'clickhouse' },
|
||||
prismaRawQuery: vi.fn(),
|
||||
prismaParseFilters: vi.fn(),
|
||||
getTimestampDiffSQL: vi.fn(),
|
||||
clickhouseRawQuery: vi.fn(),
|
||||
clickhouseParseFilters: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/db', () => ({
|
||||
CLICKHOUSE: 'clickhouse',
|
||||
PRISMA: 'prisma',
|
||||
runQuery: vi.fn((queries: Record<string, () => unknown>) => queries[state.mode]()),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
rawQuery: prismaRawQuery,
|
||||
parseFilters: prismaParseFilters,
|
||||
getTimestampDiffSQL,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/clickhouse', () => ({
|
||||
default: {
|
||||
rawQuery: clickhouseRawQuery,
|
||||
parseFilters: clickhouseParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
const parseFiltersResult = {
|
||||
queryParams: { websiteId: 'website-1' },
|
||||
filterQuery: 'and website_event.utm_source = {{utmSource}}',
|
||||
joinSessionQuery: 'join session on session.session_id = website_event.session_id',
|
||||
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
|
||||
excludeBounceQuery: 'and website_event.visit_id in (select visit_id from bounces)',
|
||||
dateQuery: 'and created_at between {{startDate}} and {{endDate}}',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
state.mode = 'prisma';
|
||||
prismaRawQuery.mockReset();
|
||||
prismaParseFilters.mockReset();
|
||||
getTimestampDiffSQL.mockReset();
|
||||
clickhouseRawQuery.mockReset();
|
||||
clickhouseParseFilters.mockReset();
|
||||
|
||||
prismaParseFilters.mockReturnValue(parseFiltersResult);
|
||||
clickhouseParseFilters.mockReturnValue(parseFiltersResult);
|
||||
getTimestampDiffSQL.mockReturnValue('ts_diff(min_time, max_time)');
|
||||
prismaRawQuery.mockResolvedValue([]);
|
||||
clickhouseRawQuery.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('getChannelExpandedMetrics postgres branch', () => {
|
||||
test('builds the expanded aggregation query with parseFilters fragments', async () => {
|
||||
await getChannelExpandedMetrics('website-1', {});
|
||||
|
||||
expect(prismaRawQuery).toHaveBeenCalledTimes(1);
|
||||
const [query, params, tag] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('WITH prefix AS');
|
||||
expect(query).toContain('visit_stats as');
|
||||
expect(query).toContain('sum(visit_stats.c) as "pageviews"');
|
||||
expect(query).toContain('count(distinct visit_stats.session_id) as "visitors"');
|
||||
expect(query).toContain('count(distinct visit_stats.visit_id) as "visits"');
|
||||
expect(query).toContain('sum(case when visit_stats.c = 1 then 1 else 0 end) as "bounces"');
|
||||
// getTimestampDiffSQL output is injected into the totaltime column
|
||||
expect(query).toContain('sum(ts_diff(min_time, max_time)) as "totaltime"');
|
||||
expect(getTimestampDiffSQL).toHaveBeenCalledWith(
|
||||
'visit_stats.min_time',
|
||||
'visit_stats.max_time',
|
||||
);
|
||||
expect(query).toContain(parseFiltersResult.filterQuery);
|
||||
expect(query).toContain(parseFiltersResult.cohortQuery);
|
||||
expect(query).toContain(parseFiltersResult.excludeBounceQuery);
|
||||
expect(query).toContain(parseFiltersResult.joinSessionQuery);
|
||||
expect(params).toBe(parseFiltersResult.queryParams);
|
||||
expect(tag).toBe('getChannelExpandedMetrics');
|
||||
});
|
||||
|
||||
test('toPostgresPositionClause renders one ilike per array entry', async () => {
|
||||
await getChannelExpandedMetrics('website-1', {});
|
||||
const [query] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
for (const val of PAID_AD_PARAMS) {
|
||||
expect(query).toContain(`url_query ilike '%${val}%'`);
|
||||
}
|
||||
for (const domainList of [
|
||||
LLM_DOMAINS,
|
||||
SEARCH_DOMAINS,
|
||||
SOCIAL_DOMAINS,
|
||||
EMAIL_DOMAINS,
|
||||
SHOPPING_DOMAINS,
|
||||
VIDEO_DOMAINS,
|
||||
]) {
|
||||
for (const val of domainList) {
|
||||
expect(query).toContain(`referrer_domain ilike '%${val}%'`);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('getChannelExpandedMetrics clickhouse branch', () => {
|
||||
beforeEach(() => {
|
||||
state.mode = 'clickhouse';
|
||||
});
|
||||
|
||||
test('builds the multiSearchAny aggregation query', async () => {
|
||||
await getChannelExpandedMetrics('website-1', {});
|
||||
|
||||
expect(clickhouseRawQuery).toHaveBeenCalledTimes(1);
|
||||
expect(prismaRawQuery).not.toHaveBeenCalled();
|
||||
const [query, params, tag] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('uniq(t.session_id) as "visitors"');
|
||||
expect(query).toContain('uniq(t.visit_id) as "visits"');
|
||||
expect(query).toContain('sum(if(t.c = 1, 1, 0)) as "bounces"');
|
||||
expect(query).toContain('sum(max_time-min_time) as "totaltime"');
|
||||
expect(query).toContain('where website_id = {websiteId:UUID}');
|
||||
expect(query).toContain(
|
||||
'and created_at between {startDate:DateTime64} and {endDate:DateTime64}',
|
||||
);
|
||||
expect(query).toContain(parseFiltersResult.filterQuery);
|
||||
expect(query).toContain(parseFiltersResult.cohortQuery);
|
||||
expect(query).toContain(parseFiltersResult.excludeBounceQuery);
|
||||
expect(params).toBe(parseFiltersResult.queryParams);
|
||||
expect(tag).toBe('getChannelExpandedMetrics');
|
||||
// clickhouse expanded query does not use getTimestampDiffSQL or joinSessionQuery
|
||||
expect(getTimestampDiffSQL).not.toHaveBeenCalled();
|
||||
expect(query).not.toContain(parseFiltersResult.joinSessionQuery);
|
||||
});
|
||||
|
||||
test('toClickHouseStringArray quotes and comma-joins each entry', async () => {
|
||||
await getChannelExpandedMetrics('website-1', {});
|
||||
const [query] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain(
|
||||
`multiSearchAny(lower(url_query), [${PAID_AD_PARAMS.map(v => `'${v}'`).join(', ')}])`,
|
||||
);
|
||||
expect(query).toContain(
|
||||
`multiSearchAny(lower(referrer_domain), [${SOCIAL_DOMAINS.map(v => `'${v}'`).join(', ')}])`,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,208 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import {
|
||||
EMAIL_DOMAINS,
|
||||
LLM_DOMAINS,
|
||||
PAID_AD_PARAMS,
|
||||
SEARCH_DOMAINS,
|
||||
SHOPPING_DOMAINS,
|
||||
SOCIAL_DOMAINS,
|
||||
VIDEO_DOMAINS,
|
||||
} from '@/lib/constants';
|
||||
import { getChannelMetrics } from './getChannelMetrics';
|
||||
|
||||
const { state, prismaRawQuery, prismaParseFilters, clickhouseRawQuery, clickhouseParseFilters } =
|
||||
vi.hoisted(() => ({
|
||||
state: { mode: 'prisma' as 'prisma' | 'clickhouse' },
|
||||
prismaRawQuery: vi.fn(),
|
||||
prismaParseFilters: vi.fn(),
|
||||
clickhouseRawQuery: vi.fn(),
|
||||
clickhouseParseFilters: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/db', () => ({
|
||||
CLICKHOUSE: 'clickhouse',
|
||||
PRISMA: 'prisma',
|
||||
runQuery: vi.fn((queries: Record<string, () => unknown>) => queries[state.mode]()),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
rawQuery: prismaRawQuery,
|
||||
parseFilters: prismaParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/clickhouse', () => ({
|
||||
default: {
|
||||
rawQuery: clickhouseRawQuery,
|
||||
parseFilters: clickhouseParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
const parseFiltersResult = {
|
||||
queryParams: { websiteId: 'website-1' },
|
||||
filterQuery: 'and website_event.utm_source = {{utmSource}}',
|
||||
joinSessionQuery: 'join session on session.session_id = website_event.session_id',
|
||||
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
|
||||
excludeBounceQuery: 'and website_event.visit_id in (select visit_id from bounces)',
|
||||
dateQuery: 'and created_at between {{startDate}} and {{endDate}}',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
state.mode = 'prisma';
|
||||
prismaRawQuery.mockReset();
|
||||
prismaParseFilters.mockReset();
|
||||
clickhouseRawQuery.mockReset();
|
||||
clickhouseParseFilters.mockReset();
|
||||
|
||||
prismaParseFilters.mockReturnValue(parseFiltersResult);
|
||||
clickhouseParseFilters.mockReturnValue(parseFiltersResult);
|
||||
prismaRawQuery.mockResolvedValue([]);
|
||||
clickhouseRawQuery.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('getChannelMetrics postgres branch', () => {
|
||||
test('builds the channel classification CTE with parseFilters fragments injected', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
|
||||
expect(prismaRawQuery).toHaveBeenCalledTimes(1);
|
||||
const [query, params, tag] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('WITH prefix AS');
|
||||
expect(query).toContain('channels as');
|
||||
expect(query).toContain('visit_channels as');
|
||||
expect(query).toContain('where website_event.website_id = {{websiteId::uuid}}');
|
||||
expect(query).toContain('and website_event.event_type NOT IN (2, 5)');
|
||||
// parseFilters fragments are interpolated verbatim
|
||||
expect(query).toContain(parseFiltersResult.filterQuery);
|
||||
expect(query).toContain(parseFiltersResult.cohortQuery);
|
||||
expect(query).toContain(parseFiltersResult.excludeBounceQuery);
|
||||
expect(query).toContain(parseFiltersResult.joinSessionQuery);
|
||||
expect(query).toContain(parseFiltersResult.dateQuery);
|
||||
expect(params).toBe(parseFiltersResult.queryParams);
|
||||
expect(tag).toBe('getChannelMetrics');
|
||||
});
|
||||
|
||||
test('emits the direct/paidAds/referral CASE branches in order', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
const [query] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain("when referrer_domain = '' and url_query = '' then 'direct'");
|
||||
expect(query).toContain("then 'paidAds'");
|
||||
expect(query).toContain("utm_medium ilike '%affiliate%' then 'affiliate'");
|
||||
expect(query).toContain("utm_medium ilike '%sms%' or utm_source ilike '%sms%' then 'sms'");
|
||||
expect(query).toContain("concat(prefix, 'Search')");
|
||||
expect(query).toContain("concat(prefix, 'Social')");
|
||||
expect(query).toContain("concat(prefix, 'Shopping')");
|
||||
expect(query).toContain("concat(prefix, 'Video')");
|
||||
});
|
||||
|
||||
test('toPostgresLikeClause renders one ilike per array entry joined by OR', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
const [query] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
for (const val of PAID_AD_PARAMS) {
|
||||
expect(query).toContain(`url_query ilike '%${val}%'`);
|
||||
}
|
||||
for (const val of SEARCH_DOMAINS) {
|
||||
expect(query).toContain(`referrer_domain ilike '%${val}%'`);
|
||||
}
|
||||
for (const domainList of [
|
||||
LLM_DOMAINS,
|
||||
SOCIAL_DOMAINS,
|
||||
EMAIL_DOMAINS,
|
||||
SHOPPING_DOMAINS,
|
||||
VIDEO_DOMAINS,
|
||||
]) {
|
||||
for (const val of domainList) {
|
||||
expect(query).toContain(`referrer_domain ilike '%${val}%'`);
|
||||
}
|
||||
}
|
||||
// referral clause uses a literal array, not a constant
|
||||
expect(query).toContain("utm_medium ilike '%referral%'");
|
||||
expect(query).toContain("utm_medium ilike '%app%'");
|
||||
expect(query).toContain("utm_medium ilike '%link%'");
|
||||
});
|
||||
|
||||
test('coerces the y column to a number in results', async () => {
|
||||
prismaRawQuery.mockResolvedValue([{ x: 'direct', y: '42' }]);
|
||||
|
||||
const result = await getChannelMetrics('website-1', {});
|
||||
|
||||
expect(result).toEqual([{ x: 'direct', y: 42 }]);
|
||||
});
|
||||
|
||||
// NOTE: toPostgresLikeClause only escapes single quotes (' -> ''). LIKE
|
||||
// wildcards (% and _) and backslashes pass through unescaped. This is safe
|
||||
// today because the helper is private and only ever receives the hardcoded
|
||||
// constant arrays, but it would be an escaping gap if user input reached it.
|
||||
// See getChannelMetrics.ts:180-182.
|
||||
test('single-quote escaping doubles quotes (documenting current helper behavior)', async () => {
|
||||
// None of the real constants contain quotes, so assert the round-trip of
|
||||
// the escaping regex against every constant value: no bare `''` artifacts
|
||||
// should appear inside a domain fragment.
|
||||
await getChannelMetrics('website-1', {});
|
||||
const [query] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
for (const val of [...PAID_AD_PARAMS, ...LLM_DOMAINS, ...SEARCH_DOMAINS]) {
|
||||
const escaped = val.replace(/'/g, "''");
|
||||
expect(query).toContain(`'%${escaped}%'`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('getChannelMetrics clickhouse branch', () => {
|
||||
beforeEach(() => {
|
||||
state.mode = 'clickhouse';
|
||||
});
|
||||
|
||||
test('builds the multiSearchAny channel query with parseFilters fragments', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
|
||||
expect(clickhouseRawQuery).toHaveBeenCalledTimes(1);
|
||||
expect(prismaRawQuery).not.toHaveBeenCalled();
|
||||
const [query, params, tag] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('WITH channels as');
|
||||
expect(query).toContain('where website_id = {websiteId:UUID}');
|
||||
expect(query).toContain('and event_type NOT IN (2, 5)');
|
||||
expect(query).toContain(
|
||||
"multiSearchAny(lower(utm_medium), ['cp', 'ppc', 'retargeting', 'paid'])",
|
||||
);
|
||||
expect(query).toContain(parseFiltersResult.filterQuery);
|
||||
expect(query).toContain(parseFiltersResult.cohortQuery);
|
||||
expect(query).toContain(parseFiltersResult.excludeBounceQuery);
|
||||
expect(query).toContain(parseFiltersResult.dateQuery);
|
||||
expect(params).toBe(parseFiltersResult.queryParams);
|
||||
expect(tag).toBe('getChannelMetrics');
|
||||
// clickhouse branch does not join a session table
|
||||
expect(query).not.toContain(parseFiltersResult.joinSessionQuery);
|
||||
});
|
||||
|
||||
test('toClickHouseStringArray quotes and comma-joins each entry', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
const [query] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain(
|
||||
`multiSearchAny(lower(url_query), [${PAID_AD_PARAMS.map(v => `'${v}'`).join(', ')}])`,
|
||||
);
|
||||
expect(query).toContain(
|
||||
`multiSearchAny(lower(referrer_domain), [${LLM_DOMAINS.map(v => `'${v}'`).join(', ')}])`,
|
||||
);
|
||||
expect(query).toContain(
|
||||
`multiSearchAny(lower(referrer_domain), [${SEARCH_DOMAINS.map(v => `'${v}'`).join(', ')}])`,
|
||||
);
|
||||
});
|
||||
|
||||
// NOTE: toClickHouseStringArray escapes single quotes as \' but leaves
|
||||
// backslashes and other metacharacters untouched (getChannelMetrics.ts:176-178).
|
||||
// Safe today (constants only), documented here as current behavior.
|
||||
test('clickhouse array escaping only handles single quotes', async () => {
|
||||
await getChannelMetrics('website-1', {});
|
||||
const [query] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
for (const val of [...VIDEO_DOMAINS, ...SHOPPING_DOMAINS]) {
|
||||
expect(query).toContain(`'${val.replace(/'/g, "\\'")}'`);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,182 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import { getAttribution } from './getAttribution';
|
||||
|
||||
const { state, prismaRawQuery, prismaParseFilters, clickhouseRawQuery, clickhouseParseFilters } =
|
||||
vi.hoisted(() => ({
|
||||
state: { mode: 'prisma' as 'prisma' | 'clickhouse' },
|
||||
prismaRawQuery: vi.fn(),
|
||||
prismaParseFilters: vi.fn(),
|
||||
clickhouseRawQuery: vi.fn(),
|
||||
clickhouseParseFilters: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/db', () => ({
|
||||
CLICKHOUSE: 'clickhouse',
|
||||
PRISMA: 'prisma',
|
||||
runQuery: vi.fn((queries: Record<string, () => unknown>) => queries[state.mode]()),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
rawQuery: prismaRawQuery,
|
||||
parseFilters: prismaParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/clickhouse', () => ({
|
||||
default: {
|
||||
rawQuery: clickhouseRawQuery,
|
||||
parseFilters: clickhouseParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
const parseFiltersResult = {
|
||||
queryParams: { websiteId: 'website-1' },
|
||||
filterQuery: 'and website_event.utm_source = {{utmSource}}',
|
||||
joinSessionQuery: 'join session on session.session_id = website_event.session_id',
|
||||
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
|
||||
};
|
||||
|
||||
const parameters = {
|
||||
startDate: new Date('2026-05-18T00:00:00.000Z'),
|
||||
endDate: new Date('2026-05-19T00:00:00.000Z'),
|
||||
model: 'last-click',
|
||||
type: 'path',
|
||||
step: '/checkout',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
state.mode = 'prisma';
|
||||
prismaRawQuery.mockReset();
|
||||
prismaParseFilters.mockReset();
|
||||
clickhouseRawQuery.mockReset();
|
||||
clickhouseParseFilters.mockReset();
|
||||
|
||||
prismaParseFilters.mockReturnValue(parseFiltersResult);
|
||||
clickhouseParseFilters.mockReturnValue(parseFiltersResult);
|
||||
prismaRawQuery.mockResolvedValue([]);
|
||||
clickhouseRawQuery.mockResolvedValue([]);
|
||||
});
|
||||
|
||||
describe('getAttribution postgres branch', () => {
|
||||
test('runs eight rawQuery calls covering referrer, paid ads, utm columns and totals', async () => {
|
||||
await getAttribution('website-1', parameters, {});
|
||||
|
||||
expect(prismaRawQuery).toHaveBeenCalledTimes(8);
|
||||
expect(clickhouseRawQuery).not.toHaveBeenCalled();
|
||||
|
||||
const queries = prismaRawQuery.mock.calls.map(c => c[0] as string);
|
||||
expect(queries.some(q => q.includes('we.referrer_domain'))).toBe(true);
|
||||
expect(queries.some(q => q.includes("then 'Google Ads'"))).toBe(true);
|
||||
expect(queries.some(q => q.includes('we.utm_source'))).toBe(true);
|
||||
expect(queries.some(q => q.includes('we.utm_medium'))).toBe(true);
|
||||
expect(queries.some(q => q.includes('we.utm_campaign'))).toBe(true);
|
||||
expect(queries.some(q => q.includes('we.utm_content'))).toBe(true);
|
||||
expect(queries.some(q => q.includes('we.utm_term'))).toBe(true);
|
||||
});
|
||||
|
||||
test('type=path selects url_path column and uses the step param', async () => {
|
||||
await getAttribution('website-1', parameters, {});
|
||||
|
||||
// eventType / column derive from `type`; column is embedded in the SQL string
|
||||
const eventQueryCall = prismaRawQuery.mock.calls.find(c =>
|
||||
(c[0] as string).includes('WITH events AS'),
|
||||
);
|
||||
expect(eventQueryCall?.[0]).toContain('website_event.url_path = {{step}}');
|
||||
expect(prismaParseFilters).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ websiteId: 'website-1', eventType: 1 }),
|
||||
);
|
||||
});
|
||||
|
||||
test('type=event selects event_name column and customEvent event type', async () => {
|
||||
await getAttribution('website-1', { ...parameters, type: 'event' }, {});
|
||||
|
||||
const eventQueryCall = prismaRawQuery.mock.calls.find(c =>
|
||||
(c[0] as string).includes('WITH events AS'),
|
||||
);
|
||||
expect(eventQueryCall?.[0]).toContain('website_event.event_name = {{step}}');
|
||||
expect(prismaParseFilters).toHaveBeenCalledWith(expect.objectContaining({ eventType: 2 }));
|
||||
});
|
||||
|
||||
test('first-click model uses min(created_at) with no upper-bound clause', async () => {
|
||||
await getAttribution('website-1', { ...parameters, model: 'first-click' }, {});
|
||||
|
||||
const referrerQuery = prismaRawQuery.mock.calls
|
||||
.map(c => c[0] as string)
|
||||
.find(q => q.includes('we.referrer_domain') && q.includes('model AS'));
|
||||
expect(referrerQuery).toContain('min(we.created_at) created_at');
|
||||
expect(referrerQuery).not.toContain('and we.created_at < e.max_dt');
|
||||
});
|
||||
|
||||
test('last-click model uses max(created_at) bounded by e.max_dt', async () => {
|
||||
await getAttribution('website-1', { ...parameters, model: 'last-click' }, {});
|
||||
|
||||
const referrerQuery = prismaRawQuery.mock.calls
|
||||
.map(c => c[0] as string)
|
||||
.find(q => q.includes('we.referrer_domain') && q.includes('model AS'));
|
||||
expect(referrerQuery).toContain('max(we.created_at) created_at');
|
||||
expect(referrerQuery).toContain('and we.created_at < e.max_dt');
|
||||
});
|
||||
|
||||
test('returns the shaped attribution result object', async () => {
|
||||
prismaRawQuery
|
||||
.mockResolvedValueOnce([{ name: 'a', value: 1 }]) // referrer
|
||||
.mockResolvedValueOnce([{ name: 'Google Ads', value: 2 }]) // paidAds
|
||||
.mockResolvedValueOnce([{ name: 's', value: 3 }]) // source
|
||||
.mockResolvedValueOnce([{ name: 'm', value: 4 }]) // medium
|
||||
.mockResolvedValueOnce([{ name: 'c', value: 5 }]) // campaign
|
||||
.mockResolvedValueOnce([{ name: 'ct', value: 6 }]) // content
|
||||
.mockResolvedValueOnce([{ name: 't', value: 7 }]) // term
|
||||
.mockResolvedValueOnce([{ pageviews: 10, visitors: 8, visits: 9 }]); // total
|
||||
|
||||
const result = await getAttribution('website-1', parameters, {});
|
||||
|
||||
expect(result.referrer).toEqual([{ name: 'a', value: 1 }]);
|
||||
expect(result.paidAds).toEqual([{ name: 'Google Ads', value: 2 }]);
|
||||
expect(result.utm_source).toEqual([{ name: 's', value: 3 }]);
|
||||
expect(result.total).toEqual({ pageviews: 10, visitors: 8, visits: 9 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAttribution clickhouse branch', () => {
|
||||
beforeEach(() => {
|
||||
state.mode = 'clickhouse';
|
||||
});
|
||||
|
||||
test('runs eight rawQuery calls with clickhouse-typed params', async () => {
|
||||
await getAttribution('website-1', parameters, {});
|
||||
|
||||
expect(clickhouseRawQuery).toHaveBeenCalledTimes(8);
|
||||
expect(prismaRawQuery).not.toHaveBeenCalled();
|
||||
|
||||
const queries = clickhouseRawQuery.mock.calls.map(c => c[0] as string);
|
||||
expect(queries.some(q => q.includes('where website_id = {websiteId:UUID}'))).toBe(true);
|
||||
expect(
|
||||
queries.some(q =>
|
||||
q.includes('created_at between {startDate:DateTime64} and {endDate:DateTime64}'),
|
||||
),
|
||||
).toBe(true);
|
||||
// paid ads use multiIf in clickhouse rather than a CASE expression
|
||||
expect(queries.some(q => q.includes("multiIf(gclid != '', 'Google Ads'"))).toBe(true);
|
||||
});
|
||||
|
||||
test('type=path uses url_path with {step:String} and pageView event type', async () => {
|
||||
await getAttribution('website-1', parameters, {});
|
||||
|
||||
const eventQuery = clickhouseRawQuery.mock.calls
|
||||
.map(c => c[0] as string)
|
||||
.find(q => q.includes('WITH events AS'));
|
||||
expect(eventQuery).toContain('url_path = {step:String}');
|
||||
expect(clickhouseParseFilters).toHaveBeenCalledWith(expect.objectContaining({ eventType: 1 }));
|
||||
});
|
||||
|
||||
test('first-click model uses min(created_at) with no upper bound', async () => {
|
||||
await getAttribution('website-1', { ...parameters, model: 'first-click' }, {});
|
||||
|
||||
const referrerQuery = clickhouseRawQuery.mock.calls
|
||||
.map(c => c[0] as string)
|
||||
.find(q => q.includes('we.referrer_domain') && q.includes('model AS'));
|
||||
expect(referrerQuery).toContain('min(we.created_at) created_at');
|
||||
expect(referrerQuery).not.toContain('where we.created_at < e.max_dt');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import { getFunnel } from './getFunnel';
|
||||
|
||||
const {
|
||||
state,
|
||||
prismaRawQuery,
|
||||
prismaParseFilters,
|
||||
getAddIntervalQuery,
|
||||
clickhouseRawQuery,
|
||||
clickhouseParseFilters,
|
||||
} = vi.hoisted(() => ({
|
||||
state: { mode: 'prisma' as 'prisma' | 'clickhouse' },
|
||||
prismaRawQuery: vi.fn(),
|
||||
prismaParseFilters: vi.fn(),
|
||||
getAddIntervalQuery: vi.fn(),
|
||||
clickhouseRawQuery: vi.fn(),
|
||||
clickhouseParseFilters: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/db', () => ({
|
||||
CLICKHOUSE: 'clickhouse',
|
||||
PRISMA: 'prisma',
|
||||
runQuery: vi.fn((queries: Record<string, () => unknown>) => queries[state.mode]()),
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
rawQuery: prismaRawQuery,
|
||||
parseFilters: prismaParseFilters,
|
||||
getAddIntervalQuery,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/clickhouse', () => ({
|
||||
default: {
|
||||
rawQuery: clickhouseRawQuery,
|
||||
parseFilters: clickhouseParseFilters,
|
||||
},
|
||||
}));
|
||||
|
||||
const parseFiltersResult = {
|
||||
queryParams: { websiteId: 'website-1' },
|
||||
filterQuery: 'and website_event.utm_source = {{utmSource}}',
|
||||
joinSessionQuery: 'join session on session.session_id = website_event.session_id',
|
||||
cohortQuery: 'join cohort on cohort.session_id = website_event.session_id',
|
||||
};
|
||||
|
||||
const baseParameters = {
|
||||
startDate: new Date('2026-05-18T00:00:00.000Z'),
|
||||
endDate: new Date('2026-05-19T00:00:00.000Z'),
|
||||
window: 30,
|
||||
steps: [
|
||||
{ type: 'path', value: '/home' },
|
||||
{ type: 'path', value: '/checkout' },
|
||||
],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
state.mode = 'prisma';
|
||||
prismaRawQuery.mockReset();
|
||||
prismaParseFilters.mockReset();
|
||||
getAddIntervalQuery.mockReset();
|
||||
clickhouseRawQuery.mockReset();
|
||||
clickhouseParseFilters.mockReset();
|
||||
|
||||
prismaParseFilters.mockReturnValue(parseFiltersResult);
|
||||
clickhouseParseFilters.mockReturnValue(parseFiltersResult);
|
||||
getAddIntervalQuery.mockReturnValue('add_interval(l.created_at, 30 minute)');
|
||||
// formatResults reads results[0].count etc, so provide numeric counts
|
||||
prismaRawQuery.mockResolvedValue([{ count: 100 }, { count: 40 }]);
|
||||
clickhouseRawQuery.mockResolvedValue([{ count: 100 }, { count: 40 }]);
|
||||
});
|
||||
|
||||
describe('getFunnel postgres branch', () => {
|
||||
test('builds level CTEs and a UNION sum query, one per step', async () => {
|
||||
await getFunnel('website-1', baseParameters, {});
|
||||
|
||||
expect(prismaRawQuery).toHaveBeenCalledTimes(1);
|
||||
const [query, params] = prismaRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('WITH level1 AS');
|
||||
expect(query).toContain(', level2 AS');
|
||||
expect(query).toContain('select 1 as level, count(distinct(session_id)) as count from level1');
|
||||
expect(query).toContain('union ');
|
||||
expect(query).toContain('select 2 as level, count(distinct(session_id)) as count from level2');
|
||||
expect(query).toContain('ORDER BY level');
|
||||
// step values become positional params keyed by index
|
||||
expect(params).toMatchObject({ 0: '/home', 1: '/checkout' });
|
||||
// merged with parseFilters queryParams
|
||||
expect(params).toMatchObject({ websiteId: 'website-1' });
|
||||
// level1 filters use equality against {{0}}; level2 against {{1}}
|
||||
expect(query).toContain('and url_path = {{0}}');
|
||||
expect(query).toContain('and we.url_path = {{1}}');
|
||||
// level2 window uses getAddIntervalQuery output
|
||||
expect(query).toContain('add_interval(l.created_at, 30 minute)');
|
||||
expect(getAddIntervalQuery).toHaveBeenCalledWith('l.created_at ', '30 minute');
|
||||
});
|
||||
|
||||
test('wildcard step values switch to LIKE and translate * to %', async () => {
|
||||
await getFunnel(
|
||||
'website-1',
|
||||
{
|
||||
...baseParameters,
|
||||
steps: [
|
||||
{ type: 'path', value: '/blog/*' },
|
||||
{ type: 'path', value: '*/thanks' },
|
||||
],
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const [query, params] = prismaRawQuery.mock.calls[0];
|
||||
expect(query).toContain('and url_path like {{0}}');
|
||||
expect(query).toContain('and we.url_path like {{1}}');
|
||||
expect(params).toMatchObject({ 0: '/blog/%', 1: '%/thanks' });
|
||||
});
|
||||
|
||||
test('event steps with contains filter emit exists() subqueries and ilike params', async () => {
|
||||
await getFunnel(
|
||||
'website-1',
|
||||
{
|
||||
...baseParameters,
|
||||
steps: [
|
||||
{
|
||||
type: 'event',
|
||||
value: 'purchase',
|
||||
filters: [{ property: 'plan', operator: 'c', value: 'pro' }],
|
||||
},
|
||||
{ type: 'event', value: 'confirm' },
|
||||
],
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const [query, params] = prismaRawQuery.mock.calls[0];
|
||||
expect(query).toContain('and event_name = {{0}}');
|
||||
expect(query).toContain('and exists (');
|
||||
expect(query).toContain('_ed0_0.data_key = {{f_0_0_k}}');
|
||||
expect(query).toContain('ilike {{f_0_0_v}}');
|
||||
// contains -> ilike operator, value wrapped in % ... %
|
||||
expect(params).toMatchObject({ f_0_0_k: 'plan', f_0_0_v: '%pro%' });
|
||||
});
|
||||
|
||||
test('filter operators map to the expected SQL operators', async () => {
|
||||
await getFunnel(
|
||||
'website-1',
|
||||
{
|
||||
...baseParameters,
|
||||
steps: [
|
||||
{
|
||||
type: 'event',
|
||||
value: 'e',
|
||||
filters: [
|
||||
{ property: 'a', operator: 'eq', value: 'x' },
|
||||
{ property: 'b', operator: 'neq', value: 'y' },
|
||||
{ property: 'c', operator: 'dnc', value: 'z' },
|
||||
],
|
||||
},
|
||||
{ type: 'event', value: 'f' },
|
||||
],
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const [query, params] = prismaRawQuery.mock.calls[0];
|
||||
// eq -> '=' with raw value
|
||||
expect(query).toContain('= {{f_0_0_v}}');
|
||||
expect(params).toMatchObject({ f_0_0_v: 'x' });
|
||||
// neq -> '!=' with raw value
|
||||
expect(query).toContain('!= {{f_0_1_v}}');
|
||||
expect(params).toMatchObject({ f_0_1_v: 'y' });
|
||||
// dnc -> 'not ilike' with wrapped value
|
||||
expect(query).toContain('not ilike {{f_0_2_v}}');
|
||||
expect(params).toMatchObject({ f_0_2_v: '%z%' });
|
||||
});
|
||||
|
||||
test('formatResults computes dropoff / remaining relative to first step', async () => {
|
||||
prismaRawQuery.mockResolvedValue([{ count: 100 }, { count: 40 }]);
|
||||
|
||||
const result = await getFunnel('website-1', baseParameters, {});
|
||||
|
||||
expect(result[0]).toMatchObject({ visitors: 100, previous: 0, dropped: 0, remaining: 1 });
|
||||
expect(result[1]).toMatchObject({
|
||||
visitors: 40,
|
||||
previous: 100,
|
||||
dropped: 60,
|
||||
remaining: 0.4,
|
||||
});
|
||||
expect(result[1].dropoff).toBeCloseTo(0.6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFunnel clickhouse branch', () => {
|
||||
beforeEach(() => {
|
||||
state.mode = 'clickhouse';
|
||||
});
|
||||
|
||||
test('builds level0 base CTE, step filter predicate and UNION ALL sum query', async () => {
|
||||
await getFunnel('website-1', baseParameters, {});
|
||||
|
||||
expect(clickhouseRawQuery).toHaveBeenCalledTimes(1);
|
||||
expect(prismaRawQuery).not.toHaveBeenCalled();
|
||||
const [query, params] = clickhouseRawQuery.mock.calls[0];
|
||||
|
||||
expect(query).toContain('WITH level0 AS');
|
||||
expect(query).toContain('level1 AS');
|
||||
expect(query).toContain(', level2 AS');
|
||||
expect(query).toContain('union all ');
|
||||
expect(query).toContain('and website_id = {websiteId:UUID}');
|
||||
// step filter predicate uses param0/param1
|
||||
expect(query).toContain('url_path = {param0:String}');
|
||||
expect(query).toContain('y.url_path = {param1:String}');
|
||||
// window interval expressed inline
|
||||
expect(query).toContain('x.created_at + interval 30 minute');
|
||||
expect(params).toMatchObject({ param0: '/home', param1: '/checkout', websiteId: 'website-1' });
|
||||
});
|
||||
|
||||
test('wildcard values switch to LIKE with % substitution', async () => {
|
||||
await getFunnel(
|
||||
'website-1',
|
||||
{
|
||||
...baseParameters,
|
||||
steps: [
|
||||
{ type: 'path', value: '/a/*' },
|
||||
{ type: 'path', value: '/b' },
|
||||
],
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const [query, params] = clickhouseRawQuery.mock.calls[0];
|
||||
expect(query).toContain('url_path like {param0:String}');
|
||||
expect(params).toMatchObject({ param0: '/a/%', param1: '/b' });
|
||||
});
|
||||
|
||||
test('event data filters emit event_id IN subqueries with typed params', async () => {
|
||||
await getFunnel(
|
||||
'website-1',
|
||||
{
|
||||
...baseParameters,
|
||||
steps: [
|
||||
{
|
||||
type: 'event',
|
||||
value: 'purchase',
|
||||
filters: [{ property: 'plan', operator: 'c', value: 'pro' }],
|
||||
},
|
||||
{ type: 'event', value: 'confirm' },
|
||||
],
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
const [query, params] = clickhouseRawQuery.mock.calls[0];
|
||||
// first step uses level0 alias for event_id
|
||||
expect(query).toContain('and level0.event_id in (');
|
||||
expect(query).toContain('data_key = {f_0_0_k:String}');
|
||||
expect(query).toContain('like {f_0_0_v:String}');
|
||||
expect(params).toMatchObject({ f_0_0_k: 'plan', f_0_0_v: '%pro%' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user