Library add through the catalog lookup, no atoms from the page

b513f30 · Gabe Morell · 2026-11-04 21:24

9 files +35 -9

Files changed

modified lib/lambent_ex_web/live/machines/index.ex
+11 −9
@@ -131,16 +131,18 @@ defmodule LambentExWeb.MachinesLive.Index do
131 131 {:noreply, push_patch(socket, to: ~p"/cfg/machines/new?cls=#{cls}&entry=#{entry}")}
132 132 end
133 133
134 + # the class and entry come from the page, so anything not in the catalog is
135 + # dropped rather than made into atoms
134 136 def handle_event("library-add", %{"cls" => cls, "entry" => entry}, socket) do
135 − spec =
136 − LambentEx.Machines.Library.machines()
137 − |> get_in([String.to_atom(cls), String.to_atom(entry)])
138 −
139 − opts = spec[:mach_opts] || []
140 −
141 − LambentEx.MachineSupervisor.start_child(spec[:machine], spec[:args], entry, opts)
142 −
143 − {:noreply, socket}
137 + case LambentEx.Machines.Library.entry(cls, entry) do
138 + nil ->
139 + {:noreply, socket}
140 +
141 + spec ->
142 + opts = spec[:mach_opts] || []
143 + LambentEx.MachineSupervisor.start_child(spec[:machine], spec[:args], entry, opts)
144 + {:noreply, socket}
145 + end
144 146 end
145 147
146 148 def handle_info(:update_library, socket) do
modified test/lambent_ex_web/live/machines_test.exs
+24 −0
@@ -93,4 +93,28 @@ defmodule LambentExWeb.MachinesTest do
93 93 render_click(live, "faster", %{"id" => "machines-test-speed"})
94 94 assert Machine.info("machines-test-speed").speed == 500
95 95 end
96 +
97 + test "a catalog entry is started from the library", %{conn: conn} do
98 + on_exit(fn -> Machine.quit("red") end)
99 + {:ok, live, _html} = live(conn, "/cfg/machines")
100 +
101 + render_click(live, "library-add", %{"cls" => "solid", "entry" => "red"})
102 +
103 + assert Machine.running?("red")
104 + end
105 +
106 + test "an entry not in the catalog is ignored, and makes no atoms", %{conn: conn} do
107 + {:ok, live, _html} = live(conn, "/cfg/machines")
108 + cls = "no-such-class-#{System.unique_integer([:positive])}"
109 + entry = "no-such-entry-#{System.unique_integer([:positive])}"
110 +
111 + render_click(live, "library-add", %{"cls" => cls, "entry" => entry})
112 + render_click(live, "library-add", %{"cls" => "solid", "entry" => entry})
113 +
114 + # a crash in the handler would have taken the page, and this test, down
115 + assert render(live)
116 + refute Machine.running?(entry)
117 + assert_raise ArgumentError, fn -> String.to_existing_atom(cls) end
118 + assert_raise ArgumentError, fn -> String.to_existing_atom(entry) end
119 + end
96 120 end

Parents: 69c87f3