1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_VARIABLE_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_VARIABLE_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsWatchVariableTest : public TestActions {
23public:
24    JsWatchVariableTest()
25    {
26        testAction = {
27            {SocketAction::SEND, "enable"},
28            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29            {SocketAction::SEND, "runtime-enable"},
30            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31            {SocketAction::SEND, "run"},
32            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33            // load step.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35            // break on start
36            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
37                [](auto recv, auto, auto) -> bool {
38                    std::unique_ptr<PtJson> json = PtJson::Parse(recv);
39                    DebuggerClient debuggerClient(0);
40                    debuggerClient.RecvReply(std::move(json));
41                    return true;
42                }},
43            // set breakpoint
44            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 26"},
45            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 31"},
47            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
48            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 38"},
49            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
50            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 52"},
51            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
52            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 62"},
53            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
54            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 64"},
55            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
56            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 77"},
57            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
58            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 87"},
59            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
60            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 98"},
61            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
62            // hit breakpoint after resume first time
63            {SocketAction::SEND, "resume"},
64            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
65            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
67            {SocketAction::SEND, "watch b"}, // b = 3
68            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
69                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "3"); }},
70            // hit breakpoint after resume second time
71            {SocketAction::SEND, "resume"},
72            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
73            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
74            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
75            {SocketAction::SEND, "watch a"}, // a = 2
76            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
77                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "2"); }},
78
79            {SocketAction::SEND, "resume"},
80            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
81            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
82            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
83            {SocketAction::SEND, "watch c"}, // c = 2
84            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
85                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "2"); }},
86
87            {SocketAction::SEND, "resume"},
88            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
89            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
90            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
91            {SocketAction::SEND, "watch a"}, // a = 6
92            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
93                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "6"); }},
94
95            {SocketAction::SEND, "resume"},
96            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
97            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
98            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
99            {SocketAction::SEND, "watch c"}, // c = 1
100            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
101                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "1"); }},
102
103            {SocketAction::SEND, "resume"},
104            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
105            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
106            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
107            {SocketAction::SEND, "watch d"}, // d = 1
108            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
109                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "1"); }},
110
111            {SocketAction::SEND, "resume"},
112            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
113            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
114            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
115            {SocketAction::SEND, "watch a"}, // a = 23
116            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
117                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "23"); }},
118
119            {SocketAction::SEND, "resume"},
120            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
121            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
122            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
123            {SocketAction::SEND, "watch a"}, // a = 100
124            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
125                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "100"); }},
126
127            {SocketAction::SEND, "resume"},
128            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
129            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
130            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
131            {SocketAction::SEND, "watch b"}, // b = 13
132            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
133                [this](auto recv, auto, auto) -> bool { return RecvWatchVariableInfo(recv, "13"); }},
134
135            // reply success and run
136            {SocketAction::SEND, "success"},
137            {SocketAction::SEND, "resume"},
138            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
139            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
140        };
141    }
142
143    bool RecvWatchVariableInfo(std::string recv, std::string var_value)
144    {
145        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
146        Result ret;
147        int id = 0;
148        ret = json->GetInt("id", &id);
149        if (ret != Result::SUCCESS) {
150            return false;
151        }
152
153        std::unique_ptr<PtJson> result = nullptr;
154        ret = json->GetObject("result", &result);
155        if (ret != Result::SUCCESS) {
156            return false;
157        }
158
159        std::unique_ptr<PtJson> watchResult = nullptr;
160        ret = result->GetObject("result", &watchResult);
161        if (ret != Result::SUCCESS) {
162            return false;
163        }
164
165        std::string type = "";
166        ret = watchResult->GetString("type", &type);
167        if (ret != Result::SUCCESS || type != "number") {
168            return false;
169        }
170
171        std::string value = "";
172        ret = watchResult->GetString("unserializableValue", &value);
173        if (ret != Result::SUCCESS || value != var_value) {
174            return false;
175        }
176        return true;
177    }
178
179    std::pair<std::string, std::string> GetEntryPoint() override
180    {
181        return {pandaFile_, entryPoint_};
182    }
183    ~JsWatchVariableTest() = default;
184
185private:
186    std::string pandaFile_ = DEBUGGER_ABC_DIR "step.abc";
187    std::string sourceFile_ = DEBUGGER_JS_DIR "step.js";
188    std::string entryPoint_ = "_GLOBAL::func_main_0";
189};
190
191std::unique_ptr<TestActions> GetJsWatchVariableTest()
192{
193    return std::make_unique<JsWatchVariableTest>();
194}
195}  // namespace panda::ecmascript::tooling::test
196
197#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_WATCH_VARIABLE_TEST_H
198