import { tap, expect } from '@git.zone/tstest/tapbundle'; import * as fs from 'node:fs/promises'; import * as os from 'node:os'; import * as path from 'node:path'; import { SshConfigWriter } from '../ts/index.js'; tap.test('should add managed host blocks and update existing hosts', async () => { const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'dap-sshconfigwriter-')); try { const sshDir = path.join(tempDir, '.ssh'); await fs.mkdir(sshDir, { recursive: true }); const mainConfigPath = path.join(sshDir, 'config'); await fs.writeFile(mainConfigPath, ['Host old', ' HostName old.example.com', ''].join('\n')); const writer = new SshConfigWriter({ homeDir: tempDir, mainConfigPath }); const addResult = await writer.addOrReplaceManagedHost({ alias: 'alpha', hostName: 'alpha.example.com', user: 'deploy', port: '22', identityFile: '~/.ssh/id_ed25519', }); const addedContent = await fs.readFile(mainConfigPath, 'utf8'); expect(addResult.changed).toEqual(true); expect(addedContent.includes('# dap:begin alpha')).toEqual(true); expect(addedContent.includes('HostName alpha.example.com')).toEqual(true); const preview = await writer.previewUpdateHost('old', { user: 'root', port: '2222' }); expect(preview.dapManaged).toEqual(false); expect(preview.diff.includes('+ User root')).toEqual(true); const editResult = await writer.updateHost('old', { user: 'root', port: '2222' }); const editedContent = await fs.readFile(mainConfigPath, 'utf8'); expect(editResult.changed).toEqual(true); expect(Boolean(editResult.backupPath)).toEqual(true); expect(editedContent.includes(' User root')).toEqual(true); expect(editedContent.includes(' Port 2222')).toEqual(true); } finally { await fs.rm(tempDir, { recursive: true, force: true }); } }); export default tap.start();