Skip to content

Commit a2ce532

Browse files
authored
Task: Increase test coverage of LiveDebugger.Structs modules (#342)
* add tests for LiveDebugger.Structs.TreeNode * add tests for LiveDebugger.Structs.LvProcess * add tests for LiveDebugger.Structs.TraceDisplay * add tests for LiveDebugger.Structs.Trace
1 parent cba93d1 commit a2ce532

6 files changed

Lines changed: 662 additions & 63 deletions

File tree

lib/live_debugger/structs/tree_node.ex

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ defmodule LiveDebugger.Structs.TreeNode do
4848
end
4949
end
5050

51-
@doc """
52-
Same as `id_from_string/1`, but raises an ArgumentError if the ID is invalid.
53-
"""
54-
@spec id_from_string!(id :: String.t()) :: id()
55-
def id_from_string!(string) do
56-
case id_from_string(string) do
57-
{:ok, id} -> id
58-
:error -> raise ArgumentError, "Invalid ID: #{inspect(string)}"
59-
end
60-
end
61-
6251
@doc """
6352
Adds a child to the parent node.
6453
"""

test/structs/lv_process_test.exs

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
defmodule LiveDebugger.Structs.LvProcessTest do
2+
use ExUnit.Case, async: true
3+
4+
import Mox
5+
6+
alias LiveDebugger.Structs.LvProcess
7+
8+
describe "new/2" do
9+
test "creates a new LvProcess struct with the given pid and socket" do
10+
pid = self()
11+
12+
socket = %Phoenix.LiveView.Socket{
13+
id: "socket_id",
14+
root_pid: pid,
15+
parent_pid: nil,
16+
transport_pid: nil,
17+
view: LiveDebuggerTest.TestView,
18+
host_uri: :not_mounted_at_router
19+
}
20+
21+
lv_process = LvProcess.new(pid, socket)
22+
23+
assert %LvProcess{
24+
socket_id: "socket_id",
25+
root_pid: ^pid,
26+
parent_pid: nil,
27+
pid: ^pid,
28+
transport_pid: nil,
29+
module: LiveDebuggerTest.TestView,
30+
nested?: false,
31+
debugger?: false,
32+
embedded?: true
33+
} = lv_process
34+
end
35+
end
36+
37+
describe "new/1" do
38+
test "returns nil if the process is not found" do
39+
LiveDebugger.MockProcessService
40+
|> expect(:state, fn _pid ->
41+
{:error, :not_alive}
42+
end)
43+
44+
assert LvProcess.new(self()) == nil
45+
end
46+
47+
test "returns a new LvProcess struct if the process is found" do
48+
socket_id = "socket_id"
49+
pid = self()
50+
root_pid = pid
51+
parent_pid = nil
52+
transport_pid = nil
53+
module = LiveDebuggerTest.TestView
54+
55+
LiveDebugger.MockProcessService
56+
|> expect(:state, fn _pid ->
57+
{:ok,
58+
LiveDebugger.Fakes.state(
59+
socket_id: socket_id,
60+
root_pid: root_pid,
61+
parent_pid: parent_pid,
62+
transport_pid: transport_pid,
63+
module: module
64+
)}
65+
end)
66+
67+
assert %LvProcess{
68+
socket_id: ^socket_id,
69+
root_pid: ^root_pid,
70+
parent_pid: ^parent_pid,
71+
pid: ^pid,
72+
transport_pid: ^transport_pid,
73+
module: ^module,
74+
nested?: false,
75+
debugger?: false,
76+
embedded?: false
77+
} = LvProcess.new(pid)
78+
end
79+
80+
test "sets embedded? when LiveView process is Embedded Live View" do
81+
socket_id = "socket_id"
82+
pid = self()
83+
root_pid = pid
84+
parent_pid = nil
85+
transport_pid = :c.pid(0, 7, 0)
86+
module = LiveDebuggerTest.TestView
87+
88+
LiveDebugger.MockProcessService
89+
|> expect(:state, fn _pid ->
90+
{:ok,
91+
LiveDebugger.Fakes.state(
92+
socket_id: socket_id,
93+
root_pid: root_pid,
94+
parent_pid: parent_pid,
95+
transport_pid: transport_pid,
96+
module: module,
97+
host_uri: :not_mounted_at_router
98+
)}
99+
end)
100+
101+
assert %LvProcess{
102+
socket_id: ^socket_id,
103+
root_pid: ^root_pid,
104+
parent_pid: ^parent_pid,
105+
pid: ^pid,
106+
transport_pid: ^transport_pid,
107+
module: ^module,
108+
nested?: false,
109+
debugger?: false,
110+
embedded?: true
111+
} = LvProcess.new(pid)
112+
end
113+
114+
test "sets debugger? when LiveView process is LiveDebugger process" do
115+
socket_id = "socket_id"
116+
pid = self()
117+
root_pid = pid
118+
parent_pid = nil
119+
transport_pid = nil
120+
module = LiveDebugger.TestView
121+
122+
LiveDebugger.MockProcessService
123+
|> expect(:state, fn _pid ->
124+
{:ok,
125+
LiveDebugger.Fakes.state(
126+
socket_id: socket_id,
127+
root_pid: root_pid,
128+
parent_pid: parent_pid,
129+
transport_pid: transport_pid,
130+
module: module
131+
)}
132+
end)
133+
134+
assert %LvProcess{
135+
socket_id: ^socket_id,
136+
root_pid: ^root_pid,
137+
parent_pid: ^parent_pid,
138+
pid: ^pid,
139+
transport_pid: ^transport_pid,
140+
module: ^module,
141+
nested?: false,
142+
debugger?: true,
143+
embedded?: false
144+
} = LvProcess.new(pid)
145+
end
146+
147+
test "sets nested? when LiveView process is a Nested Live View" do
148+
socket_id = "socket_id"
149+
pid = :c.pid(0, 0, 1)
150+
parent_pid = :c.pid(0, 0, 0)
151+
root_pid = parent_pid
152+
transport_pid = nil
153+
module = LiveDebuggerTest.TestView
154+
155+
LiveDebugger.MockProcessService
156+
|> expect(:state, fn _pid ->
157+
{:ok,
158+
LiveDebugger.Fakes.state(
159+
socket_id: socket_id,
160+
root_pid: root_pid,
161+
parent_pid: parent_pid,
162+
transport_pid: transport_pid,
163+
module: module
164+
)}
165+
end)
166+
167+
assert %LvProcess{
168+
socket_id: ^socket_id,
169+
root_pid: ^root_pid,
170+
parent_pid: ^parent_pid,
171+
pid: ^pid,
172+
transport_pid: ^transport_pid,
173+
module: ^module,
174+
nested?: true,
175+
debugger?: false,
176+
embedded?: false
177+
} = LvProcess.new(pid)
178+
end
179+
end
180+
181+
describe "parent/1" do
182+
test "returns the parent process of the given LvProcess" do
183+
socket_id = "socket_id"
184+
pid = :c.pid(0, 0, 1)
185+
parent_pid = :c.pid(0, 0, 0)
186+
root_pid = parent_pid
187+
transport_pid = nil
188+
module = LiveDebuggerTest.TestView
189+
190+
LiveDebugger.MockProcessService
191+
|> expect(:state, fn ^parent_pid ->
192+
{:ok,
193+
LiveDebugger.Fakes.state(
194+
socket_id: socket_id,
195+
root_pid: root_pid,
196+
parent_pid: nil,
197+
transport_pid: transport_pid,
198+
module: module
199+
)}
200+
end)
201+
202+
lv_process = %LvProcess{
203+
socket_id: socket_id,
204+
root_pid: root_pid,
205+
parent_pid: parent_pid,
206+
pid: pid,
207+
transport_pid: transport_pid,
208+
module: module,
209+
nested?: true,
210+
debugger?: false,
211+
embedded?: false
212+
}
213+
214+
assert %LvProcess{
215+
pid: ^parent_pid
216+
} = LvProcess.parent(lv_process)
217+
end
218+
219+
test "returns nil if no parent" do
220+
lv_process = %LvProcess{
221+
socket_id: "socket_id",
222+
root_pid: self(),
223+
parent_pid: nil,
224+
pid: self(),
225+
transport_pid: nil,
226+
module: LiveDebuggerTest.TestView,
227+
nested?: false,
228+
debugger?: false,
229+
embedded?: false
230+
}
231+
232+
assert LvProcess.parent(lv_process) == nil
233+
end
234+
end
235+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defmodule LiveDebugger.Structs.TraceDisplayTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LiveDebugger.Structs.TraceDisplay
5+
alias LiveDebugger.Structs.Trace
6+
7+
@trace %Trace{
8+
id: 1,
9+
module: LiveDebuggerTest.TestView,
10+
function: :handle_event,
11+
arity: 3,
12+
args: ["event", %{"key" => "value"}, %{}],
13+
socket_id: "socket_id",
14+
transport_pid: self(),
15+
pid: self(),
16+
cid: nil,
17+
timestamp: System.system_time(:millisecond)
18+
}
19+
20+
test "from_trace/1 creates a TraceDisplay struct" do
21+
trace = @trace
22+
trace_display = TraceDisplay.from_trace(trace)
23+
24+
assert %TraceDisplay{id: 1, trace: ^trace, render_body?: false} = trace_display
25+
end
26+
27+
test "render_body/1 sets render_body? to true" do
28+
trace_display = %TraceDisplay{
29+
id: 1,
30+
trace: @trace,
31+
render_body?: false,
32+
counter: 0
33+
}
34+
35+
updated_trace_display = TraceDisplay.render_body(trace_display)
36+
37+
assert %TraceDisplay{render_body?: true} = updated_trace_display
38+
end
39+
end

0 commit comments

Comments
 (0)