import { Lock } from '../../src/libs/lock';

describe('Lock', () => {
  let lock: Lock;

  beforeEach(() => {
    lock = new Lock('test');
  });

  it('should acquire the lock when it is not locked', async () => {
    const result = await lock.acquire();
    expect(result).toBe(true);
    expect(lock.locked).toBe(true);
  });

  it('should wait to acquire the lock when it is already locked', async () => {
    lock.locked = true;

    setTimeout(() => lock.release(), 200); // Release the lock after 1 second

    const result = await lock.acquire(); // This should wait for the lock to be released
    expect(result).toBe(true);
    expect(lock.locked).toBe(true);
  });

  it('should release the lock', () => {
    lock.locked = true;
    lock.release();
    expect(lock.locked).toBe(false);
  });
});
