test(image-analysis): cover runtime auth and fallback

This commit is contained in:
Tam Nhu Tran committed 2026-04-02 15:44:13 -04:00
1 parent 813b2dd4d0
commit f379a4001f
15 files changed
+1077 -52

No files matched your search

@@ -0,0 +1,39 @@
import { describe, expect, it } from 'bun:test';
import {
appendThirdPartyImageAnalysisToolArgs,
getImageAnalysisSteeringPrompt,
} from '../../../../src/utils/image-analysis';
describe('appendThirdPartyImageAnalysisToolArgs', () => {
it('appends the steering prompt for image analysis', () => {
const args = appendThirdPartyImageAnalysisToolArgs(['-p', 'describe the screenshot']);
expect(args).toEqual(['-p', 'describe the screenshot', '--append-system-prompt', getImageAnalysisSteeringPrompt()]);
});
it('does not duplicate the steering prompt when already present', () => {
const steeringPrompt = getImageAnalysisSteeringPrompt();
const args = appendThirdPartyImageAnalysisToolArgs([
'-p',
'describe the screenshot',
'--append-system-prompt',
steeringPrompt,
]);
expect(args.filter((arg) => arg === steeringPrompt)).toHaveLength(1);
expect(args.filter((arg) => arg === '--append-system-prompt')).toHaveLength(1);
});
it('preserves trailing arguments after --', () => {
const args = appendThirdPartyImageAnalysisToolArgs(['-p', 'describe', '--', 'extra']);
expect(args).toEqual([
'-p',
'describe',
'--append-system-prompt',
getImageAnalysisSteeringPrompt(),
'--',
'extra',
]);
});
});
@@ -0,0 +1,180 @@
import { afterEach, describe, expect, it, mock } from 'bun:test';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {
ensureImageAnalysisMcp,
getImageAnalysisMcpRuntimePath,
getImageAnalysisMcpServerName,
getImageAnalysisMcpServerPath,
uninstallImageAnalysisMcp,
} from '../../../../src/utils/image-analysis';
describe('ensureImageAnalysisMcp', () => {
let tempHome: string | undefined;
let originalCcsHome: string | undefined;
function setupTempHome(): string {
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-image-analysis-mcp-'));
originalCcsHome = process.env.CCS_HOME;
process.env.CCS_HOME = tempHome;
return tempHome;
}
function getCcsDir(): string {
if (!tempHome) {
throw new Error('tempHome not initialized');
}
return path.join(tempHome, '.ccs');
}
function writeEnabledConfig(): void {
const ccsDir = getCcsDir();
fs.mkdirSync(ccsDir, { recursive: true });
fs.writeFileSync(
path.join(ccsDir, 'config.yaml'),
[
'version: 12',
'image_analysis:',
' enabled: true',
' timeout: 60',
' fallback_backend: agy',
' provider_models:',
' agy: gemini-3-1-flash-preview',
'',
].join('\n'),
'utf8'
);
}
function getManagedConfig() {
return {
type: 'stdio',
command: 'node',
args: [getImageAnalysisMcpServerPath()],
env: {},
};
}
afterEach(() => {
mock.restore();
if (originalCcsHome !== undefined) {
process.env.CCS_HOME = originalCcsHome;
} else {
delete process.env.CCS_HOME;
}
if (tempHome && fs.existsSync(tempHome)) {
fs.rmSync(tempHome, { recursive: true, force: true });
}
tempHome = undefined;
originalCcsHome = undefined;
});
it('installs the MCP server and preserves existing user mcpServers entries', () => {
setupTempHome();
writeEnabledConfig();
const claudeUserConfigPath = path.join(tempHome as string, '.claude.json');
fs.writeFileSync(
claudeUserConfigPath,
JSON.stringify(
{
mcpServers: {
existing: { command: 'uvx', args: ['some-server'] },
},
},
null,
2
) + '\n',
'utf8'
);
expect(ensureImageAnalysisMcp()).toBe(true);
expect(fs.existsSync(getImageAnalysisMcpServerPath())).toBe(true);
expect(fs.existsSync(getImageAnalysisMcpRuntimePath())).toBe(true);
const config = JSON.parse(fs.readFileSync(claudeUserConfigPath, 'utf8')) as {
mcpServers: Record<string, unknown>;
};
expect(config.mcpServers.existing).toEqual({ command: 'uvx', args: ['some-server'] });
expect(config.mcpServers[getImageAnalysisMcpServerName()]).toEqual(getManagedConfig());
});
it('removes the managed MCP runtime while preserving unrelated server entries', () => {
setupTempHome();
writeEnabledConfig();
const claudeUserConfigPath = path.join(tempHome as string, '.claude.json');
fs.writeFileSync(
claudeUserConfigPath,
JSON.stringify(
{
mcpServers: {
existing: { command: 'uvx', args: ['some-server'] },
},
},
null,
2
) + '\n',
'utf8'
);
expect(ensureImageAnalysisMcp()).toBe(true);
const instancePath = path.join(tempHome as string, '.ccs', 'instances', 'work');
fs.mkdirSync(instancePath, { recursive: true });
fs.writeFileSync(
path.join(instancePath, '.claude.json'),
JSON.stringify(
{
mcpServers: {
existing: { command: 'uvx', args: ['instance-server'] },
[getImageAnalysisMcpServerName()]: { command: 'node', args: ['/tmp/override.cjs'] },
},
otherKey: 'keep-me',
},
null,
2
) + '\n',
'utf8'
);
expect(uninstallImageAnalysisMcp()).toBe(true);
expect(fs.existsSync(getImageAnalysisMcpServerPath())).toBe(false);
expect(fs.existsSync(getImageAnalysisMcpRuntimePath())).toBe(false);
const globalConfig = JSON.parse(fs.readFileSync(claudeUserConfigPath, 'utf8')) as {
mcpServers: Record<string, { command: string; args: string[] }>;
};
expect(globalConfig.mcpServers).toEqual({
existing: { command: 'uvx', args: ['some-server'] },
});
const instanceConfig = JSON.parse(
fs.readFileSync(path.join(instancePath, '.claude.json'), 'utf8')
) as {
otherKey: string;
mcpServers: Record<string, { command: string; args: string[] }>;
};
expect(instanceConfig.otherKey).toBe('keep-me');
expect(instanceConfig.mcpServers).toEqual({
existing: { command: 'uvx', args: ['instance-server'] },
});
});
it('installs the first-class MCP runtime even when the legacy hooks path is unusable', () => {
setupTempHome();
writeEnabledConfig();
const hooksPath = path.join(getCcsDir(), 'hooks');
fs.writeFileSync(hooksPath, 'not-a-directory', 'utf8');
expect(ensureImageAnalysisMcp()).toBe(true);
expect(fs.existsSync(getImageAnalysisMcpServerPath())).toBe(true);
expect(fs.existsSync(getImageAnalysisMcpRuntimePath())).toBe(true);
});
});