import { describe, expect, it } from 'bun:test' import type { QueryEntry } from '../src/types' import { analyzeQueries, isBookkeepingSql, normalizeSql } from '../src/sql' function query(sql: string, params: unknown[] = [], duration: number = 1, extra: Partial = {}): QueryEntry { return { sql, params, start: 0, duration, ...extra } } const options = { slowThresholdMs: 100, nPlusOneThreshold: 5 } describe('statement shapes', () => { it('ignores values, whitespace and case', () => { const shape = normalizeSql('select * from users where id = ?') expect(normalizeSql('SELECT * FROM users\n WHERE id = 42')).toBe(shape) expect(normalizeSql('select * from users where id = $1')).toBe(shape) expect(normalizeSql('select * from users where email = \'a@b.c\'')).toBe(normalizeSql('select * from users where email = ?')) expect(normalizeSql('select * from users where id in (1, 2, 3)')).toBe(normalizeSql('select * from users where id in (?)')) }) it('keeps different tables apart', () => { expect(normalizeSql('select * from users where id = 1')).not.toBe(normalizeSql('select * from cars where id = 1')) }) it('knows the framework\'s own bookkeeping', () => { expect(isBookkeepingSql('INSERT INTO query_logs (query) VALUES (?)')).toBe(true) expect(isBookkeepingSql('EXPLAIN SELECT 1')).toBe(true) expect(isBookkeepingSql('PRAGMA foreign_keys = ON')).toBe(true) expect(isBookkeepingSql('SELECT id FROM query_logs WHERE executed_at < ? LIMIT 5000')).toBe(true) expect(isBookkeepingSql('DELETE FROM "query_logs" WHERE id IN (?)')).toBe(true) expect(isBookkeepingSql('select * from query_logbook')).toBe(false) expect(isBookkeepingSql('select * from users where note = ?')).toBe(false) }) }) describe('query analysis', () => { it('counts exact repeats as duplicates', () => { const section = analyzeQueries([ query('select * from cars where id = ?', [1]), query('select * from cars where id = ?', [1]), query('select * from cars where id = ?', [1]), query('select * from cars where id = ?', [2]), ], options) expect(section.duplicates).toBe(2) expect(section.statements.map(statement => statement.duplicates)).toEqual([3, 3, 3, undefined]) expect(section.statements.map(statement => statement.similar)).toEqual([4, 4, 4, 4]) expect(section.nPlusOne).toEqual([]) }) it('flags a read shape repeated to the threshold as a possible N+1', () => { const statements = [ query('select * from cars limit 5'), ...Array.from({ length: 5 }, (_, index) => query('select * from owners where car_id = ?', [index + 1], 1, { source: 'app/Actions/CarIndexAction.ts:12' })), ] const section = analyzeQueries(statements, options) expect(section.nPlusOne).toHaveLength(1) expect(section.nPlusOne[0]).toMatchObject({ count: 5, sample: 'select * from owners where car_id = ?', source: 'app/Actions/CarIndexAction.ts:12' }) expect(section.duplicates).toBe(0) }) it('does not call four repeats, or repeated writes, an N+1', () => { expect(analyzeQueries(Array.from({ length: 4 }, (_, index) => query('select * from owners where car_id = ?', [index])), options).nPlusOne).toEqual([]) expect(analyzeQueries(Array.from({ length: 9 }, (_, index) => query('insert into logs (line) values (?)', [index])), options).nPlusOne).toEqual([]) }) it('honours the configured threshold', () => { const statements = Array.from({ length: 3 }, (_, index) => query('select * from owners where car_id = ?', [index])) expect(analyzeQueries(statements, { ...options, nPlusOneThreshold: 3 }).nPlusOne).toHaveLength(1) }) it('marks slow and failed statements and totals the time', () => { const section = analyzeQueries([ query('select 1', [], 5), query('select * from big', [], 250), query('select * from missing', [], 2, { error: 'no such table' }), ], { ...options, dropped: 4 }) expect(section.duration).toBe(257) expect(section.slow).toBe(1) expect(section.failed).toBe(1) expect(section.statements[1]!.slow).toBe(true) expect(section.count).toBe(7) expect(section.dropped).toBe(4) }) })