@@ -18,6 +18,21 @@ describe("HV machine network", function()
1818 mineunit :execute_globalstep (60 )
1919 world .set_default_node (" air" )
2020
21+ -- Execute multiple globalsteps: run_network(times = 1, dtime = 1)
22+ local run_network = spec_utility .run_globalsteps
23+
24+ -- Place itemstack into inventory slot 1: place_itemstack(pos, itemstack, listname = "src")
25+ local place_itemstack = spec_utility .place_itemstack
26+
27+ -- Get itemstack for inspection without removing it: get_itemstack(pos, listname = "dst", index = 1)
28+ local get_itemstack = spec_utility .get_itemstack
29+
30+ -- Execute this many 1 second glopbalstep cycles for each RE machine
31+ local RUN_CYCLES = 4
32+ -- Function to calculate amount of items produced by base machines within completed network cycles
33+ -- usage: base_machine_expected_amount(machine_speed, recipe_time, output_amount)
34+ local base_machine_expected_amount = spec_utility .base_machine_output_calculator (RUN_CYCLES )
35+
2136 local machines = {
2237 " technic:hv_generator" ,
2338 " technic:hv_battery_box0" ,
@@ -29,45 +44,23 @@ describe("HV machine network", function()
2944 " technic:hv_solar_array" ,
3045 }
3146
47+ local function reset_machine (pos )
48+ world .place_node (pos , machines [pos .x ], player )
49+ end
50+
3251 world .clear ()
3352 world .place_node ({x = 0 ,y = 51 ,z = 0 }, " technic:switching_station" , player )
3453 for x = 0 , 10 do
3554 world .place_node ({x = x ,y = 50 ,z = 0 }, " technic:hv_cable" , player )
3655 end
37- for x , name in ipairs (machines ) do
38- world .place_node ({x = x ,y = 51 ,z = 0 }, name , player )
39- end
40-
41- -- Helper function to execute netowork
42- local function run_network (times )
43- times = times or 1
44- for i = 1 , times do
45- -- Globalstep every second instead of every 0.1 seconds
46- mineunit :execute_globalstep (1 )
47- end
48- end
49-
50- -- Helper function to place itemstack into machine inventory
51- local function place_itemstack (pos , itemstack , listname )
52- local meta = minetest .get_meta (pos )
53- local inv = meta :get_inventory ()
54- if not inv :room_for_item (listname or " src" , itemstack ) then
55- inv :set_stack (listname or " src" , 1 , ItemStack (nil ))
56- end
57- inv :add_item (listname or " src" , itemstack )
58- end
59-
60- -- Get itemstack in inventory for inspection without removing it
61- local function get_itemstack (pos , listname , index )
62- local meta = minetest .get_meta (pos )
63- local inv = meta :get_inventory ()
64- return inv :get_stack (listname or " dst" , index or 1 )
56+ for x = 1 , # machines do
57+ reset_machine ({x = x ,y = 51 ,z = 0 })
6558 end
6659
6760 it (" executes network" , function ()
6861 spy .on (technic , " network_run" )
69- run_network (60 )
70- assert .spy (technic .network_run ).called (60 )
62+ run_network (4 )
63+ assert .spy (technic .network_run ).called (4 )
7164 local id = technic .pos2network ({x = 0 ,y = 50 ,z = 0 })
7265 assert .not_nil (technic .networks [id ])
7366 assert .gt (technic .networks [id ].supply , 0 )
@@ -95,48 +88,61 @@ describe("HV machine network", function()
9588
9689 it (" smelts ores" , function ()
9790 local machine_pos = {x = 3 ,y = 51 ,z = 0 }
91+ reset_machine (machine_pos )
9892 place_itemstack (machine_pos , " technic:lead_lump 99" )
99- run_network (50 )
100- -- Check results, at least 10 items processed and results in correct stuff
93+ -- Extra cycle: powers up the machine but wont produce anything
94+ run_network (RUN_CYCLES + 1 )
95+ -- Check results
10196 local stack = get_itemstack (machine_pos )
102- assert .gt (stack :get_count (), 9 )
10397 assert .equals (stack :get_name (), " technic:lead_ingot" )
98+ -- Expected amount of items produced: machine speed, recipe time, items per cycle
99+ assert .equals (base_machine_expected_amount (12 , 3 , 1 ), stack :get_count ())
104100 end )
105101
106102 it (" grinds ores" , function ()
107103 local machine_pos = {x = 4 ,y = 51 ,z = 0 }
104+ reset_machine (machine_pos )
108105 place_itemstack (machine_pos , " technic:lead_lump 99" )
109- run_network (50 )
110- -- Check results, at least 10 items processed and results in correct stuff
106+ -- Extra cycle: powers up the machine but wont produce anything
107+ run_network (RUN_CYCLES + 1 )
108+ -- Check results
111109 local stack = get_itemstack (machine_pos )
112- assert .gt (stack :get_count (), 9 )
113110 assert .equals (stack :get_name (), " technic:lead_dust" )
111+ -- Expected amount of items produced: machine speed, recipe time, items per cycle
112+ assert .equals (base_machine_expected_amount (5 , 3 , 2 ), stack :get_count ())
114113 end )
115114
116115 it (" accepts battery upgrades" , function ()
117116 local machine_pos = {x = 4 ,y = 51 ,z = 0 }
118- mineunit : clear_InvRef (machine_pos )
117+ reset_machine (machine_pos )
119118 place_itemstack (machine_pos , " technic:battery 1" , " upgrade1" )
120119 place_itemstack (machine_pos , " technic:battery 1" , " upgrade2" )
121120 place_itemstack (machine_pos , " technic:lead_lump 99" )
122- run_network (6 )
123- -- Check results, at least 2 items processed and results in correct stuff
121+ -- Extra cycle: powers up the machine but wont produce anything
122+ run_network (RUN_CYCLES + 1 )
123+ -- Check results
124124 local stack = get_itemstack (machine_pos )
125- assert .gt (stack :get_count (), 2 )
126125 assert .equals (stack :get_name (), " technic:lead_dust" )
126+ -- Expected amount of items produced: machine speed, recipe time, items per cycle
127+ assert .equals (base_machine_expected_amount (5 , 3 , 2 ), stack :get_count ())
127128 end )
128129
129130 it (" accepts control logic upgrades" , function ()
130131 local machine_pos = {x = 4 ,y = 51 ,z = 0 }
131- mineunit : clear_InvRef (machine_pos )
132+ reset_machine (machine_pos )
132133 place_itemstack (machine_pos , " technic:control_logic_unit 1" , " upgrade1" )
133134 place_itemstack (machine_pos , " technic:control_logic_unit 1" , " upgrade2" )
134135 place_itemstack (machine_pos , " technic:lead_lump 99" )
135- run_network (6 )
136- -- Check results, at least 2 items processed and results in correct stuff
136+ -- Extra cycle: powers up the machine but wont produce anything
137+ run_network (RUN_CYCLES + 1 )
138+ -- Check results
137139 local stack = get_itemstack (machine_pos )
138- assert .gt (stack :get_count (), 1 )
139140 assert .equals (stack :get_name (), " technic:lead_dust" )
141+ -- Expected amount of items produced: machine speed, recipe time, items per cycle
142+ -- Minus items sent away by CLUs: single item per cycle except first cycle which only produces
143+ local expected_amount = base_machine_expected_amount (5 , 3 , 2 ) - RUN_CYCLES + 1
144+ assert .equals (expected_amount , stack :get_count ())
145+ -- Pipeworks isn't loaded and tubes are handled with no-op functions, sent items are lost
140146 end )
141147
142148end )
0 commit comments