Skip to content
Draft
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
168 changes: 13 additions & 155 deletions cli/src/commands/__tests__/theme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,162 +97,20 @@ describe("/theme command", () => {
refreshTerminalMock = vi.fn().mockResolvedValue(undefined)

// Create mock config
mockConfig = {
version: "1.0.0",
mode: "code",
telemetry: true,
provider: "test-provider",
providers: [],
customThemes: {
"custom-theme": mockTheme,
},
}

// Mock config loading
vi.doMock("../../config/persistence.js", () => ({
loadConfig: vi.fn().mockResolvedValue({
config: {
customThemes: {
"custom-theme": mockTheme,
},
},
}),
}))

// Mock the constants/themes/index.js functions
vi.doMock("../../constants/themes/index.js", () => ({
getAvailableThemes: vi.fn(() => [
"alpha",
"dark",
"dracula",
"github-dark",
"light",
"github-light",
"custom-theme",
]),
getThemeById: vi.fn((id: string) => {
const themes: Record<string, Theme> = {
dark: {
id: "dark",
name: "Dark",
type: "dark",
brand: { primary: "#3b82f6", secondary: "#1d4ed8" },
semantic: {
success: "#4ade80",
error: "#f87171",
warning: "#fbbf24",
info: "#60a5fa",
neutral: "#6b7280",
},
interactive: {
prompt: "#3b82f6",
selection: "#1e40af",
hover: "#2563eb",
disabled: "#9ca3af",
focus: "#1d4ed8",
},
messages: { user: "#10b981", assistant: "#8b5cf6", system: "#f59e0b", error: "#ef4444" },
actions: { approve: "#10b981", reject: "#ef4444", cancel: "#6b7280", pending: "#f59e0b" },
code: {
addition: "#10b981",
deletion: "#ef4444",
modification: "#f59e0b",
context: "#6b7280",
lineNumber: "#9ca3af",
},
ui: {
border: { default: "#374151", active: "#3b82f6", warning: "#f59e0b", error: "#ef4444" },
text: { primary: "#f9fafb", secondary: "#d1d5db", dimmed: "#9ca3af", highlight: "#fbbf24" },
background: { default: "#111827", elevated: "#1f2937" },
},
status: { online: "#10b981", offline: "#6b7280", busy: "#f59e0b", idle: "#94a3b8" },
},
light: {
id: "light",
name: "Light",
type: "light",
brand: { primary: "#3b82f6", secondary: "#1d4ed8" },
semantic: {
success: "#4ade80",
error: "#f87171",
warning: "#fbbf24",
info: "#60a5fa",
neutral: "#6b7280",
},
interactive: {
prompt: "#3b82f6",
selection: "#1e40af",
hover: "#2563eb",
disabled: "#9ca3af",
focus: "#1d4ed8",
},
messages: { user: "#10b981", assistant: "#8b5cf6", system: "#f59e0b", error: "#ef4444" },
actions: { approve: "#10b981", reject: "#ef4444", cancel: "#6b7280", pending: "#f59e0b" },
code: {
addition: "#10b981",
deletion: "#ef4444",
modification: "#f59e0b",
context: "#6b7280",
lineNumber: "#9ca3af",
},
ui: {
border: { default: "#e5e7eb", active: "#3b82f6", warning: "#f59e0b", error: "#ef4444" },
text: { primary: "#111827", secondary: "#6b7280", dimmed: "#9ca3af", highlight: "#fbbf24" },
background: { default: "#ffffff", elevated: "#f9fafb" },
},
status: { online: "#10b981", offline: "#6b7280", busy: "#f59e0b", idle: "#94a3b8" },
},
mockConfig = {
version: "1.0.0",
mode: "code",
telemetry: true,
provider: "test-provider",
providers: [],
customThemes: {
"custom-theme": mockTheme,
}
return (
themes[id] || {
id: "unknown",
name: "Unknown Theme",
type: "dark",
brand: { primary: "#000000", secondary: "#000000" },
semantic: {
success: "#000000",
error: "#000000",
warning: "#000000",
info: "#000000",
neutral: "#000000",
},
interactive: {
prompt: "#000000",
selection: "#000000",
hover: "#000000",
disabled: "#000000",
focus: "#000000",
},
messages: { user: "#000000", assistant: "#000000", system: "#000000", error: "#000000" },
actions: { approve: "#000000", reject: "#000000", cancel: "#000000", pending: "#000000" },
code: {
addition: "#000000",
deletion: "#000000",
modification: "#000000",
context: "#000000",
lineNumber: "#000000",
},
ui: {
border: { default: "#000000", active: "#000000", warning: "#000000", error: "#000000" },
text: { primary: "#000000", secondary: "#000000", dimmed: "#000000", highlight: "#000000" },
background: { default: "#000000", elevated: "#000000" },
},
status: { online: "#000000", offline: "#000000", busy: "#000000", idle: "#000000" },
}
)
}),
isValidThemeId: vi.fn(() => true),
}))

// Mock getBuiltinThemeIds
vi.doMock("../../constants/themes/custom.js", () => ({
getBuiltinThemeIds: vi.fn(() => ["alpha", "dark", "dracula", "github-dark", "light", "github-light"]),
}))

mockContext = createMockContext({
input: "/theme",
config: mockConfig,
},
}

mockContext = createMockContext({
input: "/theme",
config: mockConfig,
addMessage: addMessageMock,
setTheme: setThemeMock,
refreshTerminal: refreshTerminalMock,
Expand Down
8 changes: 5 additions & 3 deletions cli/src/config/__tests__/auto-approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("Auto Approval Configuration", () => {
describe("DEFAULT_AUTO_APPROVAL", () => {
it("should have correct default values", () => {
expect(DEFAULT_AUTO_APPROVAL).toEqual({
enabled: true,
enabled: false, // Safe default: auto-approval disabled until explicitly enabled
read: {
enabled: true,
outside: false,
Expand Down Expand Up @@ -47,8 +47,10 @@ describe("Auto Approval Configuration", () => {
})
})

it("should have global enabled set to true by default", () => {
expect(DEFAULT_AUTO_APPROVAL.enabled).toBe(true)
it("should have global enabled set to false by default for safety", () => {
// Safe default: auto-approval is disabled until explicitly enabled
// This prevents accidental auto-approval when config isn't loaded yet
expect(DEFAULT_AUTO_APPROVAL.enabled).toBe(false)
})

it("should have safe defaults for write operations", () => {
Expand Down
139 changes: 139 additions & 0 deletions cli/src/config/__tests__/env-config-json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"
import { buildConfigFromEnv, applyEnvOverrides } from "../env-config.js"
import { ENV_VARS } from "../env-utils.js"
import type { CLIConfig, AutoApprovalConfig } from "../types.js"

describe("env-config JSON auto-approval", () => {
const originalEnv = process.env

beforeEach(() => {
// Reset environment for each test
process.env = { ...originalEnv }
})

afterEach(() => {
process.env = originalEnv
})

describe("KILO_AUTO_APPROVAL_JSON parsing", () => {
const baseConfig: CLIConfig = {
version: "1.0.0",
mode: "code",
telemetry: true,
provider: "kilocode",
providers: [{ id: "kilocode", provider: "kilocode" }],
autoApproval: {
enabled: false,
read: { enabled: false },
write: { enabled: false },
},
}

it("parses valid JSON from KILO_AUTO_APPROVAL_JSON", () => {
const jsonConfig: AutoApprovalConfig = {
enabled: true,
read: { enabled: true, outside: true },
write: { enabled: true, outside: false, protected: true },
browser: { enabled: true },
mcp: { enabled: false },
execute: { enabled: true, allowed: ["npm test"], denied: ["rm"] },
}

process.env[ENV_VARS.AUTO_APPROVAL_JSON] = JSON.stringify(jsonConfig)

const result = applyEnvOverrides(baseConfig)

expect(result.autoApproval).toEqual(jsonConfig)
})

it("ignores individual env vars when JSON is set", () => {
const jsonConfig: AutoApprovalConfig = {
enabled: true,
read: { enabled: true },
}

process.env[ENV_VARS.AUTO_APPROVAL_JSON] = JSON.stringify(jsonConfig)
// These should be ignored
process.env[ENV_VARS.AUTO_APPROVAL_ENABLED] = "false"
process.env[ENV_VARS.AUTO_APPROVAL_READ_ENABLED] = "false"

const result = applyEnvOverrides(baseConfig)

// Should use JSON config, not individual env vars
expect(result.autoApproval?.enabled).toBe(true)
expect(result.autoApproval?.read?.enabled).toBe(true)
})

it("falls back to individual env vars when JSON is not set", () => {
process.env[ENV_VARS.AUTO_APPROVAL_ENABLED] = "true"
process.env[ENV_VARS.AUTO_APPROVAL_READ_ENABLED] = "true"

const result = applyEnvOverrides(baseConfig)

expect(result.autoApproval?.enabled).toBe(true)
expect(result.autoApproval?.read?.enabled).toBe(true)
})

it("falls back to individual env vars when JSON is invalid", () => {
process.env[ENV_VARS.AUTO_APPROVAL_JSON] = "not valid json"
process.env[ENV_VARS.AUTO_APPROVAL_ENABLED] = "true"

const result = applyEnvOverrides(baseConfig)

// Should fall back to individual env vars
expect(result.autoApproval?.enabled).toBe(true)
})

it("handles empty JSON string gracefully", () => {
process.env[ENV_VARS.AUTO_APPROVAL_JSON] = ""
process.env[ENV_VARS.AUTO_APPROVAL_ENABLED] = "true"

const result = applyEnvOverrides(baseConfig)

// Empty string should fall back to individual env vars
expect(result.autoApproval?.enabled).toBe(true)
})

it("preserves all auto-approval fields from JSON", () => {
const fullConfig: AutoApprovalConfig = {
enabled: true,
read: { enabled: true, outside: true },
write: { enabled: true, outside: true, protected: true },
browser: { enabled: true },
retry: { enabled: true, delay: 15 },
mcp: { enabled: true },
mode: { enabled: true },
subtasks: { enabled: true },
execute: { enabled: true, allowed: ["npm", "pnpm"], denied: ["rm -rf"] },
question: { enabled: false, timeout: 30000 },
todo: { enabled: true },
}

process.env[ENV_VARS.AUTO_APPROVAL_JSON] = JSON.stringify(fullConfig)

const result = applyEnvOverrides(baseConfig)

expect(result.autoApproval).toEqual(fullConfig)
})
})

describe("buildConfigFromEnv with JSON", () => {
it("uses JSON config when building from env", () => {
const jsonConfig: AutoApprovalConfig = {
enabled: true,
read: { enabled: true },
write: { enabled: false },
}

// Set up minimal required env vars for buildConfigFromEnv to work
process.env[ENV_VARS.PROVIDER_TYPE] = "kilocode"
process.env["KILOCODE_TOKEN"] = "test-token"
process.env[ENV_VARS.AUTO_APPROVAL_JSON] = JSON.stringify(jsonConfig)

const result = buildConfigFromEnv()

expect(result).not.toBeNull()
expect(result?.autoApproval).toEqual(jsonConfig)
})
})
})
Loading
Loading