polserver/testsuite/pol/scripts/tests.src
Fernando Rozenblit 721e4fefe0
Refactor Item states to be explicit instead of implicit (#919)
* extend tests

* more tests for world save

* explicit handle the special layers in wornitem

* add Location variant and relocate() methods

* refactor GottenItem and ensure Chr, not wornitems, is saved

* deflake escript watch test

* dropitem and undo refactoring

* uomod changes

* add more item tests (buy, sell, hair...)

* use location for undoing trade container properly

* rewrite loaddata to use relocate, fixing startup crashes

* do remaining location changes

* fix CI tests

* remove container pointer from item and add wearer() reference

* remove layer writes and explicit intrinsic weapon handling

* add preparing state for fresh items without serial number

* move wornitems to detached state only when it has a serial number

* load corpse items into the correct layer

* do not set a layer on load before the item is equipped

* add more tests to equipped and intrinsic items

* remove layer and substitute with the location()

* add core-changes summary

* mark items as being worn on corpse if they only moved within the corpse

* block Preparing from being reached, just to be sure

* added integrity sweep check

* Reduce direect changes to item location

* Add Item::move_into() and rewrite calling sites

* Fix container slots

* get rid of the slot_index on the item, because Location() already has it

* fix race in test leading to macos issues

* fix dangling pointer

* remove corpse can_equip_list and rely on the GottenItem rules to dress the corpse again

* let item->destroy() detach the item

* make integrity checks fail the tests

* use place_at(item, newpos) for changing item position InWorld

* add boat test for item moved out of boat that should not be visible anymore

* use place_at() in more places

* ensure CreateItemAtLocation really does

* fix Claude instructions

* remove state we didn't need

* fine tuning

* reduce test flakiness
2026-08-10 18:17:53 +00:00

138 lines
5.1 KiB
Text

use uo;
use os;
use polsys;
use file;
include "testutil";
program tests()
ReloadConfiguration(); // A crude attempt to catch issues with thread-safety and pol config & ssopts.
var test_count := CInt( GetEnvironmentVariable( "POLCORE_TEST_RUN" ) );
// format-off
syslog(
$"\n{CONSOLE_COLOR_CYAN}##########################\n"
+ $"{CONSOLE_COLOR_CYAN}# starting testscripts #\n"
+ $"{CONSOLE_COLOR_CYAN}# Run: "+test_count+" #\n"
+ $"{CONSOLE_COLOR_CYAN}##########################\n",0,CONSOLE_COLOR_CYAN);
// format-on
var exitcode := 0;
var testpkgs := {};
var restartpkg;
foreach pkg in Packages()
if ( pkg.dir.find( "testpkgs" ) == 1 )
if ( pkg.name == "testrestart" )
restartpkg := { pkg.name, pkg.desc };
else
testpkgs.append( { pkg.name, pkg.desc } );
endif
endif
endforeach
testpkgs.sort( 1 );
// to prevent cleanup of other tests, TestRestart always at the end
if ( restartpkg )
testpkgs.append( restartpkg );
endif
// Add some colons to ensure array size is always >=3
var filter := ( GetEnvironmentVariable( "POLCORE_TEST_FILTER" ) ?: "" ) + ":::";
var [ filter_pkg, filter_file, filter_func ] := SplitWords( filter, ":" );
if ( filter_pkg || filter_file || filter_func )
syslog( $"## using testfilter filter_pkg=\"{filter_pkg}\", filter_file=\"{filter_file}\", filter_func=\"{filter_func}\", \n",
0, CONSOLE_COLOR_CYAN );
endif
foreach pkg in testpkgs
if ( filter_pkg and !pkg[1].find( filter_pkg ) )
continue;
endif
if ( test_count == 2 and pkg[1] != "testrestart" )
continue;
endif
var pkgstartms := ReadMillisecondClock();
syslog( pkg[2], 0, CONSOLE_COLOR_MAGENTA );
var scripts := listdirectory( ":{}:".format( pkg[1] ), "ecl" );
scripts.sort();
if ( "setup.ecl" in scripts )
syslog( " Calling setup.ecl..", 0, CONSOLE_COLOR_GREEN );
var startms := ReadMillisecondClock();
var res := run_script( ":{}:setup.ecl".format( pkg[1] ) );
if ( res == IGNORE_TEST )
continue;
elseif ( res != 1 )
syslog( " failed: " + res.errortext, 0, CONSOLE_COLOR_RED );
exitcode := 1;
continue;
endif
syslog( " ..{}ms".format( ReadMillisecondClock() - startms ), 0, CONSOLE_COLOR_GREEN );
endif
foreach file in scripts
if ( file.find( "test" ) != 1 )
continue;
elseif ( filter_file and !file.find( filter_file ) )
continue;
endif
var scriptstartms := ReadMillisecondClock();
syslog( " Calling {}..".format( file ), 0, CONSOLE_COLOR_GREEN );
var script := LoadExportedScript( ":{}:{}".format( pkg[1], file ) );
if ( !script[2] )
syslog( " failed: " + script[2], 0, CONSOLE_COLOR_RED );
exitcode := 1;
continue;
endif
if ( script[2] == IGNORE_TEST )
continue;
endif
var resources := ResourceManager();
foreach func in ( script[1].exported_functions )
if ( filter_func && !func.find( filter_func ) )
continue;
endif
var startms := ReadMillisecondClock();
syslog( " Calling {}..".format( func ), 0, CONSOLE_COLOR_GREEN );
var res := script[1].call( func, { resources } );
resources.cleanup();
if ( res != 1 )
syslog( " failed: " + res.errortext, 0, CONSOLE_COLOR_RED );
exitcode := 1;
continue;
endif
syslog( " ..{}ms".format( ReadMillisecondClock() - startms ), 0, CONSOLE_COLOR_GREEN );
endforeach
syslog( " ..{}ms".format( ReadMillisecondClock() - scriptstartms ), 0, CONSOLE_COLOR_GREEN );
endforeach
if ( "cleanup.ecl" in scripts )
syslog( " Calling cleanup.ecl..", 0, CONSOLE_COLOR_GREEN );
var startms := ReadMillisecondClock();
run_script( ":{}:cleanup.ecl".format( pkg[1] ) );
syslog( " ..{}ms".format( ReadMillisecondClock() - startms ), 0, CONSOLE_COLOR_GREEN );
endif
// Named here rather than at the end of the run so a package that leaves the world disagreeing
// with itself blames itself instead of whichever package runs next.
// The check count is logged even when clean: a sweep that stops reaching anything reports the
// same silence as a world with nothing wrong with it.
var integ := CheckItemIntegrity();
if ( integ.violations )
syslog( $" !! {integ.violations} item integrity violations, {integ.checks} checks", 0,
CONSOLE_COLOR_RED );
exitcode := 1;
else
syslog( $" item integrity ok, {integ.checks} checks", 0, CONSOLE_COLOR_GREEN );
endif
syslog( "..{}ms".format( ReadMillisecondClock() - pkgstartms ), 0, CONSOLE_COLOR_MAGENTA );
endforeach
var result := $"{CONSOLE_COLOR_GREEN}success";
if ( exitcode )
result := $"{CONSOLE_COLOR_RED}failed ";
endif
// format-off
syslog(
$"\n{CONSOLE_COLOR_CYAN}###########################\n"
+ $"{CONSOLE_COLOR_CYAN}# finished tests: {result} {CONSOLE_COLOR_CYAN}#\n"
+ $"{CONSOLE_COLOR_CYAN}###########################\n",0,CONSOLE_COLOR_CYAN);
// format-on
shutdown( exitcode );
endprogram