mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
398 lines
15 KiB
TypeScript
398 lines
15 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach, vi } from 'vitest'
|
|
import type { Database } from 'bun:sqlite'
|
|
import { execSync } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import { z } from 'zod'
|
|
import { createRepo } from '../../src/db/queries'
|
|
import { createGitService } from '../../src/services/git/GitService'
|
|
import { ProjectConfigService } from '../../src/services/project-config'
|
|
import { SecretsService } from '../../src/services/secrets'
|
|
import { RepoEnvService } from '../../src/services/repo-env'
|
|
import { deleteRepoFiles } from '../../src/services/repo'
|
|
import { invalidateRepoEnvCache } from '../../src/utils/repo-env-cache'
|
|
import { RepoProjectFileSchema } from '@opencode-manager/shared/schemas'
|
|
import type { Repo } from '@opencode-manager/shared/types'
|
|
import { createTestDatabase } from '../helpers/test-database'
|
|
import { createGitAuthService } from '../helpers/git-fixtures'
|
|
import { expectLoggedMessage } from '../helpers/log-assertions'
|
|
|
|
type RepoProjectFileInput = z.input<typeof RepoProjectFileSchema>
|
|
|
|
const gitAuthService = createGitAuthService()
|
|
|
|
const EMPTY = { env: {}, maskValues: [] }
|
|
|
|
let probeDir: string
|
|
let probeLogPath: string
|
|
let previousPath: string | undefined
|
|
let previousLogLevel: string | undefined
|
|
|
|
function topLevelProbes(): number {
|
|
try {
|
|
return fs.readFileSync(probeLogPath, 'utf-8')
|
|
.split('\n')
|
|
.filter((line) => line.includes('--show-toplevel'))
|
|
.length
|
|
} catch {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
beforeAll(() => {
|
|
probeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocm-git-probe-'))
|
|
probeLogPath = path.join(probeDir, 'git.log')
|
|
const realGitPath = execSync('command -v git', { shell: '/bin/sh' }).toString().trim()
|
|
const wrapperPath = path.join(probeDir, 'git')
|
|
fs.writeFileSync(wrapperPath, `#!/bin/sh\nprintf '%s\\n' "$*" >> '${probeLogPath}'\nexec '${realGitPath}' "$@"\n`)
|
|
fs.chmodSync(wrapperPath, 0o755)
|
|
previousPath = process.env.PATH
|
|
process.env.PATH = `${probeDir}:${previousPath ?? ''}`
|
|
previousLogLevel = process.env.LOG_LEVEL
|
|
process.env.LOG_LEVEL = 'error'
|
|
})
|
|
|
|
afterAll(() => {
|
|
if (previousPath === undefined) delete process.env.PATH
|
|
else process.env.PATH = previousPath
|
|
if (previousLogLevel === undefined) delete process.env.LOG_LEVEL
|
|
else process.env.LOG_LEVEL = previousLogLevel
|
|
fs.rmSync(probeDir, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('RepoEnvService', () => {
|
|
let db: Database
|
|
let projectConfigService: ProjectConfigService
|
|
let secretsService: SecretsService
|
|
let repoEnvService: RepoEnvService
|
|
let tempDirs: string[]
|
|
|
|
function createTempDir(prefix: string): string {
|
|
const dir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), prefix)))
|
|
tempDirs.push(dir)
|
|
return dir
|
|
}
|
|
|
|
function initGitRepo(dir: string, message = 'Initial commit'): void {
|
|
execSync(`git init "${dir}"`)
|
|
execSync(`git -C "${dir}" config user.email test@test.com`)
|
|
execSync(`git -C "${dir}" config user.name Test`)
|
|
execSync(`git -C "${dir}" commit --allow-empty -m "${message}"`)
|
|
}
|
|
|
|
function writeProjectFile(dir: string, content: RepoProjectFileInput): void {
|
|
const filePath = path.join(dir, '.ocm', 'project.json')
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
|
fs.writeFileSync(filePath, JSON.stringify(content, null, 2))
|
|
}
|
|
|
|
function registerRepo(dir: string): Repo {
|
|
return createRepo(db, {
|
|
localPath: dir,
|
|
sourcePath: dir,
|
|
defaultBranch: 'main',
|
|
cloneStatus: 'ready',
|
|
clonedAt: Date.now(),
|
|
isLocal: true,
|
|
})
|
|
}
|
|
|
|
async function trust(dir: string, repo: Repo): Promise<void> {
|
|
const hash = projectConfigService.readRepoFile(dir).hash
|
|
await projectConfigService.trustRepoFile(repo, dir, hash ?? '')
|
|
}
|
|
|
|
beforeEach(() => {
|
|
fs.writeFileSync(probeLogPath, '')
|
|
db = createTestDatabase()
|
|
projectConfigService = new ProjectConfigService(db, createGitService(gitAuthService), gitAuthService)
|
|
secretsService = new SecretsService(db)
|
|
repoEnvService = new RepoEnvService(db, projectConfigService, secretsService)
|
|
tempDirs = []
|
|
})
|
|
|
|
afterEach(() => {
|
|
db.close()
|
|
for (const dir of tempDirs) {
|
|
fs.rmSync(dir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('resolves a trusted mapping with secret precedence and the selected environment', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, {
|
|
version: 1,
|
|
environment: 'prod',
|
|
env: {
|
|
API_URL: 'https://${{ vars.HOST }}/${{ secrets.TOKEN }}',
|
|
PLAIN: 'literal',
|
|
},
|
|
})
|
|
secretsService.set('variable', repo.id, 'prod', 'HOST', 'example.com')
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'repo-token')
|
|
secretsService.set('secret', repo.id, 'prod', 'TOKEN', 'prod-token')
|
|
await trust(root, repo)
|
|
|
|
const result = await repoEnvService.resolveForDirectory(root)
|
|
|
|
expect(result.env).toEqual({
|
|
API_URL: 'https://example.com/prod-token',
|
|
PLAIN: 'literal',
|
|
})
|
|
expect(result.maskValues).toEqual(['prod-token'])
|
|
})
|
|
|
|
it('returns an empty result for an untrusted mapping', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { TOKEN: '${{ secrets.TOKEN }}' } })
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'prod-token')
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
})
|
|
|
|
it('returns an empty result when the file has no env mapping', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, projectActions: [{ id: 'serve', name: 'Serve', command: 'pnpm dev' }] })
|
|
await trust(root, repo)
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
})
|
|
|
|
it('resolves from a subdirectory of the checkout', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { PLAIN: 'literal' } })
|
|
await trust(root, repo)
|
|
const nested = path.join(root, 'packages', 'app')
|
|
fs.mkdirSync(nested, { recursive: true })
|
|
|
|
const result = await repoEnvService.resolveForDirectory(nested)
|
|
|
|
expect(result.env).toEqual({ PLAIN: 'literal' })
|
|
})
|
|
|
|
it('ignores an unregistered nested git root inside a registered repository', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'root-token')
|
|
const nested = path.join(root, 'packages', 'nested')
|
|
fs.mkdirSync(nested, { recursive: true })
|
|
initGitRepo(nested, 'Nested initial commit')
|
|
writeProjectFile(nested, { version: 1, env: { TOKEN: '${{ secrets.TOKEN }}' } })
|
|
await trust(nested, repo)
|
|
|
|
expect(await repoEnvService.resolveForDirectory(nested)).toEqual(EMPTY)
|
|
})
|
|
|
|
it('resolves a git worktree outside the repository folder to the main repo', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { PLAIN: 'literal' } })
|
|
execSync(`git -C "${root}" add -A`)
|
|
execSync(`git -C "${root}" commit -m "Add project config"`)
|
|
const repo = registerRepo(root)
|
|
await trust(root, repo)
|
|
const worktreePath = createTempDir('ocm-repo-env-worktree-')
|
|
fs.rmSync(worktreePath, { recursive: true, force: true })
|
|
execSync(`git -C "${root}" worktree add -b feature "${worktreePath}"`)
|
|
|
|
const result = await repoEnvService.resolveForDirectory(worktreePath)
|
|
|
|
expect(result.env).toEqual({ PLAIN: 'literal' })
|
|
})
|
|
|
|
it('does not resolve a separate clone of the same origin to the registered repo', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
execSync(`git -C "${root}" remote add origin https://github.com/test/shared.git`)
|
|
writeProjectFile(root, { version: 1, env: { TOKEN: '${{ secrets.TOKEN }}' } })
|
|
const repo = registerRepo(root)
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'registered-token')
|
|
await trust(root, repo)
|
|
|
|
const clone = createTempDir('ocm-repo-env-clone-')
|
|
initGitRepo(clone)
|
|
execSync(`git -C "${clone}" remote add origin https://github.com/test/shared.git`)
|
|
writeProjectFile(clone, { version: 1, env: { TOKEN: '${{ secrets.TOKEN }}' } })
|
|
|
|
expect(await repoEnvService.resolveForDirectory(clone)).toEqual(EMPTY)
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual({
|
|
env: { TOKEN: 'registered-token' },
|
|
maskValues: ['registered-token'],
|
|
})
|
|
})
|
|
|
|
it('returns an empty result for a non-git directory', async () => {
|
|
const plain = createTempDir('ocm-repo-env-plain-')
|
|
|
|
expect(await repoEnvService.resolveForDirectory(plain)).toEqual(EMPTY)
|
|
})
|
|
|
|
it('resolves missing references to an empty string', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, {
|
|
version: 1,
|
|
env: { MISSING: '${{ secrets.NOPE }}-${{ vars.NOPE2 }}' },
|
|
})
|
|
await trust(root, repo)
|
|
|
|
const result = await repoEnvService.resolveForDirectory(root)
|
|
|
|
expect(result.env).toEqual({ MISSING: '-' })
|
|
})
|
|
|
|
it('excludes vars and short secret values from maskValues', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, {
|
|
version: 1,
|
|
env: {
|
|
LONG: '${{ secrets.LONG }}',
|
|
SHORT: '${{ secrets.SHORT }}',
|
|
FROM_VAR: '${{ vars.VAR }}',
|
|
},
|
|
})
|
|
secretsService.set('secret', repo.id, null, 'LONG', 'longvalue')
|
|
secretsService.set('secret', repo.id, null, 'SHORT', 'abc')
|
|
secretsService.set('variable', repo.id, null, 'VAR', 'variablevalue')
|
|
await trust(root, repo)
|
|
|
|
const result = await repoEnvService.resolveForDirectory(root)
|
|
|
|
expect(result.maskValues).toEqual(['longvalue'])
|
|
})
|
|
|
|
it('returns an empty result and warns when the secrets resolver fails', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { TOKEN: '${{ secrets.TOKEN }}' } })
|
|
await trust(root, repo)
|
|
vi.spyOn(secretsService, 'resolveValues').mockImplementation(() => {
|
|
throw new Error('boom')
|
|
})
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
expectLoggedMessage(root, 'warn')
|
|
})
|
|
|
|
describe('caching', () => {
|
|
async function createTrustedRepo(env: Record<string, string>): Promise<{ root: string; repo: Repo }> {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, env })
|
|
await trust(root, repo)
|
|
return { root, repo }
|
|
}
|
|
|
|
it('reuses the directory resolution within the TTL without spawning git', async () => {
|
|
const { root } = await createTrustedRepo({ PLAIN: 'literal' })
|
|
|
|
await repoEnvService.resolveForDirectory(root)
|
|
const probes = topLevelProbes()
|
|
expect(probes).toBeGreaterThan(0)
|
|
|
|
const result = await repoEnvService.resolveForDirectory(root)
|
|
|
|
expect(result.env).toEqual({ PLAIN: 'literal' })
|
|
expect(topLevelProbes()).toBe(probes)
|
|
})
|
|
|
|
it('caches misses for directories without a repository', async () => {
|
|
const plain = createTempDir('ocm-repo-env-plain-')
|
|
|
|
expect(await repoEnvService.resolveForDirectory(plain)).toEqual(EMPTY)
|
|
const probes = topLevelProbes()
|
|
|
|
expect(await repoEnvService.resolveForDirectory(plain)).toEqual(EMPTY)
|
|
expect(topLevelProbes()).toBe(probes)
|
|
})
|
|
|
|
it('re-reads the project file when it changes', async () => {
|
|
const { root, repo } = await createTrustedRepo({ PLAIN: 'first' })
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'first' })
|
|
const probes = topLevelProbes()
|
|
|
|
writeProjectFile(root, { version: 1, env: { PLAIN: 'second-value' } })
|
|
await trust(root, repo)
|
|
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'second-value' })
|
|
expect(topLevelProbes()).toBe(probes)
|
|
})
|
|
|
|
it('applies a trust decision made after the file was cached', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
const repo = registerRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { PLAIN: 'literal' } })
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
|
|
await trust(root, repo)
|
|
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'literal' })
|
|
})
|
|
|
|
it('applies secret edits made after the directory was cached', async () => {
|
|
const { root, repo } = await createTrustedRepo({ TOKEN: '${{ secrets.TOKEN }}' })
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'first-token')
|
|
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ TOKEN: 'first-token' })
|
|
|
|
secretsService.set('secret', repo.id, null, 'TOKEN', 'second-token')
|
|
|
|
const result = await repoEnvService.resolveForDirectory(root)
|
|
expect(result.env).toEqual({ TOKEN: 'second-token' })
|
|
expect(result.maskValues).toEqual(['second-token'])
|
|
})
|
|
|
|
it('invalidates the cached miss when the repository is registered', async () => {
|
|
const root = createTempDir('ocm-repo-env-')
|
|
initGitRepo(root)
|
|
writeProjectFile(root, { version: 1, env: { PLAIN: 'literal' } })
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
|
|
const repo = registerRepo(root)
|
|
await trust(root, repo)
|
|
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'literal' })
|
|
})
|
|
|
|
it('invalidates the cached resolution when the repository is deleted', async () => {
|
|
const { root, repo } = await createTrustedRepo({ PLAIN: 'literal' })
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'literal' })
|
|
const probes = topLevelProbes()
|
|
|
|
await deleteRepoFiles(db, repo.id)
|
|
|
|
expect(await repoEnvService.resolveForDirectory(root)).toEqual(EMPTY)
|
|
expect(topLevelProbes()).toBeGreaterThan(probes)
|
|
})
|
|
|
|
it('invalidates the cached resolution when the repo environment cache is reset', async () => {
|
|
const { root } = await createTrustedRepo({ PLAIN: 'literal' })
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'literal' })
|
|
const probes = topLevelProbes()
|
|
|
|
invalidateRepoEnvCache()
|
|
|
|
expect((await repoEnvService.resolveForDirectory(root)).env).toEqual({ PLAIN: 'literal' })
|
|
expect(topLevelProbes()).toBeGreaterThan(probes)
|
|
})
|
|
})
|
|
})
|