mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
146 lines
5.4 KiB
TypeScript
146 lines
5.4 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
|
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
import os from 'os'
|
|
import { pathToFileURL } from 'url'
|
|
import { installGhEnvPlugin, getGhEnvPluginDir } from '../../src/services/opencode-gh-env-plugin'
|
|
|
|
type ShellEnvHook = (
|
|
input: { cwd: string },
|
|
output: { env: Record<string, string> },
|
|
) => Promise<void>
|
|
type PluginFactory = () => Promise<{ 'shell.env': ShellEnvHook }>
|
|
|
|
async function loadPlugin(configHome: string): Promise<PluginFactory> {
|
|
const file = path.join(getGhEnvPluginDir(configHome), 'ocm-gh-env.js')
|
|
const mod = await import(pathToFileURL(file).href)
|
|
return mod.default as PluginFactory
|
|
}
|
|
|
|
describe('ocm-gh-env plugin', () => {
|
|
let configHome: string
|
|
|
|
beforeEach(async () => {
|
|
configHome = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-ghenv-'))
|
|
await installGhEnvPlugin(configHome)
|
|
process.env.OCM_INTERNAL_API_URL = 'http://localhost:5003/api/internal'
|
|
process.env.OCM_INTERNAL_TOKEN = 'secret-token'
|
|
})
|
|
|
|
afterEach(async () => {
|
|
vi.unstubAllGlobals()
|
|
delete process.env.OCM_INTERNAL_API_URL
|
|
delete process.env.OCM_INTERNAL_TOKEN
|
|
await fs.rm(configHome, { recursive: true, force: true })
|
|
})
|
|
|
|
it('writes the plugin file into the auto-discovery dir', async () => {
|
|
const file = path.join(getGhEnvPluginDir(configHome), 'ocm-gh-env.js')
|
|
await expect(fs.access(file)).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('atomically replaces a symlink at the plugin path with a regular file', async () => {
|
|
const pluginDir = getGhEnvPluginDir(configHome)
|
|
const pluginPath = path.join(pluginDir, 'ocm-gh-env.js')
|
|
const symlinkTarget = path.join(pluginDir, 'attacker-hook.js')
|
|
await fs.mkdir(pluginDir, { recursive: true })
|
|
await fs.rm(pluginPath, { force: true })
|
|
await fs.writeFile(symlinkTarget, 'export default async function () {}')
|
|
await fs.symlink(symlinkTarget, pluginPath)
|
|
|
|
await installGhEnvPlugin(configHome)
|
|
|
|
const stat = await fs.lstat(pluginPath)
|
|
expect(stat.isFile()).toBe(true)
|
|
expect(stat.isSymbolicLink()).toBe(false)
|
|
expect(await fs.readFile(pluginPath, 'utf-8')).toContain('shell.env')
|
|
expect(await fs.readFile(symlinkTarget, 'utf-8')).toBe('export default async function () {}')
|
|
})
|
|
|
|
it('throws when the plugin file cannot be written instead of swallowing the failure', async () => {
|
|
const blockedHome = path.join(configHome, 'blocked')
|
|
await fs.mkdir(blockedHome, { recursive: true })
|
|
await fs.writeFile(path.join(blockedHome, 'opencode'), 'not a directory')
|
|
|
|
await expect(installGhEnvPlugin(blockedHome)).rejects.toThrow()
|
|
})
|
|
|
|
it('injects fetched GH env into output.env', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, output)
|
|
|
|
expect(output.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
|
const [url, options] = fetchMock.mock.calls[0]!
|
|
expect(url.toString()).toBe('http://localhost:5003/api/internal/git-credentials/gh-env?cwd=%2Frepo')
|
|
expect(options).toEqual({ headers: { Authorization: 'Bearer secret-token' } })
|
|
})
|
|
|
|
it('does not fetch when the internal env vars are missing', async () => {
|
|
delete process.env.OCM_INTERNAL_API_URL
|
|
delete process.env.OCM_INTERNAL_TOKEN
|
|
const fetchMock = vi.fn()
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, output)
|
|
|
|
expect(output.env).toEqual({})
|
|
expect(fetchMock).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('never throws when the fetch fails', async () => {
|
|
const fetchMock = vi.fn().mockRejectedValue(new Error('network down'))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const output = { env: {} as Record<string, string> }
|
|
|
|
await expect(hooks['shell.env']({ cwd: '/repo' }, output)).resolves.toBeUndefined()
|
|
expect(output.env).toEqual({})
|
|
})
|
|
|
|
it('caches results within the TTL so rapid calls fetch once', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
const out1 = { env: {} as Record<string, string> }
|
|
const out2 = { env: {} as Record<string, string> }
|
|
await hooks['shell.env']({ cwd: '/repo' }, out1)
|
|
await hooks['shell.env']({ cwd: '/repo' }, out2)
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
expect(out2.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
|
})
|
|
|
|
it('caches results per cwd', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
|
})
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const factory = await loadPlugin(configHome)
|
|
const hooks = await factory()
|
|
await hooks['shell.env']({ cwd: '/repo-a' }, { env: {} })
|
|
await hooks['shell.env']({ cwd: '/repo-b' }, { env: {} })
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|