Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/lib/ethui/stacks/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ defmodule Ethui.Stacks.Server do
GenServer.call(__MODULE__, :list)
end

@doc "Check if a stack with the given slug is running"
@spec is_slug_running?(slug) :: boolean
def is_slug_running?(slug) do
GenServer.call(__MODULE__, {:is_slug_running, slug})
end
Comment on lines +38 to +42
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new is_slug_running? function lacks test coverage. Based on existing tests in server_test.exs, tests should verify:

  1. Returns false when no stack with the given slug is running
  2. Returns true when a stack with the given slug is running
  3. Returns false after a stack is stopped

This would ensure the function correctly queries the instances map state.

Copilot uses AI. Check for mistakes.

def start(%Stack{} = stack) do
GenServer.cast(__MODULE__, {:start, stack})
end
Expand Down Expand Up @@ -92,6 +98,11 @@ defmodule Ethui.Stacks.Server do
{:reply, Map.keys(instances), state}
end

@impl GenServer
def handle_call({:is_slug_running, slug}, _from, %{instances: instances} = state) do
{:reply, Map.has_key?(instances, slug), state}
end

@impl GenServer
def handle_cast({:start, stack}, state) do
case start_stack(stack, state) do
Expand Down
22 changes: 22 additions & 0 deletions server/lib/ethui_web/controllers/api/stack_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ defmodule EthuiWeb.Api.StackController do
})
end

def show(conn, %{"slug" => slug}) do
user = conn.assigns[:current_user]

with %Stack{} = stack <- Repo.get_by(Stack, slug: slug),
:ok <- authorize_user_access(user, stack) do
json(conn, %{
status: "success",
data: %{
slug: stack.slug,
urls: Stacks.get_urls(stack),
status: if(Server.is_slug_running?(slug), do: "running", else: "stopped")
}
})
else
nil ->
conn |> put_status(404) |> json(%{status: "error", error: "not found"})

{:error, :unauthorized} ->
conn |> put_status(403) |> json(%{status: "error", error: "unauthorized"})
end
end
Comment on lines +35 to +55
Copy link

Copilot AI Dec 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new show endpoint lacks test coverage. Based on existing tests in stack_controller_test.exs, the following scenarios should be tested:

  1. Successfully retrieving a stack owned by the authenticated user (returns 200 with status, slug, urls)
  2. Attempting to retrieve a stack owned by another user (returns 403 unauthorized)
  3. Attempting to retrieve a non-existent stack (returns 404 not found)
  4. Correctly reporting running vs stopped status based on Server state

These tests would ensure the authentication/authorization logic and response formatting work correctly.

Copilot uses AI. Check for mistakes.

def create(conn, params) do
user = conn.assigns[:current_user]

Expand Down
13 changes: 13 additions & 0 deletions server/test/ethui/stacks/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ defmodule Ethui.Stacks.ServerTest do
Server.stop(stack)
assert Server.list() |> length == 0
end

describe "is_running?/1" do
test "returns true if the stack is running" do
stack = %Stack{id: 1, slug: "test_stack"}
Server.start(stack)
assert Server.is_running?(stack)
end

test "returns false if the stack is not running" do
stack = %Stack{id: 1, slug: "test_stack"}
assert not Server.is_running?(stack)
end
end
end
12 changes: 12 additions & 0 deletions server/test/ethui_web/controllers/api/stack_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,16 @@ defmodule EthuiWeb.Api.StackControllerTest do
assert "public-stack2" in stack_slugs
end
end

describe "stacks/:slug" do
test "returns a stack", %{conn: conn} do
stack = %Stack{id: 1, slug: "test-stack"}
Server.start(stack)

conn = conn |> get(~p"/stacks/#{stack.slug}")
assert conn.status == 200
response_data = json_response(conn, 200)["data"]
assert response_data["slug"] == stack.slug
end
end
end
Loading