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        File.enable_testing_mode!
18        @react_native_path = "../.."
19        podSpy_cleanUp()
20
21    end
22
23    def teardown
24        ENV['HERMES_ENGINE_TARBALL_PATH'] = nil
25        Open3.reset()
26        Pod::Config.reset()
27        Pod::UI.reset()
28        podSpy_cleanUp()
29        ENV['USE_HERMES'] = '1'
30        ENV['REACT_NATIVE_CI'] = nil
31        File.reset()
32    end
33
34    # =============== #
35    # TEST - setupJsc #
36    # =============== #
37    def test_setupJsc_installsPods
38        # Arrange
39        fabric_enabled = false
40
41        # Act
42        setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
43
44        # Assert
45        assert_equal($podInvocationCount, 2)
46        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
47        assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsc")
48    end
49
50    def test_setupJsc_installsPods_installsFabricSubspecWhenFabricEnabled
51        # Arrange
52        fabric_enabled = true
53
54        # Act
55        setup_jsc!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
56
57        # Assert
58        assert_equal($podInvocationCount, 3)
59        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
60        assert_equal($podInvocation["React-jsc"][:path], "../../ReactCommon/jsc")
61        assert_equal($podInvocation["React-jsc/Fabric"][:path], "../../ReactCommon/jsc")
62    end
63
64    # ================== #
65    # TEST - setupHermes #
66    # ================== #
67    def test_setupHermes_whenHermesScriptFails_abort
68        # Arrange
69        fabric_enabled = false
70        Pod::Config.instance.installation_root.set_installation_root("Pods/")
71        Open3.set_returned_status(1)
72        Open3.set_returned_text("This test\nshould fail")
73
74        # Act
75        assert_raises {
76            setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
77        }
78
79        # Assert
80        assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"])
81        assert_equal(Open3.collected_dirs, ["Pods/../.."])
82        assert_equal(Pod::UI.collected_infoes, ["This test", "should fail"])
83        assert_equal($podInvocationCount, 0)
84        assert_equal($podInvocation, {})
85    end
86
87    def test_setupHermes_whenHermesScriptSucceeds_installsPods
88        # Arrange
89        fabric_enabled = false
90        Pod::Config.instance.installation_root.set_installation_root("Pods/")
91        Open3.set_returned_status(0)
92        Open3.set_returned_text("This is\nthe text\nreturned by\nprepare-hermes-for-build")
93
94        # Act
95        setup_hermes!(:react_native_path => @react_native_path, :fabric_enabled => fabric_enabled)
96
97        # Assert
98        assert_equal(Open3.collected_commands, ["node scripts/hermes/prepare-hermes-for-build"])
99        assert_equal(Open3.collected_dirs, ["Pods/../.."])
100        assert_equal(Pod::UI.collected_infoes, [
101            "This is",
102            "the text",
103            "returned by",
104            "prepare-hermes-for-build",
105        ])
106        assert_equal($podInvocationCount, 4)
107        assert_equal($podInvocation["React-jsi"][:path], "../../ReactCommon/jsi")
108        assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes")
109        assert_equal($podInvocation["libevent"][:version], "~> 2.1.12")
110        assert_equal($podInvocation["hermes-engine"][:podspec], "../../sdks/hermes-engine/hermes-engine.podspec")
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        assert_equal($podInvocation["hermes-engine"][:podspec], "../../sdks/hermes-engine/hermes-engine.podspec")
124        assert_equal($podInvocation["React-hermes"][:path], "../../ReactCommon/hermes")
125        assert_equal($podInvocation["libevent"][:version], "~> 2.1.12")
126    end
127
128    # ================================= #
129    # TEST - isBuildingHermesFromSource #
130    # ================================= #
131    def test_isBuildingHermesFromSource_whenTarballIsNilAndVersionIsNotNightly_returnTrue
132        assert_true(is_building_hermes_from_source("1000.0.0", '../..'))
133    end
134
135    def test_isBuildingHermesFromSource_whenTarballIsNilAndInReleaseBranch_returnTrue
136        ENV['REACT_NATIVE_CI'] = 'true'
137        File.mocked_existing_files(['../../sdks/.hermesversion'])
138        assert_true(is_building_hermes_from_source("0.999.0", '../..'))
139    end
140
141    def test_isBuildingHermesFromSource_whenTarballIsNotNil_returnFalse
142        ENV['HERMES_ENGINE_TARBALL_PATH'] = "~/Downloads/hermes-ios-debug.tar.gz"
143        assert_false(is_building_hermes_from_source("1000.0.0", '../..'))
144    end
145
146    def test_isBuildingHermesFromSource_whenIsNigthly_returnsFalse
147        assert_false(is_building_hermes_from_source("0.0.0-", '../..'))
148    end
149
150    def test_isBuildingHermesFromSource_whenIsStbleRelease_returnsFalse
151        assert_false(is_building_hermes_from_source("0.71.0", '../..'))
152    end
153
154end
155