OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
// Job/cron-fragment wiring only; no ClickHouse/Postgres touched.
import { expect, test } from 'bun:test';
import { testCoreConfig } from '../../../test/config-fixture';
import type { AppDeps, Buffers, JobCtx } from '../../context';
import { createRecordingProducers } from '../../jobs/testing';
import { queues } from '../../jobs.registry';
import type { Logger } from '../../logger';
import type { Services } from '../../services';
import { gscCronJobs, gscQueueJobs } from './gsc.jobs';
import type { createGscService } from './gsc.service';
function stubLogger(): Logger {
const noop = () => undefined;
const logger: Logger = {
fatal: noop,
error: noop,
warn: noop,
info: noop,
debug: noop,
trace: noop,
child: () => logger,
};
return logger;
}
function stubJobCtx(
gsc: Partial<ReturnType<typeof createGscService>>,
jobName = 'test'
): JobCtx {
const services: Services = {
auth: {} as Services['auth'],
notification: {} as Services['notification'],
insight: {} as Services['insight'],
integration: {} as Services['integration'],
cohort: {} as Services['cohort'],
import: {} as Services['import'],
ingest: {} as Services['ingest'],
gsc: gsc as ReturnType<typeof createGscService>,
organization: {} as Services['organization'],
onboarding: {} as Services['onboarding'],
session: {} as Services['session'],
customEvent: {} as Services['customEvent'],
event: {} as Services['event'],
profile: {} as Services['profile'],
group: {} as Services['group'],
chart: {} as Services['chart'],
funnel: {} as Services['funnel'],
conversion: {} as Services['conversion'],
sankey: {} as Services['sankey'],
retention: {} as Services['retention'],
overview: {} as Services['overview'],
pages: {} as Services['pages'],
realtime: {} as Services['realtime'],
misc: {} as Services['misc'],
report: {} as Services['report'],
dashboard: {} as Services['dashboard'],
export: {} as Services['export'],
share: {} as Services['share'],
reference: {} as Services['reference'],
client: {} as Services['client'],
project: {} as Services['project'],
user: {} as Services['user'],
subscription: {} as Services['subscription'],
salt: {} as Services['salt'],
conversation: {} as Services['conversation'],
assistant: {} as Services['assistant'],
mcp: {} as Services['mcp'],
ai: {} as Services['ai'],
};
return {
auth: null,
db: {} as AppDeps['db'],
prisma: {} as AppDeps['prisma'],
ch: {} as AppDeps['ch'],
redis: {} as AppDeps['redis'],
clients: {} as AppDeps['clients'],
buffers: {} as Buffers,
logger: stubLogger(),
queues: createRecordingProducers(queues).queues,
config: testCoreConfig(),
services,
requestId: 'req_1',
job: { id: 'job_1', attempt: 0, queue: 'gsc', name: jobName },
};
}
test('the gsc jobs registry declares gscProjectSync and gscProjectBackfill on the gsc queue', () => {
expect(queues.gsc.jobs.gscProjectSync).toMatchObject({
queue: 'gsc',
name: 'gscProjectSync',
});
expect(queues.gsc.jobs.gscProjectBackfill).toMatchObject({
queue: 'gsc',
name: 'gscProjectBackfill',
});
});
test('the gsc cron fragment is spread into the cron queue', () => {
expect(queues.cron.jobs.gscSync).toBeDefined();
});
test('gscProjectSync validates its payload', () => {
expect(() => gscQueueJobs.gscProjectSync.payload.parse({})).toThrow();
expect(
gscQueueJobs.gscProjectSync.payload.parse({ projectId: 'p1' })
).toEqual({ projectId: 'p1' });
});
test('gscProjectBackfill validates its payload', () => {
expect(() => gscQueueJobs.gscProjectBackfill.payload.parse({})).toThrow();
expect(
gscQueueJobs.gscProjectBackfill.payload.parse({ projectId: 'p1' })
).toEqual({ projectId: 'p1' });
});
test('gscProjectSync delegates to runProjectSync', async () => {
const calls: unknown[] = [];
const ctx = stubJobCtx({
runProjectSync: async (projectId) => {
calls.push(projectId);
},
});
await gscQueueJobs.gscProjectSync.handler({
payload: { projectId: 'p1' },
ctx,
});
expect(calls).toEqual(['p1']);
});
test('gscProjectBackfill delegates to runProjectBackfill', async () => {
const calls: unknown[] = [];
const ctx = stubJobCtx({
runProjectBackfill: async (projectId) => {
calls.push(projectId);
},
});
await gscQueueJobs.gscProjectBackfill.handler({
payload: { projectId: 'p1' },
ctx,
});
expect(calls).toEqual(['p1']);
});
test('gscSync fans out one gscProjectSync enqueue per connected project', async () => {
const producers = createRecordingProducers(queues);
const ctx: JobCtx = {
auth: null,
db: {} as AppDeps['db'],
prisma: {} as AppDeps['prisma'],
ch: {} as AppDeps['ch'],
redis: {} as AppDeps['redis'],
clients: {} as AppDeps['clients'],
buffers: {} as Buffers,
logger: stubLogger(),
queues: producers.queues,
config: testCoreConfig(),
services: {
auth: {} as Services['auth'],
notification: {} as Services['notification'],
insight: {} as Services['insight'],
integration: {} as Services['integration'],
cohort: {} as Services['cohort'],
import: {} as Services['import'],
ingest: {} as Services['ingest'],
gsc: {
listConnectionsForSync: async () => [
{ projectId: 'p1' },
{ projectId: 'p2' },
],
} as ReturnType<typeof createGscService>,
organization: {} as Services['organization'],
onboarding: {} as Services['onboarding'],
session: {} as Services['session'],
customEvent: {} as Services['customEvent'],
event: {} as Services['event'],
profile: {} as Services['profile'],
group: {} as Services['group'],
chart: {} as Services['chart'],
funnel: {} as Services['funnel'],
conversion: {} as Services['conversion'],
sankey: {} as Services['sankey'],
retention: {} as Services['retention'],
overview: {} as Services['overview'],
pages: {} as Services['pages'],
realtime: {} as Services['realtime'],
misc: {} as Services['misc'],
report: {} as Services['report'],
dashboard: {} as Services['dashboard'],
export: {} as Services['export'],
share: {} as Services['share'],
reference: {} as Services['reference'],
client: {} as Services['client'],
project: {} as Services['project'],
user: {} as Services['user'],
subscription: {} as Services['subscription'],
salt: {} as Services['salt'],
conversation: {} as Services['conversation'],
assistant: {} as Services['assistant'],
mcp: {} as Services['mcp'],
ai: {} as Services['ai'],
},
requestId: 'req_1',
job: { id: 'job_1', attempt: 0, queue: 'cron', name: 'gscSync' },
};
await gscCronJobs.gscSync.handler({ payload: null, ctx });
expect(producers.recorded).toHaveLength(2);
expect(producers.recorded.map((r) => r.job)).toEqual([
'gscProjectSync',
'gscProjectSync',
]);
expect(producers.recorded.map((r) => r.payload)).toEqual([
{ projectId: 'p1' },
{ projectId: 'p2' },
]);
});
// Pins the id/cadence, read straight off the registry.
test('the gsc cron fragment keeps its scheduler id and cadence', () => {
expect(queues.cron.jobs.gscSync.cron).toEqual({ pattern: '0 3 * * *' });
});