Skip to content
Closed
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ smithery auth token --policy '<json>' # Mint a restricted token

### Namespaces

Switch between personal and team namespaces without re-authenticating. Your login grants access to all namespaces (personal + team memberships), and you can switch between them instantly.

```bash
smithery namespace list # List your namespaces
smithery namespace use <name> # Set current namespace
smithery namespace list # List available namespaces (shows current)
smithery namespace use <name> # Switch to a different namespace
smithery namespace show # Show current namespace
smithery namespace create <name> # Create and claim a new namespace
```

The active namespace persists in your local config (`~/.config/smithery/settings.json`) and applies to all subsequent commands until you switch again.

### Publishing

```bash
Expand Down
157 changes: 157 additions & 0 deletions src/commands/namespace/__tests__/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { beforeEach, describe, expect, test, vi } from "vitest"

// Mock dependencies
vi.mock("../../../lib/smithery-client", () => ({
createSmitheryClient: vi.fn(),
}))

vi.mock("../../../utils/smithery-settings", () => ({
getNamespace: vi.fn(),
}))

vi.mock("../../../utils/output", () => ({
isJsonMode: vi.fn(),
outputTable: vi.fn(),
}))

import { createSmitheryClient } from "../../../lib/smithery-client"
import { isJsonMode, outputTable } from "../../../utils/output"
import { getNamespace } from "../../../utils/smithery-settings"
import { listNamespaces } from "../list"

describe("listNamespaces", () => {
let mockClient: {
namespaces: {
list: ReturnType<typeof vi.fn>
}
}

beforeEach(() => {
vi.clearAllMocks()

mockClient = {
namespaces: {
list: vi.fn(),
},
}

vi.mocked(createSmitheryClient).mockResolvedValue(
mockClient as unknown as Awaited<ReturnType<typeof createSmitheryClient>>,
)
vi.mocked(isJsonMode).mockReturnValue(false)
})

test("lists namespaces with current marker", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [
{ name: "personal" },
{ name: "team-alpha" },
{ name: "team-beta" },
],
})
vi.mocked(getNamespace).mockResolvedValue("team-alpha")

await listNamespaces()

expect(mockClient.namespaces.list).toHaveBeenCalledOnce()
expect(getNamespace).toHaveBeenCalledOnce()
expect(outputTable).toHaveBeenCalledWith({
data: [
{ name: "personal", current: "" },
{ name: "team-alpha", current: "✓" },
{ name: "team-beta", current: "" },
],
columns: [
{ key: "name", header: "NAME" },
{ key: "current", header: "CURRENT" },
],
json: false,
jsonData: {
namespaces: ["personal", "team-alpha", "team-beta"],
current: "team-alpha",
},
tip: "Use smithery namespace use <name> to switch namespaces.",
})
})

test("handles no current namespace", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [{ name: "personal" }, { name: "team-alpha" }],
})
vi.mocked(getNamespace).mockResolvedValue(undefined)

await listNamespaces()

expect(outputTable).toHaveBeenCalledWith(
expect.objectContaining({
data: [
{ name: "personal", current: "" },
{ name: "team-alpha", current: "" },
],
jsonData: {
namespaces: ["personal", "team-alpha"],
current: null,
},
}),
)
})

test("handles empty namespace list", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [],
})
vi.mocked(getNamespace).mockResolvedValue(undefined)

await listNamespaces()

expect(outputTable).toHaveBeenCalledWith(
expect.objectContaining({
data: [],
jsonData: {
namespaces: [],
current: null,
},
}),
)
})

test("handles JSON output mode", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [{ name: "ns1" }, { name: "ns2" }],
})
vi.mocked(getNamespace).mockResolvedValue("ns1")
vi.mocked(isJsonMode).mockReturnValue(true)

await listNamespaces()

expect(outputTable).toHaveBeenCalledWith(
expect.objectContaining({
json: true,
}),
)
})

test("handles API error", async () => {
mockClient.namespaces.list.mockRejectedValue(new Error("API error"))

await expect(listNamespaces()).rejects.toThrow("API error")

expect(outputTable).not.toHaveBeenCalled()
})

test("marks current namespace correctly in large list", async () => {
const namespaces = Array.from({ length: 10 }, (_, i) => ({
name: `namespace-${i}`,
}))
mockClient.namespaces.list.mockResolvedValue({ namespaces })
vi.mocked(getNamespace).mockResolvedValue("namespace-5")

await listNamespaces()

const callArg = vi.mocked(outputTable).mock.calls[0][0]
const data = callArg.data as Array<{ name: string; current: string }>

expect(data.find((d) => d.name === "namespace-5")?.current).toBe("✓")
expect(data.filter((d) => d.current === "✓")).toHaveLength(1)
})
})
133 changes: 133 additions & 0 deletions src/commands/namespace/__tests__/use.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import pc from "picocolors"
import { beforeEach, describe, expect, test, vi } from "vitest"

// Mock dependencies
vi.mock("../../../lib/smithery-client", () => ({
createSmitheryClient: vi.fn(),
}))

vi.mock("../../../utils/smithery-settings", () => ({
setNamespace: vi.fn(),
}))

import { createSmitheryClient } from "../../../lib/smithery-client"
import { setNamespace } from "../../../utils/smithery-settings"
import { useNamespace } from "../use"

describe("useNamespace", () => {
let mockClient: {
namespaces: {
list: ReturnType<typeof vi.fn>
}
}
let consoleLogSpy: any
let consoleErrorSpy: any
let processExitSpy: any

beforeEach(() => {
vi.clearAllMocks()

mockClient = {
namespaces: {
list: vi.fn(),
},
}

vi.mocked(createSmitheryClient).mockResolvedValue(
mockClient as unknown as Awaited<ReturnType<typeof createSmitheryClient>>,
)

consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => {})
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
processExitSpy = vi.spyOn(process, "exit").mockImplementation((() => {
throw new Error("process.exit called")
}) as never)
})

test("successfully switches to existing namespace", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [
{ name: "personal" },
{ name: "team-alpha" },
{ name: "team-beta" },
],
})
vi.mocked(setNamespace).mockResolvedValue({
success: true,
})

await useNamespace("team-alpha")

expect(mockClient.namespaces.list).toHaveBeenCalledOnce()
expect(setNamespace).toHaveBeenCalledWith("team-alpha")
expect(consoleLogSpy).toHaveBeenCalledWith(
pc.green("Switched to namespace: team-alpha"),
)
expect(processExitSpy).not.toHaveBeenCalled()
})

test("fails when namespace does not exist", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [{ name: "personal" }, { name: "team-alpha" }],
})

await expect(useNamespace("nonexistent")).rejects.toThrow(
"process.exit called",
)

expect(consoleErrorSpy).toHaveBeenCalledWith(
pc.red('Namespace "nonexistent" not found.'),
)
expect(consoleErrorSpy).toHaveBeenCalledWith(
pc.gray("Available namespaces: personal, team-alpha"),
)
expect(setNamespace).not.toHaveBeenCalled()
expect(processExitSpy).toHaveBeenCalledWith(1)
})

test("handles save failure", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [{ name: "personal" }],
})
vi.mocked(setNamespace).mockResolvedValue({
success: false,
error: "Permission denied",
})

await expect(useNamespace("personal")).rejects.toThrow(
"process.exit called",
)

expect(consoleErrorSpy).toHaveBeenCalledWith(
pc.red("Failed to save namespace setting."),
)
expect(consoleErrorSpy).toHaveBeenCalledWith(pc.gray("Permission denied"))
expect(processExitSpy).toHaveBeenCalledWith(1)
})

test("handles API error when listing namespaces", async () => {
mockClient.namespaces.list.mockRejectedValue(new Error("Network error"))

await expect(useNamespace("any-namespace")).rejects.toThrow("Network error")

expect(setNamespace).not.toHaveBeenCalled()
expect(processExitSpy).not.toHaveBeenCalled()
})

test("lists available namespaces when target not found", async () => {
mockClient.namespaces.list.mockResolvedValue({
namespaces: [
{ name: "ns1" },
{ name: "ns2" },
{ name: "ns3" },
{ name: "ns4" },
],
})

await expect(useNamespace("wrong")).rejects.toThrow("process.exit called")

expect(consoleErrorSpy).toHaveBeenCalledWith(
pc.gray("Available namespaces: ns1, ns2, ns3, ns4"),
)
})
})
Loading