|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import matter from 'gray-matter'; |
| 6 | +import { Command } from 'commander'; |
| 7 | +import { createVault, type ClawVault } from '../lib/vault.js'; |
| 8 | +import { patchCommand, registerPatchCommand } from './patch.js'; |
| 9 | + |
| 10 | +const createdTempDirs: string[] = []; |
| 11 | + |
| 12 | +async function makeVault(): Promise<{ vaultPath: string; vault: ClawVault }> { |
| 13 | + const vaultPath = fs.mkdtempSync(path.join(os.tmpdir(), 'clawvault-patch-cmd-')); |
| 14 | + createdTempDirs.push(vaultPath); |
| 15 | + const vault = await createVault(vaultPath, { name: 'patch-test' }, { skipGraph: true }); |
| 16 | + return { vaultPath, vault }; |
| 17 | +} |
| 18 | + |
| 19 | +function readDoc(vaultPath: string, id: string): string { |
| 20 | + const filePath = path.join(vaultPath, `${id}.md`); |
| 21 | + return fs.readFileSync(filePath, 'utf-8'); |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + while (createdTempDirs.length > 0) { |
| 26 | + const dir = createdTempDirs.pop(); |
| 27 | + if (dir) { |
| 28 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 29 | + } |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +describe('patchCommand', () => { |
| 34 | + it('appends content while preserving frontmatter and indexed docs', async () => { |
| 35 | + const { vaultPath, vault } = await makeVault(); |
| 36 | + const target = await vault.store({ |
| 37 | + category: 'decisions', |
| 38 | + title: 'patch-target', |
| 39 | + content: 'Line A', |
| 40 | + frontmatter: { owner: 'ops' } |
| 41 | + }); |
| 42 | + const untouched = await vault.store({ |
| 43 | + category: 'inbox', |
| 44 | + title: 'untouched', |
| 45 | + content: 'Do not change me' |
| 46 | + }); |
| 47 | + |
| 48 | + await patchCommand(target.id, { |
| 49 | + vaultPath, |
| 50 | + append: 'Line B' |
| 51 | + }); |
| 52 | + |
| 53 | + const raw = readDoc(vaultPath, target.id); |
| 54 | + const parsed = matter(raw); |
| 55 | + expect(parsed.data.owner).toBe('ops'); |
| 56 | + expect(parsed.content).toContain('Line A'); |
| 57 | + expect(parsed.content.trimEnd()).toMatch(/Line A\nLine B$/); |
| 58 | + |
| 59 | + const indexPath = path.join(vaultPath, '.clawvault-index.json'); |
| 60 | + const indexData = JSON.parse(fs.readFileSync(indexPath, 'utf-8')) as { |
| 61 | + documents: Array<{ id: string }>; |
| 62 | + }; |
| 63 | + const indexedIds = indexData.documents.map((doc) => doc.id); |
| 64 | + expect(indexedIds).toContain(target.id); |
| 65 | + expect(indexedIds).toContain(untouched.id); |
| 66 | + }); |
| 67 | + |
| 68 | + it('replaces all occurrences for --replace/--with mode', async () => { |
| 69 | + const { vaultPath, vault } = await makeVault(); |
| 70 | + const target = await vault.store({ |
| 71 | + category: 'decisions', |
| 72 | + title: 'replace-target', |
| 73 | + content: 'old value and old value' |
| 74 | + }); |
| 75 | + |
| 76 | + await patchCommand(target.id, { |
| 77 | + vaultPath, |
| 78 | + replace: 'old', |
| 79 | + with: 'new' |
| 80 | + }); |
| 81 | + |
| 82 | + const raw = readDoc(vaultPath, target.id); |
| 83 | + const parsed = matter(raw); |
| 84 | + expect(parsed.content).toContain('new value and new value'); |
| 85 | + expect(parsed.content).not.toContain('old value'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('upserts an existing markdown section body', async () => { |
| 89 | + const { vaultPath, vault } = await makeVault(); |
| 90 | + const target = await vault.store({ |
| 91 | + category: 'projects', |
| 92 | + title: 'section-existing', |
| 93 | + content: [ |
| 94 | + 'Intro', |
| 95 | + '', |
| 96 | + '## Plan', |
| 97 | + 'Old plan', |
| 98 | + '', |
| 99 | + '## Next', |
| 100 | + 'Keep this' |
| 101 | + ].join('\n') |
| 102 | + }); |
| 103 | + |
| 104 | + await patchCommand(target.id, { |
| 105 | + vaultPath, |
| 106 | + section: '## Plan', |
| 107 | + content: 'Updated plan' |
| 108 | + }); |
| 109 | + |
| 110 | + const raw = readDoc(vaultPath, target.id); |
| 111 | + const parsed = matter(raw); |
| 112 | + expect(parsed.content).toContain('## Plan\nUpdated plan'); |
| 113 | + expect(parsed.content).toContain('## Next\nKeep this'); |
| 114 | + const headingMatches = parsed.content.match(/^## Plan$/gm) ?? []; |
| 115 | + expect(headingMatches).toHaveLength(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('upserts a missing markdown section by appending it', async () => { |
| 119 | + const { vaultPath, vault } = await makeVault(); |
| 120 | + const target = await vault.store({ |
| 121 | + category: 'projects', |
| 122 | + title: 'section-missing', |
| 123 | + content: 'Intro only' |
| 124 | + }); |
| 125 | + |
| 126 | + await patchCommand(target.id, { |
| 127 | + vaultPath, |
| 128 | + section: '## Notes', |
| 129 | + content: 'Added body' |
| 130 | + }); |
| 131 | + |
| 132 | + const raw = readDoc(vaultPath, target.id); |
| 133 | + const parsed = matter(raw); |
| 134 | + expect(parsed.content).toContain('Intro only'); |
| 135 | + expect(parsed.content.trimEnd()).toMatch(/## Notes\nAdded body$/); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('registerPatchCommand', () => { |
| 140 | + it('registers patch command and applies append mode', async () => { |
| 141 | + const { vaultPath, vault } = await makeVault(); |
| 142 | + const target = await vault.store({ |
| 143 | + category: 'inbox', |
| 144 | + title: 'cli-append', |
| 145 | + content: 'start' |
| 146 | + }); |
| 147 | + const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined); |
| 148 | + const program = new Command(); |
| 149 | + registerPatchCommand(program); |
| 150 | + |
| 151 | + await program.parseAsync( |
| 152 | + ['patch', target.id, '--append', 'finish', '--vault', vaultPath], |
| 153 | + { from: 'user' } |
| 154 | + ); |
| 155 | + |
| 156 | + const raw = readDoc(vaultPath, target.id); |
| 157 | + const parsed = matter(raw); |
| 158 | + expect(parsed.content.trimEnd()).toMatch(/start\nfinish$/); |
| 159 | + expect(logSpy).toHaveBeenCalledWith(`Patched: ${target.id}`); |
| 160 | + logSpy.mockRestore(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('throws when more than one patch mode is selected', async () => { |
| 164 | + const program = new Command(); |
| 165 | + registerPatchCommand(program); |
| 166 | + |
| 167 | + await expect( |
| 168 | + program.parseAsync( |
| 169 | + ['patch', 'decisions/test', '--append', 'one', '--replace', 'one', '--with', 'two'], |
| 170 | + { from: 'user' } |
| 171 | + ) |
| 172 | + ).rejects.toThrow( |
| 173 | + 'Select exactly one patch mode: --append, --replace with --with, or --section with --content.' |
| 174 | + ); |
| 175 | + }); |
| 176 | +}); |
0 commit comments