<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CheckTestOutput;
using DotVVM.Framework.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.ResourceManagement;
using DotVVM.Framework.Compilation;

namespace DotVVM.Framework.Tests.ControlTests
{
    [TestClass]
    public class ViewModulesServerSideTests
    {
        ControlTestHelper cth = new ControlTestHelper(config: config => {
            config.Resources.Register("myModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Resources.Register("viewModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Resources.Register("controlModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Markup.AddMarkupControl("cc", "CustomControlWithModule", "CustomControlWithModule.dotcontrol");
            // config.Resources.Register
        });
        OutputChecker check = new OutputChecker("testoutputs");

        [TestMethod]
        public async Task IncludeViewModule()
        {
            var r = await cth.RunPage(typeof(object), @"
                ",
                directives: "@js myModule",
                renderResources: true
            );
            check.CheckString(r.FormattedHtml, fileExtension: "html");
        }

        [TestMethod]
        [ExpectedException(typeof(DotvvmCompilationException))]
        public async Task NamedCommandWithoutViewModule()
        {
            await cth.RunPage(typeof(object), @"
                <dot:NamedCommand Name=""Command"" Command=""{staticCommand: ;}"" />");
        }

        [TestMethod]
        public async Task IncludeViewModuleInControl()
        {
            var r = await cth.RunPage(typeof(TestViewModel), @"

                <cc:CustomControlWithModule />

                <dot:Repeater DataSource={value: Collection}><commit_msg>Improved reporting when error occurres in a viewmodel function<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CheckTestOutput;
using DotVVM.Framework.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DotVVM.Framework.ViewModel;
using DotVVM.Framework.ResourceManagement;
using DotVVM.Framework.Compilation;

namespace DotVVM.Framework.Tests.ControlTests
{
    [TestClass]
    public class ViewModulesServerSideTests
    {
        ControlTestHelper cth = new ControlTestHelper(config: config => {
            config.Resources.Register("myModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Resources.Register("viewModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Resources.Register("controlModule", new ScriptModuleResource(new InlineResourceLocation("export const jsCommand = { myCommand() { } }")));
            config.Markup.AddMarkupControl("cc", "CustomControlWithModule", "CustomControlWithModule.dotcontrol");
            // config.Resources.Register
        });
        OutputChecker check = new OutputChecker("testoutputs");

        [TestMethod]
        public async Task IncludeViewModule()
        {
            var r = await cth.RunPage(typeof(object), @"
                ",
                directives: "@js myModule",
                renderResources: true
            );
            check.CheckString(r.FormattedHtml, fileExtension: "html");
        }

        [TestMethod]
        public async Task NamedCommandWithoutViewModule()
        {
            var r = await Assert.ThrowsExceptionAsync<DotvvmCompilationException>(() => cth.RunPage(typeof(object), @"
                <dot:NamedCommand Name=""Command"" Command=""{staticCommand: ;}"" />"));
            Assert.AreEqual("The NamedCommand control can be used only in pages or controls that have the @js directive.", r.Message);
        }

        [TestMethod]
        public async Task IncludeViewModuleInControl()
        {
            var r = await cth.RunPage(typeof(TestViewModel), @"

                <cc:CustomControlWithModule />

                <dot:Repeater DataSource={value: Collection}>
{'fraction_non_alphanumeric': 0.10623452622102184, 'fraction_numerical': 0.0, 'mean_word_length': 3.04, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941100', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1104, 'n_words': 228}