Files
dusk/test/script/test_overworldscene.c
T
YourWishes b9d2fe60fd Split overworldscene.js's player/plane/camera into their own classes
Extracts Player (extends Entity), TestPlane, and PlayerCamera into
their own files; the camera now tracks the player's live position
at a fixed offset instead of orbiting the world origin over time.
Updates test_overworldscene.c to match the new entity order and to
build an in-memory zip fixture, since overworldscene.js now
require()s these sibling files and require() always resolves
through ASSET.zip.
2026-08-03 19:35:54 -05:00

405 lines
14 KiB
C

/**
* 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 "asset/asset.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 "script/module/scene/modulescene.h"
#include "script/module/require/modulerequire.h"
#include <zip.h>
#include <stdio.h>
#include <string.h>
#ifndef DUSK_ASSETS_DIR
#error "DUSK_ASSETS_DIR must be defined"
#endif
static char_t *readFile(const char_t *relativePath) {
char_t path[512];
snprintf(path, sizeof(path), "%s/%s", DUSK_ASSETS_DIR, relativePath);
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;
}
// Runs the real, shipped overworldscene.js through Scene.set() the same
// way require()/init.js would -- not a copy embedded in this test, so
// this actually verifies what ships.
static errorret_t installOverworldScene(void) {
char_t *fileSrc = readFile("scripts/overworldscene.js");
const char_t *prefix = "var module = { exports: {} };\n(function(module){\n";
const char_t *suffix = "\n})(module);\nScene.set(module.exports);";
size_t wrappedLen = strlen(prefix) + strlen(fileSrc) + strlen(suffix);
char_t *wrapped = (char_t *)memoryAllocate(wrappedLen + 1);
snprintf(
wrapped, wrappedLen + 1, "%s%s%s", prefix, fileSrc, suffix
);
memoryFree(fileSrc);
// overworldscene.js now require()s sibling files (Player.js etc.) --
// push the same base directory scriptManagerExecFile() would push for
// a real load, since this test bypasses that path to exec a wrapped
// copy of the source directly instead.
moduleRequireDirPush("scripts/");
errorret_t ret = scriptManagerExec(wrapped, NULL);
moduleRequireDirPop();
memoryFree(wrapped);
return ret;
}
static zip_t *g_zip = NULL;
// overworldscene.js require()s sibling files (Player.js/TestPlane.js/
// PlayerCamera.js), which resolve through the asset system's ASSET.zip --
// there's no real-filesystem fallback (see assetFileInit()). Package the
// real, shipped copies of those files (read straight off disk, not
// hardcoded here) into an in-memory zip so require() finds the same
// content a real build would.
static int overworld_setup(void **state) {
sceneInit();
errorret_t ret = scriptManagerInit();
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
zip_error_t err;
zip_error_init(&err);
zip_source_t *write_src = zip_source_buffer_create(NULL, 0, 1, &err);
if(!write_src) return -1;
zip_t *za = zip_open_from_source(write_src, ZIP_TRUNCATE, &err);
if(!za) { zip_source_free(write_src); return -1; }
const char_t *requiredScripts[] = {
"scripts/Player.js", "scripts/TestPlane.js", "scripts/PlayerCamera.js"
};
char_t *scriptSrcs[3];
for(size_t i = 0; i < 3; i++) {
scriptSrcs[i] = readFile(requiredScripts[i]);
zip_source_t *s = zip_source_buffer(
za, scriptSrcs[i], strlen(scriptSrcs[i]), 0
);
if(zip_file_add(za, requiredScripts[i], s, ZIP_FL_OVERWRITE) < 0) {
zip_close(za);
return -1;
}
}
zip_source_keep(write_src);
if(zip_close(za) != 0) { zip_source_free(write_src); return -1; }
// zip_close() has fully read every source added above by now, so the
// backing buffers are safe to free.
for(size_t i = 0; i < 3; i++) memoryFree(scriptSrcs[i]);
zip_stat_t zs;
memset(&zs, 0, sizeof(zs));
if(zip_source_stat(write_src, &zs) != 0 || !(zs.valid & ZIP_STAT_SIZE)) {
zip_source_free(write_src);
return -1;
}
void *zipbuf = malloc((size_t)zs.size);
if(!zipbuf) { zip_source_free(write_src); return -1; }
if(zip_source_open(write_src) != 0) {
free(zipbuf);
zip_source_free(write_src);
return -1;
}
zip_source_read(write_src, zipbuf, (zip_uint64_t)zs.size);
zip_source_close(write_src);
zip_source_free(write_src);
zip_error_init(&err);
zip_source_t *read_src = zip_source_buffer_create(
zipbuf, (zip_uint64_t)zs.size, 1, &err
);
if(!read_src) { free(zipbuf); return -1; }
g_zip = zip_open_from_source(read_src, 0, &err);
if(!g_zip) { zip_source_free(read_src); return -1; }
ASSET.zip = g_zip;
return 0;
}
static int overworld_teardown(void **state) {
errorret_t ret = scriptManagerDispose();
if(errorIsNotOk(ret)) errorCatch(ret);
sceneDispose();
if(g_zip) { zip_close(g_zip); g_zip = NULL; }
ASSET.zip = NULL;
// 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) {
errorret_t ret = installOverworldScene();
assert_true(errorIsOk(ret));
sceneid_t sceneId = sceneGetActive();
assert_true(sceneId != SCENE_ID_INVALID);
entitymanager_t *mgr = sceneGetEntities(sceneId);
// Entity 0: player (new Player() runs first in init()).
entityid_t playerEntity = 0;
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
);
// Entity 1: static ground plane (new TestPlane() runs second).
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: camera (new PlayerCamera(player) runs last). Position +
// Camera components, positioned at the player's local position plus
// PlayerCamera.js's fixed offset (angle=0, radius=18, height=10) --
// player is at (0, 2, 0), so eye=(18, 12, 0).
entityid_t camEntity = 2;
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], 12.0f, 0.0001f);
assert_float_equal(camPosition[2], 0.0f, 0.0001f);
}
static void test_overworldscene_camera_follows_player(void **state) {
errorret_t ret = installOverworldScene();
assert_true(errorIsOk(ret));
sceneid_t sceneId = sceneGetActive();
entitymanager_t *mgr = sceneGetEntities(sceneId);
entityid_t playerEntity = 0;
componentid_t playerPos = entityGetComponent(
mgr, playerEntity, COMPONENT_TYPE_POSITION
);
entityid_t camEntity = 2;
componentid_t camPos = entityGetComponent(
mgr, camEntity, COMPONENT_TYPE_POSITION
);
// Move the player and confirm the camera's next update() re-centers on
// the player's new position at the same fixed offset (PlayerCamera.js
// no longer orbits over time -- it just tracks the player).
entityPositionSetLocalPosition(
mgr, playerEntity, playerPos, (vec3){ 5.0f, 2.0f, -3.0f }
);
ret = moduleSceneUpdateCurrent();
assert_true(errorIsOk(ret));
vec3 camPosition;
entityPositionGetLocalPosition(mgr, camEntity, camPos, camPosition);
assert_float_equal(camPosition[0], 5.0f + 18.0f, 0.0001f);
assert_float_equal(camPosition[1], 2.0f + 10.0f, 0.0001f);
assert_float_equal(camPosition[2], -3.0f, 0.0001f);
}
static void test_scene_set_switches_and_disposes(void **state) {
errorret_t ret = installOverworldScene();
assert_true(errorIsOk(ret));
assert_true(sceneGetActive() != SCENE_ID_INVALID);
// Install a second, trivial scene module in place of the first. This
// must: call the first module's dispose(), destroy its scene, then
// create+activate a new one and call the new module's init().
ret = scriptManagerExec(
"var switchState = { updateCount: 0, disposed: false };\n"
"Scene.set({\n"
" init: function() { var e = new Entity(); e.add(POSITION); },\n"
" update: function() { switchState.updateCount++; },\n"
" dispose: function() { switchState.disposed = true; }\n"
"});",
NULL
);
assert_true(errorIsOk(ret));
sceneid_t secondSceneId = sceneGetActive();
assert_true(secondSceneId != SCENE_ID_INVALID);
entitymanager_t *mgr = sceneGetEntities(secondSceneId);
assert_true(
entityGetComponent(mgr, 0, COMPONENT_TYPE_POSITION) !=
COMPONENT_ID_INVALID
);
// The first module's plane entity (entity 1, POSITION+PHYSICS+
// RENDERABLE) must be gone -- proves the old scene was actually torn
// down, not just deactivated. (Scene IDs are a small reused pool --
// SCENE_COUNT_MAX slots -- so secondSceneId == firstSceneId here is
// expected, not a bug: sceneCreate() picks the first free slot, and
// destroying firstSceneId frees that exact slot right back up.)
assert_true(
entityGetComponent(mgr, 1, COMPONENT_TYPE_POSITION) ==
COMPONENT_ID_INVALID
);
ret = moduleSceneUpdateCurrent();
assert_true(errorIsOk(ret));
jerry_value_t result;
ret = scriptManagerExec("switchState.updateCount", &result);
assert_true(errorIsOk(ret));
assert_true(jerry_value_is_number(result));
assert_int_equal((int)jerry_value_as_number(result), 1);
jerry_value_free(result);
// Switching again must call the second module's dispose().
ret = scriptManagerExec(
"Scene.set({ init: function() {} });", NULL
);
assert_true(errorIsOk(ret));
ret = scriptManagerExec("switchState.disposed", &result);
assert_true(errorIsOk(ret));
assert_true(jerry_value_is_true(result));
jerry_value_free(result);
}
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_camera_follows_player,
overworld_setup, overworld_teardown
),
cmocka_unit_test_setup_teardown(
test_scene_set_switches_and_disposes,
overworld_setup, overworld_teardown
),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}