|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { execa } from 'execa'; |
| 4 | +import { checkRepositories, updateRepositories } from '../commands/lifecycle.js'; |
| 5 | +import { getConfig, setConfig, getUserProjectConfig } from '../config.js'; |
| 6 | +import { getCombinedProjectConfig } from '../project-config.js'; |
| 7 | +import { cloneOrUpdateRepo } from '../git.js'; |
| 8 | +import { installAllUserEntries, installEntriesForAdapter } from '../commands/install.js'; |
| 9 | + |
| 10 | +vi.mock('execa', () => ({ |
| 11 | + execa: vi.fn() |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock('../config.js', () => ({ |
| 15 | + getConfig: vi.fn(), |
| 16 | + setConfig: vi.fn(), |
| 17 | + getReposBaseDir: vi.fn(() => '/tmp/repos'), |
| 18 | + getUserProjectConfig: vi.fn() |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('../project-config.js', () => ({ |
| 22 | + getCombinedProjectConfig: vi.fn() |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../git.js', () => ({ |
| 26 | + cloneOrUpdateRepo: vi.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock('../commands/install.js', () => ({ |
| 30 | + installAllUserEntries: vi.fn(), |
| 31 | + installEntriesForAdapter: vi.fn() |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock('../adapters/index.js', () => ({ |
| 35 | + adapterRegistry: { |
| 36 | + all: vi.fn(() => [{ |
| 37 | + name: 'cursor-rules', |
| 38 | + tool: 'cursor', |
| 39 | + subtype: 'rules', |
| 40 | + configPath: ['cursor', 'rules'], |
| 41 | + defaultSourceDir: '.cursor/rules' |
| 42 | + }]) |
| 43 | + } |
| 44 | +})); |
| 45 | + |
| 46 | +describe('lifecycle check/update', () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.resetAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should detect update-available repositories', async () => { |
| 52 | + vi.mocked(getCombinedProjectConfig).mockResolvedValue({ |
| 53 | + cursor: { |
| 54 | + rules: { |
| 55 | + react: 'https://example.com/rules.git' |
| 56 | + } |
| 57 | + } |
| 58 | + } as any); |
| 59 | + vi.mocked(getConfig).mockResolvedValue({ |
| 60 | + currentRepo: 'rules', |
| 61 | + repos: { |
| 62 | + rules: { |
| 63 | + name: 'rules', |
| 64 | + url: 'https://example.com/rules.git', |
| 65 | + path: '/tmp/rules' |
| 66 | + } |
| 67 | + } |
| 68 | + } as any); |
| 69 | + |
| 70 | + vi.spyOn(fs, 'pathExists').mockImplementation(async (target: fs.PathLike) => { |
| 71 | + const value = String(target); |
| 72 | + return value === '/tmp/rules' || value === '/tmp/rules/.git'; |
| 73 | + }); |
| 74 | + |
| 75 | + const execaMock = vi.mocked(execa as any); |
| 76 | + execaMock.mockImplementation(async (_cmd: string, args: string[]) => { |
| 77 | + const command = args.join(' '); |
| 78 | + if (command === 'fetch --quiet') return { stdout: '' }; |
| 79 | + if (command === 'rev-parse --short HEAD') return { stdout: 'abc1234' }; |
| 80 | + if (command === 'rev-parse --abbrev-ref --symbolic-full-name @{u}') return { stdout: 'origin/main' }; |
| 81 | + if (command === 'rev-list --left-right --count HEAD...@{u}') return { stdout: '0\t2' }; |
| 82 | + throw new Error(`Unexpected git command: ${command}`); |
| 83 | + }); |
| 84 | + |
| 85 | + const result = await checkRepositories({ |
| 86 | + projectPath: '/tmp/project' |
| 87 | + }); |
| 88 | + |
| 89 | + expect(result.total).toBe(1); |
| 90 | + expect(result.updateAvailable).toBe(1); |
| 91 | + expect(result.entries[0]).toMatchObject({ |
| 92 | + repoUrl: 'https://example.com/rules.git', |
| 93 | + status: 'update-available', |
| 94 | + ahead: 0, |
| 95 | + behind: 2 |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should preview updates in dry-run mode without pulling', async () => { |
| 100 | + vi.mocked(getCombinedProjectConfig).mockResolvedValue({ |
| 101 | + cursor: { |
| 102 | + rules: { |
| 103 | + react: 'https://example.com/rules.git' |
| 104 | + } |
| 105 | + } |
| 106 | + } as any); |
| 107 | + vi.mocked(getUserProjectConfig).mockResolvedValue({} as any); |
| 108 | + vi.mocked(getConfig).mockResolvedValue({ |
| 109 | + currentRepo: 'rules', |
| 110 | + repos: { |
| 111 | + rules: { |
| 112 | + name: 'rules', |
| 113 | + url: 'https://example.com/rules.git', |
| 114 | + path: '/tmp/rules' |
| 115 | + } |
| 116 | + } |
| 117 | + } as any); |
| 118 | + |
| 119 | + vi.spyOn(fs, 'pathExists').mockImplementation(async (target: fs.PathLike) => { |
| 120 | + const value = String(target); |
| 121 | + return value === '/tmp/rules' || value === '/tmp/rules/.git'; |
| 122 | + }); |
| 123 | + |
| 124 | + const execaMock = vi.mocked(execa as any); |
| 125 | + execaMock.mockImplementation(async (_cmd: string, args: string[]) => { |
| 126 | + const command = args.join(' '); |
| 127 | + if (command === 'fetch --quiet') return { stdout: '' }; |
| 128 | + if (command === 'rev-parse --short HEAD') return { stdout: 'abc1234' }; |
| 129 | + if (command === 'rev-parse --abbrev-ref --symbolic-full-name @{u}') return { stdout: 'origin/main' }; |
| 130 | + if (command === 'rev-list --left-right --count HEAD...@{u}') return { stdout: '0\t1' }; |
| 131 | + throw new Error(`Unexpected git command: ${command}`); |
| 132 | + }); |
| 133 | + |
| 134 | + const result = await updateRepositories({ |
| 135 | + projectPath: '/tmp/project', |
| 136 | + dryRun: true |
| 137 | + }); |
| 138 | + |
| 139 | + expect(result.dryRun).toBe(true); |
| 140 | + expect(result.total).toBe(1); |
| 141 | + expect(result.entries[0]).toMatchObject({ |
| 142 | + repoUrl: 'https://example.com/rules.git', |
| 143 | + action: 'would-update' |
| 144 | + }); |
| 145 | + expect(cloneOrUpdateRepo).not.toHaveBeenCalled(); |
| 146 | + expect(setConfig).not.toHaveBeenCalled(); |
| 147 | + expect(installAllUserEntries).not.toHaveBeenCalled(); |
| 148 | + expect(installEntriesForAdapter).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | +}); |
0 commit comments