section 1: Phase 1: Shell environment owner and repo working-directory

This commit is contained in:
Chris Scott committed 2026-10-03 00:56:39 -04:00
1 parent 52b6147ed3
commit b494ee9f4a
11 files changed
+280 -17

No files matched your search

@@ -4,7 +4,7 @@ import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
import { SettingsService } from '../../src/services/settings'
import { CredentialProvider } from '../../src/services/credential-provider'
import { createRepo, setRepoGitCredentialId, setRepoSandboxGitCredentials } from '../../src/db/queries'
import { createRepo, setRepoGitCredentialId, setRepoGitIdentityId, setRepoSandboxGitCredentials } from '../../src/db/queries'
import { SANDBOX_MAX_FORWARDED_GIT_CONFIGS } from '../../src/services/sandbox/shell-shim'
import type { GitCredential } from '@opencode-manager/shared'
@@ -316,4 +316,65 @@ describe('CredentialProvider', () => {
}
})
})
describe('shell env', () => {
function createGithubRepo() {
return createRepo(db, {
repoUrl: 'https://github.com/acme/repo.git',
localPath: 'repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
})
}
beforeEach(() => {
settingsService.updateSettings({
gitCredentials: [createPatCredential('github', 'github.com', 'ghp_test_token', undefined, 'github-id')],
gitIdentities: [{ id: 'identity-1', name: 'Alice', email: 'alice@example.com' }],
})
})
it('getShellEnv returns only the GH CLI env without an assigned identity', () => {
expect(provider.getShellEnv()).toEqual({ GH_TOKEN: 'ghp_test_token', GITHUB_TOKEN: 'ghp_test_token' })
})
it('getAssignedGitIdentityEnv returns an empty object without an assignment', () => {
const repo = createGithubRepo()
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({})
})
it('getAssignedGitIdentityEnv returns the identity env for an assigned profile', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'identity-1')
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({
GIT_AUTHOR_NAME: 'Alice',
GIT_AUTHOR_EMAIL: 'alice@example.com',
GIT_COMMITTER_NAME: 'Alice',
GIT_COMMITTER_EMAIL: 'alice@example.com',
})
})
it('getShellEnv merges the GH CLI env with the assigned identity env', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'identity-1')
expect(provider.getShellEnv({ cwd: repo.fullPath })).toEqual({
GH_TOKEN: 'ghp_test_token',
GITHUB_TOKEN: 'ghp_test_token',
GIT_AUTHOR_NAME: 'Alice',
GIT_AUTHOR_EMAIL: 'alice@example.com',
GIT_COMMITTER_NAME: 'Alice',
GIT_COMMITTER_EMAIL: 'alice@example.com',
})
})
it('getAssignedGitIdentityEnv ignores a dangling assigned identity', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'missing-identity')
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({})
})
})
})
@@ -0,0 +1,98 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { findSiblingByDirectory, resolveRepoWorkingDirectory } from '../../src/services/repo'
import type { Repo } from '@opencode-manager/shared/types'
describe('resolveRepoWorkingDirectory', () => {
let root: string
let repoPath: string
let siblingPath: string
let unrelatedPath: string
let repoLinkPath: string
let siblingLinkPath: string
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'ocm-repo-working-directory-'))
repoPath = path.join(root, 'repo')
siblingPath = path.join(root, 'sibling')
unrelatedPath = path.join(root, 'unrelated')
repoLinkPath = path.join(root, 'repo-link')
siblingLinkPath = path.join(root, 'sibling-link')
fs.mkdirSync(repoPath)
fs.mkdirSync(siblingPath)
fs.mkdirSync(unrelatedPath)
fs.symlinkSync(repoPath, repoLinkPath, 'dir')
fs.symlinkSync(siblingPath, siblingLinkPath, 'dir')
})
afterAll(() => {
fs.rmSync(root, { recursive: true, force: true })
})
function createRepo(): Repo {
return { fullPath: repoPath } as Repo
}
it('returns the repo path when no directory is given', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), undefined, async () => [])).toBe(repoPath)
})
it('returns the repo path for the repo directory itself', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), repoPath, async () => [])).toBe(repoPath)
})
it('returns the repo path for a symlinked equivalent of the repo directory', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), repoLinkPath, async () => [])).toBe(repoPath)
})
it('returns a matching sibling path', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), siblingPath, async () => siblings)).toBe(siblingPath)
})
it('matches a sibling through a symlinked directory', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), siblingLinkPath, async () => siblings)).toBe(siblingPath)
})
it('returns null for an unrelated directory', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), unrelatedPath, async () => siblings)).toBeNull()
})
})
describe('findSiblingByDirectory', () => {
let root: string
let siblingPath: string
let siblingLinkPath: string
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'ocm-find-sibling-'))
siblingPath = path.join(root, 'sibling')
siblingLinkPath = path.join(root, 'sibling-link')
fs.mkdirSync(siblingPath)
fs.symlinkSync(siblingPath, siblingLinkPath, 'dir')
})
afterAll(() => {
fs.rmSync(root, { recursive: true, force: true })
})
it('returns the sibling whose canonical path matches', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], siblingPath)).toBe(sibling)
})
it('matches through a symlinked directory', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], siblingLinkPath)).toBe(sibling)
})
it('returns undefined when no sibling matches', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], path.join(root, 'missing'))).toBeUndefined()
})
})