import { tap, expect } from '@push.rocks/tapbundle'; import { LimitedArray } from '../ts/index.js'; let testLimitedArray: LimitedArray; tap.test('should create a LimitedArray', async () => { testLimitedArray = new LimitedArray(6); expect(testLimitedArray).toBeInstanceOf(LimitedArray); expect(testLimitedArray.length).toEqual(0); }); tap.test('should never be longer than the set length', async () => { testLimitedArray.addMany(['hi', 'this', 'is', 'quite', 'a', 'long', 'string', ':)']); expect(testLimitedArray.array.length < 7).toBeTrue(); expect(testLimitedArray.length).toEqual(6); }); tap.test('addOne respects limit', async () => { const arr = new LimitedArray(3); arr.addOne(1); arr.addOne(2); arr.addOne(3); arr.addOne(4); expect(arr.length).toEqual(3); }); tap.test('setLimit truncates when lowered', async () => { const arr = new LimitedArray(5); arr.addMany([1, 2, 3, 4, 5]); expect(arr.length).toEqual(5); arr.setLimit(2); expect(arr.length).toEqual(2); }); tap.test('getAverage returns correct average for numbers', async () => { const arr = new LimitedArray(10); arr.addMany([10, 20, 30]); expect(arr.getAverage()).toEqual(20); }); tap.test('getAverage returns 0 for empty array', async () => { const arr = new LimitedArray(10); expect(arr.getAverage()).toEqual(0); }); tap.test('getAverage returns null for non-number array', async () => { const arr = new LimitedArray(10); arr.addOne('hello'); expect(arr.getAverage()).toBeNull(); }); tap.test('remove removes an item', async () => { const arr = new LimitedArray(10); arr.addMany(['a', 'b', 'c']); const removed = arr.remove('b'); expect(removed).toBeTrue(); expect(arr.length).toEqual(2); const notFound = arr.remove('zzz'); expect(notFound).toBeFalse(); }); tap.test('clear empties the array', async () => { const arr = new LimitedArray(10); arr.addMany([1, 2, 3]); arr.clear(); expect(arr.length).toEqual(0); }); tap.test('getArray returns a copy', async () => { const arr = new LimitedArray(10); arr.addMany([1, 2, 3]); const copy = arr.getArray(); expect(copy.length).toEqual(3); copy.push(99); expect(arr.length).toEqual(3); }); tap.test('Symbol.iterator enables for...of', async () => { const arr = new LimitedArray(10); arr.addMany([5, 10, 15]); const collected: number[] = []; for (const item of arr) { collected.push(item); } expect(collected.length).toEqual(3); }); export default tap.start();