|
| 1 | +defmodule NodeJS.DebugModeTest do |
| 2 | + use ExUnit.Case |
| 3 | + import ExUnit.CaptureLog |
| 4 | + |
| 5 | + describe "debug_mode functionality" do |
| 6 | + test "logs Node.js stdout messages when debug_mode is enabled" do |
| 7 | + defmodule TestDebugHandler do |
| 8 | + use GenServer |
| 9 | + require Logger |
| 10 | + |
| 11 | + def start_link do |
| 12 | + GenServer.start_link(__MODULE__, nil) |
| 13 | + end |
| 14 | + |
| 15 | + def init(_) do |
| 16 | + {:ok, nil} |
| 17 | + end |
| 18 | + |
| 19 | + def debug_mode? do |
| 20 | + Application.get_env(:nodejs, :debug_mode, false) |
| 21 | + end |
| 22 | + |
| 23 | + # The implementation from worker.ex |
| 24 | + def handle_info({_pid, {:data, {_flag, msg}} = _data}, state) do |
| 25 | + if debug_mode?() do |
| 26 | + Logger.info("NodeJS: #{msg}") |
| 27 | + end |
| 28 | + |
| 29 | + {:noreply, state} |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Start the test process |
| 34 | + {:ok, pid} = TestDebugHandler.start_link() |
| 35 | + |
| 36 | + # Test with debug_mode disabled (default) |
| 37 | + log_without_debug = |
| 38 | + capture_log(fn -> |
| 39 | + # Simulate debugger message from Node.js |
| 40 | + send(pid, {self(), {:data, {:eol, "Debugger listening on ws://127.0.0.1:9229/abc123"}}}) |
| 41 | + # Wait for any potential logging |
| 42 | + Process.sleep(50) |
| 43 | + end) |
| 44 | + |
| 45 | + # Verify no logging occurred |
| 46 | + refute log_without_debug =~ "NodeJS: Debugger listening" |
| 47 | + |
| 48 | + # Enable debug_mode |
| 49 | + Application.put_env(:nodejs, :debug_mode, true) |
| 50 | + |
| 51 | + # Test with debug_mode enabled |
| 52 | + log_with_debug = |
| 53 | + capture_log(fn -> |
| 54 | + # Simulate debugger message from Node.js |
| 55 | + send(pid, {self(), {:data, {:eol, "Debugger listening on ws://127.0.0.1:9229/abc123"}}}) |
| 56 | + # Wait for any potential logging |
| 57 | + Process.sleep(50) |
| 58 | + end) |
| 59 | + |
| 60 | + # Clean up |
| 61 | + Application.delete_env(:nodejs, :debug_mode) |
| 62 | + |
| 63 | + # Verify logging occurred |
| 64 | + assert log_with_debug =~ "NodeJS: Debugger listening" |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + describe "port safety" do |
| 69 | + test "reset_terminal handles invalid ports gracefully" do |
| 70 | + defmodule TestPortHandler do |
| 71 | + use GenServer |
| 72 | + require Logger |
| 73 | + |
| 74 | + def start_link do |
| 75 | + GenServer.start_link(__MODULE__, nil) |
| 76 | + end |
| 77 | + |
| 78 | + def init(_) do |
| 79 | + {:ok, nil} |
| 80 | + end |
| 81 | + |
| 82 | + # The implementation from worker.ex |
| 83 | + def reset_terminal(port) do |
| 84 | + try do |
| 85 | + Port.command(port, "\x1b[0m\x1b[?7h\x1b[?25h\x1b[H\x1b[2J") |
| 86 | + Port.command(port, "\x1b[!p\x1b[?47l") |
| 87 | + rescue |
| 88 | + _ -> |
| 89 | + Logger.debug("NodeJS: Could not reset terminal - port may be closed") |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + log = |
| 95 | + capture_log(fn -> |
| 96 | + # Try to reset an invalid port |
| 97 | + TestPortHandler.reset_terminal(:invalid_port) |
| 98 | + Process.sleep(50) |
| 99 | + end) |
| 100 | + |
| 101 | + # Verify proper error handling |
| 102 | + assert log =~ "NodeJS: Could not reset terminal" |
| 103 | + end |
| 104 | + end |
| 105 | +end |
0 commit comments