local o=require("\078\101\120\117\115\085\110\105\116\084\101\115\116\105\110\103")local T=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local m=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local w=require((T:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local e=w:GetResource("\083\116\097\116\101\046\080\097\116\104\070\105\110\100\046\077\097\112\071\114\097\112\104\083\111\108\118\101\114")local C=o.UnitTest:Extend()function C.Setup(o)o.CuT=e.new()end o:RegisterUnitTest((C.new("\084\101\120\116\071\101\116\067\101\108\108\115\070\111\114\076\105\110\101")):SetRun(function(o)local function T(o,T)if o.X~=T.X then return o.X<T.X end return o.Y<T.Y end local function w(w,e)table.sort(w,T)table.sort(e,T)o:AssertEquals(m:JSONEncode(w),m:JSONEncode(e),"\076\105\110\101 \112\111\105\110\116\115 \097\114\101\110\'\116 \116\104\101 \115\097\109\101\046")end w(o.CuT:GetCellsForLine(5,5,5,5),{{X=5;Y=5}})w(o.CuT:GetCellsForLine(5,5,8,5),{{X=5;Y=5},{X=6,Y=5},{X=7;Y=5};{X=8,Y=5}})w(o.CuT:GetCellsForLine(8,5,5,5),{{X=5,Y=5};{X=6;Y=5};{X=7;Y=5},{X=8,Y=5}})w(o.CuT:GetCellsForLine(5,5,5,8),{{X=5;Y=5},{X=5,Y=6};{X=5,Y=7},{X=5;Y=8}})w(o.CuT:GetCellsForLine(5,8,5,5),{{X=5;Y=5},{X=5;Y=6},{X=5,Y=7};{X=5,Y=8}})w(o.CuT:GetCellsForLine(5,5,8,8),{{X=5;Y=5};{X=6,Y=6};{X=7;Y=7};{X=8;Y=8}})w(o.CuT:GetCellsForLine(8,8,5,5),{{X=5;Y=5};{X=6,Y=6};{X=7;Y=7};{X=8,Y=8}})w(o.CuT:GetCellsForLine(5,5,7,10),{{X=5;Y=5};{X=5;Y=6};{X=6;Y=7};{X=6;Y=8},{X=7,Y=9},{X=7,Y=10}})w(o.CuT:GetCellsForLine(7,10,5,5),{{X=5,Y=5};{X=5;Y=6};{X=6;Y=7};{X=6,Y=8};{X=7;Y=9},{X=7;Y=10}})w(o.CuT:GetCellsForLine(5,5,10,7),{{X=5,Y=5};{X=6;Y=5};{X=7;Y=6};{X=8,Y=6},{X=9;Y=7};{X=10,Y=7}})w(o.CuT:GetCellsForLine(10,7,5,5),{{X=5,Y=5},{X=6,Y=5},{X=7,Y=6},{X=8,Y=6};{X=9;Y=7};{X=10,Y=7}})end))o:RegisterUnitTest((C.new("\084\101\115\116\073\115\080\097\116\104\066\108\111\099\107\101\100")):SetRun(function(o)o:AssertFalse(o.CuT:IsPathBlocked(183,15,183,19),"\080\097\116\104 \105\115 \098\108\111\099\107\101\100\046")o:AssertFalse(o.CuT:IsPathBlocked(173,35,178,63),"\080\097\116\104 \105\115 \098\108\111\099\107\101\100\046")o:AssertFalse(o.CuT:IsPathBlocked(185,181,180,154),"\080\097\116\104 \105\115 \098\108\111\099\107\101\100\046")o:AssertFalse(o.CuT:IsPathBlocked(13,88,55,100),"\080\097\116\104 \105\115 \098\108\111\099\107\101\100\046")o:AssertFalse(o.CuT:IsPathBlocked(59,34,138,33),"\080\097\116\104 \105\115 \098\108\111\099\107\101\100\046")o:AssertTrue(o.CuT:IsPathBlocked(173,7,178,8),"\080\097\116\104 \105\115\110\'\116 \098\108\111\099\107\101\100\046")o:AssertTrue(o.CuT:IsPathBlocked(187,15,192,15),"\080\097\116\104 \105\115\110\'\116 \098\108\111\099\107\101\100\046")o:AssertTrue(o.CuT:IsPathBlocked(167,83,147,65),"\080\097\116\104 \105\115\110\'\116 \098\108\111\099\107\101\100\046")o:AssertTrue(o.CuT:IsPathBlocked(149,62,152,67),"\080\097\116\104 \105\115\110\'\116 \098\108\111\099\107\101\100\046")o:AssertTrue(o.CuT:IsPathBlocked(108,198,100,193),"\080\097\116\104 \105\115\110\'\116 \098\108\111\099\107\101\100\046")end))o:RegisterUnitTest((C.new("\084\101\115\116\067\101\108\108\073\115\082\111\097\100\078\111\100\101")):SetRun(function(o)o:AssertFalse(o.CuT:CellIsRoadNode(179,12),"\067\117\115\116\111\109 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(177,10),"\072\111\117\115\101 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(175,15),"\087\097\116\101\114 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(177,76),"\071\114\097\115\115 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(182,26),"\083\119\097\109\112 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(181.148),"\080\117\109\112\107\105\110\115 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(181,1),"\082\111\099\107 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(-1,-1),"\086\111\105\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(173,10),"\072\111\114\105\122\111\110\116\097\108 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(174,12),"\086\101\114\116\105\099\097\108 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertTrue(o.CuT:CellIsRoadNode(199,7),"\069\110\100 \114\111\097\100 \105\115\110\'\116 \097 \110\111\100\101 \099\101\108\108\046")o:AssertTrue(o.CuT:CellIsRoadNode(181,15),"\069\110\100 \114\111\097\100 \105\115\110\'\116 \097 \110\111\100\101 \099\101\108\108\046")o:AssertTrue(o.CuT:CellIsRoadNode(191,63),"\069\110\100 \114\111\097\100 \105\115\110\'\116 \097 \110\111\100\101 \099\101\108\108\046")o:AssertTrue(o.CuT:CellIsRoadNode(167,14),"\069\110\100 \114\111\097\100 \105\115\110\'\116 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(175,12),"\067\111\114\110\101\114 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(175,14),"\067\111\114\110\101\114 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(178,7),"\067\111\114\110\101\114 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(186,11),"\067\111\114\110\101\114 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(186,19),"\051-\119\097\121 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(186,22),"\051-\119\097\121 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(183,124),"\051-\119\097\121 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(172,177),"\051-\119\097\121 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")o:AssertFalse(o.CuT:CellIsRoadNode(192,19),"\052-\119\097\121 \114\111\097\100 \105\115 \097 \110\111\100\101 \099\101\108\108\046")end))return true
--[[
TheNexusAvenger

Tests the MapGraphSolver class.
--]]

local NexusUnitTesting = require("NexusUnitTesting")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HttpService = game:GetService("HttpService")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local MapGraphSolver = ReplicatedStorageProject:GetResource("State.PathFind.MapGraphSolver")



--[[
Setups up the test.
--]]
local MapGraphSolverTest = NexusUnitTesting.UnitTest:Extend()
function MapGraphSolverTest:Setup()
    --Create the component under testing.
    self.CuT = MapGraphSolver.new()
end

--[[
Tests getting the cells for a line.
--]]
NexusUnitTesting:RegisterUnitTest(MapGraphSolverTest.new("TextGetCellsForLine"):SetRun(function(self)
    --[[
    Sorts 2 points.
    --]]
    local function SortPoints(Point1,Point2)
        if Point1.X ~= Point2.X then
            return Point1.X < Point2.X
        end
        return Point1.Y < Point2.Y
    end

    --[[
    Asserts that two sets of points match.
    --]]
    local function AssertLines(Line1,Line2)
        --Sort the lines.
        table.sort(Line1,SortPoints)
        table.sort(Line2,SortPoints)

        --Assert the lines are equal.
        self:AssertEquals(HttpService:JSONEncode(Line1),HttpService:JSONEncode(Line2),"Line points aren't the same.")
    end

    AssertLines(self.CuT:GetCellsForLine(5,5,5,5),{{X=5,Y=5}})
    AssertLines(self.CuT:GetCellsForLine(5,5,8,5),{{X=5,Y=5},{X=6,Y=5},{X=7,Y=5},{X=8,Y=5}})
    AssertLines(self.CuT:GetCellsForLine(8,5,5,5),{{X=5,Y=5},{X=6,Y=5},{X=7,Y=5},{X=8,Y=5}})
    AssertLines(self.CuT:GetCellsForLine(5,5,5,8),{{X=5,Y=5},{X=5,Y=6},{X=5,Y=7},{X=5,Y=8}})
    AssertLines(self.CuT:GetCellsForLine(5,8,5,5),{{X=5,Y=5},{X=5,Y=6},{X=5,Y=7},{X=5,Y=8}})
    AssertLines(self.CuT:GetCellsForLine(5,5,8,8),{{X=5,Y=5},{X=6,Y=6},{X=7,Y=7},{X=8,Y=8}})
    AssertLines(self.CuT:GetCellsForLine(8,8,5,5),{{X=5,Y=5},{X=6,Y=6},{X=7,Y=7},{X=8,Y=8}})
    AssertLines(self.CuT:GetCellsForLine(5,5,7,10),{{X=5,Y=5},{X=5,Y=6},{X=6,Y=7},{X=6,Y=8},{X=7,Y=9},{X=7,Y=10}})
    AssertLines(self.CuT:GetCellsForLine(7,10,5,5),{{X=5,Y=5},{X=5,Y=6},{X=6,Y=7},{X=6,Y=8},{X=7,Y=9},{X=7,Y=10}})
    AssertLines(self.CuT:GetCellsForLine(5,5,10,7),{{X=5,Y=5},{X=6,Y=5},{X=7,Y=6},{X=8,Y=6},{X=9,Y=7},{X=10,Y=7}})
    AssertLines(self.CuT:GetCellsForLine(10,7,5,5),{{X=5,Y=5},{X=6,Y=5},{X=7,Y=6},{X=8,Y=6},{X=9,Y=7},{X=10,Y=7}})
end))

--[[
Tests paths on the map being blocked.
--]]
NexusUnitTesting:RegisterUnitTest(MapGraphSolverTest.new("TestIsPathBlocked"):SetRun(function(self)
    --Assert lines over non-terrain and non-water arne't blocked.
    self:AssertFalse(self.CuT:IsPathBlocked(183,15,183,19),"Path is blocked.")
    self:AssertFalse(self.CuT:IsPathBlocked(173,35,178,63),"Path is blocked.")
    self:AssertFalse(self.CuT:IsPathBlocked(185,181,180,154),"Path is blocked.")
    self:AssertFalse(self.CuT:IsPathBlocked(13,88,55,100),"Path is blocked.")
    self:AssertFalse(self.CuT:IsPathBlocked(59,34,138,33),"Path is blocked.")

    --Asert lines over water and terrain are blocked.
    self:AssertTrue(self.CuT:IsPathBlocked(173,7,178,8),"Path isn't blocked.")
    self:AssertTrue(self.CuT:IsPathBlocked(187,15,192,15),"Path isn't blocked.")
    self:AssertTrue(self.CuT:IsPathBlocked(167,83,147,65),"Path isn't blocked.")
    self:AssertTrue(self.CuT:IsPathBlocked(149,62,152,67),"Path isn't blocked.")
    self:AssertTrue(self.CuT:IsPathBlocked(108,198,100,193),"Path isn't blocked.")
end))

--[[
Tests checking for road nodes.
--]]
NexusUnitTesting:RegisterUnitTest(MapGraphSolverTest.new("TestCellIsRoadNode"):SetRun(function(self)
    --Check non-roads not being nodes.
    self:AssertFalse(self.CuT:CellIsRoadNode(179,12),"Custom is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(177,10),"House is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(175,15),"Water is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(177,76),"Grass is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(182,26),"Swamp is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(181.148),"Pumpkins is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(181,1),"Rock is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(-1,-1),"Void is a node cell.")

    --Check road sections not being nodes.
    self:AssertFalse(self.CuT:CellIsRoadNode(173,10),"Horizontal road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(174,12),"Vertical road is a node cell.")

    --Check road ends being nodes.
    self:AssertTrue(self.CuT:CellIsRoadNode(199,7),"End road isn't a node cell.")
    self:AssertTrue(self.CuT:CellIsRoadNode(181,15),"End road isn't a node cell.")
    self:AssertTrue(self.CuT:CellIsRoadNode(191,63),"End road isn't a node cell.")
    self:AssertTrue(self.CuT:CellIsRoadNode(167,14),"End road isn't a node cell.")

    --Check road corners being nodes.
    self:AssertFalse(self.CuT:CellIsRoadNode(175,12),"Corner road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(175,14),"Corner road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(178,7),"Corner road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(186,11),"Corner road is a node cell.")

    --Check 3-way and 4-way road intersecitons not being nodes.
    self:AssertFalse(self.CuT:CellIsRoadNode(186,19),"3-way road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(186,22),"3-way road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(183,124),"3-way road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(172,177),"3-way road is a node cell.")
    self:AssertFalse(self.CuT:CellIsRoadNode(192,19),"4-way road is a node cell.")
end))



return true