|
| 1 | +const { describe, it, beforeEach, afterEach } = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | +const fs = require('node:fs'); |
| 4 | +const path = require('node:path'); |
| 5 | +const os = require('node:os'); |
| 6 | + |
| 7 | +const { generateAdapterConfigs, ADAPTER_MAP } = require('../../lib/init/generate-adapter'); |
| 8 | + |
| 9 | +describe('generateAdapterConfigs', () => { |
| 10 | + let tmpDir; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cb-gen-adapter-')); |
| 14 | + // Create a minimal .ai-rules/adapters/ with a fake adapter |
| 15 | + const adaptersDir = path.join(tmpDir, '.ai-rules', 'adapters'); |
| 16 | + fs.mkdirSync(adaptersDir, { recursive: true }); |
| 17 | + fs.writeFileSync(path.join(adaptersDir, 'cursor.md'), '# Cursor Adapter\nRules here.'); |
| 18 | + fs.writeFileSync(path.join(adaptersDir, 'claude-code.md'), '# Claude Code Adapter'); |
| 19 | + fs.writeFileSync(path.join(adaptersDir, 'codex.md'), '# Codex Adapter'); |
| 20 | + fs.writeFileSync(path.join(adaptersDir, 'antigravity.md'), '# Antigravity Adapter'); |
| 21 | + fs.writeFileSync(path.join(adaptersDir, 'q.md'), '# Q Adapter'); |
| 22 | + fs.writeFileSync(path.join(adaptersDir, 'kiro.md'), '# Kiro Adapter'); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('generates config for a detected tool', () => { |
| 30 | + const tools = [{ name: 'cursor' }]; |
| 31 | + const result = generateAdapterConfigs(tmpDir, tools); |
| 32 | + assert.equal(result.generated.length, 1); |
| 33 | + assert.equal(result.generated[0].tool, 'cursor'); |
| 34 | + assert.equal(result.generated[0].action, 'created'); |
| 35 | + // File should exist |
| 36 | + const targetPath = path.join(tmpDir, ADAPTER_MAP.cursor.target); |
| 37 | + assert.ok(fs.existsSync(targetPath)); |
| 38 | + assert.ok(fs.readFileSync(targetPath, 'utf-8').includes('Cursor Adapter')); |
| 39 | + }); |
| 40 | + |
| 41 | + it('generates configs for multiple tools', () => { |
| 42 | + const tools = [{ name: 'cursor' }, { name: 'claude-code' }, { name: 'codex' }]; |
| 43 | + const result = generateAdapterConfigs(tmpDir, tools); |
| 44 | + assert.equal(result.generated.length, 3); |
| 45 | + }); |
| 46 | + |
| 47 | + it('skips unknown tools', () => { |
| 48 | + const tools = [{ name: 'unknown-tool' }]; |
| 49 | + const result = generateAdapterConfigs(tmpDir, tools); |
| 50 | + assert.equal(result.generated.length, 0); |
| 51 | + assert.equal(result.skipped.length, 1); |
| 52 | + assert.equal(result.skipped[0].reason, 'no adapter mapping'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('backs up existing config before overwriting', () => { |
| 56 | + // Create existing config |
| 57 | + const targetDir = path.join(tmpDir, '.cursor', 'rules'); |
| 58 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 59 | + fs.writeFileSync(path.join(targetDir, 'codingbuddy.mdc'), 'old content'); |
| 60 | + |
| 61 | + const tools = [{ name: 'cursor' }]; |
| 62 | + const result = generateAdapterConfigs(tmpDir, tools); |
| 63 | + assert.equal(result.backedUp.length, 1); |
| 64 | + assert.equal(result.backedUp[0].tool, 'cursor'); |
| 65 | + // Backup dir should exist |
| 66 | + assert.ok(fs.existsSync(path.join(tmpDir, '.codingbuddy-backup'))); |
| 67 | + }); |
| 68 | + |
| 69 | + it('skips backup with force option', () => { |
| 70 | + const targetDir = path.join(tmpDir, '.cursor', 'rules'); |
| 71 | + fs.mkdirSync(targetDir, { recursive: true }); |
| 72 | + fs.writeFileSync(path.join(targetDir, 'codingbuddy.mdc'), 'old content'); |
| 73 | + |
| 74 | + const tools = [{ name: 'cursor' }]; |
| 75 | + const result = generateAdapterConfigs(tmpDir, tools, { force: true }); |
| 76 | + assert.equal(result.backedUp.length, 0); |
| 77 | + assert.equal(result.generated.length, 1); |
| 78 | + }); |
| 79 | + |
| 80 | + it('dry run does not write files', () => { |
| 81 | + const tools = [{ name: 'cursor' }]; |
| 82 | + const result = generateAdapterConfigs(tmpDir, tools, { dryRun: true }); |
| 83 | + assert.equal(result.generated.length, 1); |
| 84 | + assert.equal(result.generated[0].action, 'would-create'); |
| 85 | + // File should NOT exist |
| 86 | + const targetPath = path.join(tmpDir, ADAPTER_MAP.cursor.target); |
| 87 | + assert.ok(!fs.existsSync(targetPath)); |
| 88 | + }); |
| 89 | + |
| 90 | + it('skips when adapter template not found', () => { |
| 91 | + // Remove the adapter file |
| 92 | + fs.unlinkSync(path.join(tmpDir, '.ai-rules', 'adapters', 'cursor.md')); |
| 93 | + const tools = [{ name: 'cursor' }]; |
| 94 | + const result = generateAdapterConfigs(tmpDir, tools); |
| 95 | + assert.equal(result.skipped.length, 1); |
| 96 | + assert.equal(result.skipped[0].reason, 'adapter template not found'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('ADAPTER_MAP covers all expected tools', () => { |
| 100 | + const expectedTools = ['cursor', 'claude-code', 'codex', 'antigravity', 'amazon-q', 'kiro']; |
| 101 | + for (const tool of expectedTools) { |
| 102 | + assert.ok(ADAPTER_MAP[tool], `${tool} should have adapter mapping`); |
| 103 | + assert.ok(ADAPTER_MAP[tool].adapter, `${tool} should have adapter file`); |
| 104 | + assert.ok(ADAPTER_MAP[tool].target, `${tool} should have target path`); |
| 105 | + } |
| 106 | + }); |
| 107 | +}); |
0 commit comments