Files
opencode-manager/ocm-cli/test/state.test.ts
T
Chris ScottandCS 6cc50a02ed feat(ocm-cli): add file and keychain credential backends with auth commands (#327)
* feat(ocm-cli): add file and keychain credential backends with auth commands

* feat(ocm-cli): refactor credentials into async TokenStore architecture

Replace the credential-backend module with a TokenStore interface and
file/keychain implementations behind an internal facade. Convert token
get/set/delete to async, add env-token precedence warnings on login and
logout, and surface store availability in status. Add token-store tests
and helpers; remove the old credential modules and tests. Bump to 0.2.6.

* fix(ocm-cli): keep tokens out of security argv and stop masking config read errors

Route every macOS Keychain call through `security -i`, sending the quoted
command on stdin so the token is never visible in process arguments. Reject
arguments containing line breaks, which would otherwise inject additional
security commands.

Let unexpected state file read failures propagate so a broken config dir is
reported instead of being reduced to "no manager configured", and stop
consuming the install notice when reading it fails unexpectedly.

---------

Co-authored-by: CS <chris@chriswritescode.dev>
2026-07-28 16:38:12 -04:00

144 lines
5.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
let home: string
let configDir: string
let savedHome: string | undefined
async function loadState() {
vi.resetModules()
return import('../src/state.js')
}
beforeEach(() => {
savedHome = process.env.HOME
home = mkdtempSync(join(tmpdir(), 'ocm-home-'))
process.env.HOME = home
configDir = join(home, '.config', 'opencode-manager')
})
afterEach(() => {
if (savedHome === undefined) delete process.env.HOME
else process.env.HOME = savedHome
rmSync(home, { recursive: true, force: true })
})
describe('cli state file', () => {
it('returns null when no state file exists', async () => {
const { readState } = await loadState()
expect(readState()).toBeNull()
})
it('round-trips the manager url and last repo', async () => {
const { readState, writeState } = await loadState()
writeState({ managerUrl: 'https://manager.example.com', lastRepoId: 7, lastRepoName: 'repo' })
const state = readState()
expect(state?.managerUrl).toBe('https://manager.example.com')
expect(state?.lastRepoId).toBe(7)
expect(state?.updatedAt).toBeTypeOf('number')
})
it('writes the state file with owner-only permissions', async () => {
if (process.platform === 'win32') return
const { writeState, getStatePath } = await loadState()
writeState({ managerUrl: 'https://manager.example.com' })
expect(statSync(getStatePath()).mode & 0o777).toBe(0o600)
expect(statSync(configDir).mode & 0o777).toBe(0o700)
})
it('repairs loose permissions on an existing state file', async () => {
if (process.platform === 'win32') return
const { writeState, getStatePath } = await loadState()
mkdirSync(configDir, { recursive: true })
writeFileSync(getStatePath(), '{}', { mode: 0o644 })
writeState({ managerUrl: 'https://manager.example.com' })
expect(statSync(getStatePath()).mode & 0o777).toBe(0o600)
})
it('leaves no temp file behind', async () => {
const { writeState } = await loadState()
writeState({ managerUrl: 'https://manager.example.com' })
expect(readdirSync(configDir).filter((entry) => entry.endsWith('.tmp'))).toEqual([])
})
it('treats a corrupt state file as absent', async () => {
const { readState, getStatePath } = await loadState()
mkdirSync(configDir, { recursive: true })
writeFileSync(getStatePath(), 'not json')
expect(readState()).toBeNull()
})
it('treats a state file without a manager url as absent', async () => {
const { readState, getStatePath } = await loadState()
mkdirSync(configDir, { recursive: true })
writeFileSync(getStatePath(), JSON.stringify({ lastRepoId: 1 }))
expect(readState()).toBeNull()
})
it('surfaces an unreadable state file instead of reporting no manager', async () => {
if (process.platform === 'win32' || process.getuid?.() === 0) return
const { readState, writeState, getStatePath } = await loadState()
writeState({ managerUrl: 'https://manager.example.com' })
chmodSync(getStatePath(), 0o000)
expect(() => readState()).toThrow(/EACCES|EPERM/)
})
it('clearState empties an existing state file without recreating a missing one', async () => {
const { readState, writeState, clearState, getStatePath } = await loadState()
clearState()
expect(existsSync(getStatePath())).toBe(false)
writeState({ managerUrl: 'https://manager.example.com' })
clearState()
expect(readState()).toBeNull()
expect(readFileSync(getStatePath(), 'utf-8')).toBe('{}')
})
it('reads an install notice once and then removes it', async () => {
const { readInstallNotice } = await loadState()
mkdirSync(configDir, { recursive: true })
const noticeFile = join(configDir, 'install-notice.json')
writeFileSync(noticeFile, JSON.stringify({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true }))
expect(readInstallNotice()).toEqual({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true })
expect(existsSync(noticeFile)).toBe(false)
expect(readInstallNotice()).toBeNull()
})
it('keeps an unreadable install notice instead of consuming it', async () => {
if (process.platform === 'win32' || process.getuid?.() === 0) return
const { readInstallNotice } = await loadState()
mkdirSync(configDir, { recursive: true })
const noticeFile = join(configDir, 'install-notice.json')
writeFileSync(noticeFile, JSON.stringify({ link: '~/.local/bin/ocm', binDir: '~/.local/bin', pathMissing: true }))
chmodSync(noticeFile, 0o000)
expect(readInstallNotice()).toBeNull()
expect(existsSync(noticeFile)).toBe(true)
})
it('ignores an incomplete install notice', async () => {
const { readInstallNotice } = await loadState()
mkdirSync(configDir, { recursive: true })
writeFileSync(join(configDir, 'install-notice.json'), JSON.stringify({ binDir: '~/.local/bin' }))
expect(readInstallNotice()).toBeNull()
})
})