# Copyright (c) 2015, JINGROW and Contributors
# License: MIT. See LICENSE

from contextlib import contextmanager

import jingrow
from jingrow.tests.utils import JingrowTestCase
from jingrow.website.pagetype.website_theme.website_theme import (
	after_migrate,
	get_active_theme,
	get_scss_paths,
)


@contextmanager
def website_theme_fixture(**theme):
	test_theme = "test-theme"

	jingrow.delete_pg_if_exists("Website Theme", test_theme)
	theme = jingrow.get_pg(pagetype="Website Theme", theme=test_theme, **theme)
	theme.insert()
	yield theme
	jingrow.db.set_single_value("Website Settings", "website_theme", "Standard")
	theme.delete()


class TestWebsiteTheme(JingrowTestCase):
	def test_website_theme(self):
		with website_theme_fixture(
			google_font="Inter",
			custom_scss="body { font-size: 16.5px; }",  # this will get minified!
		) as theme:
			theme_path = jingrow.get_site_path("public", theme.theme_url[1:])
			with open(theme_path) as theme_file:
				css = theme_file.read()

			self.assertTrue("body{font-size:16.5px}" in css)
			self.assertTrue("fonts.googleapis.com" in css)

	def test_get_scss_paths(self):
		self.assertIn("jingrow/public/scss/website.bundle", get_scss_paths())

	def test_imports_to_ignore(self):
		with website_theme_fixture(ignored_apps=[{"app": "jingrow"}]) as theme:
			self.assertTrue('@import "jingrow/public/scss/website"' not in theme.theme_scss)

	def test_after_migrate_hook(self):
		with website_theme_fixture(google_font="Inter") as theme:
			theme.set_as_default()
			before = get_active_theme().theme_url
			after_migrate()
			after = get_active_theme().theme_url
			self.assertNotEqual(before, after)
