immich/web/src/lib/utils/date-time.spec.ts
Johann 662d44536e
feat(web): add geolocation utility (#20758)
* feat(geolocation):  add geolocation utility

* feat(web): geolocation utility - fix code review - 1

* feat(web): geolocation utility - fix code review - 2

* chore: cleanup

* chore: feedback

* feat(web): add animation and text

animation on locations change and action text on thumbnail

* styling, messages and filtering

* selected color

* format i18n

* fix lint

---------

Co-authored-by: Jason Rasmussen <jason@rasm.me>
Co-authored-by: Alex <alex.tran1502@gmail.com>
2025-08-28 16:54:11 +00:00

99 lines
3.1 KiB
TypeScript

import { writable } from 'svelte/store';
import { buildDateRangeFromYearMonthAndDay, getAlbumDateRange, timeToSeconds } from './date-time';
describe('converting time to seconds', () => {
it('parses hh:mm:ss correctly', () => {
expect(timeToSeconds('01:02:03')).toBeCloseTo(3723);
});
it('parses hh:mm:ss.SSS correctly', () => {
expect(timeToSeconds('01:02:03.456')).toBeCloseTo(3723.456);
});
it('parses h:m:s.S correctly', () => {
expect(timeToSeconds('1:2:3.4')).toBe(0); // Non-standard format, Luxon returns NaN
});
it('parses hhh:mm:ss.SSS correctly', () => {
expect(timeToSeconds('100:02:03.456')).toBe(0); // Non-standard format, Luxon returns NaN
});
it('ignores ignores double milliseconds hh:mm:ss.SSS.SSSSSS', () => {
expect(timeToSeconds('01:02:03.456.123456')).toBe(0); // Non-standard format, Luxon returns NaN
});
// Test edge cases that can cause crashes
it('handles "0" string input', () => {
expect(timeToSeconds('0')).toBe(0);
});
it('handles empty string input', () => {
expect(timeToSeconds('')).toBe(0);
});
it('parses HH:MM format correctly', () => {
expect(timeToSeconds('01:02')).toBe(3720); // 1 hour 2 minutes = 3720 seconds
});
it('handles malformed time strings', () => {
expect(timeToSeconds('invalid')).toBe(0);
});
it('parses single hour format correctly', () => {
expect(timeToSeconds('01')).toBe(3600); // Luxon interprets "01" as 1 hour
});
it('handles time strings with invalid numbers', () => {
expect(timeToSeconds('aa:bb:cc')).toBe(0);
expect(timeToSeconds('01:bb:03')).toBe(0);
});
});
describe('getAlbumDate', () => {
beforeAll(() => {
process.env.TZ = 'UTC';
vitest.mock('$lib/stores/preferences.store', () => ({
locale: writable('en'),
}));
});
it('should work with only a start date', () => {
expect(getAlbumDateRange({ startDate: '2021-01-01T00:00:00Z' })).toEqual('Jan 1, 2021');
});
it('should work with a start and end date', () => {
expect(
getAlbumDateRange({
startDate: '2021-01-01T00:00:00Z',
endDate: '2021-01-05T00:00:00Z',
}),
).toEqual('Jan 1, 2021 - Jan 5, 2021');
});
it('should work with the new date format', () => {
expect(getAlbumDateRange({ startDate: '2021-01-01T00:00:00+05:00' })).toEqual('Jan 1, 2021');
});
});
describe('buildDateRangeFromYearMonthAndDay', () => {
it('should build correct date range for a specific day', () => {
const result = buildDateRangeFromYearMonthAndDay(2023, 1, 8);
expect(result.from).toContain('2023-01-08T00:00:00');
expect(result.to).toContain('2023-01-09T00:00:00');
});
it('should build correct date range for a month', () => {
const result = buildDateRangeFromYearMonthAndDay(2023, 2);
expect(result.from).toContain('2023-02-01T00:00:00');
expect(result.to).toContain('2023-03-01T00:00:00');
});
it('should build correct date range for a year', () => {
const result = buildDateRangeFromYearMonthAndDay(2023);
expect(result.from).toContain('2023-01-01T00:00:00');
expect(result.to).toContain('2024-01-01T00:00:00');
});
});