diff --git a/cmd/api/cmd_spec.go b/cmd/api/cmd_spec.go index 7ad145e..49f6477 100644 --- a/cmd/api/cmd_spec.go +++ b/cmd/api/cmd_spec.go @@ -13,10 +13,11 @@ import ( func addSpecCommand(parent *cli.Command) { var ( - output string - format string - title string - version string + output string + format string + title string + description string + version string ) cmd := cli.NewCommand("spec", "Generate OpenAPI specification", "", func(cmd *cli.Command, args []string) error { @@ -24,7 +25,7 @@ func addSpecCommand(parent *cli.Command) { // Additional groups can be added here as the platform grows. builder := &goapi.SpecBuilder{ Title: title, - Description: "Lethean Core API", + Description: description, Version: version, } @@ -48,6 +49,7 @@ func addSpecCommand(parent *cli.Command) { cli.StringFlag(cmd, &output, "output", "o", "", "Write spec to file instead of stdout") cli.StringFlag(cmd, &format, "format", "f", "json", "Output format: json or yaml") cli.StringFlag(cmd, &title, "title", "t", "Lethean Core API", "API title in spec") + cli.StringFlag(cmd, &description, "description", "d", "Lethean Core API", "API description in spec") cli.StringFlag(cmd, &version, "version", "V", "1.0.0", "API version in spec") parent.AddCommand(cmd) diff --git a/cmd/api/cmd_test.go b/cmd/api/cmd_test.go index b24c723..4d9130e 100644 --- a/cmd/api/cmd_test.go +++ b/cmd/api/cmd_test.go @@ -4,6 +4,8 @@ package api import ( "bytes" + "encoding/json" + "os" "testing" "forge.lthn.ai/core/cli/pkg/cli" @@ -51,11 +53,44 @@ func TestAPISpecCmd_Good_JSON(t *testing.T) { if specCmd.Flag("title") == nil { t.Fatal("expected --title flag on spec command") } + if specCmd.Flag("description") == nil { + t.Fatal("expected --description flag on spec command") + } if specCmd.Flag("version") == nil { t.Fatal("expected --version flag on spec command") } } +func TestAPISpecCmd_Good_CustomDescription(t *testing.T) { + root := &cli.Command{Use: "root"} + AddAPICommands(root) + + outputFile := t.TempDir() + "/spec.json" + root.SetArgs([]string{"api", "spec", "--description", "Custom API description", "--output", outputFile}) + root.SetErr(new(bytes.Buffer)) + + if err := root.Execute(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + var spec map[string]any + data, err := os.ReadFile(outputFile) + if err != nil { + t.Fatalf("expected spec file to be written: %v", err) + } + if err := json.Unmarshal(data, &spec); err != nil { + t.Fatalf("expected valid JSON spec, got error: %v", err) + } + + info, ok := spec["info"].(map[string]any) + if !ok { + t.Fatal("expected info object in generated spec") + } + if info["description"] != "Custom API description" { + t.Fatalf("expected custom description, got %v", info["description"]) + } +} + func TestAPISDKCmd_Bad_NoLang(t *testing.T) { root := &cli.Command{Use: "root"} AddAPICommands(root)