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