/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "lib/self_test.h" #include "lib/file/file_system.h" #include "lib/file/vfs/vfs.h" #include "lib/file/vfs/vfs_path.h" #include "lib/file/vfs/vfs_util.h" #include "lib/os_path.h" #include "lib/status.h" #include "ps/Filesystem.h" #include "ps/scripting/JSInterface_VFS.h" #include "scriptinterface/Context.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/Interface.h" #include "scriptinterface/ModuleLoader.h" #include "scriptinterface/Request.h" #include #include /** * Runs simulation/ai//tests/test_*.js as ES modules, * so they can import the AI files like the engine does. * The globalscripts are loaded first, as in the AI context. * Each test module exports a default function which is called. */ class TestAIScripts : public CxxTest::TestSuite { public: void setUp() { g_VFS = CreateVfs(); TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "mod" / "", VFS_MOUNT_MUST_EXIST)); // Ignore directory-not-found errors. g_VFS->Mount(L"", DataDir() / "mods" / "public" / "", VFS_MOUNT_MUST_EXIST, 1); } void tearDown() { g_VFS.reset(); } void test_ai_scripts() { DirectoryNames ais; if (g_VFS->GetDirectoryEntries(L"simulation/ai/", nullptr, &ais) != INFO::OK) TS_SKIP("Can't find simulation/ai/."); for (const OsPath& ai : ais) { const VfsPath testDir{VfsPath{L"simulation/ai"} / ai.string() / L"tests/"}; // An AI without tests doesn't have this directory. if (!VfsDirectoryExists(testDir)) continue; VfsPaths paths; TS_ASSERT_OK(vfs::GetPathnames(g_VFS, testDir, L"test_*.js", paths)); for (const VfsPath& path : paths) { Script::Interface script{"Engine", "Test", g_ScriptContext, [](const VfsPath&) { return true; }}; ScriptTestSetup(script); const Script::Request rq{script}; // Give the tests the same globals as the AI context (see CAIWorker). // Globalscripts may use VFS script functions. JSI_VFS::RegisterScriptFunctions_ReadOnlySimulation(rq); TSM_ASSERT(path.string8(), script.LoadGlobalScripts()); Script::ModuleLoader::Result result{script.GetModuleLoader().LoadModule(rq, path)}; g_ScriptContext->RunJobs(); Script::ModuleLoader::Future& future{*result.begin()}; TSM_ASSERT(path.string8(), future.IsDone()); if (!future.IsDone()) continue; JS::RootedObject moduleNamespace{rq.cx}; TSM_ASSERT_THROWS_NOTHING(path.string8(), moduleNamespace = future.Get()); if (!moduleNamespace) continue; JS::RootedValue module{rq.cx, JS::ObjectValue(*moduleNamespace)}; TSM_ASSERT(path.string8(), Script::Function::CallVoid(rq, module, "default")); } } } };