import { describe, it, expect, vi } from 'vitest'
import { withAdvisoryLock } from './locking.js'

describe('withAdvisoryLock', () => {
  it('postgres: executes lock query then fn', async () => {
    const execute = vi.fn().mockResolvedValue(undefined)
    const db = { execute } as any
    const fn = vi.fn().mockResolvedValue('result')

    const result = await withAdvisoryLock(db, 123n, fn, 'postgres')

    expect(execute).toHaveBeenCalledTimes(1)
    expect(fn).toHaveBeenCalledTimes(1)
    expect(result).toBe('result')
  })

  it('sqlite: skips lock query, runs fn directly', async () => {
    const execute = vi.fn()
    const db = { execute } as any
    const fn = vi.fn().mockResolvedValue('result')

    const result = await withAdvisoryLock(db, 123n, fn, 'sqlite')

    expect(execute).not.toHaveBeenCalled()
    expect(fn).toHaveBeenCalledTimes(1)
    expect(result).toBe('result')
  })
})