/**
* Unit tests for Spotify API utility functions
*/
import { describe, it, expect } from 'vitest';
import { formatDuration, calculateProgress } from '../../src/tools/spotify-api.js';
describe('Spotify API Utilities', () => {
describe('formatDuration', () => {
it('should format duration under 1 minute correctly', () => {
expect(formatDuration(30000)).toBe('0:30');
expect(formatDuration(5000)).toBe('0:05');
expect(formatDuration(59999)).toBe('0:59');
});
it('should format duration under 1 hour correctly', () => {
expect(formatDuration(60000)).toBe('1:00');
expect(formatDuration(90000)).toBe('1:30');
expect(formatDuration(222973)).toBe('3:42'); // Mr. Brightside duration
expect(formatDuration(600000)).toBe('10:00');
expect(formatDuration(3599999)).toBe('59:59');
});
it('should format duration over 1 hour correctly', () => {
expect(formatDuration(3600000)).toBe('1:00:00');
expect(formatDuration(3661000)).toBe('1:01:01');
expect(formatDuration(7200000)).toBe('2:00:00');
expect(formatDuration(7384000)).toBe('2:03:04');
});
it('should handle edge cases', () => {
expect(formatDuration(0)).toBe('0:00');
expect(formatDuration(1000)).toBe('0:01');
expect(formatDuration(999)).toBe('0:00');
});
it('should pad single digit seconds and minutes', () => {
expect(formatDuration(65000)).toBe('1:05');
expect(formatDuration(125000)).toBe('2:05');
expect(formatDuration(3605000)).toBe('1:00:05');
});
});
describe('calculateProgress', () => {
it('should calculate progress percentage correctly', () => {
expect(calculateProgress(0, 100000)).toBe(0);
expect(calculateProgress(50000, 100000)).toBe(50);
expect(calculateProgress(100000, 100000)).toBe(100);
expect(calculateProgress(25000, 100000)).toBe(25);
expect(calculateProgress(75000, 100000)).toBe(75);
});
it('should round to nearest integer', () => {
expect(calculateProgress(33333, 100000)).toBe(33);
expect(calculateProgress(66666, 100000)).toBe(67);
expect(calculateProgress(12345, 100000)).toBe(12);
});
it('should handle edge cases', () => {
expect(calculateProgress(0, 0)).toBe(0);
expect(calculateProgress(100, 0)).toBe(0);
expect(calculateProgress(0, 222973)).toBe(0);
expect(calculateProgress(222973, 222973)).toBe(100);
});
it('should work with real Spotify track durations', () => {
// Mr. Brightside: 222973ms (~3:43)
expect(calculateProgress(89523, 222973)).toBe(40); // ~40% through
expect(calculateProgress(111486, 222973)).toBe(50); // ~50% through
expect(calculateProgress(200000, 222973)).toBe(90); // ~90% through
});
});
});