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
18 changes: 11 additions & 7 deletions packages/deepctl-cmd-init/src/deepctl_cmd_init/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,16 +584,20 @@ def output_result(self, result: Any, config: Config) -> None:

def _handle_list(self, search: str | None) -> list[dict[str, Any]]:
"""List templates and return structured data."""
with console.status("[blue]Fetching templates...[/blue]"):
from deepctl_core.output import get_output_format

default_output = get_output_format() == "default"
progress_console = console if default_output else status_console
with progress_console.status("[blue]Fetching templates...[/blue]"):
response = templates_api.list_templates(search=search)

templates = response.items
if not templates:
console.print("[yellow]No templates found[/yellow]")
return []

self._print_templates_table(templates)
console.print(f"\n[dim]{len(templates)} template(s)[/dim]")
if default_output:
if templates:
self._print_templates_table(templates)
console.print(f"\n[dim]{len(templates)} template(s)[/dim]")
else:
console.print("[yellow]No templates found[/yellow]")
return [t.model_dump() for t in templates]

# ── Interactive picker ────────────────────────────────────────────────────
Expand Down
57 changes: 57 additions & 0 deletions packages/deepctl-cmd-init/tests/unit/test_init_list_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Tests for the output of dg init --list."""

import json
from unittest.mock import Mock, patch

import pytest
from deepctl_cmd_init.command import InitCommand
from deepctl_cmd_init.models import TemplateInfo, TemplateListResponse
from deepctl_core import AuthManager, Config, DeepgramClient, output


@pytest.mark.parametrize(
("format_type", "has_templates"),
[("json", True), ("json", False), ("default", True)],
)
@patch("deepctl_cmd_init.command.templates_api")
def test_list_output(
mock_api,
format_type,
has_templates,
capsys,
monkeypatch,
):
templates = (
[
TemplateInfo(
name="example",
title="Example",
description="Example template",
language="python",
)
]
if has_templates
else []
)
mock_api.list_templates.return_value = TemplateListResponse(
total=len(templates), items=templates
)
monkeypatch.setitem(output._output_config, "format", format_type)
monkeypatch.setitem(output._output_config, "quiet", False)

config = Mock(spec=Config)
init_command = InitCommand()
result = init_command.handle(
config=config,
auth_manager=Mock(spec=AuthManager),
client=Mock(spec=DeepgramClient),
list=True,
)
init_command.output_result(result, config)

stdout = capsys.readouterr().out
if format_type == "json":
assert json.loads(stdout) == [t.model_dump() for t in templates]
else:
assert "example" in stdout
assert "1 template(s)" in stdout