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

import influxframework
from influxframework.desk.form.load import getdoc
from influxframework.tests.utils import InfluxFrameworkTestCase, change_settings
from influxframework.www.printview import get_html_and_style


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

		for doctype in doctypes:
			last_doc = influxframework.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
		influxframework.response = influxframework._dict({"docs": []})
		influxframework.response.docinfo = None

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

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

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

		messages_before = influxframework.get_message_log()
		ret = get_html_and_style(doc=doc.as_json(), print_format="Standard", no_letterhead=1)
		messages_after = influxframework.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"]))
