OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[JS API] Fix process termination on invalid input in Node.js API (#40114)
Alicja Miloszewska81e9bc87ba2e
diff --git a/src/bindings/js/node/include/helper.hpp b/src/bindings/js/node/include/helper.hppindex d231874670f..0271e394bf5 100644--- a/src/bindings/js/node/include/helper.hpp+++ b/src/bindings/js/node/include/helper.hpp@@ -40,6 +40,10 @@ TargetType js_to_cpp(const Napi::Env& env, const Napi::Value& value);template <typename TargetType>TargetType js_to_cpp(const Napi::CallbackInfo& info, const size_t idx);+/** @brief A template specialization for TargetType size_t. Accepts a non-negative integer Number or BigInt. */+template <>+size_t js_to_cpp<size_t>(const Napi::Env& env, const Napi::Value& value);+/** @brief A template specialization for TargetType std::vector<size_t> */template <>std::vector<size_t> js_to_cpp<std::vector<size_t>>(const Napi::CallbackInfo& info, const size_t idx);diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.tsindex c930ddcf964..110fc329ee6 100644--- a/src/bindings/js/node/lib/addon.ts+++ b/src/bindings/js/node/lib/addon.ts@@ -736,8 +736,9 @@ export interface AsyncInferQueue {* Creates AsyncInferQueue.* @param compiledModel The compiledModel that will be used* to create InferRequests in the pool.- * @param jobs Number of InferRequest objects in the pool. If not provided,- * jobs number will be set automatically to the optimal number.+ * @param jobs Number of InferRequest objects in the pool. Must be+ * a non-negative integer. If not provided or set to 0, jobs number+ * will be set automatically to the optimal number.*/new (compiledModel: CompiledModel, jobs?: number): AsyncInferQueue;/**diff --git a/src/bindings/js/node/src/async_infer_queue.cpp b/src/bindings/js/node/src/async_infer_queue.cppindex 8d604a927ad..ff16083af66 100644--- a/src/bindings/js/node/src/async_infer_queue.cpp+++ b/src/bindings/js/node/src/async_infer_queue.cpp@@ -36,8 +36,10 @@ AsyncInferQueue::AsyncInferQueue(const Napi::CallbackInfo& info) : Napi::ObjectWov::js::get_parameters_error_msg(info, allowed_signatures));auto& compiled = Napi::ObjectWrap<CompiledModelWrap>::Unwrap(info[0].ToObject())->get_compiled_model();- size_t jobs =- info.Length() == 1 ? get_optimal_number_of_requests(compiled) : info[1].As<Napi::Number>().Int32Value();+ size_t jobs = info.Length() == 1 ? 0 : js_to_cpp<size_t>(env, info[1]);+ if (jobs == 0) {+ jobs = get_optimal_number_of_requests(compiled);+ }m_tsfn = nullptr;m_requests.reserve(jobs);@@ -52,7 +54,7 @@ AsyncInferQueue::AsyncInferQueue(const Napi::CallbackInfo& info) : Napi::ObjectWm_idle_handles.push(handle);}- } catch (const ov::Exception& e) {+ } catch (const std::exception& e) {reportError(env, e.what());}}diff --git a/src/bindings/js/node/src/compiled_model.cpp b/src/bindings/js/node/src/compiled_model.cppindex 2c5c53c25c5..3ea302e23b4 100644--- a/src/bindings/js/node/src/compiled_model.cpp+++ b/src/bindings/js/node/src/compiled_model.cpp@@ -128,10 +128,15 @@ Napi::Value CompiledModelWrap::get_node(}Napi::Value CompiledModelWrap::export_model(const Napi::CallbackInfo& info) {- std::stringstream _stream;- _compiled_model.export_model(_stream);- const auto& exported = _stream.str();- return Napi::Buffer<const char>::Copy(info.Env(), exported.c_str(), exported.size());+ try {+ std::stringstream _stream;+ _compiled_model.export_model(_stream);+ const auto& exported = _stream.str();+ return Napi::Buffer<const char>::Copy(info.Env(), exported.c_str(), exported.size());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value CompiledModelWrap::set_property(const Napi::CallbackInfo& info) {diff --git a/src/bindings/js/node/src/core_wrap.cpp b/src/bindings/js/node/src/core_wrap.cppindex bd90245a706..2b52158428b 100644--- a/src/bindings/js/node/src/core_wrap.cpp+++ b/src/bindings/js/node/src/core_wrap.cpp@@ -100,7 +100,7 @@ Napi::Value CoreWrap::read_model_sync(const Napi::CallbackInfo& info) {ov::util::set_tensors_names(ov::AUTO, *model);return cpp_to_js(info.Env(), model);- } catch (std::runtime_error& err) {+ } catch (const std::exception& err) {reportError(info.Env(), err.what());return info.Env().Undefined();@@ -114,7 +114,7 @@ Napi::Value CoreWrap::read_model_async(const Napi::CallbackInfo& info) {_readerWorker->Queue();return _readerWorker->GetPromise();- } catch (std::runtime_error& err) {+ } catch (const std::exception& err) {reportError(info.Env(), err.what());return info.Env().Undefined();@@ -247,14 +247,19 @@ Napi::Value CoreWrap::compile_model_async(const Napi::CallbackInfo& info) {}Napi::Value CoreWrap::get_available_devices(const Napi::CallbackInfo& info) {- const auto& devices = _core.get_available_devices();- Napi::Array js_devices = Napi::Array::New(info.Env(), devices.size());+ try {+ const auto& devices = _core.get_available_devices();+ Napi::Array js_devices = Napi::Array::New(info.Env(), devices.size());- uint32_t i = 0;- for (const auto& dev : devices)- js_devices[i++] = dev;+ uint32_t i = 0;+ for (const auto& dev : devices)+ js_devices[i++] = dev;- return js_devices;+ return js_devices;+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value CoreWrap::get_versions(const Napi::CallbackInfo& info) {@@ -267,14 +272,19 @@ Napi::Value CoreWrap::get_versions(const Napi::CallbackInfo& info) {reportError(info.Env(), "The argument in getVersions() method must be a string or convertible to a string.");return info.Env().Undefined();}- const auto& devices_map = _core.get_versions(device_arg.ToString());- Napi::Object versions_object = Napi::Object::New(info.Env());+ try {+ const auto& devices_map = _core.get_versions(device_arg.ToString());+ Napi::Object versions_object = Napi::Object::New(info.Env());- for (const auto& dev : devices_map) {- versions_object.Set(dev.first, cpp_to_js(info.Env(), dev.second));- }+ for (const auto& dev : devices_map) {+ versions_object.Set(dev.first, cpp_to_js(info.Env(), dev.second));+ }- return versions_object;+ return versions_object;+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value CoreWrap::import_model(const Napi::CallbackInfo& info) {@@ -423,7 +433,7 @@ Napi::Value CoreWrap::set_property(const Napi::CallbackInfo& info) {} else {_core.set_property(device_name, properties);}- } catch (std::runtime_error& err) {+ } catch (const std::exception& err) {reportError(info.Env(), err.what());}@@ -440,15 +450,20 @@ Napi::Value CoreWrap::get_property(const Napi::CallbackInfo& info) {return info.Env().Undefined();}- if (args_length == 2)- device_name = info[0].ToString();+ try {+ if (args_length == 2)+ device_name = info[0].ToString();- std::string property_name = info[args_length > 1 ? 1 : 0].ToString();+ std::string property_name = info[args_length > 1 ? 1 : 0].ToString();- ov::Any value =- device_name.empty() ? _core.get_property(property_name) : _core.get_property(device_name, property_name);+ ov::Any value =+ device_name.empty() ? _core.get_property(property_name) : _core.get_property(device_name, property_name);- return any_to_js(info, value);+ return any_to_js(info, value);+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}void CoreWrap::add_extension(const Napi::CallbackInfo& info) {@@ -457,7 +472,7 @@ void CoreWrap::add_extension(const Napi::CallbackInfo& info) {OPENVINO_THROW("addExtension method applies one argument of string type");_core.add_extension(js_to_cpp<std::filesystem::path>(info, 0));- } catch (std::runtime_error& err) {+ } catch (const std::exception& err) {reportError(info.Env(), err.what());}}diff --git a/src/bindings/js/node/src/helper.cpp b/src/bindings/js/node/src/helper.cppindex 4fe95f79800..25fb945389c 100644--- a/src/bindings/js/node/src/helper.cpp+++ b/src/bindings/js/node/src/helper.cpp@@ -5,6 +5,7 @@#include "node/include/helper.hpp"#include <cassert>+#include <cstdint>#include <sstream>#include "node/include/compiled_model.hpp"@@ -175,6 +176,22 @@ std::filesystem::path js_to_cpp<std::filesystem::path>(const Napi::CallbackInfo&return std::filesystem::path(path.ToString().Utf8Value());}+template <>+size_t js_to_cpp<size_t>(const Napi::Env& env, const Napi::Value& value) {+ OPENVINO_ASSERT(value.IsNumber() || value.IsBigInt(), "Passed argument must be of type Number or BigInt.");+ if (value.IsNumber()) {+ OPENVINO_ASSERT(is_napi_value_int(env, value), "Passed argument must be an integer.");+ const auto int_value = value.As<Napi::Number>().Int64Value();+ OPENVINO_ASSERT(int_value >= 0, "Passed argument must be non-negative for size_t.");+ OPENVINO_ASSERT(static_cast<uint64_t>(int_value) <= SIZE_MAX, "Number value is too large for size_t.");+ return static_cast<size_t>(int_value);+ }+ bool lossless = false;+ const uint64_t uint_value = value.As<Napi::BigInt>().Uint64Value(&lossless);+ OPENVINO_ASSERT(lossless && uint_value <= SIZE_MAX, "BigInt value is too large for size_t.");+ return static_cast<size_t>(uint_value);+}+template <>ov::Any js_to_cpp<ov::Any>(const Napi::Env& env, const Napi::Value& value) {if (value.IsString()) {diff --git a/src/bindings/js/node/src/model_wrap.cpp b/src/bindings/js/node/src/model_wrap.cppindex b41ac6a1b7b..66499c37759 100644--- a/src/bindings/js/node/src/model_wrap.cpp+++ b/src/bindings/js/node/src/model_wrap.cpp@@ -57,48 +57,42 @@ std::shared_ptr<ov::Model> ModelWrap::get_model() const {}Napi::Value ModelWrap::get_input(const Napi::CallbackInfo& info) {- if (info.Length() == 0) {- try {+ try {+ OPENVINO_ASSERT(info.Length() <= 1, "Invalid number of arguments -> ", info.Length());+ if (info.Length() == 0) {return Output<ov::Node>::wrap(info.Env(), _model->input());- } catch (std::exception& e) {- reportError(info.Env(), e.what());- return Napi::Value();+ } else if (info[0].IsString()) {+ const auto& tensor_name = info[0].ToString();+ return Output<ov::Node>::wrap(info.Env(), _model->input(tensor_name));+ } else if (info[0].IsNumber()) {+ const auto& idx = info[0].As<Napi::Number>().Int32Value();+ return Output<ov::Node>::wrap(info.Env(), _model->input(idx));+ } else {+ OPENVINO_THROW("'input' method called with incorrect parameters.");}- } else if (info.Length() != 1) {- reportError(info.Env(), "Invalid number of arguments -> " + std::to_string(info.Length()));- return Napi::Value();- } else if (info[0].IsString()) {- const auto& tensor_name = info[0].ToString();- return Output<ov::Node>::wrap(info.Env(), _model->input(tensor_name));- } else if (info[0].IsNumber()) {- const auto& idx = info[0].As<Napi::Number>().Int32Value();- return Output<ov::Node>::wrap(info.Env(), _model->input(idx));- } else {- reportError(info.Env(), "Error while getting model outputs.");+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());return info.Env().Undefined();}}Napi::Value ModelWrap::get_output(const Napi::CallbackInfo& info) {- if (info.Length() == 0) {- try {+ try {+ OPENVINO_ASSERT(info.Length() <= 1, "Invalid number of arguments -> ", info.Length());+ if (info.Length() == 0) {return Output<ov::Node>::wrap(info.Env(), _model->output());- } catch (std::exception& e) {- reportError(info.Env(), e.what());- return Napi::Value();+ } else if (info[0].IsString()) {+ const auto& tensor_name = info[0].ToString();+ return Output<ov::Node>::wrap(info.Env(), _model->output(tensor_name));+ } else if (info[0].IsNumber()) {+ const auto& idx = info[0].As<Napi::Number>().Int32Value();+ return Output<ov::Node>::wrap(info.Env(), _model->output(idx));+ } else {+ OPENVINO_THROW("'output' method called with incorrect parameters.");}- } else if (info.Length() != 1) {- reportError(info.Env(), "Invalid number of arguments -> " + std::to_string(info.Length()));- return Napi::Value();- } else if (info[0].IsString()) {- auto tensor_name = info[0].ToString();- return Output<ov::Node>::wrap(info.Env(), _model->output(tensor_name));- } else if (info[0].IsNumber()) {- auto idx = info[0].As<Napi::Number>().Int32Value();- return Output<ov::Node>::wrap(info.Env(), _model->output(idx));- } else {- reportError(info.Env(), "Error while getting model outputs.");- return Napi::Value();+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();}}diff --git a/src/bindings/js/node/src/node_output.cpp b/src/bindings/js/node/src/node_output.cppindex f2276ba4751..8248b1fd1ca 100644--- a/src/bindings/js/node/src/node_output.cpp+++ b/src/bindings/js/node/src/node_output.cpp@@ -5,6 +5,7 @@#include "node/include/node_output.hpp"#include "node/include/addon.hpp"+#include "node/include/errors.hpp"#include "node/include/helper.hpp"#include "node/include/partial_shape_wrap.hpp"@@ -38,15 +39,30 @@ Napi::Object Output<ov::Node>::wrap(Napi::Env env, ov::Output<ov::Node> output)}Napi::Value Output<ov::Node>::get_shape(const Napi::CallbackInfo& info) {- return cpp_to_js<ov::Shape, Napi::Array>(info, _output.get_shape());+ try {+ return cpp_to_js<ov::Shape, Napi::Array>(info, _output.get_shape());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value Output<ov::Node>::get_partial_shape(const Napi::CallbackInfo& info) {- return PartialShapeWrap::wrap(info.Env(), _output.get_partial_shape());+ try {+ return PartialShapeWrap::wrap(info.Env(), _output.get_partial_shape());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value Output<ov::Node>::get_any_name(const Napi::CallbackInfo& info) {- return Napi::String::New(info.Env(), _output.get_any_name());+ try {+ return Napi::String::New(info.Env(), _output.get_any_name());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Output<const ov::Node>::Output(const Napi::CallbackInfo& info)@@ -81,13 +97,28 @@ Napi::Object Output<const ov::Node>::wrap(Napi::Env env, ov::Output<const ov::No}Napi::Value Output<const ov::Node>::get_shape(const Napi::CallbackInfo& info) {- return cpp_to_js<ov::Shape, Napi::Array>(info, _output.get_shape());+ try {+ return cpp_to_js<ov::Shape, Napi::Array>(info, _output.get_shape());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value Output<const ov::Node>::get_partial_shape(const Napi::CallbackInfo& info) {- return PartialShapeWrap::wrap(info.Env(), _output.get_partial_shape());+ try {+ return PartialShapeWrap::wrap(info.Env(), _output.get_partial_shape());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}Napi::Value Output<const ov::Node>::get_any_name(const Napi::CallbackInfo& info) {- return Napi::String::New(info.Env(), _output.get_any_name());+ try {+ return Napi::String::New(info.Env(), _output.get_any_name());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ return info.Env().Undefined();+ }}diff --git a/src/bindings/js/node/src/preprocess/pre_post_process_wrap.cpp b/src/bindings/js/node/src/preprocess/pre_post_process_wrap.cppindex f4d7445c82a..e32b8f0dab2 100644--- a/src/bindings/js/node/src/preprocess/pre_post_process_wrap.cpp+++ b/src/bindings/js/node/src/preprocess/pre_post_process_wrap.cpp@@ -8,16 +8,19 @@#include "node/include/model_wrap.hpp"#include "node/include/preprocess/input_info.hpp"#include "node/include/preprocess/output_info.hpp"+#include "node/include/type_validation.hpp"PrePostProcessorWrap::PrePostProcessorWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<PrePostProcessorWrap>(info),_ppp(nullptr) {- if (info.Length() != 1)- reportError(info.Env(), "Invalid number of arguments for PrePostProcessor constructor.");- else {- Napi::Object obj = info[0].ToObject();- auto m = Napi::ObjectWrap<ModelWrap>::Unwrap(obj);- _ppp = std::unique_ptr<ov::preprocess::PrePostProcessor>(new ov::preprocess::PrePostProcessor(m->get_model()));+ try {+ OPENVINO_ASSERT(info.Length() == 1, "Invalid number of arguments for PrePostProcessor constructor.");+ OPENVINO_ASSERT(ov::js::validate_value<ModelWrap>(info.Env(), info[0]),+ "PrePostProcessor constructor expects a Model object.");+ auto m = Napi::ObjectWrap<ModelWrap>::Unwrap(info[0].ToObject());+ _ppp = std::make_unique<ov::preprocess::PrePostProcessor>(m->get_model());+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());}}@@ -34,19 +37,24 @@ Napi::Value PrePostProcessorWrap::input(const Napi::CallbackInfo& info) {reportError(info.Env(), "Wrong number of parameters.");return info.Env().Undefined();}- Napi::Object obj = InputInfo::get_class_constructor(info.Env()).New({});- auto input_info = Napi::ObjectWrap<InputInfo>::Unwrap(obj);- if (info.Length() == 0) {- input_info->set_input_info(_ppp->input());- } else if (info[0].IsNumber()) {- input_info->set_input_info(_ppp->input(info[0].ToNumber().Int32Value()));- } else if (info[0].IsString()) {- input_info->set_input_info(_ppp->input(info[0].ToString().Utf8Value()));- } else {- reportError(info.Env(), "Invalid parameter.");+ try {+ Napi::Object obj = InputInfo::get_class_constructor(info.Env()).New({});+ auto input_info = Napi::ObjectWrap<InputInfo>::Unwrap(obj);+ if (info.Length() == 0) {+ input_info->set_input_info(_ppp->input());+ } else if (info[0].IsNumber()) {+ input_info->set_input_info(_ppp->input(info[0].ToNumber().Int32Value()));+ } else if (info[0].IsString()) {+ input_info->set_input_info(_ppp->input(info[0].ToString().Utf8Value()));+ } else {+ reportError(info.Env(), "Invalid parameter.");+ return info.Env().Undefined();+ }+ return obj;+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());return info.Env().Undefined();}- return obj;}Napi::Value PrePostProcessorWrap::output(const Napi::CallbackInfo& info) {@@ -54,21 +62,30 @@ Napi::Value PrePostProcessorWrap::output(const Napi::CallbackInfo& info) {reportError(info.Env(), "Wrong number of parameters.");return info.Env().Undefined();}- Napi::Object obj = OutputInfo::get_class_constructor(info.Env()).New({});- auto output_info = Napi::ObjectWrap<OutputInfo>::Unwrap(obj);- if (info.Length() == 0) {- output_info->set_output_info(_ppp->output());- } else if (info[0].IsNumber()) {- output_info->set_output_info(_ppp->output(info[0].ToNumber().Int32Value()));- } else if (info[0].IsString()) {- output_info->set_output_info(_ppp->output(info[0].ToString().Utf8Value()));- } else {- reportError(info.Env(), "Invalid parameter.");+ try {+ Napi::Object obj = OutputInfo::get_class_constructor(info.Env()).New({});+ auto output_info = Napi::ObjectWrap<OutputInfo>::Unwrap(obj);+ if (info.Length() == 0) {+ output_info->set_output_info(_ppp->output());+ } else if (info[0].IsNumber()) {+ output_info->set_output_info(_ppp->output(info[0].ToNumber().Int32Value()));+ } else if (info[0].IsString()) {+ output_info->set_output_info(_ppp->output(info[0].ToString().Utf8Value()));+ } else {+ reportError(info.Env(), "Invalid parameter.");+ return info.Env().Undefined();+ }+ return obj;+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());return info.Env().Undefined();}- return obj;}void PrePostProcessorWrap::build(const Napi::CallbackInfo& info) {- _ppp->build();+ try {+ _ppp->build();+ } catch (const std::exception& e) {+ reportError(info.Env(), e.what());+ }}diff --git a/src/bindings/js/node/tests/unit/async_infer_queue.test.js b/src/bindings/js/node/tests/unit/async_infer_queue.test.jsindex b394390c12a..9149f121f8b 100644--- a/src/bindings/js/node/tests/unit/async_infer_queue.test.js+++ b/src/bindings/js/node/tests/unit/async_infer_queue.test.js@@ -39,6 +39,22 @@ describe("Tests for AsyncInferQueue.", () => {}, /'AsyncInferQueue' constructor method called with incorrect parameters./);});+ it("Test AsyncInferQueue constructor with 0 jobs uses optimal number of requests", async () => {+ const queue = new ov.AsyncInferQueue(compiledModel, 0);+ queue.setCallback(basicUserCallback);+ try {+ await queue.startAsync({ data: generateImage() });+ } finally {+ queue.release();+ }+ });++ it("Test AsyncInferQueue constructor throws for negative jobs", () => {+ for (const jobs of [-1, -2147483648]) {+ assert.throws(() => new ov.AsyncInferQueue(compiledModel, jobs), /must be non-negative/);+ }+ });+it("Test AsyncInferQueue constructor with default ctor", () => {assert.doesNotThrow(() => {new ov.AsyncInferQueue(compiledModel);diff --git a/src/bindings/js/node/tests/unit/basic.test.js b/src/bindings/js/node/tests/unit/basic.test.jsindex 671245562a6..860ff320f42 100644--- a/src/bindings/js/node/tests/unit/basic.test.js+++ b/src/bindings/js/node/tests/unit/basic.test.js@@ -119,6 +119,10 @@ describe("ov basic tests.", () => {it("getVersions() throws with non string coercable arg.", () => {assert.throws(() => core.getVersions({ deviceName: "CPU" }));});++ it("getVersions() throws for an invalid device list", () => {+ assert.throws(() => core.getVersions("BATCH:CPU,GPU"), /BATCH accepts only one device/);+ });});describe("ov.getOpenvinoVersion()", () => {@@ -301,6 +305,23 @@ describe("ov basic tests.", () => {assert.strictEqual(obj.output().anyName, "fc_out");});});++ it("output() throws for unknown tensor name or out of range index", () => {+ modelLike.forEach((obj) => {+ assert.throws(() => obj.output("nonexistent_output"), /nonexistent_output/);+ assert.throws(() => obj.output(1e9), Error);+ assert.throws(() => obj.output(-1), Error);+ });+ });++ it("output() throws for invalid arguments", () => {+ modelLike.forEach((obj) => {+ assert.throws(() => obj.output({}), Error);+ assert.throws(() => obj.output(0, 1), Error);+ });+ assert.throws(() => model.output({}), /method called with incorrect parameters/);+ assert.throws(() => model.output(0, 1), /Invalid number of arguments -> 2/);+ });});describe("ov.Model/ov.CompiledModel input()", () => {@@ -326,6 +347,32 @@ describe("ov basic tests.", () => {assert.deepStrictEqual(obj.input(0).getShape(), testModelFP32.inputShape);});});++ it("input() throws for unknown tensor name or out of range index", () => {+ modelLike.forEach((obj) => {+ assert.throws(() => obj.input("nonexistent_input"), /nonexistent_input/);+ assert.throws(() => obj.input(1e9), Error);+ assert.throws(() => obj.input(-1), Error);+ });+ });++ it("input() throws for invalid arguments", () => {+ modelLike.forEach((obj) => {+ assert.throws(() => obj.input({}), Error);+ assert.throws(() => obj.input(0, 1), Error);+ });+ assert.throws(() => model.input({}), /method called with incorrect parameters/);+ assert.throws(() => model.input(0, 1), /Invalid number of arguments -> 2/);+ });++ it("input().shape throws for a dynamic shape", () => {+ model.reshape("[?,3,32,32]");+ const dynamicCompiledModel = core.compileModelSync(model, "CPU");+ [model, dynamicCompiledModel].forEach((obj) => {+ assert.throws(() => obj.input().shape, /dynamic shape/);+ assert.throws(() => obj.input().getShape(), /dynamic shape/);+ });+ });});describe("Test exportModel()/importModel()", () => {diff --git a/src/bindings/js/node/tests/unit/core.test.js b/src/bindings/js/node/tests/unit/core.test.jsindex 87b619546af..8f7602b2d51 100644--- a/src/bindings/js/node/tests/unit/core.test.js+++ b/src/bindings/js/node/tests/unit/core.test.js@@ -38,6 +38,12 @@ describe("ov.Core tests", () => {assert.equal(cacheDir, tmpDir);});+ it("Core.getProperty() throws for an unknown property or device", () => {+ assert.throws(() => core.getProperty("NONEXISTENT_PROPERTY"), /NONEXISTENT_PROPERTY/);+ assert.throws(() => core.getProperty("CPU", "NONEXISTENT_PROPERTY"), /NONEXISTENT_PROPERTY/);+ assert.throws(() => core.getProperty("NONEXISTENT_DEVICE", "CACHE_DIR"), /NONEXISTENT_DEVICE/);+ });+it('Core.getProperty("CPU", "SUPPORTED_PROPERTIES") is Array', () => {const supportedPropertiesArray = core.getProperty("CPU", "SUPPORTED_PROPERTIES");diff --git a/src/bindings/js/node/tests/unit/pre_post_processor.test.js b/src/bindings/js/node/tests/unit/pre_post_processor.test.jsindex 665500cca88..636c8065f43 100644--- a/src/bindings/js/node/tests/unit/pre_post_processor.test.js+++ b/src/bindings/js/node/tests/unit/pre_post_processor.test.js@@ -45,6 +45,28 @@ describe("ov.preprocess.PrePostProcessor tests", () => {it("output(const std::string& tensor_name)", () => {assert.doesNotThrow(() => new ov.preprocess.PrePostProcessor(model).output("fc_out"));});++ it("constructor throws for an argument that is not a Model", () => {+ assert.throws(() => new ov.preprocess.PrePostProcessor({}), /expects a Model object/);+ assert.throws(+ () => new ov.preprocess.PrePostProcessor(new ov.Tensor(ov.element.f32, [1])),+ /expects a Model object/,+ );+ });++ it("input()/output() throw for unknown tensor name or out of range index", () => {+ const ppp = new ov.preprocess.PrePostProcessor(model);+ assert.throws(() => ppp.input("nonexistent_input"), /nonexistent_input/);+ assert.throws(() => ppp.input(99), Error);+ assert.throws(() => ppp.output("nonexistent_output"), /nonexistent_output/);+ assert.throws(() => ppp.output(99), Error);+ });++ it("build() throws for invalid preprocessing", () => {+ const ppp = new ov.preprocess.PrePostProcessor(model);+ ppp.input().preprocess().resize(ov.preprocess.resizeAlgorithm.RESIZE_LINEAR);+ assert.throws(() => ppp.build(), Error);+ });});describe("InputInfo", () => {