1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5
6require "test/unit"
7require_relative "../jsengine.rb"
8require_relative "./test_utils/podSpy.rb"
9require_relative "./test_utils/PodMock.rb"
10require_relative "./test_utils/Open3Mock.rb"
11
12class JSEngineTests < Test::Unit::TestCase
13
14    :react_native_path
15
16    def setup
17        @react_native_path = "../.."
18        podSpy_cleanUp()
19
20    end
21
22    def teardown
23        ENV['HERMES_ENGINE_TARBALL_PATH'] = nil
24        Open3.reset()
25        Pod::Config.reset()
26        Pod::UI.reset()
27        podSpy_cleanUp()
28        ENV['USE_HERMES'] = '1'
29        ENV['CI'] = nil
30    end
31
32    # =============== #
33    # TEST - setupJsc #
34    # =============== #
35    def test_setupJsc_installsPods
36        # Arrange
37        fabric_enabled = false
38
39        # Act
40        setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
41
42        # Assert
43        assert_equal($podInvocationCount, 2)
44        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
45        assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsc")
46    end
47
48    def test_setupJsc_installsPods_installsFabricSubspecWhenFabricEnabled
49        # Arrange
50        fabric_enabled = true
51
52        # Act
53        setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
54
55        # Assert
56        assert_equal($podInvocationCount, 3)
57        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
58        assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsc")
59        assert_equal($podInvocation["React-jsc/Fabric"][:path], "../../ReactCommon/jsc")
60    end
61
62    # ================== #
63    # TEST - setupHermes #
64    # ================== #
65    def test_setupHermes_whenHermesScriptFails_abort
66        # Arrange
67        fabric_enabled = false
68        Pod::Config.instance.installation_root.set_installation_root("Pods/")
69        Open3.set_returned_status(1)
70        Open3.set_returned_text("This test\nshould fail")
71
72        # Act
73        assert_raises {
74            setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
75        }
76
77        # Assert
78        assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"])
79        assert_equal(Open3.collected_dirs, ["Pods/../.."])
80        assert_equal(Pod::UI.collected_infoes, ["This test", "should fail"])
81        assert_equal($podInvocationCount, 0)
82        assert_equal($podInvocation, {})
83    end
84
85    def test_setupHermes_whenHermesScriptSucceeds_installsPods
86        # Arrange
87        fabric_enabled = false
88        Pod::Config.instance.installation_root.set_installation_root("Pods/")
89        Open3.set_returned_status(0)
90        Open3.set_returned_text("This is\nthe text\nreturned by\nprepare-hermes-for-build")
91
92        # Act
93        setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
94
95        # Assert
96        assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"])
97        assert_equal(Open3.collected_dirs, ["Pods/../.."])
98        assert_equal(Pod::UI.collected_infoes, [
99            "This is",
100            "the text",
101            "returned by",
102            "prepare-hermes-for-build",
103        ])
104        assert_equal($podInvocationCount, 4)
105        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
106        assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes")
107        assert_equal($podInvocation["libevent"][:version], "~> 2.1.12")
108        hermes_engine_pod_invocation = $podInvocation["hermes-engine"]
109        assert_equal(hermes_engine_pod_invocation[:podspec], "../../sdks/hermes-engine/hermes-engine.podspec")
110        assert_equal(hermes_engine_pod_invocation[:tag], "")
111    end
112
113    def test_setupHermes_installsPods_installsFabricSubspecWhenFabricEnabled
114        # Arrange
115        fabric_enabled = true
116
117        # Act
118        setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
119
120        # Assert
121        assert_equal($podInvocationCount, 4)
122        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
123        hermes_engine_pod_invocation = $podInvocation["hermes-engine"]
124        assert_equal(hermes_engine_pod_invocation[:podspec], "../../sdks/hermes-engine/hermes-engine.podspec")
125        assert_equal(hermes_engine_pod_invocation[:tag], "")
126        assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes")
127        assert_equal($podInvocation["libevent"][:version], "~> 2.1.12")
128    end
129
130end
131