import { describe, it, expect } from 'vitest';
import { parseNotificationEvent } from '$lib/notification/notifications';

describe('parseNotificationEvent', () => {
	const valid = {
		id: '00000000-0000-0000-0000-000000000001',
		documentId: '00000000-0000-0000-0000-000000000002',
		actorName: 'Anna Müller',
		type: 'MENTION',
		referenceId: '00000000-0000-0000-0000-000000000003',
		annotationId: null,
		read: false,
		createdAt: '2024-06-15T10:00:00',
		documentTitle: 'Geburtsurkunde Opa Karl'
	};

	it('should return parsed object for a valid payload', () => {
		const result = parseNotificationEvent(JSON.stringify(valid));
		expect(result).not.toBeNull();
		expect(result?.id).toBe(valid.id);
		expect(result?.actorName).toBe('Anna Müller');
	});

	it('should return null for invalid JSON', () => {
		expect(parseNotificationEvent('not-json')).toBeNull();
	});

	it('should return null when id is missing', () => {
		// eslint-disable-next-line @typescript-eslint/no-unused-vars
		const { id, ...noId } = valid;
		expect(parseNotificationEvent(JSON.stringify(noId))).toBeNull();
	});

	it('should return null when documentId is missing', () => {
		// eslint-disable-next-line @typescript-eslint/no-unused-vars
		const { documentId, ...noDocId } = valid;
		expect(parseNotificationEvent(JSON.stringify(noDocId))).toBeNull();
	});

	it('should return null when actorName is missing', () => {
		// eslint-disable-next-line @typescript-eslint/no-unused-vars
		const { actorName, ...noActor } = valid;
		expect(parseNotificationEvent(JSON.stringify(noActor))).toBeNull();
	});

	it('should return null for unknown notification type', () => {
		expect(parseNotificationEvent(JSON.stringify({ ...valid, type: 'UNKNOWN' }))).toBeNull();
	});

	it('should accept REPLY as a valid type', () => {
		const result = parseNotificationEvent(JSON.stringify({ ...valid, type: 'REPLY' }));
		expect(result).not.toBeNull();
		expect(result?.type).toBe('REPLY');
	});
});
