import { describe, it, expect, beforeAll, afterAll } from 'vitest' import * as db from '../src/database.js' import { registerSubscription } from '../src/actions.js' import { getJobCronSource, removeJob } from '../src/worker/email.js' import { registerSubscription as regSub } from '../src/worker/index.js' const pubkey = 'freq-test-' + Date.now() const email = 'freq-test-' + Date.now() + '@example.com' let sub: any = null describe('Frequency change reschedules cron job', () => { beforeAll(async () => { await db.migrate() }) it('creates confirmed subscription with daily frequency', async () => { const s = await db.insertSubscription(pubkey, email, 'daily') expect(s).toBeTruthy() sub = s const confirmed = await db.confirmSubscription(sub.key) expect(confirmed).toBeTruthy() sub = confirmed!.sub }) it('registers cron job with daily frequency', () => { regSub(sub) const dailySource = getJobCronSource(sub.id) expect(dailySource).toBe('0 0 17 * * *') }) it('changes frequency to weekly via registerSubscription', async () => { await registerSubscription({ pubkey, email, frequency: 'weekly' }) const weeklySource = getJobCronSource(sub.id) expect(weeklySource).toBe('0 0 17 * * 1') }) }) afterAll(async () => { const updated = await db.getSubscriptionByPubkey(pubkey) if (updated) removeJob(updated) })