2021-06-30 21:18:39 -06:00
|
|
|
config.mode = 'fortress'
|
2023-08-04 21:40:24 -06:00
|
|
|
--config.target = 'orders'
|
2021-06-30 21:18:39 -06:00
|
|
|
|
2021-07-19 11:50:16 -06:00
|
|
|
local FILE_PATH_PATTERN = 'dfhack-config/orders/%s.json'
|
|
|
|
|
|
|
|
local BACKUP_FILE_NAME = 'tmp-backup'
|
|
|
|
local BACKUP_FILE_PATH = FILE_PATH_PATTERN:format(BACKUP_FILE_NAME)
|
|
|
|
|
|
|
|
local TMP_FILE_NAME = 'tmp-test'
|
|
|
|
local TMP_FILE_PATH = FILE_PATH_PATTERN:format(TMP_FILE_NAME)
|
|
|
|
|
|
|
|
local function test_wrapper(test_fn)
|
2021-07-19 12:43:09 -06:00
|
|
|
-- backup and clear active orders
|
2021-07-19 11:50:16 -06:00
|
|
|
dfhack.run_command_silent{'orders', 'export', BACKUP_FILE_NAME}
|
2021-07-19 12:43:09 -06:00
|
|
|
dfhack.run_command_silent{'orders', 'clear'}
|
|
|
|
df.global.world.manager_order_next_id = 0
|
2021-07-19 11:50:16 -06:00
|
|
|
return dfhack.with_finalize(
|
|
|
|
function()
|
2021-07-19 12:43:09 -06:00
|
|
|
-- clear test orders, restore original orders, remove temp files
|
2021-07-19 11:50:16 -06:00
|
|
|
dfhack.run_command_silent{'orders', 'clear'}
|
2021-07-19 14:54:22 -06:00
|
|
|
df.global.world.manager_order_next_id = 0
|
2021-07-19 11:50:16 -06:00
|
|
|
dfhack.run_command_silent{'orders', 'import', BACKUP_FILE_NAME}
|
2021-07-19 12:43:09 -06:00
|
|
|
df.global.world.manager_order_next_id =
|
|
|
|
#df.global.world.manager_orders
|
2021-07-19 11:50:16 -06:00
|
|
|
os.remove(BACKUP_FILE_PATH)
|
2021-07-19 12:43:09 -06:00
|
|
|
os.remove(TMP_FILE_PATH)
|
2021-07-19 11:50:16 -06:00
|
|
|
end,
|
|
|
|
test_fn)
|
|
|
|
end
|
|
|
|
config.wrapper = test_wrapper
|
2021-06-30 21:01:51 -06:00
|
|
|
|
2021-07-19 12:43:09 -06:00
|
|
|
-- returns export command result and exported file content
|
|
|
|
function run_orders_export()
|
|
|
|
local _, result = dfhack.run_command_silent{'orders', 'export',
|
|
|
|
TMP_FILE_NAME}
|
|
|
|
local f = io.open(TMP_FILE_PATH, 'r')
|
|
|
|
return dfhack.with_finalize(
|
|
|
|
function() f:close() end,
|
|
|
|
function() return result, f:read('*all') end)
|
|
|
|
end
|
|
|
|
|
2021-06-30 21:01:51 -06:00
|
|
|
function run_orders_import(file_content)
|
|
|
|
local f = io.open(TMP_FILE_PATH, 'w')
|
|
|
|
f:write(file_content)
|
|
|
|
f:close()
|
2021-07-19 12:43:09 -06:00
|
|
|
return dfhack.run_command_silent{'orders', 'import', TMP_FILE_NAME}
|
|
|
|
end
|
2021-06-30 21:01:51 -06:00
|
|
|
|
2021-07-19 12:43:09 -06:00
|
|
|
local function normalize_whitespace(str)
|
|
|
|
return str:gsub('%s+', ' '):trim()
|
|
|
|
end
|
|
|
|
|
|
|
|
function check_export_success(expected_file_content)
|
|
|
|
local result, file_content = run_orders_export()
|
|
|
|
expect.eq(result, CR_OK)
|
|
|
|
|
|
|
|
-- ignore whitespace (otherwise the expected file content is impossible to
|
|
|
|
-- format properly in this file)
|
|
|
|
expect.eq(normalize_whitespace(expected_file_content),
|
|
|
|
normalize_whitespace(file_content))
|
2021-06-30 21:01:51 -06:00
|
|
|
end
|
|
|
|
|
2021-07-19 12:43:09 -06:00
|
|
|
function check_import_success(file_content, comment, num_expected_orders)
|
|
|
|
local prev_num_orders = #df.global.world.manager_orders
|
2021-06-30 21:01:51 -06:00
|
|
|
local output, result = run_orders_import(file_content)
|
2021-08-14 17:43:40 -06:00
|
|
|
expect.eq(result, CR_OK, comment)
|
2021-07-19 12:43:09 -06:00
|
|
|
expect.eq(prev_num_orders + num_expected_orders,
|
2021-08-14 17:43:40 -06:00
|
|
|
#df.global.world.manager_orders, comment)
|
2021-06-30 21:01:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function check_import_fail(file_content, comment, prefix)
|
|
|
|
comment = comment or ''
|
2021-06-30 21:15:56 -06:00
|
|
|
local prev_num_orders = #df.global.world.manager_orders
|
2021-06-30 21:01:51 -06:00
|
|
|
local output, result = run_orders_import(file_content)
|
2021-06-30 21:15:56 -06:00
|
|
|
expect.eq(result, CR_FAILURE, ('%s: was successful'):format(comment))
|
|
|
|
if prefix then
|
|
|
|
expect.true_(output:lower():startswith(prefix), ('%s: "%s" missing "%s"'):format(comment, output, prefix))
|
|
|
|
end
|
|
|
|
expect.eq(prev_num_orders, #df.global.world.manager_orders, ('%s: number of manager orders changed'):format(comment))
|
2021-06-30 21:01:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function test.import_empty()
|
2021-07-19 12:43:09 -06:00
|
|
|
check_import_success('[]', 'empty input', 0)
|
2021-06-30 21:01:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function test.import_non_array()
|
|
|
|
check_import_fail('{}', 'object', 'invalid')
|
|
|
|
check_import_fail('null', 'null', 'invalid')
|
|
|
|
check_import_fail('2', 'number', 'invalid')
|
|
|
|
end
|
|
|
|
|
|
|
|
function test.import_invalid_syntax()
|
|
|
|
-- for https://github.com/DFHack/dfhack/pull/1770
|
|
|
|
check_import_fail('', 'empty')
|
|
|
|
check_import_fail(']', 'missing opening bracket')
|
|
|
|
check_import_fail([[
|
|
|
|
[
|
|
|
|
{
|
2021-06-30 21:15:56 -06:00
|
|
|
"amount_left" : 0,
|
|
|
|
"amount_total" : 0,
|
|
|
|
"frequency" : "OneTime",
|
|
|
|
"id" : 0,
|
|
|
|
"is_active" : false,
|
|
|
|
"is_validated" : true,
|
|
|
|
"job" : "CustomReaction",
|
|
|
|
"reaction" : "BRASS_MAKING"
|
2021-06-30 21:01:51 -06:00
|
|
|
}
|
|
|
|
]], 'missing closing bracket')
|
|
|
|
end
|
|
|
|
|
2021-06-30 21:15:56 -06:00
|
|
|
function test.import_missing_fields()
|
|
|
|
check_import_fail('[{}]', 'empty order', 'invalid')
|
|
|
|
end
|
|
|
|
|
2021-06-30 21:01:51 -06:00
|
|
|
function test.import_invalid_id()
|
|
|
|
-- for https://github.com/DFHack/dfhack/issues/1893
|
|
|
|
check_import_fail([[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"amount_left": 0,
|
|
|
|
"amount_total": 0,
|
|
|
|
"frequency": "OneTime",
|
|
|
|
"id": "",
|
|
|
|
"is_active": false,
|
|
|
|
"is_validated": false,
|
|
|
|
"item_category": [
|
|
|
|
"finished_goods"
|
|
|
|
],
|
|
|
|
"job": "EncrustWithGems",
|
|
|
|
"material": "INORGANIC:AMBER OPAL"
|
|
|
|
}
|
|
|
|
]
|
2021-06-30 21:15:56 -06:00
|
|
|
]], 'string id instead of int', 'error')
|
|
|
|
end
|
|
|
|
|
|
|
|
function test.import_valid_and_invalid_orders()
|
2021-06-30 21:18:39 -06:00
|
|
|
-- check_import_fail([[
|
|
|
|
-- [
|
|
|
|
-- {
|
|
|
|
-- "amount_left" : 1,
|
|
|
|
-- "amount_total" : 1,
|
|
|
|
-- "frequency" : "OneTime",
|
|
|
|
-- "id" : 0,
|
|
|
|
-- "is_active" : false,
|
|
|
|
-- "is_validated" : true,
|
|
|
|
-- "job" : "ConstructTable",
|
|
|
|
-- "material" : "INORGANIC:IRON"
|
|
|
|
-- },
|
|
|
|
-- {}
|
|
|
|
-- ]
|
|
|
|
-- ]], 'empty order after valid order')
|
2021-06-30 21:15:56 -06:00
|
|
|
|
|
|
|
check_import_fail([[
|
|
|
|
[
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
"amount_left" : 1,
|
|
|
|
"amount_total" : 1,
|
|
|
|
"frequency" : "OneTime",
|
|
|
|
"id" : 0,
|
|
|
|
"is_active" : false,
|
|
|
|
"is_validated" : true,
|
|
|
|
"job" : "ConstructTable",
|
|
|
|
"material" : "INORGANIC:IRON"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]], 'empty order before valid order')
|
2021-06-30 21:01:51 -06:00
|
|
|
end
|
2021-07-19 12:43:09 -06:00
|
|
|
|
|
|
|
function test.import_export_reaction_condition()
|
|
|
|
local file_content = [[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"amount_left" : 1,
|
|
|
|
"amount_total" : 1,
|
|
|
|
"frequency" : "Daily",
|
|
|
|
"id" : 0,
|
|
|
|
"is_active" : false,
|
|
|
|
"is_validated" : false,
|
|
|
|
"item_conditions" :
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"condition" : "AtLeast",
|
|
|
|
"contains" :
|
|
|
|
[
|
|
|
|
"lye"
|
|
|
|
],
|
|
|
|
"reaction_id" : "MAKE_SOAP_FROM_TALLOW",
|
|
|
|
"value" : 5
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"job" : "CustomReaction",
|
|
|
|
"reaction" : "MAKE_SOAP_FROM_TALLOW"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]]
|
|
|
|
check_import_success(file_content, 'valid reaction condition', 1)
|
|
|
|
check_export_success(file_content)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_last_order()
|
|
|
|
return df.global.world.manager_orders[#df.global.world.manager_orders-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
function test.import_invalid_reaction_conditions()
|
|
|
|
check_import_success([[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"amount_left" : 1,
|
|
|
|
"amount_total" : 1,
|
|
|
|
"frequency" : "OneTime",
|
|
|
|
"id" : 0,
|
|
|
|
"is_active" : false,
|
|
|
|
"is_validated" : true,
|
|
|
|
"item_conditions" :
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"condition" : "AtLeast",
|
|
|
|
"contains" :
|
|
|
|
[
|
|
|
|
"lye"
|
|
|
|
],
|
|
|
|
"reaction_id" : "MAKE_SOAP_FROM_TALLOW_xxx",
|
|
|
|
"value" : 5
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"job" : "CustomReaction",
|
|
|
|
"reaction" : "MAKE_SOAP_FROM_TALLOW"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]], 'condition ignored for bad reaction id', 1)
|
|
|
|
expect.eq(0, #get_last_order().item_conditions)
|
|
|
|
|
|
|
|
check_import_success([[
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"amount_left" : 1,
|
|
|
|
"amount_total" : 1,
|
|
|
|
"frequency" : "OneTime",
|
|
|
|
"id" : 0,
|
|
|
|
"is_active" : false,
|
|
|
|
"is_validated" : true,
|
|
|
|
"item_conditions" :
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"condition" : "AtLeast",
|
|
|
|
"contains" :
|
|
|
|
[
|
|
|
|
"lye_xxx"
|
|
|
|
],
|
|
|
|
"reaction_id" : "MAKE_SOAP_FROM_TALLOW",
|
|
|
|
"value" : 5
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"job" : "CustomReaction",
|
|
|
|
"reaction" : "MAKE_SOAP_FROM_TALLOW"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]], 'condition ignored for bad reagent name', 1)
|
|
|
|
expect.eq(0, #get_last_order().item_conditions)
|
|
|
|
end
|
2021-09-07 23:05:47 -06:00
|
|
|
|
|
|
|
function test.list()
|
|
|
|
local output, status = dfhack.run_command_silent('orders', 'list')
|
|
|
|
expect.eq(CR_OK, status)
|
|
|
|
expect.str_find(BACKUP_FILE_NAME:gsub('%-', '%%-'), output)
|
|
|
|
end
|