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_STEPOVER_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsStepoverTest : public TestActions {
23public:
24    JsStepoverTest()
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 common_func.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35            // break on start
36            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37            // set first breakpoint
38            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 22"},
39            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40
41            // hit breakpoint after resume first time
42            {SocketAction::SEND, "resume"},
43            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
44            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
45            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
46                [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv); }},
47
48            {SocketAction::SEND, "sov"},
49            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
50            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
51            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
52                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, 22); }},
53
54            {SocketAction::SEND, "sov"},
55            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
56            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
57            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
58                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, 23); }},
59
60            {SocketAction::SEND, "resume"},
61            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
62            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
63            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
64
65            // reply success and run
66            {SocketAction::SEND, "success"},
67            {SocketAction::SEND, "resume"},
68            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
69            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
70        };
71    }
72
73    bool RecvBreakInfo(std::string recv)
74    {
75        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
76        Result ret;
77        std::string method = "";
78        ret = json->GetString("method", &method);
79        if (ret != Result::SUCCESS || method != "Debugger.paused") {
80            return false;
81        }
82
83        std::unique_ptr<PtJson> params = nullptr;
84        ret = json->GetObject("params", &params);
85        if (ret != Result::SUCCESS) {
86            return false;
87        }
88
89        std::unique_ptr<PtJson> hitBreakpoints = nullptr;
90        ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
91        if (ret != Result::SUCCESS) {
92            return false;
93        }
94
95        std::string breakpoint = "";
96        breakpoint = hitBreakpoints->Get(0)->GetString();
97        if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
98            breakpoint.find("21") == std::string::npos) {
99            return false;
100        }
101
102        DebuggerClient debuggerClient(0);
103        debuggerClient.PausedReply(std::move(json));
104        return true;
105    }
106
107    bool RecvStepoverInfo(std::string recv, int lineNumber)
108    {
109        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
110        Result ret;
111        std::string method = "";
112        ret = json->GetString("method", &method);
113        if (ret != Result::SUCCESS || method != "Debugger.paused") {
114            return false;
115        }
116
117        std::unique_ptr<PtJson> params = nullptr;
118        ret = json->GetObject("params", &params);
119        if (ret != Result::SUCCESS) {
120            return false;
121        }
122
123        std::unique_ptr<PtJson> callFrames = nullptr;
124        ret = params->GetArray("callFrames", &callFrames);
125        if (ret != Result::SUCCESS) {
126            return false;
127        }
128
129        std::string functionName = "";
130        ret = callFrames->Get(0)->GetString("functionName", &functionName);
131        if (ret != Result::SUCCESS || functionName != "func_main_0") {
132            return false;
133        }
134
135        std::unique_ptr<PtJson> location = nullptr;
136        ret = callFrames->Get(0)->GetObject("location", &location);
137        if (ret != Result::SUCCESS) {
138            return false;
139        }
140
141        int lineNum = 0;
142        ret = location->GetInt("lineNumber", &lineNum);
143        if (ret != Result::SUCCESS || lineNum != lineNumber) {
144            return false;
145        }
146
147        DebuggerClient debuggerClient(0);
148        debuggerClient.PausedReply(std::move(json));
149        return true;
150    }
151
152    std::pair<std::string, std::string> GetEntryPoint() override
153    {
154        return {pandaFile_, entryPoint_};
155    }
156    ~JsStepoverTest() = default;
157
158private:
159    std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
160    std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
161    std::string entryPoint_ = "_GLOBAL::func_main_0";
162};
163
164std::unique_ptr<TestActions> GetJsStepoverTest()
165{
166    return std::make_unique<JsStepoverTest>();
167}
168}  // namespace panda::ecmascript::tooling::test
169
170#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_TEST_H
171