import { describe, it, expect } from 'bun:test';
import Lock from '@common/lock';

describe('Lock', () => {
    describe('constructor', () => {
        it('should create an unlocked lock', () => {
            const lock = new Lock();
            expect(lock.locked).toBe(false);
        });
    });

    describe('locked', () => {
        it('should return false when lock is not acquired', () => {
            const lock = new Lock();
            expect(lock.locked).toBe(false);
        });

        it('should return true after wait is called', () => {
            const lock = new Lock();
            lock.wait();
            expect(lock.locked).toBe(true);
        });
    });

    describe('wait', () => {
        it('should return a promise', () => {
            const lock = new Lock();
            const result = lock.wait();
            expect(result).toBeInstanceOf(Promise);
        });
    });

    describe('release', () => {
        it('should not throw when releasing an unlocked lock', () => {
            const lock = new Lock();
            expect(() => lock.release()).not.toThrow();
        });

        it('should unlock the lock after wait', () => {
            const lock = new Lock();
            lock.wait();
            lock.release();
            expect(lock.locked).toBe(false);
        });
    });
});
