#include <string>

#include "test_env.h"

extern "C" void init_test_null(napi_env env, napi_value exports);

class Test26ObjectNull : public FixtureTestBase {};

TEST_F(Test26ObjectNull, PortedCoreFlow) {
  EnvScope s(runtime_.get());
  napi_value exports = nullptr;
  ASSERT_EQ(napi_create_object(s.env, &exports), napi_ok);
  init_test_null(s.env, exports);

  napi_value global = nullptr;
  ASSERT_EQ(napi_get_global(s.env, &global), napi_ok);
  ASSERT_EQ(napi_set_named_property(s.env, global, "__ton", exports), napi_ok);

  auto run_js = [&](const char* source_text) {
    v8::TryCatch tc(s.isolate);
    std::string wrapped = std::string("(() => { ") + source_text + " })();";
    v8::Local<v8::String> source =
        v8::String::NewFromUtf8(s.isolate, wrapped.c_str(), v8::NewStringType::kNormal)
            .ToLocalChecked();
    v8::Local<v8::Script> script;
    if (!v8::Script::Compile(s.context, source).ToLocal(&script)) return false;
    v8::Local<v8::Value> out;
    if (!script->Run(s.context).ToLocal(&out)) {
      if (tc.HasCaught()) {
        v8::String::Utf8Value msg(s.isolate, tc.Exception());
        ADD_FAILURE() << "JS exception: " << (*msg ? *msg : "<empty>")
                      << " while running: " << source_text;
      }
      return false;
    }
    return true;
  };

  ASSERT_TRUE(run_js(R"JS(
const eProp = { envIsNull:'Invalid argument', objectIsNull:'Invalid argument', keyIsNull:'Invalid argument', valueIsNull:'Invalid argument' };
const tn = __ton.testNull;
const eq = (a,b) => JSON.stringify(a) === JSON.stringify(b);
if (!eq(tn.setProperty(), eProp)) throw new Error('setProperty');
if (!eq(tn.getProperty(), eProp)) throw new Error('getProperty');
if (!eq(tn.hasProperty(), eProp)) throw new Error('hasProperty');
if (!eq(tn.hasOwnProperty(), eProp)) throw new Error('hasOwnProperty');
if (!eq(tn.deleteProperty(), { ...eProp, valueIsNull: 'napi_ok' })) throw new Error('deleteProperty');
if (!eq(tn.setNamedProperty(), eProp)) throw new Error('setNamedProperty');
if (!eq(tn.getNamedProperty(), eProp)) throw new Error('getNamedProperty');
if (!eq(tn.hasNamedProperty(), eProp)) throw new Error('hasNamedProperty');
const eElem = { envIsNull:'Invalid argument', objectIsNull:'Invalid argument', valueIsNull:'Invalid argument' };
if (!eq(tn.setElement(), eElem)) throw new Error('setElement');
if (!eq(tn.getElement(), eElem)) throw new Error('getElement');
if (!eq(tn.hasElement(), eElem)) throw new Error('hasElement');
if (!eq(tn.deleteElement(), { ...eElem, valueIsNull: 'napi_ok' })) throw new Error('deleteElement');
if (!eq(tn.defineProperties(), { envIsNull:'Invalid argument', objectIsNull:'Invalid argument', descriptorListIsNull:'Invalid argument', utf8nameIsNull:'Invalid argument', methodIsNull:'Invalid argument' })) throw new Error('defineProperties');
if (!eq(tn.getPropertyNames(), eElem)) throw new Error('getPropertyNames');
if (!eq(tn.getAllPropertyNames(), eElem)) throw new Error('getAllPropertyNames');
if (!eq(tn.getPrototype(), eElem)) throw new Error('getPrototype');
)JS"));
}
