""" smoak tests to check basic functionality calls on known form loads."""

import xhiveframework
from xhiveframework.desk.form.load import getdoc
from xhiveframework.tests.utils import XhiveframeworkTestCase, change_settings
from xhiveframework.www.printview import get_html_and_style


class TestFormLoads(XhiveframeworkTestCase):
	@change_settings("Print Settings", {"allow_print_for_cancelled": 1})
	def test_load(self):
		xhiveerp_modules = xhiveframework.get_all("Module Def", filters={"app_name": "xhiveerp"}, pluck="name")
		doctypes = xhiveframework.get_all(
			"DocType",
			{"istable": 0, "issingle": 0, "is_virtual": 0, "module": ("in", xhiveerp_modules)},
			pluck="name",
		)

		for doctype in doctypes:
			last_doc = xhiveframework.db.get_value(doctype, {}, "name", order_by="modified desc")
			if not last_doc:
				continue
			with self.subTest(msg=f"Loading {doctype} - {last_doc}", doctype=doctype, last_doc=last_doc):
				self.assertFormLoad(doctype, last_doc)
				self.assertDocPrint(doctype, last_doc)

	def assertFormLoad(self, doctype, docname):
		# reset previous response
		xhiveframework.response = xhiveframework._dict({"docs": []})
		xhiveframework.response.docinfo = None

		try:
			getdoc(doctype, docname)
		except Exception as e:
			self.fail(f"Failed to load {doctype}-{docname}: {e}")

		self.assertTrue(
			xhiveframework.response.docs, msg=f"expected document in reponse, found: {xhiveframework.response.docs}"
		)
		self.assertTrue(
			xhiveframework.response.docinfo, msg=f"expected docinfo in reponse, found: {xhiveframework.response.docinfo}"
		)

	def assertDocPrint(self, doctype, docname):
		doc = xhiveframework.get_doc(doctype, docname)
		doc.set("__onload", xhiveframework._dict())
		doc.run_method("onload")

		messages_before = xhiveframework.get_message_log()
		ret = get_html_and_style(doc=doc.as_json(), print_format="Standard", no_letterhead=1)
		messages_after = xhiveframework.get_message_log()

		if len(messages_after) > len(messages_before):
			new_messages = messages_after[len(messages_before) :]
			self.fail("Print view showing error/warnings: \n" + "\n".join(str(msg) for msg in new_messages))

		# html should exist
		self.assertTrue(bool(ret["html"]))
