mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Refactor SSE event handling and consolidate notification contexts
This commit is contained in:
1 parent
9851ec06e0
commit
f7fd3176d4
30 files changed
+2226
-988
No files matched your search
Vendored
+46
@@ -0,0 +1,46 @@
|
||||
import { mkdir, writeFile, rm } from 'fs/promises'
|
||||
import { join } from 'path'
|
||||
import { executeCommand } from '../../src/utils/process'
|
||||
|
||||
export function gitReuseDir(name: string): string {
|
||||
const base = process.env.TMPDIR || '/tmp'
|
||||
return join(base, `opencode-test-${name}`)
|
||||
}
|
||||
|
||||
export async function setupGitTestRepo(repoPath: string): Promise<void> {
|
||||
try {
|
||||
await rm(repoPath, { recursive: true, force: true })
|
||||
} catch {
|
||||
// Ignore if dir doesn't exist
|
||||
}
|
||||
|
||||
await mkdir(repoPath, { recursive: true })
|
||||
await executeCommand(['git', 'init'], { cwd: repoPath })
|
||||
await executeCommand(['git', 'config', 'user.name', 'Test User'], { cwd: repoPath })
|
||||
await executeCommand(['git', 'config', 'user.email', 'test@example.com'], { cwd: repoPath })
|
||||
await executeCommand(['git', 'config', 'commit.gpgSign', 'false'], { cwd: repoPath })
|
||||
}
|
||||
|
||||
export async function cleanupTestRepo(repoPath: string): Promise<void> {
|
||||
try {
|
||||
await rm(repoPath, { recursive: true, force: true })
|
||||
} catch {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
|
||||
export async function createTestFile(repoPath: string, fileName: string, content: string): Promise<void> {
|
||||
const filePath = join(repoPath, fileName)
|
||||
const dir = join(repoPath, fileName.split('/').slice(0, -1).join('/'))
|
||||
|
||||
if (dir !== repoPath) {
|
||||
await mkdir(dir, { recursive: true })
|
||||
}
|
||||
|
||||
await writeFile(filePath, content, 'utf-8')
|
||||
}
|
||||
|
||||
export async function stageAndCommitTestFile(repoPath: string, fileName: string): Promise<void> {
|
||||
await executeCommand(['git', 'add', fileName], { cwd: repoPath })
|
||||
await executeCommand(['git', 'commit', '-m', `Add ${fileName}`], { cwd: repoPath })
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
||||
import { gitReuseDir, setupGitTestRepo, cleanupTestRepo, createTestFile, stageAndCommitTestFile } from '../fixtures/git-helpers'
|
||||
|
||||
vi.mock('bun:sqlite', () => ({
|
||||
Database: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('../../src/services/settings', () => ({
|
||||
SettingsService: vi.fn()
|
||||
}))
|
||||
|
||||
import { getFileDiff } from '../../src/services/git-operations'
|
||||
|
||||
const testRepoPath = gitReuseDir('git-operations-test')
|
||||
|
||||
describe('getFileDiff', () => {
|
||||
beforeAll(async () => {
|
||||
await setupGitTestRepo(testRepoPath)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanupTestRepo(testRepoPath)
|
||||
})
|
||||
|
||||
describe('untracked file diff', () => {
|
||||
it('should extract diff from git --no-index output when exit code 1', async () => {
|
||||
const fileName = 'new-file.txt'
|
||||
const content = 'First line\nSecond line\nThird line\n'
|
||||
await createTestFile(testRepoPath, fileName, content)
|
||||
|
||||
const result = await getFileDiff(testRepoPath, fileName)
|
||||
|
||||
expect(result.status).toBe('untracked')
|
||||
expect(result.diff).toBeTruthy()
|
||||
expect(result.diff).toContain('diff --git')
|
||||
expect(result.diff).toContain(`a/${fileName}`)
|
||||
expect(result.diff).toContain(`b/${fileName}`)
|
||||
expect(result.additions).toBe(3)
|
||||
expect(result.deletions).toBe(0)
|
||||
expect(result.isBinary).toBe(false)
|
||||
})
|
||||
|
||||
it('should handle new file with multiple additions', async () => {
|
||||
const fileName = 'new-code-file.js'
|
||||
const content = 'function hello() {\n console.log("Hello world");\n return true;\n}\n'
|
||||
await createTestFile(testRepoPath, fileName, content)
|
||||
|
||||
const result = await getFileDiff(testRepoPath, fileName)
|
||||
|
||||
expect(result.status).toBe('untracked')
|
||||
expect(result.additions).toBeGreaterThan(0)
|
||||
expect(result.diff).toContain('diff --git')
|
||||
})
|
||||
})
|
||||
|
||||
describe('tracked file diff', () => {
|
||||
it('should get diff for modified tracked file', async () => {
|
||||
const fileName = 'modified-file.txt'
|
||||
const originalContent = 'Original line 1\nOriginal line 2\n'
|
||||
await createTestFile(testRepoPath, fileName, originalContent)
|
||||
await stageAndCommitTestFile(testRepoPath, fileName)
|
||||
|
||||
const modifiedContent = 'Original line 1\nModified line 2\nNew line 3\n'
|
||||
await createTestFile(testRepoPath, fileName, modifiedContent)
|
||||
|
||||
const result = await getFileDiff(testRepoPath, fileName)
|
||||
|
||||
expect(result.status).toBe('modified')
|
||||
expect(result.diff).toBeTruthy()
|
||||
expect(result.diff).toContain('-Original line 2')
|
||||
expect(result.diff).toContain('+Modified line 2')
|
||||
expect(result.diff).toContain('+New line 3')
|
||||
expect(result.additions).toBe(2)
|
||||
expect(result.deletions).toBe(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user