feat: Add Amp rule profile with AGENT.md and MCP config (#973)

* Amp profile + tests

* generatlize to Agent instead of Claude Code to support any agent

* add changeset

* unnecessary tab formatting

* fix exports

* fix formatting
This commit is contained in:
Joe Danziger
2025-07-16 08:44:37 -04:00
committed by Ralph Khreish
parent 8774e7d5ae
commit 6c5e0f97f8
10 changed files with 958 additions and 10 deletions

View File

@@ -0,0 +1,346 @@
import { jest } from '@jest/globals';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { getRulesProfile } from '../../../src/utils/rule-transformer.js';
import { convertAllRulesToProfileRules } from '../../../src/utils/rule-transformer.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
describe('Amp Profile Init Functionality', () => {
let tempDir;
let ampProfile;
beforeEach(() => {
// Create temporary directory for testing
tempDir = fs.mkdtempSync(path.join(__dirname, 'temp-amp-'));
// Get the Amp profile
ampProfile = getRulesProfile('amp');
});
afterEach(() => {
// Clean up temporary directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
describe('Profile Configuration', () => {
test('should have correct profile metadata', () => {
expect(ampProfile).toBeDefined();
expect(ampProfile.profileName).toBe('amp');
expect(ampProfile.displayName).toBe('Amp');
expect(ampProfile.profileDir).toBe('.vscode');
expect(ampProfile.rulesDir).toBe('.');
expect(ampProfile.mcpConfig).toBe(true);
expect(ampProfile.mcpConfigName).toBe('settings.json');
expect(ampProfile.mcpConfigPath).toBe('.vscode/settings.json');
expect(ampProfile.includeDefaultRules).toBe(false);
});
test('should have correct file mapping', () => {
expect(ampProfile.fileMap).toBeDefined();
expect(ampProfile.fileMap['AGENTS.md']).toBe('.taskmaster/AGENT.md');
});
test('should have lifecycle functions', () => {
expect(typeof ampProfile.onAddRulesProfile).toBe('function');
expect(typeof ampProfile.onRemoveRulesProfile).toBe('function');
expect(typeof ampProfile.onPostConvertRulesProfile).toBe('function');
});
});
describe('AGENT.md Handling', () => {
test('should create AGENT.md with import when none exists', () => {
// Create mock AGENTS.md source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, assetsDir);
// Check that AGENT.md was created with import
const agentFile = path.join(tempDir, 'AGENT.md');
expect(fs.existsSync(agentFile)).toBe(true);
const content = fs.readFileSync(agentFile, 'utf8');
expect(content).toContain('# Amp Instructions');
expect(content).toContain('## Task Master AI Instructions');
expect(content).toContain('@./.taskmaster/AGENT.md');
// Check that .taskmaster/AGENT.md was created
const taskMasterAgent = path.join(tempDir, '.taskmaster', 'AGENT.md');
expect(fs.existsSync(taskMasterAgent)).toBe(true);
});
test('should append import to existing AGENT.md', () => {
// Create existing AGENT.md
const existingContent =
'# My Existing Amp Instructions\n\nSome content here.';
fs.writeFileSync(path.join(tempDir, 'AGENT.md'), existingContent);
// Create mock AGENTS.md source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, assetsDir);
// Check that import was appended
const agentFile = path.join(tempDir, 'AGENT.md');
const content = fs.readFileSync(agentFile, 'utf8');
expect(content).toContain('# My Existing Amp Instructions');
expect(content).toContain('Some content here.');
expect(content).toContain('## Task Master AI Instructions');
expect(content).toContain('@./.taskmaster/AGENT.md');
});
test('should not duplicate import if already exists', () => {
// Create AGENT.md with existing import
const existingContent =
"# My Amp Instructions\n\n## Task Master AI Instructions\n**Import Task Master's development workflow commands and guidelines, treat as if import is in the main AGENT.md file.**\n@./.taskmaster/AGENT.md";
fs.writeFileSync(path.join(tempDir, 'AGENT.md'), existingContent);
// Create mock AGENTS.md source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, assetsDir);
// Check that import was not duplicated
const agentFile = path.join(tempDir, 'AGENT.md');
const content = fs.readFileSync(agentFile, 'utf8');
const importCount = (content.match(/@\.\/.taskmaster\/AGENT\.md/g) || [])
.length;
expect(importCount).toBe(1);
});
});
describe('MCP Configuration', () => {
test('should rename mcpServers to amp.mcpServers', () => {
// Create .vscode directory and settings.json with mcpServers
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
mcpServers: {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
}
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Call onPostConvertRulesProfile (which should transform mcpServers to amp.mcpServers)
ampProfile.onPostConvertRulesProfile(
tempDir,
path.join(tempDir, 'assets')
);
// Check that mcpServers was renamed to amp.mcpServers
const settingsFile = path.join(vscodeDirPath, 'settings.json');
const content = fs.readFileSync(settingsFile, 'utf8');
const config = JSON.parse(content);
expect(config.mcpServers).toBeUndefined();
expect(config['amp.mcpServers']).toBeDefined();
expect(config['amp.mcpServers']['task-master-ai']).toBeDefined();
});
test('should not rename if amp.mcpServers already exists', () => {
// Create .vscode directory and settings.json with both mcpServers and amp.mcpServers
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
mcpServers: {
'some-other-server': {
command: 'other-command'
}
},
'amp.mcpServers': {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
}
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, path.join(tempDir, 'assets'));
// Check that both sections remain unchanged
const settingsFile = path.join(vscodeDirPath, 'settings.json');
const content = fs.readFileSync(settingsFile, 'utf8');
const config = JSON.parse(content);
expect(config.mcpServers).toBeDefined();
expect(config.mcpServers['some-other-server']).toBeDefined();
expect(config['amp.mcpServers']).toBeDefined();
expect(config['amp.mcpServers']['task-master-ai']).toBeDefined();
});
});
describe('Removal Functionality', () => {
test('should remove AGENT.md import and clean up files', () => {
// Setup: Create AGENT.md with import and .taskmaster/AGENT.md
const agentContent =
"# My Amp Instructions\n\nSome content.\n\n## Task Master AI Instructions\n**Import Task Master's development workflow commands and guidelines, treat as if import is in the main AGENT.md file.**\n@./.taskmaster/AGENT.md\n";
fs.writeFileSync(path.join(tempDir, 'AGENT.md'), agentContent);
fs.mkdirSync(path.join(tempDir, '.taskmaster'), { recursive: true });
fs.writeFileSync(
path.join(tempDir, '.taskmaster', 'AGENT.md'),
'Task Master instructions'
);
// Call onRemoveRulesProfile
ampProfile.onRemoveRulesProfile(tempDir);
// Check that .taskmaster/AGENT.md was removed
expect(fs.existsSync(path.join(tempDir, '.taskmaster', 'AGENT.md'))).toBe(
false
);
// Check that import was removed from AGENT.md
const remainingContent = fs.readFileSync(
path.join(tempDir, 'AGENT.md'),
'utf8'
);
expect(remainingContent).not.toContain('## Task Master AI Instructions');
expect(remainingContent).not.toContain('@./.taskmaster/AGENT.md');
expect(remainingContent).toContain('# My Amp Instructions');
expect(remainingContent).toContain('Some content.');
});
test('should remove empty AGENT.md if only contained import', () => {
// Setup: Create AGENT.md with only import
const agentContent =
"# Amp Instructions\n\n## Task Master AI Instructions\n**Import Task Master's development workflow commands and guidelines, treat as if import is in the main AGENT.md file.**\n@./.taskmaster/AGENT.md";
fs.writeFileSync(path.join(tempDir, 'AGENT.md'), agentContent);
fs.mkdirSync(path.join(tempDir, '.taskmaster'), { recursive: true });
fs.writeFileSync(
path.join(tempDir, '.taskmaster', 'AGENT.md'),
'Task Master instructions'
);
// Call onRemoveRulesProfile
ampProfile.onRemoveRulesProfile(tempDir);
// Check that AGENT.md was removed
expect(fs.existsSync(path.join(tempDir, 'AGENT.md'))).toBe(false);
});
test('should remove amp.mcpServers section from settings.json', () => {
// Setup: Create .vscode/settings.json with amp.mcpServers and other settings
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
'amp.mcpServers': {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
},
'other.setting': 'value'
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Call onRemoveRulesProfile
ampProfile.onRemoveRulesProfile(tempDir);
// Check that amp.mcpServers was removed but other settings remain
const settingsFile = path.join(vscodeDirPath, 'settings.json');
expect(fs.existsSync(settingsFile)).toBe(true);
const content = fs.readFileSync(settingsFile, 'utf8');
const config = JSON.parse(content);
expect(config['amp.mcpServers']).toBeUndefined();
expect(config['other.setting']).toBe('value');
});
test('should remove settings.json and .vscode directory if empty after removal', () => {
// Setup: Create .vscode/settings.json with only amp.mcpServers
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
'amp.mcpServers': {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
}
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Call onRemoveRulesProfile
ampProfile.onRemoveRulesProfile(tempDir);
// Check that settings.json and .vscode directory were removed
expect(fs.existsSync(path.join(vscodeDirPath, 'settings.json'))).toBe(
false
);
expect(fs.existsSync(vscodeDirPath)).toBe(false);
});
});
describe('Full Integration', () => {
test('should work with convertAllRulesToProfileRules', () => {
// This test ensures the profile works with the full rule transformer
const result = convertAllRulesToProfileRules(tempDir, ampProfile);
expect(result.success).toBeGreaterThan(0);
expect(result.failed).toBe(0);
// Check that .taskmaster/AGENT.md was created
expect(fs.existsSync(path.join(tempDir, '.taskmaster', 'AGENT.md'))).toBe(
true
);
// Check that AGENT.md was created with import
expect(fs.existsSync(path.join(tempDir, 'AGENT.md'))).toBe(true);
const agentContent = fs.readFileSync(
path.join(tempDir, 'AGENT.md'),
'utf8'
);
expect(agentContent).toContain('@./.taskmaster/AGENT.md');
});
});
});

View File

@@ -0,0 +1,299 @@
import { jest } from '@jest/globals';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { getRulesProfile } from '../../../src/utils/rule-transformer.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
describe('Amp Profile Integration', () => {
let tempDir;
let ampProfile;
beforeEach(() => {
// Create temporary directory for testing
tempDir = fs.mkdtempSync(path.join(__dirname, 'temp-amp-unit-'));
// Get the Amp profile
ampProfile = getRulesProfile('amp');
});
afterEach(() => {
// Clean up temporary directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
describe('Profile Structure', () => {
test('should have expected profile structure', () => {
expect(ampProfile).toBeDefined();
expect(ampProfile.profileName).toBe('amp');
expect(ampProfile.displayName).toBe('Amp');
expect(ampProfile.profileDir).toBe('.vscode');
expect(ampProfile.rulesDir).toBe('.');
expect(ampProfile.mcpConfig).toBe(true);
expect(ampProfile.mcpConfigName).toBe('settings.json');
expect(ampProfile.mcpConfigPath).toBe('.vscode/settings.json');
expect(ampProfile.includeDefaultRules).toBe(false);
});
test('should have correct file mapping', () => {
expect(ampProfile.fileMap).toEqual({
'AGENTS.md': '.taskmaster/AGENT.md'
});
});
test('should not create unnecessary directories', () => {
// Unlike profiles that copy entire directories, Amp should only create what's needed
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, assetsDir);
// Should only have created .taskmaster directory and AGENT.md
expect(fs.existsSync(path.join(tempDir, '.taskmaster'))).toBe(true);
expect(fs.existsSync(path.join(tempDir, 'AGENT.md'))).toBe(true);
// Should not have created any other directories (like .claude)
expect(fs.existsSync(path.join(tempDir, '.amp'))).toBe(false);
expect(fs.existsSync(path.join(tempDir, '.claude'))).toBe(false);
});
});
describe('AGENT.md Import Logic', () => {
test('should handle missing source file gracefully', () => {
// Call onAddRulesProfile without creating source file
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
// Should not throw error
expect(() => {
ampProfile.onAddRulesProfile(tempDir, assetsDir);
}).not.toThrow();
// Should not create any files
expect(fs.existsSync(path.join(tempDir, 'AGENT.md'))).toBe(false);
expect(fs.existsSync(path.join(tempDir, '.taskmaster', 'AGENT.md'))).toBe(
false
);
});
test('should preserve existing content when adding import', () => {
// Create existing AGENT.md with specific content
const existingContent =
'# My Custom Amp Setup\n\nThis is my custom configuration.\n\n## Custom Section\n\nSome custom rules here.';
fs.writeFileSync(path.join(tempDir, 'AGENT.md'), existingContent);
// Create mock source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onAddRulesProfile
ampProfile.onAddRulesProfile(tempDir, assetsDir);
// Check that existing content is preserved
const updatedContent = fs.readFileSync(
path.join(tempDir, 'AGENT.md'),
'utf8'
);
expect(updatedContent).toContain('# My Custom Amp Setup');
expect(updatedContent).toContain('This is my custom configuration.');
expect(updatedContent).toContain('## Custom Section');
expect(updatedContent).toContain('Some custom rules here.');
expect(updatedContent).toContain('@./.taskmaster/AGENT.md');
});
});
describe('MCP Configuration Handling', () => {
test('should handle missing .vscode directory gracefully', () => {
// Call onAddRulesProfile without .vscode directory
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
// Should not throw error
expect(() => {
ampProfile.onAddRulesProfile(tempDir, assetsDir);
}).not.toThrow();
});
test('should handle malformed JSON gracefully', () => {
// Create .vscode directory with malformed JSON
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
'{ malformed json'
);
// Should not throw error
expect(() => {
ampProfile.onAddRulesProfile(tempDir, path.join(tempDir, 'assets'));
}).not.toThrow();
});
test('should preserve other VS Code settings when renaming', () => {
// Create .vscode/settings.json with various settings
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
'editor.fontSize': 14,
'editor.tabSize': 2,
mcpServers: {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
},
'workbench.colorTheme': 'Dark+'
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Call onPostConvertRulesProfile (which handles MCP transformation)
ampProfile.onPostConvertRulesProfile(
tempDir,
path.join(tempDir, 'assets')
);
// Check that other settings are preserved
const settingsFile = path.join(vscodeDirPath, 'settings.json');
const content = fs.readFileSync(settingsFile, 'utf8');
const config = JSON.parse(content);
expect(config['editor.fontSize']).toBe(14);
expect(config['editor.tabSize']).toBe(2);
expect(config['workbench.colorTheme']).toBe('Dark+');
expect(config['amp.mcpServers']).toBeDefined();
expect(config.mcpServers).toBeUndefined();
});
});
describe('Removal Logic', () => {
test('should handle missing files gracefully during removal', () => {
// Should not throw error when removing non-existent files
expect(() => {
ampProfile.onRemoveRulesProfile(tempDir);
}).not.toThrow();
});
test('should handle malformed JSON gracefully during removal', () => {
// Create .vscode directory with malformed JSON
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
'{ malformed json'
);
// Should not throw error
expect(() => {
ampProfile.onRemoveRulesProfile(tempDir);
}).not.toThrow();
});
test('should preserve .vscode directory if it contains other files', () => {
// Create .vscode directory with amp.mcpServers and other files
const vscodeDirPath = path.join(tempDir, '.vscode');
fs.mkdirSync(vscodeDirPath, { recursive: true });
const initialConfig = {
'amp.mcpServers': {
'task-master-ai': {
command: 'npx',
args: ['-y', '--package=task-master-ai', 'task-master-ai']
}
}
};
fs.writeFileSync(
path.join(vscodeDirPath, 'settings.json'),
JSON.stringify(initialConfig, null, '\t')
);
// Create another file in .vscode
fs.writeFileSync(path.join(vscodeDirPath, 'launch.json'), '{}');
// Call onRemoveRulesProfile
ampProfile.onRemoveRulesProfile(tempDir);
// Check that .vscode directory is preserved
expect(fs.existsSync(vscodeDirPath)).toBe(true);
expect(fs.existsSync(path.join(vscodeDirPath, 'launch.json'))).toBe(true);
});
});
describe('Lifecycle Function Integration', () => {
test('should have all required lifecycle functions', () => {
expect(typeof ampProfile.onAddRulesProfile).toBe('function');
expect(typeof ampProfile.onRemoveRulesProfile).toBe('function');
expect(typeof ampProfile.onPostConvertRulesProfile).toBe('function');
});
test('onPostConvertRulesProfile should behave like onAddRulesProfile', () => {
// Create mock source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
fs.writeFileSync(
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Call onPostConvertRulesProfile
ampProfile.onPostConvertRulesProfile(tempDir, assetsDir);
// Should have same result as onAddRulesProfile
expect(fs.existsSync(path.join(tempDir, '.taskmaster', 'AGENT.md'))).toBe(
true
);
expect(fs.existsSync(path.join(tempDir, 'AGENT.md'))).toBe(true);
const agentContent = fs.readFileSync(
path.join(tempDir, 'AGENT.md'),
'utf8'
);
expect(agentContent).toContain('@./.taskmaster/AGENT.md');
});
});
describe('Error Handling', () => {
test('should handle file system errors gracefully', () => {
// Mock fs.writeFileSync to throw an error
const originalWriteFileSync = fs.writeFileSync;
fs.writeFileSync = jest.fn().mockImplementation(() => {
throw new Error('Permission denied');
});
// Create mock source
const assetsDir = path.join(tempDir, 'assets');
fs.mkdirSync(assetsDir, { recursive: true });
originalWriteFileSync.call(
fs,
path.join(assetsDir, 'AGENTS.md'),
'Task Master instructions'
);
// Should not throw error
expect(() => {
ampProfile.onAddRulesProfile(tempDir, assetsDir);
}).not.toThrow();
// Restore original function
fs.writeFileSync = originalWriteFileSync;
});
});
});

View File

@@ -143,6 +143,8 @@ describe('MCP Configuration Validation', () => {
const profileDirs = new Set();
// Profiles that use root directory (can share the same directory)
const rootProfiles = ['claude', 'codex', 'gemini'];
// Profiles that intentionally share the same directory
const sharedDirectoryProfiles = ['amp', 'vscode']; // Both use .vscode
RULE_PROFILES.forEach((profileName) => {
const profile = getRulesProfile(profileName);
@@ -152,10 +154,18 @@ describe('MCP Configuration Validation', () => {
expect(profile.rulesDir).toBe('.');
}
// Profile directories should be unique (except for root profiles)
if (!rootProfiles.includes(profileName) || profile.profileDir !== '.') {
expect(profileDirs.has(profile.profileDir)).toBe(false);
profileDirs.add(profile.profileDir);
// Profile directories should be unique (except for root profiles and shared directory profiles)
if (
!rootProfiles.includes(profileName) &&
!sharedDirectoryProfiles.includes(profileName)
) {
if (profile.profileDir !== '.') {
expect(profileDirs.has(profile.profileDir)).toBe(false);
profileDirs.add(profile.profileDir);
}
} else if (sharedDirectoryProfiles.includes(profileName)) {
// Shared directory profiles should use .vscode
expect(profile.profileDir).toBe('.vscode');
}
});
});
@@ -307,6 +317,7 @@ describe('MCP Configuration Validation', () => {
describe('Profile structure validation', () => {
const mcpProfiles = [
'amp',
'cursor',
'gemini',
'roo',
@@ -315,7 +326,7 @@ describe('MCP Configuration Validation', () => {
'trae',
'vscode'
];
const profilesWithLifecycle = ['claude'];
const profilesWithLifecycle = ['amp', 'claude'];
const profilesWithoutLifecycle = ['codex'];
test.each(mcpProfiles)(

View File

@@ -180,6 +180,11 @@ describe('Rule Transformer - General', () => {
it('should have correct MCP configuration for each profile', () => {
const expectedConfigs = {
amp: {
mcpConfig: true,
mcpConfigName: 'settings.json',
expectedPath: '.vscode/settings.json'
},
claude: {
mcpConfig: true,
mcpConfigName: '.mcp.json',