import { resolvePrismaSlowQueryThreshold, shouldLogPrismaQuery } from './prisma-monitoring';

describe('prisma monitoring helpers', () => {
  const originalEnv = process.env.PRISMA_SLOW_QUERY_MS;

  afterEach(() => {
    process.env.PRISMA_SLOW_QUERY_MS = originalEnv;
  });

  it('uses a safe default threshold when env is missing or invalid', () => {
    delete process.env.PRISMA_SLOW_QUERY_MS;
    expect(resolvePrismaSlowQueryThreshold()).toBe(300);

    process.env.PRISMA_SLOW_QUERY_MS = '-1';
    expect(resolvePrismaSlowQueryThreshold()).toBe(300);
  });

  it('uses a positive configured threshold', () => {
    process.env.PRISMA_SLOW_QUERY_MS = '750';

    expect(resolvePrismaSlowQueryThreshold()).toBe(750);
  });

  it('detects slow query events at or above the threshold', () => {
    expect(shouldLogPrismaQuery(299, 300)).toBe(false);
    expect(shouldLogPrismaQuery(300, 300)).toBe(true);
    expect(shouldLogPrismaQuery(450, 300)).toBe(true);
  });
});
