<commit_before>﻿using System;
using Caliburn.Micro;
using Workbench.ViewModels;
using Moq;
using NUnit.Framework;
using Workbench.Core;
using Workbench.Messages;
using Workbench.Services;

namespace Workbench.UI.Tests.Unit.ViewModels
{
    [TestFixture]
    public class WorkspaceViewModelSolveTests
    {
        private Mock<IEventAggregator> _eventAggregatorMock;

        public WorkspaceViewModel Workspace { get; private set; }

        [SetUp]
        public void Initialize()
        {
            _eventAggregatorMock = new Mock<IEventAggregator>();
            Workspace = CreateSut();
            ScreenExtensions.TryActivate(Workspace);
            Workspace.AddSingletonVariable(new SingletonVariableBuilder().WithName("x")
                                                                         .WithDomain("1..10")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .Build());
            Workspace.AddAggregateVariable(new AggregateVariableBuilder().WithName("y")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .WithSize(2)
                                                                         .WithDomain("1..10")
                                                                         .Build());
            Workspace.AddExpressionConstraint(new ExpressionConstraintBuilder().WithExpression("$x > 1")
                                                                               .Inside(Workspace.WorkspaceModel.Model)
                                                                               .Build());
            Workspace.AddExpressionConstraint(new ExpressionConstraintBuilder().WithExpression("$y[0] <> $y[1]")
                                                                               .Inside(Workspace.WorkspaceModel.Model)
                                                                               .Build());
        }

        [TearDown]
        public void Teardown()
        {
            ScreenExtensions.TryDeactivate(Workspace, close:true);
        }

        [Test]
        public void SolveWithValidModelReturnsSuccessStatus()
        {
            var actualStatus = Workspace.SolveModel();
            Assert.That(actualStatus.IsSuccess, Is.True);
        }

        [Test]
        public void AddWithValidSingletonVariablePublishesVariableAddedMessage()
        {
            Workspace.AddSingletonVariable(new SingletonVariableBuilder().WithName("z")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .Build());
            _eventAggregatorMock.Verify(_ => _.Publish(It.Is<SingletonVariableAddedMessage>(msg => msg.NewVariableName == "z"), It.IsAny<Action<System.Action>>()),
                                            Times.Once);
        }

        [Test]
        public void AddWithValidAggregatorVariablePublishesVariableAddedMessage()<commit_msg>Adds test to make sure the ModelSolvedMessage is sent when the model is solved.<commit_after>﻿using System;
using Caliburn.Micro;
using Workbench.ViewModels;
using Moq;
using NUnit.Framework;
using Workbench.Core;
using Workbench.Core.Solver;
using Workbench.Messages;
using Workbench.Services;

namespace Workbench.UI.Tests.Unit.ViewModels
{
    [TestFixture]
    public class WorkspaceViewModelSolveTests
    {
        private Mock<IEventAggregator> _eventAggregatorMock;

        public WorkspaceViewModel Workspace { get; private set; }

        [SetUp]
        public void Initialize()
        {
            _eventAggregatorMock = new Mock<IEventAggregator>();
            Workspace = CreateSut();
            ScreenExtensions.TryActivate(Workspace);
            Workspace.AddSingletonVariable(new SingletonVariableBuilder().WithName("x")
                                                                         .WithDomain("1..10")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .Build());
            Workspace.AddAggregateVariable(new AggregateVariableBuilder().WithName("y")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .WithSize(2)
                                                                         .WithDomain("1..10")
                                                                         .Build());
            Workspace.AddExpressionConstraint(new ExpressionConstraintBuilder().WithExpression("$x > 1")
                                                                               .Inside(Workspace.WorkspaceModel.Model)
                                                                               .Build());
            Workspace.AddExpressionConstraint(new ExpressionConstraintBuilder().WithExpression("$y[0] <> $y[1]")
                                                                               .Inside(Workspace.WorkspaceModel.Model)
                                                                               .Build());
        }

        [TearDown]
        public void Teardown()
        {
            ScreenExtensions.TryDeactivate(Workspace, close:true);
        }

        [Test]
        public void SolveWithValidModelReturnsSuccessStatus()
        {
            var actualStatus = Workspace.SolveModel();
            Assert.That(actualStatus.IsSuccess, Is.True);
        }

        [Test]
        public void SolveWithValidModelPublishesModelSolvedMessage()
        {
            Workspace.SolveModel();
            _eventAggregatorMock.Verify(_ => _.Publish(It.Is<ModelSolvedMessage>(msg => msg.Result.Status == SolveStatus.Success), It.IsAny<Action<System.Action>>()),
                                        Times.Once);
        }

        [Test]
        public void AddWithValidSingletonVariablePublishesVariableAddedMessage()
        {
            Workspace.AddSingletonVariable(new SingletonVariableBuilder().WithName("z")
                                                                         .Inside(Workspace.WorkspaceModel.Model)
                                                                         .Build());
            _eventAggregatorMock.Verify(_ => _.Publish(It.Is<SingletonVariableAddedMessage>(msg => msg.NewVariableName == "z"), It.IsAny<Action<System.Action>>()),
                                            Times.Once);
        }

        [Test]
        public void AddWithValidAggregatorVariablePublishesVariableAddedMessage()
{'fraction_non_alphanumeric': 0.07830626450116009, 'fraction_numerical': 0.002900232018561485, 'mean_word_length': 1.1757097791798108, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 83, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4046514', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1325, 'n_words': 215}