/** * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "dusktest.h" #include "util/memory.h" #include "time/time.h" #include "scene/scene.h" #include "entity/entitymanager.h" #include "entity/component.h" #include "entity/component/display/entityposition.h" #include "entity/component/physics/entityphysics.h" #include "entity/component/display/entityrenderable.h" #include "display/mesh/plane.h" #include "display/mesh/capsule.h" #include "script/scriptmanager.h" #include #include #ifndef DUSK_ASSETS_DIR #error "DUSK_ASSETS_DIR must be defined" #endif // Reads the real, shipped overworldscene.js from disk (not a copy embedded // in this test) so this test actually verifies what ships. static char_t *readScriptSource(void) { char_t path[512]; snprintf( path, sizeof(path), "%s/scripts/overworldscene.js", DUSK_ASSETS_DIR ); FILE *f = fopen(path, "rb"); assert_non_null(f); fseek(f, 0, SEEK_END); long size = ftell(f); fseek(f, 0, SEEK_SET); char_t *buf = (char_t *)memoryAllocate((size_t)size + 1); size_t read = fread(buf, 1, (size_t)size, f); fclose(f); buf[read] = '\0'; return buf; } static int overworld_setup(void **state) { sceneInit(); errorret_t ret = scriptManagerInit(); if(errorIsNotOk(ret)) { errorCatch(ret); return -1; } return 0; } static int overworld_teardown(void **state) { errorret_t ret = scriptManagerDispose(); if(errorIsNotOk(ret)) errorCatch(ret); sceneDispose(); // JerryScript defers freeing native-wrapped handles (Entity/Scene/ // Position/Physics/Renderable instances) until GC/jerry_cleanup() runs, // so the leak check can only be meaningful after scriptManagerDispose() // has actually run above -- not inside the test body. assert_int_equal(memoryGetAllocatedCount(), 0); return 0; } static void test_overworldscene_builds_expected_entities(void **state) { char_t *src = readScriptSource(); errorret_t ret = scriptManagerExec(src, NULL); memoryFree(src); assert_true(errorIsOk(ret)); sceneid_t sceneId = sceneGetActive(); assert_true(sceneId != SCENE_ID_INVALID); entitymanager_t *mgr = sceneGetEntities(sceneId); // Entity 0: camera. Position + Camera components, initial orbit // position placed with angle=0 (Time.delta defaults to 0 with no // timeInit()/timeUpdate() in this test), i.e. eye=(radius, height, 0). entityid_t camEntity = 0; componentid_t camPos = entityGetComponent( mgr, camEntity, COMPONENT_TYPE_POSITION ); assert_true(camPos != COMPONENT_ID_INVALID); assert_true( entityGetComponent(mgr, camEntity, COMPONENT_TYPE_CAMERA) != COMPONENT_ID_INVALID ); vec3 camPosition; entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition); assert_float_equal(camPosition[0], 18.0f, 0.0001f); assert_float_equal(camPosition[1], 10.0f, 0.0001f); assert_float_equal(camPosition[2], 0.0f, 0.0001f); // Entity 1: static ground plane. entityid_t planeEntity = 1; componentid_t planePos = entityGetComponent( mgr, planeEntity, COMPONENT_TYPE_POSITION ); assert_true(planePos != COMPONENT_ID_INVALID); vec3 planePosition, planeScale; entityPositionGetLocalPosition(mgr, planeEntity, planePos, planePosition); entityPositionGetLocalScale(mgr, planeEntity, planePos, planeScale); assert_float_equal(planePosition[0], -10.0f, 0.0001f); assert_float_equal(planePosition[1], 0.0f, 0.0001f); assert_float_equal(planePosition[2], -10.0f, 0.0001f); assert_float_equal(planeScale[0], 20.0f, 0.0001f); assert_float_equal(planeScale[1], 1.0f, 0.0001f); assert_float_equal(planeScale[2], 20.0f, 0.0001f); componentid_t planePhysics = entityGetComponent( mgr, planeEntity, COMPONENT_TYPE_PHYSICS ); assert_true(planePhysics != COMPONENT_ID_INVALID); assert_int_equal( entityPhysicsGetBodyType(mgr, planeEntity, planePhysics), PHYSICS_BODY_STATIC ); physicsshape_t planeShape = entityPhysicsGetShape( mgr, planeEntity, planePhysics ); assert_int_equal(planeShape.type, PHYSICS_SHAPE_PLANE); assert_float_equal(planeShape.data.plane.normal[0], 0.0f, 0.0001f); assert_float_equal(planeShape.data.plane.normal[1], 1.0f, 0.0001f); assert_float_equal(planeShape.data.plane.normal[2], 0.0f, 0.0001f); assert_float_equal(planeShape.data.plane.distance, 0.0f, 0.0001f); componentid_t planeRenderable = entityGetComponent( mgr, planeEntity, COMPONENT_TYPE_RENDERABLE ); assert_true(planeRenderable != COMPONENT_ID_INVALID); entityrenderable_t *planeR = componentGetData( mgr, planeEntity, planeRenderable, COMPONENT_TYPE_RENDERABLE ); assert_ptr_equal(planeR->data.material.meshes[0], &PLANE_MESH_SIMPLE); assert_int_equal(planeR->data.material.material.unlit.color.r, 128); assert_int_equal(planeR->data.material.material.unlit.color.g, 128); assert_int_equal(planeR->data.material.material.unlit.color.b, 128); assert_int_equal(planeR->data.material.material.unlit.color.a, 255); // Entity 2: player. Dynamic capsule body (default body type), PLAYER // component present. entityid_t playerEntity = 2; componentid_t playerPos = entityGetComponent( mgr, playerEntity, COMPONENT_TYPE_POSITION ); assert_true(playerPos != COMPONENT_ID_INVALID); vec3 playerPosition; entityPositionGetLocalPosition(mgr, playerEntity, playerPos, playerPosition); assert_float_equal(playerPosition[0], 0.0f, 0.0001f); assert_float_equal(playerPosition[1], 2.0f, 0.0001f); assert_float_equal(playerPosition[2], 0.0f, 0.0001f); componentid_t playerPhysics = entityGetComponent( mgr, playerEntity, COMPONENT_TYPE_PHYSICS ); assert_true(playerPhysics != COMPONENT_ID_INVALID); assert_int_equal( entityPhysicsGetBodyType(mgr, playerEntity, playerPhysics), PHYSICS_BODY_DYNAMIC ); physicsshape_t playerShape = entityPhysicsGetShape( mgr, playerEntity, playerPhysics ); assert_int_equal(playerShape.type, PHYSICS_SHAPE_CAPSULE); assert_float_equal(playerShape.data.capsule.radius, 0.5f, 0.0001f); assert_float_equal(playerShape.data.capsule.halfHeight, 0.5f, 0.0001f); componentid_t playerRenderable = entityGetComponent( mgr, playerEntity, COMPONENT_TYPE_RENDERABLE ); assert_true(playerRenderable != COMPONENT_ID_INVALID); entityrenderable_t *playerR = componentGetData( mgr, playerEntity, playerRenderable, COMPONENT_TYPE_RENDERABLE ); assert_ptr_equal(playerR->data.material.meshes[0], &CAPSULE_MESH_SIMPLE); assert_int_equal(playerR->data.material.material.unlit.color.r, 0); assert_int_equal(playerR->data.material.material.unlit.color.g, 0); assert_int_equal(playerR->data.material.material.unlit.color.b, 255); assert_int_equal(playerR->data.material.material.unlit.color.a, 255); assert_true( entityGetComponent(mgr, playerEntity, COMPONENT_TYPE_PLAYER) != COMPONENT_ID_INVALID ); } static void test_overworldscene_update_orbits_camera(void **state) { char_t *src = readScriptSource(); errorret_t ret = scriptManagerExec(src, NULL); memoryFree(src); assert_true(errorIsOk(ret)); sceneid_t sceneId = sceneGetActive(); entitymanager_t *mgr = sceneGetEntities(sceneId); entityid_t camEntity = 0; componentid_t camPos = entityGetComponent( mgr, camEntity, COMPONENT_TYPE_POSITION ); // Drive one frame with a known delta and confirm the camera orbited by // exactly angle = delta * speed (0.1 * 0.5 = 0.05 rad). TIME.delta = 0.1f; ret = scriptManagerCallGlobal("update"); assert_true(errorIsOk(ret)); vec3 camPosition; entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition); const float_t expectedAngle = 0.1f * 0.5f; assert_float_equal( camPosition[0], cosf(expectedAngle) * 18.0f, 0.0001f ); assert_float_equal(camPosition[1], 10.0f, 0.0001f); assert_float_equal( camPosition[2], sinf(expectedAngle) * 18.0f, 0.0001f ); TIME.delta = 0.0f; } int main(void) { assertInit(); const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown( test_overworldscene_builds_expected_entities, overworld_setup, overworld_teardown ), cmocka_unit_test_setup_teardown( test_overworldscene_update_orbits_camera, overworld_setup, overworld_teardown ), }; return cmocka_run_group_tests(tests, NULL, NULL); }