Files
my-biz/backend/tests/MyBiz.Tests/UserTests.cs
2026-02-21 11:51:50 +03:00

561 lines
15 KiB
C#

using Xunit;
using MyBiz.Core;
namespace MyBiz.Tests;
public class UserTests
{
[Fact]
public void User_Creation_ShouldInitializeProperties()
{
var user = new User
{
Username = "testuser",
Email = "test@example.com"
};
Assert.NotNull(user.Id);
Assert.Equal("testuser", user.Username);
Assert.Equal("test@example.com", user.Email);
Assert.True(user.IsActive);
Assert.Null(user.Company);
Assert.NotNull(user.Stats);
}
[Fact]
public void User_Company_Assignment_ShouldWork()
{
var user = new User();
var company = new Company { Name = "Test Corp" };
user.Company = company;
user.CompanyId = company.Id;
Assert.NotNull(user.Company);
Assert.Equal(company.Id, user.CompanyId);
Assert.Equal("Test Corp", user.Company.Name);
}
[Fact]
public void UserStats_ShouldInitializeDefaults()
{
var stats = new UserStats();
Assert.Equal(0, stats.GamesPlayed);
Assert.Equal(0, stats.TotalHoursPlayed);
Assert.Equal(0, stats.MaxNetWorth);
Assert.Equal(0, stats.TotalBuildingsBuilt);
Assert.Null(stats.FirstGameDate);
Assert.Null(stats.LastGameDate);
}
}
public class AuthServiceTests
{
[Fact]
public void AuthService_Register_ShouldCreateUser()
{
var authService = new AuthService();
var result = authService.Register("testuser", "test@example.com", "password123", "Test Corp");
Assert.True(result.Success);
Assert.NotNull(result.User);
Assert.NotNull(result.Token);
Assert.Equal("testuser", result.User.Username);
Assert.NotNull(result.User.Company);
Assert.Equal("Test Corp", result.User.Company.Name);
}
[Fact]
public void AuthService_Register_DuplicateUsername_ShouldFail()
{
var authService = new AuthService();
authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var result = authService.Register("testuser", "other@example.com", "password456", "Other Corp");
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
Assert.Null(result.User);
}
[Fact]
public void AuthService_Login_CorrectCredentials_ShouldSucceed()
{
var authService = new AuthService();
authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var result = authService.Login("testuser", "password123");
Assert.True(result.Success);
Assert.NotNull(result.User);
Assert.NotNull(result.Token);
Assert.Equal("testuser", result.User.Username);
}
[Fact]
public void AuthService_Login_WrongPassword_ShouldFail()
{
var authService = new AuthService();
authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var result = authService.Login("testuser", "wrongpassword");
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
Assert.Null(result.User);
}
[Fact]
public void AuthService_Login_NonExistentUser_ShouldFail()
{
var authService = new AuthService();
var result = authService.Login("nonexistent", "password123");
Assert.False(result.Success);
Assert.NotNull(result.ErrorMessage);
Assert.Null(result.User);
}
[Fact]
public void AuthService_Logout_ShouldInvalidateToken()
{
var authService = new AuthService();
var registerResult = authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var token = registerResult.Token!;
authService.Logout(token);
var user = authService.ValidateToken(token);
Assert.Null(user);
}
[Fact]
public void AuthService_ValidateToken_ValidToken_ShouldReturnUser()
{
var authService = new AuthService();
var result = authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var user = authService.ValidateToken(result.Token!);
Assert.NotNull(user);
Assert.Equal("testuser", user.Username);
}
[Fact]
public void AuthService_GetUserById_ShouldReturnUser()
{
var authService = new AuthService();
var result = authService.Register("testuser", "test@example.com", "password123", "Test Corp");
var user = authService.GetUserById(result.User!.Id);
Assert.NotNull(user);
Assert.Equal(result.User.Id, user.Id);
}
}
public class BusinessUnitTests
{
[Fact]
public void BusinessUnit_Creation_ShouldInitializeProperties()
{
var unit = new BusinessUnit
{
Name = "Test Unit",
Type = BusinessUnitType.Shop,
CompanyId = Guid.NewGuid(),
CityId = "city_1",
X = 100,
Y = 200
};
Assert.Equal("Test Unit", unit.Name);
Assert.Equal(BusinessUnitType.Shop, unit.Type);
Assert.Equal(1, unit.Level);
Assert.Equal(100, unit.Efficiency);
Assert.True(unit.IsActive);
}
[Fact]
public void BusinessUnit_HireEmployee_ShouldIncreaseCount()
{
var unit = new BusinessUnit { MaxEmployees = 10 };
var hired = unit.HireEmployee();
Assert.True(hired);
Assert.Equal(1, unit.Employees);
}
[Fact]
public void BusinessUnit_HireEmployee_FullCapacity_ShouldFail()
{
var unit = new BusinessUnit { MaxEmployees = 1, Employees = 1 };
var hired = unit.HireEmployee();
Assert.False(hired);
Assert.Equal(1, unit.Employees);
}
[Fact]
public void BusinessUnit_FireEmployee_ShouldDecreaseCount()
{
var unit = new BusinessUnit { Employees = 5 };
var fired = unit.FireEmployee();
Assert.True(fired);
Assert.Equal(4, unit.Employees);
}
[Fact]
public void BusinessUnit_FireEmployee_NoEmployees_ShouldFail()
{
var unit = new BusinessUnit { Employees = 0 };
var fired = unit.FireEmployee();
Assert.False(fired);
Assert.Equal(0, unit.Employees);
}
[Fact]
public void BusinessUnit_Upgrade_ShouldIncreaseLevel()
{
var unit = new BusinessUnit { Level = 1, MaxLevel = 10 };
var upgraded = unit.Upgrade();
Assert.True(upgraded);
Assert.Equal(2, unit.Level);
}
[Fact]
public void BusinessUnit_Upgrade_MaxLevel_ShouldFail()
{
var unit = new BusinessUnit { Level = 10, MaxLevel = 10 };
var upgraded = unit.Upgrade();
Assert.False(upgraded);
Assert.Equal(10, unit.Level);
}
[Fact]
public void BusinessUnit_UpdateEfficiency_FullStaff_ShouldBeMax()
{
var unit = new BusinessUnit { MaxEmployees = 10, Employees = 10 };
unit.UpdateEfficiency();
Assert.Equal(100, unit.Efficiency);
}
[Fact]
public void BusinessUnit_UpdateEfficiency_HalfStaff_ShouldBeReduced()
{
var unit = new BusinessUnit { MaxEmployees = 10, Employees = 5 };
unit.UpdateEfficiency();
Assert.Equal(50, unit.Efficiency);
}
[Fact]
public void BusinessUnit_UpdateEfficiency_NoStaff_ShouldBeZero()
{
var unit = new BusinessUnit { MaxEmployees = 10, Employees = 0 };
unit.UpdateEfficiency();
Assert.Equal(0, unit.Efficiency);
}
}
public class ShopTests
{
[Fact]
public void Shop_Creation_ShouldInitializeDefaults()
{
var shop = new Shop();
Assert.Equal(BusinessUnitType.Shop, shop.Type);
Assert.Equal(20, shop.MaxEmployees);
Assert.Equal(50000m, shop.BuildCost);
Assert.Equal(1000m, shop.UpkeepCost);
Assert.NotNull(shop.SoldProductTypes);
Assert.Equal(0, shop.DailyRevenue);
Assert.Equal(0, shop.DailyCustomers);
}
[Fact]
public void Shop_AverageCheck_Calculation_ShouldBeCorrect()
{
var shop = new Shop { DailyRevenue = 1000m, DailyCustomers = 10 };
var avgCheck = shop.AverageCheck;
Assert.Equal(100m, avgCheck);
}
[Fact]
public void Shop_AverageCheck_NoCustomers_ShouldBeZero()
{
var shop = new Shop { DailyRevenue = 1000m, DailyCustomers = 0 };
var avgCheck = shop.AverageCheck;
Assert.Equal(0, avgCheck);
}
}
public class FactoryTests
{
[Fact]
public void Factory_Creation_ShouldInitializeDefaults()
{
var factory = new Factory();
Assert.Equal(BusinessUnitType.Factory, factory.Type);
Assert.Equal(100, factory.MaxEmployees);
Assert.Equal(200000m, factory.BuildCost);
Assert.Equal(5000m, factory.UpkeepCost);
Assert.Null(factory.OutputProductId);
Assert.NotNull(factory.RequiredInputs);
}
[Fact]
public void Factory_IsIdle_NoProduction_ShouldBeTrue()
{
var factory = new Factory();
Assert.True(factory.IsIdle);
}
[Fact]
public void Factory_IsIdle_ActiveProduction_ShouldBeFalse()
{
var factory = new Factory();
var config = new ProductionChainConfig
{
Id = "test_chain",
OutputProductId = "product_1",
RequiredBuildingId = "building_1"
};
factory.ActiveProduction = new ActiveProductionChain
{
Config = config,
Building = new Building(),
IsActive = true
};
Assert.False(factory.IsIdle);
}
}
public class WarehouseTests
{
[Fact]
public void Warehouse_Creation_ShouldInitializeDefaults()
{
var warehouse = new Warehouse();
Assert.Equal(BusinessUnitType.Warehouse, warehouse.Type);
Assert.Equal(10, warehouse.MaxEmployees);
Assert.Equal(30000m, warehouse.BuildCost);
Assert.Equal(500m, warehouse.UpkeepCost);
Assert.Equal(10000, warehouse.Capacity);
}
[Fact]
public void Warehouse_AddToInventory_ShouldIncreaseStock()
{
var warehouse = new Warehouse();
var added = warehouse.AddToInventory("product_1", 100);
Assert.True(added);
Assert.Equal(100, warehouse.Inventory["product_1"]);
Assert.Equal(100, warehouse.UsedCapacity);
}
[Fact]
public void Warehouse_AddToInventory_NoCapacity_ShouldFail()
{
var warehouse = new Warehouse { Capacity = 50 };
var added = warehouse.AddToInventory("product_1", 100);
Assert.False(added);
Assert.Empty(warehouse.Inventory);
}
[Fact]
public void Warehouse_RemoveFromInventory_ShouldDecreaseStock()
{
var warehouse = new Warehouse();
warehouse.AddToInventory("product_1", 100);
var removed = warehouse.RemoveFromInventory("product_1", 30);
Assert.Equal(30, removed);
Assert.Equal(70, warehouse.Inventory["product_1"]);
}
[Fact]
public void Warehouse_RemoveFromInventory_NoStock_ShouldReturnZero()
{
var warehouse = new Warehouse();
var removed = warehouse.RemoveFromInventory("product_1", 30);
Assert.Equal(0, removed);
}
[Fact]
public void Warehouse_FreeCapacity_Calculation_ShouldBeCorrect()
{
var warehouse = new Warehouse { Capacity = 1000 };
warehouse.AddToInventory("product_1", 300);
warehouse.AddToInventory("product_2", 200);
var freeCapacity = warehouse.FreeCapacity;
Assert.Equal(500, freeCapacity);
}
}
public class ResearchLabTests
{
[Fact]
public void ResearchLab_Creation_ShouldInitializeDefaults()
{
var lab = new ResearchLab();
Assert.Equal(BusinessUnitType.ResearchLab, lab.Type);
Assert.Equal(30, lab.MaxEmployees);
Assert.Equal(150000m, lab.BuildCost);
Assert.Equal(4000m, lab.UpkeepCost);
Assert.Null(lab.CurrentResearch);
Assert.Equal(0, lab.ResearchProgress);
}
[Fact]
public void ResearchLab_StartResearch_ShouldSetCurrentResearch()
{
var lab = new ResearchLab();
lab.StartResearch("tech_advanced_production");
Assert.Equal("tech_advanced_production", lab.CurrentResearch);
Assert.Equal(0, lab.ResearchProgress);
}
[Fact]
public void ResearchLab_SciencePerTick_Calculation_ShouldBeCorrect()
{
var lab = new ResearchLab { Employees = 10, Efficiency = 100 };
var science = lab.SciencePerTick;
Assert.Equal(100, science);
}
[Fact]
public void ResearchLab_Tick_ShouldIncreaseProgress()
{
var lab = new ResearchLab { Employees = 10, Efficiency = 100 };
lab.StartResearch("tech_advanced_production");
lab.Tick();
Assert.Equal(33, lab.ResearchProgress); // 10 * 10 * 100 / 100 = 100, но ограничено 33 за тик
}
}
public class CompanyBusinessUnitTests
{
[Fact]
public void Company_AddBusinessUnit_ShouldAddToList()
{
var company = new Company { Name = "Test Corp" };
var shop = new Shop { Name = "My Shop" };
company.AddBusinessUnit(shop);
Assert.Single(company.BusinessUnits);
Assert.Equal(company.Id, shop.CompanyId);
}
[Fact]
public void Company_RemoveBusinessUnit_ShouldRemoveFromList()
{
var company = new Company();
var shop = new Shop();
company.AddBusinessUnit(shop);
company.RemoveBusinessUnit(shop.Id);
Assert.Empty(company.BusinessUnits);
}
[Fact]
public void Company_Shops_Filter_ShouldReturnOnlyShops()
{
var company = new Company();
company.AddBusinessUnit(new Shop());
company.AddBusinessUnit(new Factory());
company.AddBusinessUnit(new Shop());
var shops = company.Shops.ToList();
Assert.Equal(2, shops.Count);
Assert.All(shops, s => Assert.IsType<Shop>(s));
}
[Fact]
public void Company_Factories_Filter_ShouldReturnOnlyFactories()
{
var company = new Company();
company.AddBusinessUnit(new Shop());
company.AddBusinessUnit(new Factory());
company.AddBusinessUnit(new Factory());
var factories = company.Factories.ToList();
Assert.Equal(2, factories.Count);
Assert.All(factories, f => Assert.IsType<Factory>(f));
}
[Fact]
public void Company_CalculateTotalProfit_ShouldSumAllUnits()
{
var company = new Company();
var shop1 = new Shop { LastPeriodIncome = 1000m, LastPeriodExpenses = 500m };
var shop2 = new Shop { LastPeriodIncome = 2000m, LastPeriodExpenses = 800m };
company.AddBusinessUnit(shop1);
company.AddBusinessUnit(shop2);
var totalProfit = company.CalculateTotalProfit();
Assert.Equal(1700m, totalProfit); // (1000-500) + (2000-800)
}
[Fact]
public void Company_Tick_ShouldUpdateAllUnits()
{
var company = new Company();
var shop = new Shop { Employees = 5, MaxEmployees = 10 };
company.AddBusinessUnit(shop);
company.Tick();
Assert.Equal(50, shop.Efficiency); // 5/10 = 50%
}
}