mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
269 lines
12 KiB
TypeScript
269 lines
12 KiB
TypeScript
import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { join } from 'path'
|
|
import { tmpdir } from 'os'
|
|
import { mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile } from 'fs/promises'
|
|
import type { Database } from 'bun:sqlite'
|
|
import { z } from 'zod'
|
|
|
|
vi.mock('@opencode-manager/shared/config/env', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@opencode-manager/shared/config/env')>()
|
|
return {
|
|
...actual,
|
|
FILE_LIMITS: {
|
|
MAX_SIZE_BYTES: 1024 * 1024,
|
|
MAX_UPLOAD_SIZE_BYTES: 500,
|
|
},
|
|
}
|
|
})
|
|
|
|
const mockUpsertDefaultOpenCodeConfig = vi.fn()
|
|
|
|
vi.mock('../../src/services/settings', () => ({
|
|
SettingsService: vi.fn(() => ({
|
|
upsertDefaultOpenCodeConfig: mockUpsertDefaultOpenCodeConfig,
|
|
})),
|
|
}))
|
|
|
|
import { SettingsService } from '../../src/services/settings'
|
|
|
|
interface TestUploadFile {
|
|
relativePath: string
|
|
content: Buffer
|
|
}
|
|
|
|
function payload(files: Array<[string, string]>): TestUploadFile[] {
|
|
return files.map(([relativePath, content]) => ({
|
|
relativePath,
|
|
content: Buffer.from(content),
|
|
}))
|
|
}
|
|
|
|
async function listParentEntries(parent: string): Promise<string[]> {
|
|
try {
|
|
return await readdir(parent)
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
function createConfigZodError(): z.ZodError {
|
|
try {
|
|
z.object({ theme: z.string() }).parse({ theme: 42 })
|
|
} catch (error) {
|
|
return error as z.ZodError
|
|
}
|
|
throw new Error('expected schema parse to fail')
|
|
}
|
|
|
|
describe('replaceOpenCodeConfigDirectory', () => {
|
|
let tempDir: string
|
|
let configDir: string
|
|
let mockDb: Database
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks()
|
|
tempDir = await mkdtemp(join(tmpdir(), 'oc-config-dir-test-'))
|
|
configDir = join(tempDir, '.config', 'opencode')
|
|
vi.spyOn(await import('@opencode-manager/shared/config/env'), 'getConfigPath').mockReturnValue(configDir)
|
|
mockDb = { query: vi.fn() } as unknown as Database
|
|
mockUpsertDefaultOpenCodeConfig.mockReturnValue({ name: 'default', isDefault: true })
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await rm(tempDir, { recursive: true, force: true })
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('replaces the directory with the uploaded tree, normalizing the config and restoring executables', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(join(configDir, 'agents'), { recursive: true })
|
|
await writeFile(join(configDir, 'agents', 'old.md'), 'old')
|
|
|
|
const jsonc = `{
|
|
// comment
|
|
"$schema": "https://opencode.ai/config.json",
|
|
"theme": "dark"
|
|
}`
|
|
const result = await replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['my-config/opencode.jsonc', jsonc],
|
|
['my-config/AGENTS.md', '# Project'],
|
|
['my-config/agents/team/planner.md', '# Planner'],
|
|
['my-config/commands/deploy.md', '# Deploy'],
|
|
['my-config/skills/quo-api/SKILL.md', '---\nname: quo-api\n---\nBody'],
|
|
['my-config/skills/quo-api/scripts/quo-spec.sh', '#!/usr/bin/env bash\necho hi'],
|
|
['my-config/plugin/opencode-forge/dist/index.js', 'console.log(1)'],
|
|
['my-config/vendor/x.js', 'var x = 1'],
|
|
['my-config/postgres-mcp-manager.sh', '#!/bin/sh\npsql'],
|
|
]))
|
|
|
|
expect(SettingsService).toHaveBeenCalledWith(mockDb)
|
|
expect(mockUpsertDefaultOpenCodeConfig).toHaveBeenCalledWith(jsonc, 'default')
|
|
expect(result.configDirectory).toBe(configDir)
|
|
expect(result.configSourceFilename).toBe('opencode.jsonc')
|
|
expect(result.filesInstalled).toEqual([
|
|
'opencode.json',
|
|
'AGENTS.md',
|
|
'agents/team/planner.md',
|
|
'commands/deploy.md',
|
|
'skills/quo-api/SKILL.md',
|
|
'skills/quo-api/scripts/quo-spec.sh',
|
|
'plugin/opencode-forge/dist/index.js',
|
|
'vendor/x.js',
|
|
'postgres-mcp-manager.sh',
|
|
])
|
|
expect(result.executablesRestored).toEqual(['skills/quo-api/scripts/quo-spec.sh', 'postgres-mcp-manager.sh'])
|
|
expect(result.skippedPaths).toEqual([])
|
|
expect(result.preservedEntries).toEqual([])
|
|
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe(jsonc)
|
|
await expect(readFile(join(configDir, 'opencode.jsonc'), 'utf8')).rejects.toThrow()
|
|
expect(await readFile(join(configDir, 'skills/quo-api/scripts/quo-spec.sh'), 'utf8')).toBe('#!/usr/bin/env bash\necho hi')
|
|
expect(await readFile(join(configDir, 'postgres-mcp-manager.sh'), 'utf8')).toBe('#!/bin/sh\npsql')
|
|
expect(await readFile(join(configDir, 'plugin/opencode-forge/dist/index.js'), 'utf8')).toBe('console.log(1)')
|
|
|
|
expect((await stat(join(configDir, 'skills/quo-api/scripts/quo-spec.sh'))).mode & 0o111).toBe(0o111)
|
|
expect((await stat(join(configDir, 'postgres-mcp-manager.sh'))).mode & 0o111).toBe(0o111)
|
|
expect((await stat(join(configDir, 'AGENTS.md'))).mode & 0o111).toBe(0)
|
|
|
|
await expect(readFile(join(configDir, 'agents', 'old.md'), 'utf8')).rejects.toThrow()
|
|
|
|
const parentEntries = await listParentEntries(join(tempDir, '.config'))
|
|
expect(parentEntries.filter((name) => name.startsWith('.opencode-config-staging-') || name.startsWith('.opencode-config-backup-'))).toEqual([])
|
|
})
|
|
|
|
it('prefers opencode.json and reports the uploaded opencode.jsonc as skipped', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
const result = await replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['opencode.json', '{"theme":"dark"}'],
|
|
['opencode.jsonc', '{ // c\n"theme":"light"\n}'],
|
|
]))
|
|
|
|
expect(result.configSourceFilename).toBe('opencode.json')
|
|
expect(result.skippedPaths).toEqual(['opencode.jsonc'])
|
|
expect(mockUpsertDefaultOpenCodeConfig).toHaveBeenCalledWith('{"theme":"dark"}', 'default')
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"dark"}')
|
|
await expect(readFile(join(configDir, 'opencode.jsonc'), 'utf8')).rejects.toThrow()
|
|
})
|
|
|
|
it('preserves an existing node_modules directory while skipping uploaded excluded entries', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(join(configDir, 'node_modules', 'dep'), { recursive: true })
|
|
await writeFile(join(configDir, 'node_modules', 'dep', 'index.js'), 'module.exports = 1')
|
|
|
|
const result = await replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['opencode.json', '{"theme":"dark"}'],
|
|
['node_modules/installed.js', 'x'],
|
|
['plugin/pkg/node_modules/inner.js', 'y'],
|
|
['.git/config', '[core]'],
|
|
['.DS_Store', 'junk'],
|
|
['agents/team.md', '# Team'],
|
|
]))
|
|
|
|
expect(result.preservedEntries).toEqual(['node_modules'])
|
|
expect(result.skippedPaths).toEqual([
|
|
'node_modules/installed.js',
|
|
'plugin/pkg/node_modules/inner.js',
|
|
'.git/config',
|
|
'.DS_Store',
|
|
])
|
|
expect(await readFile(join(configDir, 'node_modules', 'dep', 'index.js'), 'utf8')).toBe('module.exports = 1')
|
|
await expect(readFile(join(configDir, 'node_modules', 'installed.js'), 'utf8')).rejects.toThrow()
|
|
await expect(readFile(join(configDir, '.git', 'config'), 'utf8')).rejects.toThrow()
|
|
await expect(readFile(join(configDir, '.DS_Store'), 'utf8')).rejects.toThrow()
|
|
})
|
|
|
|
it('rejects a payload with no root config file, leaving the old directory untouched', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(join(configDir, 'agents'), { recursive: true })
|
|
await writeFile(join(configDir, 'agents', 'existing.md'), 'keep me')
|
|
await writeFile(join(configDir, 'opencode.json'), '{"theme":"old"}')
|
|
|
|
await expect(replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['AGENTS.md', '# Project'],
|
|
['agents/planner.md', '# Planner'],
|
|
]))).rejects.toThrow('Uploaded directory must contain opencode.json or opencode.jsonc at its root')
|
|
|
|
expect(mockUpsertDefaultOpenCodeConfig).not.toHaveBeenCalled()
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"old"}')
|
|
expect(await readFile(join(configDir, 'agents', 'existing.md'), 'utf8')).toBe('keep me')
|
|
const parentEntries = await listParentEntries(join(tempDir, '.config'))
|
|
expect(parentEntries.filter((name) => name.startsWith('.opencode-config-staging-') || name.startsWith('.opencode-config-backup-'))).toEqual([])
|
|
})
|
|
|
|
it('rejects when the config fails validation before touching disk', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
mockUpsertDefaultOpenCodeConfig.mockImplementationOnce(() => {
|
|
throw createConfigZodError()
|
|
})
|
|
await mkdir(join(configDir, 'agents'), { recursive: true })
|
|
await writeFile(join(configDir, 'agents', 'existing.md'), 'keep me')
|
|
await writeFile(join(configDir, 'opencode.json'), '{"theme":"old"}')
|
|
|
|
const error = await replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['opencode.json', '{"theme": 42}'],
|
|
['agents/planner.md', '# Planner'],
|
|
])).catch((caught) => caught)
|
|
|
|
expect(error).toBeInstanceOf(z.ZodError)
|
|
expect(mockUpsertDefaultOpenCodeConfig).toHaveBeenCalledWith('{"theme": 42}', 'default')
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"old"}')
|
|
expect(await readFile(join(configDir, 'agents', 'existing.md'), 'utf8')).toBe('keep me')
|
|
const parentEntries = await listParentEntries(join(tempDir, '.config'))
|
|
expect(parentEntries.filter((name) => name.startsWith('.opencode-config-staging-') || name.startsWith('.opencode-config-backup-'))).toEqual([])
|
|
})
|
|
|
|
it('rejects a path containing .. before any disk mutation', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(configDir, { recursive: true })
|
|
await writeFile(join(configDir, 'opencode.json'), '{"theme":"old"}')
|
|
|
|
await expect(replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['opencode.json', '{"theme":"new"}'],
|
|
['../escape.md', 'x'],
|
|
]))).rejects.toThrow('Path must not contain ".."')
|
|
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"old"}')
|
|
expect(mockUpsertDefaultOpenCodeConfig).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('rejects payloads exceeding the upload size limit', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(configDir, { recursive: true })
|
|
await writeFile(join(configDir, 'opencode.json'), '{"theme":"old"}')
|
|
|
|
const big = 'x'.repeat(600)
|
|
await expect(replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['opencode.json', '{"theme":"new"}'],
|
|
['plugin/opencode-forge/dist/big.js', big],
|
|
]))).rejects.toThrow('Uploaded config directory files exceed maximum upload size')
|
|
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"old"}')
|
|
expect(mockUpsertDefaultOpenCodeConfig).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('rejects payloads with more files than the configured maximum', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await mkdir(configDir, { recursive: true })
|
|
await writeFile(join(configDir, 'opencode.json'), '{"theme":"old"}')
|
|
|
|
const tooMany = Array.from({ length: 5001 }, (_, index) => [`file${index}.md`, '# x'] as [string, string])
|
|
await expect(replaceOpenCodeConfigDirectory(mockDb, payload(tooMany)))
|
|
.rejects.toThrow('Uploaded config directory contains too many files (max 5000)')
|
|
|
|
expect(await readFile(join(configDir, 'opencode.json'), 'utf8')).toBe('{"theme":"old"}')
|
|
expect(mockUpsertDefaultOpenCodeConfig).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('rejects when every uploaded entry is excluded', async () => {
|
|
const { replaceOpenCodeConfigDirectory } = await import('../../src/services/opencode-config-directory')
|
|
await expect(replaceOpenCodeConfigDirectory(mockDb, payload([
|
|
['.DS_Store', 'junk'],
|
|
['node_modules/x.js', 'x'],
|
|
]))).rejects.toThrow('No files were provided for the OpenCode config directory replace')
|
|
|
|
expect(mockUpsertDefaultOpenCodeConfig).not.toHaveBeenCalled()
|
|
const parentEntries = await listParentEntries(join(tempDir, '.config'))
|
|
expect(parentEntries.filter((name) => name.startsWith('.opencode-config-staging-') || name.startsWith('.opencode-config-backup-'))).toEqual([])
|
|
})
|
|
})
|