import { describe, expect, it } from 'bun:test'; import { createProxyUrl, sleep } from '@stock-bot/utils/common'; import { dateUtils, utcDates } from '@stock-bot/utils/dates'; describe('restored general utilities', () => { describe('common utilities', () => { it('should create proxy URL with auth', () => { const proxy = { protocol: 'http', host: '192.168.1.1', port: 8080, username: 'user', password: 'pass', }; const url = createProxyUrl(proxy); expect(url).toBe('http://user:pass@192.168.1.1:8080'); }); it('should create proxy URL without auth', () => { const proxy = { protocol: 'socks5', host: '192.168.1.1', port: 1080, }; const url = createProxyUrl(proxy); expect(url).toBe('socks5://192.168.1.1:1080'); }); it('should sleep for specified milliseconds', async () => { const start = Date.now(); await sleep(100); const elapsed = Date.now() - start; expect(elapsed).toBeGreaterThanOrEqual(90); }); it('UTC weekday helpers preserve the supplied instant time and never mutate inputs', () => { const friday = new Date('2024-03-08T23:30:00.000Z'); expect(utcDates.nextWeekday(friday).toISOString()).toBe('2024-03-11T23:30:00.000Z'); expect(friday.toISOString()).toBe('2024-03-08T23:30:00.000Z'); expect(utcDates.previousWeekday(new Date('2024-03-11T23:30:00Z')).toISOString()).toBe( '2024-03-08T23:30:00.000Z' ); // Christmas is a weekday, not a promise that any particular exchange is open. expect(utcDates.isWeekday(new Date('2023-12-25T00:00:00Z'))).toBe(true); const detached = utcDates.nextWeekday; expect(detached(friday).getUTCDay()).toBe(1); }); it('strict UTC dates reject invalid inputs and impossible calendar dates', () => { expect(utcDates.parseDate('2024-02-29').toISOString()).toBe('2024-02-29T00:00:00.000Z'); for (const value of ['2023-02-29', '2024-02-30', '2024-13-01', '2024-01-01T00:00:00Z', '']) { expect(() => utcDates.parseDate(value)).toThrow(); } expect(() => utcDates.nextWeekday(new Date(NaN))).toThrow(); expect(() => utcDates.formatDate(new Date(NaN))).toThrow(); }); it('proxy credentials remain encoded and sleep remains asynchronous', async () => { expect( createProxyUrl({ protocol: 'http', host: 'localhost', port: 8080, username: 'a@b', password: 'p:/?', }) ).toBe('http://a%40b:p%3A%2F%3F@localhost:8080'); const wait = sleep(0); expect(wait).toBeInstanceOf(Promise); await wait; }); }); describe('date utilities', () => { it('should check if date is trading day', () => { const monday = new Date('2023-12-25'); // Monday const saturday = new Date('2023-12-23'); // Saturday const sunday = new Date('2023-12-24'); // Sunday expect(dateUtils.isTradingDay(monday)).toBe(true); expect(dateUtils.isTradingDay(saturday)).toBe(false); expect(dateUtils.isTradingDay(sunday)).toBe(false); }); it('should get next trading day', () => { const friday = new Date('2023-12-22'); // Friday const nextDay = dateUtils.getNextTradingDay(friday); expect(nextDay.getUTCDay()).toBe(1); // Monday }); it('should get previous trading day', () => { const monday = new Date('2023-12-25'); // Monday const prevDay = dateUtils.getPreviousTradingDay(monday); expect(prevDay.getUTCDay()).toBe(5); // Friday }); it('should format date as YYYY-MM-DD', () => { const date = new Date('2023-12-25T10:30:00Z'); const formatted = dateUtils.formatDate(date); expect(formatted).toBe('2023-12-25'); }); it('should parse date from string', () => { const date = dateUtils.parseDate('2023-12-25'); expect(date.getUTCFullYear()).toBe(2023); expect(date.getUTCMonth()).toBe(11); // 0-based expect(date.getUTCDate()).toBe(25); }); }); });