Files
oh-my-opencode/src/features/skill-mcp-manager/env-cleaner.test.ts
T
YeonGyu-Kim fc70dd2492 refactor: major codebase cleanup - BDD comments, file splitting, bug fixes (#1350)
* style(tests): normalize BDD comments from '// #given' to '// given'

- Replace 4,668 Python-style BDD comments across 107 test files
- Patterns changed: // #given -> // given, // #when -> // when, // #then -> // then
- Also handles no-space variants: //#given -> // given

* fix(rules-injector): prefer output.metadata.filePath over output.title

- Extract file path resolution to dedicated output-path.ts module
- Prefer metadata.filePath which contains actual file path
- Fall back to output.title only when metadata unavailable
- Fixes issue where rules weren't injected when tool output title was a label

* feat(slashcommand): add optional user_message parameter

- Add user_message optional parameter for command arguments
- Model can now call: command='publish' user_message='patch'
- Improves error messages with clearer format guidance
- Helps LLMs understand correct parameter usage

* feat(hooks): restore compaction-context-injector hook

- Restore hook deleted in cbbc7bd0 for session compaction context
- Injects 7 mandatory sections: User Requests, Final Goal, Work Completed,
  Remaining Tasks, Active Working Context, MUST NOT Do, Agent Verification State
- Re-register in hooks/index.ts and main plugin entry

* refactor(background-agent): split manager.ts into focused modules

- Extract constants.ts for TTL values and internal types (52 lines)
- Extract state.ts for TaskStateManager class (204 lines)
- Extract spawner.ts for task creation logic (244 lines)
- Extract result-handler.ts for completion handling (265 lines)
- Reduce manager.ts from 1377 to 755 lines (45% reduction)
- Maintain backward compatible exports

* refactor(agents): split prometheus-prompt.ts into subdirectory

- Move 1196-line prometheus-prompt.ts to prometheus/ subdirectory
- Organize prompt sections into separate files for maintainability
- Update agents/index.ts exports

* refactor(delegate-task): split tools.ts into focused modules

- Extract categories.ts for category definitions and routing
- Extract executor.ts for task execution logic
- Extract helpers.ts for utility functions
- Extract prompt-builder.ts for prompt construction
- Reduce tools.ts complexity with cleaner separation of concerns

* refactor(builtin-skills): split skills.ts into individual skill files

- Move each skill to dedicated file in skills/ subdirectory
- Create barrel export for backward compatibility
- Improve maintainability with focused skill modules

* chore: update import paths and lockfile

- Update prometheus import path after refactor
- Update bun.lock

* fix(tests): complete BDD comment normalization

- Fix remaining #when/#then patterns missed by initial sed
- Affected: state.test.ts, events.test.ts

---------

Co-authored-by: justsisyphus <justsisyphus@users.noreply.github.com>
2026-02-01 16:47:50 +09:00

202 lines
6.6 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from "bun:test"
import { createCleanMcpEnvironment, EXCLUDED_ENV_PATTERNS } from "./env-cleaner"
describe("createCleanMcpEnvironment", () => {
// Store original env to restore after tests
const originalEnv = { ...process.env }
afterEach(() => {
// Restore original environment
for (const key of Object.keys(process.env)) {
if (!(key in originalEnv)) {
delete process.env[key]
}
}
for (const [key, value] of Object.entries(originalEnv)) {
process.env[key] = value
}
})
describe("NPM_CONFIG_* filtering", () => {
it("filters out uppercase NPM_CONFIG_* variables", () => {
// given
process.env.NPM_CONFIG_REGISTRY = "https://private.registry.com"
process.env.NPM_CONFIG_CACHE = "/some/cache/path"
process.env.NPM_CONFIG_PREFIX = "/some/prefix"
process.env.PATH = "/usr/bin"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.NPM_CONFIG_REGISTRY).toBeUndefined()
expect(cleanEnv.NPM_CONFIG_CACHE).toBeUndefined()
expect(cleanEnv.NPM_CONFIG_PREFIX).toBeUndefined()
expect(cleanEnv.PATH).toBe("/usr/bin")
})
it("filters out lowercase npm_config_* variables", () => {
// given
process.env.npm_config_registry = "https://private.registry.com"
process.env.npm_config_cache = "/some/cache/path"
process.env.npm_config_https_proxy = "http://proxy:8080"
process.env.npm_config_proxy = "http://proxy:8080"
process.env.HOME = "/home/user"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.npm_config_registry).toBeUndefined()
expect(cleanEnv.npm_config_cache).toBeUndefined()
expect(cleanEnv.npm_config_https_proxy).toBeUndefined()
expect(cleanEnv.npm_config_proxy).toBeUndefined()
expect(cleanEnv.HOME).toBe("/home/user")
})
})
describe("YARN_* filtering", () => {
it("filters out YARN_* variables", () => {
// given
process.env.YARN_CACHE_FOLDER = "/yarn/cache"
process.env.YARN_ENABLE_IMMUTABLE_INSTALLS = "true"
process.env.YARN_REGISTRY = "https://yarn.registry.com"
process.env.NODE_ENV = "production"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.YARN_CACHE_FOLDER).toBeUndefined()
expect(cleanEnv.YARN_ENABLE_IMMUTABLE_INSTALLS).toBeUndefined()
expect(cleanEnv.YARN_REGISTRY).toBeUndefined()
expect(cleanEnv.NODE_ENV).toBe("production")
})
})
describe("PNPM_* filtering", () => {
it("filters out PNPM_* variables", () => {
// given
process.env.PNPM_HOME = "/pnpm/home"
process.env.PNPM_STORE_DIR = "/pnpm/store"
process.env.USER = "testuser"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.PNPM_HOME).toBeUndefined()
expect(cleanEnv.PNPM_STORE_DIR).toBeUndefined()
expect(cleanEnv.USER).toBe("testuser")
})
})
describe("NO_UPDATE_NOTIFIER filtering", () => {
it("filters out NO_UPDATE_NOTIFIER variable", () => {
// given
process.env.NO_UPDATE_NOTIFIER = "1"
process.env.SHELL = "/bin/bash"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.NO_UPDATE_NOTIFIER).toBeUndefined()
expect(cleanEnv.SHELL).toBe("/bin/bash")
})
})
describe("custom environment overlay", () => {
it("merges custom env on top of clean process.env", () => {
// given
process.env.PATH = "/usr/bin"
process.env.NPM_CONFIG_REGISTRY = "https://private.registry.com"
const customEnv = {
MCP_API_KEY: "secret-key",
CUSTOM_VAR: "custom-value",
}
// when
const cleanEnv = createCleanMcpEnvironment(customEnv)
// then
expect(cleanEnv.PATH).toBe("/usr/bin")
expect(cleanEnv.NPM_CONFIG_REGISTRY).toBeUndefined()
expect(cleanEnv.MCP_API_KEY).toBe("secret-key")
expect(cleanEnv.CUSTOM_VAR).toBe("custom-value")
})
it("custom env can override process.env values", () => {
// given
process.env.NODE_ENV = "development"
const customEnv = {
NODE_ENV: "production",
}
// when
const cleanEnv = createCleanMcpEnvironment(customEnv)
// then
expect(cleanEnv.NODE_ENV).toBe("production")
})
})
describe("undefined value handling", () => {
it("skips undefined values from process.env", () => {
// given - process.env can have undefined values in TypeScript
const envWithUndefined = { ...process.env, UNDEFINED_VAR: undefined }
Object.assign(process.env, envWithUndefined)
// when
const cleanEnv = createCleanMcpEnvironment()
// then - should not throw and should not include undefined values
expect(cleanEnv.UNDEFINED_VAR).toBeUndefined()
expect(Object.values(cleanEnv).every((v) => v !== undefined)).toBe(true)
})
})
describe("mixed case handling", () => {
it("filters both uppercase and lowercase npm config variants", () => {
// given - pnpm/yarn can set both cases simultaneously
process.env.NPM_CONFIG_CACHE = "/uppercase/cache"
process.env.npm_config_cache = "/lowercase/cache"
process.env.NPM_CONFIG_REGISTRY = "https://uppercase.registry.com"
process.env.npm_config_registry = "https://lowercase.registry.com"
// when
const cleanEnv = createCleanMcpEnvironment()
// then
expect(cleanEnv.NPM_CONFIG_CACHE).toBeUndefined()
expect(cleanEnv.npm_config_cache).toBeUndefined()
expect(cleanEnv.NPM_CONFIG_REGISTRY).toBeUndefined()
expect(cleanEnv.npm_config_registry).toBeUndefined()
})
})
})
describe("EXCLUDED_ENV_PATTERNS", () => {
it("contains patterns for npm, yarn, and pnpm configs", () => {
// given / #when / #then
expect(EXCLUDED_ENV_PATTERNS.length).toBeGreaterThanOrEqual(4)
// Test that patterns match expected strings
const testCases = [
{ pattern: "NPM_CONFIG_REGISTRY", shouldMatch: true },
{ pattern: "npm_config_registry", shouldMatch: true },
{ pattern: "YARN_CACHE_FOLDER", shouldMatch: true },
{ pattern: "PNPM_HOME", shouldMatch: true },
{ pattern: "NO_UPDATE_NOTIFIER", shouldMatch: true },
{ pattern: "PATH", shouldMatch: false },
{ pattern: "HOME", shouldMatch: false },
{ pattern: "NODE_ENV", shouldMatch: false },
]
for (const { pattern, shouldMatch } of testCases) {
const matches = EXCLUDED_ENV_PATTERNS.some((regex: RegExp) => regex.test(pattern))
expect(matches).toBe(shouldMatch)
}
})
})