import { describe, it, expect } from 'vitest';
import { formatAuthorName, formatAuthorDisplayName, formatPublishedAt } from './utils';

describe('formatAuthorName', () => {
	it('joins firstName and lastName with a space', () => {
		expect(formatAuthorName({ firstName: 'Anna', lastName: 'Schmidt' })).toBe('Anna Schmidt');
	});

	it('returns firstName alone when lastName is absent', () => {
		expect(formatAuthorName({ firstName: 'Anna' })).toBe('Anna');
	});

	it('returns lastName alone when firstName is absent', () => {
		expect(formatAuthorName({ lastName: 'Schmidt' })).toBe('Schmidt');
	});

	it('falls back to [Unbekannt] when both names are absent', () => {
		expect(formatAuthorName({})).toBe('[Unbekannt]');
	});

	it('returns empty string for null input', () => {
		expect(formatAuthorName(null)).toBe('');
	});

	it('returns empty string for undefined input', () => {
		expect(formatAuthorName(undefined)).toBe('');
	});
});

describe('formatAuthorDisplayName', () => {
	it('returns displayName when present', () => {
		expect(formatAuthorDisplayName({ displayName: 'Anna Schmidt' })).toBe('Anna Schmidt');
	});

	it('returns empty string for null input', () => {
		expect(formatAuthorDisplayName(null)).toBe('');
	});

	it('returns empty string for undefined input', () => {
		expect(formatAuthorDisplayName(undefined)).toBe('');
	});
});

describe('formatPublishedAt', () => {
	it('returns null for null input', () => {
		expect(formatPublishedAt(null)).toBeNull();
	});

	it('returns null for undefined input', () => {
		expect(formatPublishedAt(undefined)).toBeNull();
	});

	it('formats an ISO datetime string to a localised date', () => {
		const result = formatPublishedAt('2026-04-15T10:00:00Z', 'short');
		expect(result).not.toBeNull();
		expect(result).toContain('2026');
	});

	it('slices to date-only before formatting (no TZ off-by-one)', () => {
		// Both dates should format identically regardless of timezone offset
		const a = formatPublishedAt('2026-04-15T00:00:00Z', 'short');
		const b = formatPublishedAt('2026-04-15T23:59:59Z', 'short');
		expect(a).toBe(b);
	});
});
