|
| 1 | +#include <catch2/catch_test_macros.hpp> |
| 2 | +#include <fstream> |
| 3 | +#include "nodeobs_api.h" |
| 4 | +#include "osn-error.hpp" |
| 5 | +#include <obs.h> |
| 6 | +#include "shared.hpp" |
| 7 | +#include <string> |
| 8 | +#include "test-helper.hpp" |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace osn::tests { |
| 12 | + |
| 13 | +// Reads the `wd` export from tests/osn-tests/osn/index.ts. |
| 14 | +// The line has the form: const wd: string = "..."; |
| 15 | +static std::string parseWdFromIndexTs() |
| 16 | +{ |
| 17 | + const std::string indexTsPath = std::string(OSN_SOURCE_DIR) + "/tests/osn-tests/osn/index.ts"; |
| 18 | + |
| 19 | + std::ifstream file(indexTsPath); |
| 20 | + INFO("Could not open " + indexTsPath); |
| 21 | + REQUIRE(file.is_open()); |
| 22 | + |
| 23 | + std::string line; |
| 24 | + while (std::getline(file, line)) { |
| 25 | + const std::string prefix = "const wd"; |
| 26 | + if (line.find(prefix) == std::string::npos) |
| 27 | + continue; |
| 28 | + |
| 29 | + auto first = line.find('"'); |
| 30 | + auto last = line.rfind('"'); |
| 31 | + INFO("Could not parse wd value from: " + line); |
| 32 | + REQUIRE(first != std::string::npos); |
| 33 | + REQUIRE(last != first); |
| 34 | + |
| 35 | + return line.substr(first + 1, last - first - 1); |
| 36 | + } |
| 37 | + |
| 38 | + FAIL("wd declaration not found in " + indexTsPath); |
| 39 | + return {}; |
| 40 | +} |
| 41 | + |
| 42 | +void setWorkingFolder(const std::string &wd) |
| 43 | +{ |
| 44 | + std::vector<ipc::value> args = {ipc::value(wd)}; |
| 45 | + std::vector<ipc::value> response; |
| 46 | + OBS_API::SetWorkingDirectory(nullptr, 0, args, response); |
| 47 | + CHECK(response.size() >= 2); |
| 48 | + ErrorCode error = (ErrorCode)response[0].value_union.ui64; |
| 49 | + CHECK(error == ErrorCode::Ok); |
| 50 | +} |
| 51 | + |
| 52 | +void setupApi() |
| 53 | +{ |
| 54 | +#if defined(__APPLE__) |
| 55 | + g_util_osx = new UtilInt(); |
| 56 | + g_util_osx->init(); |
| 57 | + // Workaround normal app startup where "browser_source" plugin is initialized |
| 58 | + CHECK(!g_util_osx->hasInitApi()); |
| 59 | + g_util_osx->nextState(); |
| 60 | + CHECK(g_util_osx->hasInitApi()); |
| 61 | +#endif |
| 62 | + const std::string appPath = std::string(OSN_SOURCE_DIR) + "/tests/osn-tests/osnData/slobs-client"; |
| 63 | + // osn.NodeObs.OBS_API_initAPI(this.language, this.obsPath, this.version, this.crashServer); |
| 64 | + std::vector<ipc::value> args = {ipc::value(appPath), ipc::value("en-US"), ipc::value("0.00.00-preview.0"), ipc::value("")}; |
| 65 | + std::vector<ipc::value> response; |
| 66 | + OBS_API::OBS_API_initAPI(nullptr, 0, args, response); |
| 67 | + CHECK(response.size() >= 2); |
| 68 | + ErrorCode error = (ErrorCode)response[0].value_union.ui64; |
| 69 | + CHECK(error == ErrorCode::Ok); |
| 70 | +} |
| 71 | + |
| 72 | +void TestHelper::initializeOBS() |
| 73 | +{ |
| 74 | + const std::string wd = parseWdFromIndexTs(); |
| 75 | + setWorkingFolder(wd); |
| 76 | + setupApi(); |
| 77 | +} |
| 78 | + |
| 79 | +void TestHelper::finalizeOBS() |
| 80 | +{ |
| 81 | + std::vector<ipc::value> args = {}; |
| 82 | + std::vector<ipc::value> response; |
| 83 | + OBS_API::OBS_API_destroyOBS_API(nullptr, 0, args, response); |
| 84 | + CHECK(response.size() >= 1); |
| 85 | + ErrorCode error = (ErrorCode)response[0].value_union.ui64; |
| 86 | + CHECK(error == ErrorCode::Ok); |
| 87 | +} |
| 88 | + |
| 89 | +} // namespace osn::tests |
0 commit comments