/* Component test for History molecule.
 * Copyright (C) 2024 André Jaenisch
 * SPDX-FileCopyrightText: 2024 André Jaenisch
 * SPDX-License-Identifier: AGPL-3.0-or-later
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/svelte';
import { init, locale, register } from 'svelte-i18n';

import History from '../../../src/lib/components/molecules/History.svelte';
import enMessages from '../../../src/lib/i18n/locales/en.json';

describe('History.svelte', () => {
	beforeEach(() => {
		register('en', () => import('../../../src/lib/i18n/locales/en.json'));
		init({ fallbackLocale: 'en', initialLocale: 'en' });
		locale.set('en');
	});

	it('should mount', () => {
		// Arrange
		// Nothing to prepare

		// Act
		const { container } = render(History);

		// Assert
		expect(container).toBeTruthy();
	});

	describe('when there are no commits', () => {
		it('should show nothing', () => {
			// Arrange
			const commitsMap = [];
			const followingsMap = [];

			// Act
			render(History, { commitsMap, followingsMap });
			const listItems = screen.queryAllByRole('listitem');

			// Assert
			expect(listItems).toHaveLength(0);
		});
	});

	describe('when there are commits', () => {
		describe('when they are all on the same date', () => {
			describe('when they are all on the same project', () => {
				describe('when there is a single commit', () => {
					it('should show the commit by date and project', () => {
						// Arrange
						// TODO: Figure out why beforeEach is not executed
						register('en', () => import('../../../src/lib/i18n/locales/en.json'));
						init({ fallbackLocale: 'en', initialLocale: 'en' });
						locale.set('en');

						const commitsMap = [
							{
								'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
								id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
								type: 'Commit',
								context: 'https://fig.fr33domlover.site/repos/9nOkn',
								attributedTo: 'https://fig.fr33domlover.site/people/qn870',
								committedBy: {
									name: 'vervis',
									mbox: 'mailto:vervis@vervis.vervis'
								},
								name: 'Best commit ever',
								hash: '36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
								created: '2022-09-28T16:01:30Z',
								committed: '2022-09-28T16:06:36Z'
							}
						];

						const followingsMap = [
							{
								'@context': [
									'https://www.w3.org/ns/activitystreams',
									'https://w3id.org/security/v2',
									'https://forgefed.org/ns'
								],
								id: 'https://fig.fr33domlover.site/repos/9nOkn',
								inbox: 'https://fig.fr33domlover.site/repos/9nOkn/inbox',
								outbox: 'https://fig.fr33domlover.site/repos/9nOkn/outbox',
								followers: 'https://fig.fr33domlover.site/repos/9nOkn/followers',
								publicKey: [
									'https://fig.fr33domlover.site/akey1',
									'https://fig.fr33domlover.site/akey2'
								],
								type: 'Repository',
								name: 'Very fun demo',
								summary: 'Testing MR demo',
								team: null,
								versionControlSystem: 'https://forgefed.org/ns#git',
								sendPatchesTo: 'https://fig.fr33domlover.site/looms/9nOkn',
								cloneUri: 'https://fig.fr33domlover.site/repos/9nOkn',
								collaborators: 'https://fig.fr33domlover.site/repos/9nOkn/collabs',
								context: 'https://fig.fr33domlover.site/repos/9nOkn/projects'
							}
						];

						// Act
						render(History, { commitsMap, followingsMap });
						const listItems = screen.getAllByRole('listitem');

						// Assert
						// One listitem because of date and one for the commit
						expect(listItems).toHaveLength(1 + 1);
					});
				});

				describe('when there are multiple commits', () => {
					it('should group all commits together', () => {
						// Arrange
						// TODO: Figure out why beforeEach is not executed
						register('en', () => import('../../../src/lib/i18n/locales/en.json'));
						init({ fallbackLocale: 'en', initialLocale: 'en' });
						locale.set('en');

						const commitsMap = [
							{
								'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
								id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
								type: 'Commit',
								context: 'https://fig.fr33domlover.site/repos/9nOkn',
								attributedTo: 'https://fig.fr33domlover.site/people/qn870',
								committedBy: {
									name: 'vervis',
									mbox: 'mailto:vervis@vervis.vervis'
								},
								name: 'Best commit ever',
								hash: '36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
								created: '2022-09-28T16:01:30Z',
								committed: '2022-09-28T16:06:36Z'
							},
							{
								'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
								id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/d6e409a6b536e026c95f64d2732946ba0136ddb1',
								type: 'Commit',
								context: 'https://fig.fr33domlover.site/repos/9nOkn',
								attributedTo: 'https://fig.fr33domlover.site/people/qn870',
								committedBy: {
									name: 'vervis',
									mbox: 'mailto:vervis@vervis.vervis'
								},
								name: 'Add more cool content',
								hash: 'd6e409a6b536e026c95f64d2732946ba0136ddb1',
								created: '2022-09-28T17:01:30Z',
								committed: '2022-09-28T17:06:36Z'
							}
						];

						const followingsMap = [
							{
								'@context': [
									'https://www.w3.org/ns/activitystreams',
									'https://w3id.org/security/v2',
									'https://forgefed.org/ns'
								],
								id: 'https://fig.fr33domlover.site/repos/9nOkn',
								inbox: 'https://fig.fr33domlover.site/repos/9nOkn/inbox',
								outbox: 'https://fig.fr33domlover.site/repos/9nOkn/outbox',
								followers: 'https://fig.fr33domlover.site/repos/9nOkn/followers',
								publicKey: [
									'https://fig.fr33domlover.site/akey1',
									'https://fig.fr33domlover.site/akey2'
								],
								type: 'Repository',
								name: 'Very fun demo',
								summary: 'Testing MR demo',
								team: null,
								versionControlSystem: 'https://forgefed.org/ns#git',
								sendPatchesTo: 'https://fig.fr33domlover.site/looms/9nOkn',
								cloneUri: 'https://fig.fr33domlover.site/repos/9nOkn',
								collaborators: 'https://fig.fr33domlover.site/repos/9nOkn/collabs',
								context: 'https://fig.fr33domlover.site/repos/9nOkn/projects'
							}
						];

						// Act
						render(History, { commitsMap, followingsMap });
						const listItems = screen.getAllByRole('listitem');

						// Assert
						// One listitem for the date and two due to commits
						expect(listItems).toHaveLength(1 + 2);
					});
				});
			});

			describe('when they are on different projects', () => {
				it('should spread them out', () => {
					// Arrange
					// TODO: Figure out why beforeEach is not executed
					register('en', () => import('../../../src/lib/i18n/locales/en.json'));
					init({ fallbackLocale: 'en', initialLocale: 'en' });
					locale.set('en');

					const commitsMap = [
						{
							'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
							id: 'https://fig.fr33domlover.site/repos/Pn9Yn/commits/36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
							type: 'Commit',
							context: 'https://fig.fr33domlover.site/repos/Pn9Yn',
							attributedTo: 'https://fig.fr33domlover.site/people/qn870',
							committedBy: {
								name: 'vervis',
								mbox: 'mailto:vervis@vervis.vervis'
							},
							name: 'Best commit ever',
							hash: '36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
							created: '2022-09-28T16:01:30Z',
							committed: '2022-09-28T16:06:36Z'
						},
						{
							'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
							id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/d6e409a6b536e026c95f64d2732946ba0136ddb1',
							type: 'Commit',
							context: 'https://fig.fr33domlover.site/repos/9nOkn',
							attributedTo: 'https://fig.fr33domlover.site/people/qn870',
							committedBy: {
								name: 'vervis',
								mbox: 'mailto:vervis@vervis.vervis'
							},
							name: 'Add more cool content',
							hash: 'd6e409a6b536e026c95f64d2732946ba0136ddb1',
							created: '2022-09-28T17:01:30Z',
							committed: '2022-09-28T17:06:36Z'
						}
					];

					const followingsMap = [
						{
							'@context': [
								'https://www.w3.org/ns/activitystreams',
								'https://w3id.org/security/v2',
								'https://forgefed.org/ns'
							],
							id: 'https://fig.fr33domlover.site/repos/Pn9Yn',
							inbox: 'https://fig.fr33domlover.site/repos/Pn9Yn/inbox',
							outbox: 'https://fig.fr33domlover.site/repos/Pn9Yn/outbox',
							followers: 'https://fig.fr33domlover.site/repos/Pn9Yn/followers',
							publicKey: [
								'https://fig.fr33domlover.site/akey1',
								'https://fig.fr33domlover.site/akey2'
							],
							type: 'Repository',
							name: 'Anvil',
							summary: 'Mirror of Anvil so I can develop its UI',
							team: null,
							versionControlSystem: 'https://forgefed.org/ns#git',
							cloneUri: 'https://fig.fr33domlover.site/repos/Pn9Yn',
							collaborators: 'https://fig.fr33domlover.site/repos/Pn9Yn/collabs',
							context: 'https://fig.fr33domlover.site/repos/Pn9Yn/projects'
						},
						{
							'@context': [
								'https://www.w3.org/ns/activitystreams',
								'https://w3id.org/security/v2',
								'https://forgefed.org/ns'
							],
							id: 'https://fig.fr33domlover.site/repos/9nOkn',
							inbox: 'https://fig.fr33domlover.site/repos/9nOkn/inbox',
							outbox: 'https://fig.fr33domlover.site/repos/9nOkn/outbox',
							followers: 'https://fig.fr33domlover.site/repos/9nOkn/followers',
							publicKey: [
								'https://fig.fr33domlover.site/akey1',
								'https://fig.fr33domlover.site/akey2'
							],
							type: 'Repository',
							name: 'Very fun demo',
							summary: 'Testing MR demo',
							team: null,
							versionControlSystem: 'https://forgefed.org/ns#git',
							sendPatchesTo: 'https://fig.fr33domlover.site/looms/9nOkn',
							cloneUri: 'https://fig.fr33domlover.site/repos/9nOkn',
							collaborators: 'https://fig.fr33domlover.site/repos/9nOkn/collabs',
							context: 'https://fig.fr33domlover.site/repos/9nOkn/projects'
						}
					];

					// Act
					render(History, { commitsMap, followingsMap });
					const listItems = screen.getAllByRole('listitem');

					// Assert
					// Two listitems for the projects and two due to commits
					expect(listItems).toHaveLength(2 + 2);
				});
			});
		});

		describe('when they are on different dates', () => {
			it('should spread them out', () => {
				// Arrange
				// TODO: Figure out why beforeEach is not executed
				register('en', () => import('../../../src/lib/i18n/locales/en.json'));
				init({ fallbackLocale: 'en', initialLocale: 'en' });
				locale.set('en');

				const commitsMap = [
					{
						'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
						id: 'https://fig.fr33domlover.site/repos/Pn9Yn/commits/36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
						type: 'Commit',
						context: 'https://fig.fr33domlover.site/repos/Pn9Yn',
						attributedTo: 'https://fig.fr33domlover.site/people/qn870',
						committedBy: {
							name: 'vervis',
							mbox: 'mailto:vervis@vervis.vervis'
						},
						name: 'Best commit ever',
						hash: '36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
						created: '2022-09-28T16:01:30Z',
						committed: '2022-09-28T16:06:36Z'
					},
					{
						'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
						id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
						type: 'Commit',
						context: 'https://fig.fr33domlover.site/repos/9nOkn',
						attributedTo: 'https://fig.fr33domlover.site/people/qn870',
						committedBy: {
							name: 'vervis',
							mbox: 'mailto:vervis@vervis.vervis'
						},
						name: 'Best commit ever',
						hash: '36e1bb146fa3a6fb5c8d490f76ff4cca5f8f2e78',
						created: '2022-09-28T16:01:30Z',
						committed: '2022-09-28T16:06:36Z'
					},
					{
						'@context': ['https://www.w3.org/ns/activitystreams', 'https://forgefed.org/ns'],
						id: 'https://fig.fr33domlover.site/repos/9nOkn/commits/d6e409a6b536e026c95f64d2732946ba0136ddb1',
						type: 'Commit',
						context: 'https://fig.fr33domlover.site/repos/9nOkn',
						attributedTo: 'https://fig.fr33domlover.site/people/qn870',
						committedBy: {
							name: 'vervis',
							mbox: 'mailto:vervis@vervis.vervis'
						},
						name: 'Add more cool content',
						hash: 'd6e409a6b536e026c95f64d2732946ba0136ddb1',
						created: '2022-09-27T17:01:30Z',
						committed: '2022-09-27T17:06:36Z'
					}
				];

				const followingsMap = [
					{
						'@context': [
							'https://www.w3.org/ns/activitystreams',
							'https://w3id.org/security/v2',
							'https://forgefed.org/ns'
						],
						id: 'https://fig.fr33domlover.site/repos/Pn9Yn',
						inbox: 'https://fig.fr33domlover.site/repos/Pn9Yn/inbox',
						outbox: 'https://fig.fr33domlover.site/repos/Pn9Yn/outbox',
						followers: 'https://fig.fr33domlover.site/repos/Pn9Yn/followers',
						publicKey: [
							'https://fig.fr33domlover.site/akey1',
							'https://fig.fr33domlover.site/akey2'
						],
						type: 'Repository',
						name: 'Anvil',
						summary: 'Mirror of Anvil so I can develop its UI',
						team: null,
						versionControlSystem: 'https://forgefed.org/ns#git',
						cloneUri: 'https://fig.fr33domlover.site/repos/Pn9Yn',
						collaborators: 'https://fig.fr33domlover.site/repos/Pn9Yn/collabs',
						context: 'https://fig.fr33domlover.site/repos/Pn9Yn/projects'
					},
					{
						'@context': [
							'https://www.w3.org/ns/activitystreams',
							'https://w3id.org/security/v2',
							'https://forgefed.org/ns'
						],
						id: 'https://fig.fr33domlover.site/repos/9nOkn',
						inbox: 'https://fig.fr33domlover.site/repos/9nOkn/inbox',
						outbox: 'https://fig.fr33domlover.site/repos/9nOkn/outbox',
						followers: 'https://fig.fr33domlover.site/repos/9nOkn/followers',
						publicKey: [
							'https://fig.fr33domlover.site/akey1',
							'https://fig.fr33domlover.site/akey2'
						],
						type: 'Repository',
						name: 'Very fun demo',
						summary: 'Testing MR demo',
						team: null,
						versionControlSystem: 'https://forgefed.org/ns#git',
						sendPatchesTo: 'https://fig.fr33domlover.site/looms/9nOkn',
						cloneUri: 'https://fig.fr33domlover.site/repos/9nOkn',
						collaborators: 'https://fig.fr33domlover.site/repos/9nOkn/collabs',
						context: 'https://fig.fr33domlover.site/repos/9nOkn/projects'
					}
				];

				// Act
				render(History, { commitsMap, followingsMap });
				const listItems = screen.getAllByRole('listitem');

				// Assert
				// 2 listitems for the dates, 2 for the projects and 2 for the commits
				// Or something like that.
				expect(listItems).toHaveLength(2 + 2 + 2);
			});
		});
	});
});