diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..50fdc4b --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,40 @@ +name: Busted + + +on: [push, pull_request] + + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: checkout + uses: actions/checkout@v2 + with: + submodules: true + + - name: get lua + uses: leafo/gh-actions-lua@v10 + with: + luaVersion: "5.1" + + - name: get luarocks + uses: leafo/gh-actions-luarocks@v4 + with: + luaVersion: "5.1" + + - name: get busted and luasocket + run: | + luarocks install busted + luarocks install luasocket + luarocks install luasec + + - name: fetch dependencies + run: | + chmod +x ./fetch-deps.sh + ./fetch-deps.sh + + - name: test + run: | + busted tests diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..403acf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/libs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..af0bf3d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "Lua.diagnostics.globals": [ + "describe", + "before_each", + "it" + ] +} \ No newline at end of file diff --git a/fetch-deps.sh b/fetch-deps.sh new file mode 100755 index 0000000..0806d65 --- /dev/null +++ b/fetch-deps.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# ---- Whats happening ---- # + +# This fetches the dependencies listed in the "libs" variable and saves them in the targetFolder + +set -e + +libs=( + "TestSuite-lib" + "TurtleEmulator-lib" + "scanner-lib" + "turtleController-lib" + "ccClass-lib" + "miningClient-lib" + "helperFunctions-lib" + "eventHandler-lib" + "turtleResourceManager-lib" + "config-lib" +) + +# Basic setup variables +repo="mc-cc-scripts" +branch="master" +targetFolderName=libs + + +# fetch files.txt and save each file into the targetFolder +fetch() { + files_txt=$(curl -fsSL "https://raw.githubusercontent.com/$repo/$1/$branch/files.txt") + if [ -z "$files_txt" ]; then + echo "Could not load files.txt for $1" + exit 1 + fi + while IFS= read -r FILE; do + url="https://raw.githubusercontent.com/$repo/$1/$branch/$FILE" + + mkdir -p "$(dirname "$targetFolderName/$FILE")" # create the folder (and subfolders specified in the files.txt) + rm -f $targetFolderName/$FILE.lua # rm existing file + if ! curl -s -o "$targetFolderName/$FILE" "$url"; then + echo "could not get / write the file $i: '$FILE' to the folder '$targetFolderName'" + exit 1 + fi + # echo "saved $1: '$FILE' in '$targetFolderName'" + done < <(echo "$files_txt") +} + +if [[ $# -eq 0 ]]; then + # No arguments given, fetch all + for i in "${libs[@]}"; do + fetch "$i" + done +else + # Argument given, fetch arguemt + fetch "$1" +fi \ No newline at end of file diff --git a/libs/miningClient-lib b/libs/miningClient-lib deleted file mode 160000 index e7268fe..0000000 --- a/libs/miningClient-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e7268feb6e6f62178fcea5807b3e24c8bffdee82 diff --git a/miningClientSmall.lua b/miningClientSmall.lua index 1c766f2..7cc7ef2 100644 --- a/miningClientSmall.lua +++ b/miningClientSmall.lua @@ -1,10 +1,5 @@ ---Requirements -local pretty = require "cc.pretty" - ---@class scm local scm = require("./scm") ----@class SettingManager -local sM = scm:load("settingsManager") ---@class HelperFunctions local helper = scm:load("helperFunctions") ---@class miningLib @@ -53,7 +48,6 @@ local function config() }; miningSettings = sM.setget('MiningSettings', miningSettings, miningSettings); end - pretty.pretty_print(miningSettings) end ---main script Function @@ -87,4 +81,4 @@ local function main() miningLib:main(points); end -main() \ No newline at end of file +main() diff --git a/scanResultMock.lua b/scanResultMock.lua new file mode 100644 index 0000000..5bea597 --- /dev/null +++ b/scanResultMock.lua @@ -0,0 +1,260 @@ +return { + { + y = 0, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 1, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = -1, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = 0, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = 1, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = -1, + name = "minecraft:deepslate_iron_ore", + z = 2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 0, + x = 0, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 0, + x = 0, + name = "computercraft:turtle_advanced", + z = 0, + tags = { + "minecraft:block/computercraft:turtle", + "minecraft:block/minecraft:mineable/pickaxe", + }, + }, + { + y = 1, + x = 0, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = 0, + name = "minecraft:deepslate_iron_ore", + z = 2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 1, + x = 1, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = 1, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = 1, + name = "minecraft:deepslate_iron_ore", + z = 2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = 2, + name = "minecraft:deepslate_iron_ore", + z = -2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 2, + x = 2, + name = "minecraft:deepslate_iron_ore", + z = 2, + tags = { + "minecraft:block/minecraft:mineable/pickaxe", + "minecraft:block/forge:ores/iron", + "minecraft:block/minecraft:needs_stone_tool", + "minecraft:block/minecraft:overworld_carver_replaceables", + "minecraft:block/forge:ore_rates/singular", + "minecraft:block/forge:ores_in_ground/deepslate", + "minecraft:block/forge:ores", + "minecraft:block/minecraft:iron_ores", + }, + }, + { + y = 0, + x = 1, + name = "minecraft:dirt", + z = -3, + tags = { + }, + }, +} \ No newline at end of file diff --git a/scanner-lib b/scanner-lib deleted file mode 160000 index 2880a00..0000000 --- a/scanner-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2880a007c4c8eb9e06a1a6baee8a968798473cd5 diff --git a/tests/test_spec.lua b/tests/test_spec.lua new file mode 100644 index 0000000..c8d4d93 --- /dev/null +++ b/tests/test_spec.lua @@ -0,0 +1,196 @@ +---@class are +---@field same function +---@field equal function +---@field equals function + +---@class is +---@field truthy function +---@field falsy function +---@field not_true function +---@field not_false function + +---@class has +---@field error function +---@field errors function + +---@class assert +---@field are are +---@field is is +---@field are_not are +---@field is_not is +---@field has has +---@field has_no has +---@field True function +---@field False function +---@field has_error function +---@field is_false function +---@field is_true function +---@field equal function +assert = assert + +package.path = "libs/?.lua;" + .. "libs/inventory/?.lua;" + .. "libs/peripherals/?.lua;" + .. package.path + + +_G.settings = require("settings") +local Vector = require("vector") +local scanResultMock = require("scanResultMock") +---@type TurtleEmulator +local turtleEmulator = require("turtleEmulator") + + +-- require the files which depend on the globals +local geoScanner +local miningClient + +local geoScannerPeripheral +local readOperationCounter = 0 +_G.read = function () + readOperationCounter = readOperationCounter + 1 + if readOperationCounter == 1 then + return "X" + end + if readOperationCounter == 2 then + return "1" + end +end + +--#endregion + +---comment +---@param turtle TurtleProxy +---@param distance number +---@return Vector[] +local function createPoints(turtle, distance, areaSize) + local points = distance / areaSize + local pointTable = {} + local currentPositon = turtle.position * 1 + table.insert(pointTable, currentPositon) + for i = 1, points,1 do + currentPositon = currentPositon + (turtle.facing * areaSize) + table.insert(pointTable, currentPositon) + end + return pointTable +end + +local function countblocks(blockName) + local count = 0 + local lastBlock + for _, block in pairs(turtleEmulator.blocks) do + if block.item.name == blockName then + count = count + 1 + lastBlock = block + end + end + return count +end + +local function beforeEach() + local geoScannerItem = { + name = "advancedperipherals:geo_scanner", + count = 1, + equipable = true, + placeAble = false + } + local chuckyItem = { + name = "advancedperipherals:chunk_controller", + count = 1, + equipable = true, + placeAble = false + } + local pickaxe = { + name = "minecraft:diamond_pickaxe", + count = 1, + equipable = true, + placeAble = false + } + local coal = { + name = "minecraft:coal", + count = 64, + equipable = false, + placeAble = false, + fuelgain = 8 + } + + turtleEmulator:clearBlocks() + turtleEmulator:clearTurtles() + + if package.loaded["turtleController"] ~= nil then + package.loaded["turtleController"] = nil + package.loaded["miningClient"] = nil + end + -- reset globals + ---@type TurtleMock + _G.turtle = turtleEmulator:createTurtle() + local peripheral = turtle.getPeripheralModule() + _G.peripheral = peripheral + + -- require the files which depend on the "new" globals + geoScanner = require("geoScanner") + miningClient = require("miningClient") + geoScannerPeripheral = turtleEmulator:addPeripheralToItem(geoScannerItem, geoScanner, turtle) + + turtle.addItemToInventory(geoScannerItem, 1) + turtle.addItemToInventory(chuckyItem, 2) + turtle.addItemToInventory(pickaxe, 3) + turtle.addItemToInventory(coal, 4) + + -- require the files which depend on the globals +end + + + +describe("empty World", function() + before_each(function() + beforeEach() + end) + it(" - return to start", function() + ---@cast geoScannerPeripheral GeoScanner + geoScannerPeripheral.scanResult = {} + miningClient.scanStartFacingTo = "X" + miningClient:main(createPoints(turtle, 10, 1)) + assert.are.equal(Vector.new(0, 0, 0), turtle.position) + end) +end) +describe("World with ores", function() + before_each(function() + beforeEach() + end) + it(" - cleared all Ores", function() + for i = 5, 15, 1 do + turtle.addItemToInventory({name = "minecraft:stone", count = 64, maxcount = 64, placeAble = true}) + end + local tmpChest = {name = "enderchests:ender_chest", count = 1, maxcount = 1, placeAble = true} + turtle.addItemToInventory(tmpChest, 16) + local enderCHest = turtleEmulator:addInventoryToItem(tmpChest) + + + local mappingFunc = function(scanData) + return { + item = { + name = scanData.name, + tags = scanData.tags + }, + position = Vector.new(scanData.x, scanData.y, scanData.z), + checkActionValidFunc = function() + return false + end + } + end + turtleEmulator:readBlocks(scanResultMock, mappingFunc) + ---@cast geoScannerPeripheral GeoScanner + geoScannerPeripheral.scanEmulator = true + miningClient.scanStartFacingTo = "X" + assert.are.equal(15, countblocks("minecraft:deepslate_iron_ore")) + miningClient:main(createPoints(turtle, 2, 1)) + local count = countblocks("minecraft:deepslate_iron_ore") + local dirt = countblocks("minecraft:dirt") + assert.are.equal(0, count) + assert.are.equal(Vector.new(0, 0, 0), turtle.position) + assert.are.equal(Vector.new(1, 0, 0), turtle.facing) + assert.is.not_false(dirt) + assert.is.falsy(turtle.getItemDetail(10)) + end) +end) \ No newline at end of file diff --git a/turtleController-lib b/turtleController-lib deleted file mode 160000 index 8816bf4..0000000 --- a/turtleController-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 8816bf4de67da590aa75c2a6ad7b537b63aca446 diff --git a/turtleEmulator-lib b/turtleEmulator-lib deleted file mode 160000 index 6c41199..0000000 --- a/turtleEmulator-lib +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 6c411996cd8ac7117def9ebcf67c91a9ea82d946