import { afterEach, describe, expect, it, vi } from 'vitest';
const sentryMocks = vi.hoisted(() => ({
    init: vi.fn(),
    captureException: vi.fn(),
}));
vi.mock('@sentry/node', () => ({
    init: sentryMocks.init,
    captureException: sentryMocks.captureException,
}));
const { initServerSentry, captureServerException } = await import('../../diagnostics/sentry.js');
describe('server Sentry', () => {
    const originalDsn = process.env.SENTRY_DSN;
    const originalRelease = process.env.SENTRY_RELEASE;
    const originalVercelEnv = process.env.VERCEL_ENV;
    afterEach(() => {
        if (originalDsn === undefined)
            delete process.env.SENTRY_DSN;
        else
            process.env.SENTRY_DSN = originalDsn;
        if (originalRelease === undefined)
            delete process.env.SENTRY_RELEASE;
        else
            process.env.SENTRY_RELEASE = originalRelease;
        if (originalVercelEnv === undefined)
            delete process.env.VERCEL_ENV;
        else
            process.env.VERCEL_ENV = originalVercelEnv;
        sentryMocks.init.mockClear();
        sentryMocks.captureException.mockClear();
    });
    it('does not initialize when SENTRY_DSN is unset', () => {
        delete process.env.SENTRY_DSN;
        initServerSentry();
        expect(sentryMocks.init).not.toHaveBeenCalled();
    });
    it('initializes once with release and environment tags', () => {
        process.env.SENTRY_DSN = 'https://example@sentry.io/1';
        process.env.SENTRY_RELEASE = 'actuate@1.2.3';
        process.env.VERCEL_ENV = 'production';
        initServerSentry();
        initServerSentry();
        expect(sentryMocks.init).toHaveBeenCalledTimes(1);
        expect(sentryMocks.init).toHaveBeenCalledWith(expect.objectContaining({
            dsn: 'https://example@sentry.io/1',
            environment: 'production',
            release: 'actuate@1.2.3',
            tracesSampleRate: 0,
        }));
    });
    it('captures exceptions with scope metadata when configured', () => {
        process.env.SENTRY_DSN = 'https://example@sentry.io/1';
        const err = new Error('boom');
        captureServerException(err, { scope: 'api', extra: { context: 'cron cleanup' } });
        expect(sentryMocks.captureException).toHaveBeenCalledWith(err, {
            tags: { scope: 'api' },
            extra: { context: 'cron cleanup' },
        });
    });
});
//# sourceMappingURL=sentry.test.js.map