claude-code-best/tests/mocks/toolContext.ts
James Feng 5b0c0fa46f fix: sync missing test mocks and agentToolFilter from upstream
Copied 6 missing mock files (auth, axios, childProcess, log, state,
toolContext) and agentToolFilter.ts from claude-code-best/claude-code.

Result: +92 tests pass, 9 'module not found' errors resolved.
LocalMemoryRecallTool has 40 new failures (mock/impl mismatch, TBD).
2026-06-02 00:59:36 +08:00

53 lines
1.5 KiB
TypeScript

/**
* Shared minimal ToolUseContext stub for tool unit tests.
*
* Provides only the fields tools actually access in tests:
* - getAppState() returns a context with empty rule arrays for every source
* - toolUseId / parentMessageId / assistantMessageId / turnId can be
* overridden per test for budget tracking tests
*
* Usage:
* import { mockToolContext } from 'tests/mocks/toolContext'
* const ctx = mockToolContext({ toolUseId: 't1' })
*
* Per memory feedback "Mock dependency not subject" — this exists so each
* tool test file does not redefine the same partial stub.
*/
const emptyRules = {
user: [],
project: [],
local: [],
session: [],
cliArg: [],
}
export interface MockToolContextOptions {
toolUseId?: string
parentMessageId?: string
assistantMessageId?: string
turnId?: string
/** Override toolPermissionContext fields (e.g. mode, alwaysAllowRules). */
permissionOverrides?: Record<string, unknown>
}
export function mockToolContext(opts: MockToolContextOptions = {}): never {
return {
toolUseId: opts.toolUseId,
parentMessageId: opts.parentMessageId,
assistantMessageId: opts.assistantMessageId,
turnId: opts.turnId,
getAppState: () => ({
toolPermissionContext: {
mode: 'default',
additionalWorkingDirectories: new Set(),
alwaysAllowRules: { ...emptyRules },
alwaysDenyRules: { ...emptyRules },
alwaysAskRules: { ...emptyRules },
isBypassPermissionsModeAvailable: false,
...(opts.permissionOverrides ?? {}),
},
}),
} as never
}