Files
opencode-manager/backend/test/utils/env-expressions.test.ts
T
Chris Scott e0331b43bf feat(secrets): repository secrets and variables injected into agent shells and terminals
Add a GitHub Actions-style store for secrets and variables so tests can read
credentials from the environment without .env files in the repo.

- secrets table keyed by kind, repo (NULL for global) and environment;
  secrets are encrypted with encryptSecret and write-only, variables are plain
  text. Names follow GitHub's rules and reject Manager-reserved names.
- Global (/api/secrets, /api/variables) and repo
  (/api/repos/:id/secrets|variables) routes.
- The trusted .ocm/project.json gains environment and env fields that map
  names to ${{ secrets.X }} / ${{ vars.X }} expressions. Both are part of the
  trust hash; files without them hash as before, so existing trust survives.
- RepoEnvService.resolveForDirectory resolves the repo and approved mapping
  for any directory, including worktrees outside the repo folder, with
  environment > repo > global precedence. It never throws.
- /api/internal/shell-env replaces /git-credentials/gh-env and feeds the
  ocm-gh-env plugin, the sandbox shell plan and TerminalService.create.
- The plugin masks injected secret values as *** in completed tool results.
- The sandbox shell plan is now the only source of the guest environment: the
  plugin pins OCM_SANDBOX_FORWARD_ENV and the shim forwards only those names.
2026-10-23 22:59:57 -04:00

93 lines
3.2 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { interpolateEnvExpressions, parseEnvExpressionReferences } from '@opencode-manager/shared/utils'
describe('parseEnvExpressionReferences', () => {
it('accepts a single secrets reference', () => {
expect(parseEnvExpressionReferences('${{ secrets.TOKEN }}')).toEqual({
ok: true,
references: [{ context: 'secrets', name: 'TOKEN' }],
})
})
it('accepts interpolation inside a larger string and multiple references', () => {
expect(parseEnvExpressionReferences('Bearer ${{ secrets.TOKEN }} on ${{ vars.HOST }}')).toEqual({
ok: true,
references: [
{ context: 'secrets', name: 'TOKEN' },
{ context: 'vars', name: 'HOST' },
],
})
})
it('treats the context case-insensitively and upper-cases the name', () => {
expect(parseEnvExpressionReferences('${{ Secrets.my_token }}')).toEqual({
ok: true,
references: [{ context: 'secrets', name: 'MY_TOKEN' }],
})
expect(parseEnvExpressionReferences('${{ VARS.Host }}')).toEqual({
ok: true,
references: [{ context: 'vars', name: 'HOST' }],
})
})
it('returns no references for a literal value', () => {
expect(parseEnvExpressionReferences('plain value')).toEqual({ ok: true, references: [] })
})
it('rejects an unknown context', () => {
const result = parseEnvExpressionReferences('${{ github.sha }}')
expect(result.ok).toBe(false)
if (!result.ok) {
expect(result.error).toContain('${{ github.sha }}')
expect(result.error).toContain('secrets.<NAME>')
}
})
it('rejects an operator inside the expression', () => {
expect(parseEnvExpressionReferences("${{ secrets.A || 'x' }}").ok).toBe(false)
})
it('rejects a function call inside the expression', () => {
expect(parseEnvExpressionReferences("${{ format('{0}', vars.A) }}").ok).toBe(false)
})
it('rejects a reference with an invalid name', () => {
expect(parseEnvExpressionReferences('${{ secrets.1BAD }}').ok).toBe(false)
})
})
describe('interpolateEnvExpressions', () => {
const lookup = (reference: { context: string; name: string }): string | undefined => {
const values: Record<string, string> = {
'secrets.TOKEN': 'super-secret',
'vars.HOST': 'example.com',
}
return values[`${reference.context}.${reference.name}`]
}
it('replaces references and interpolates inside a larger string', () => {
expect(interpolateEnvExpressions('https://${{ vars.HOST }}/${{ secrets.TOKEN }}', lookup)).toEqual({
value: 'https://example.com/super-secret',
missing: [],
})
})
it('resolves a missing reference to an empty string and reports it', () => {
expect(interpolateEnvExpressions('x-${{ secrets.MISSING }}-y', lookup)).toEqual({
value: 'x--y',
missing: [{ context: 'secrets', name: 'MISSING' }],
})
})
it('leaves an unsupported expression untouched', () => {
expect(interpolateEnvExpressions('${{ github.sha }}', lookup)).toEqual({
value: '${{ github.sha }}',
missing: [],
})
})
it('leaves a literal value untouched', () => {
expect(interpolateEnvExpressions('literal', lookup)).toEqual({ value: 'literal', missing: [] })
})
})