Files
archived-orca/src/main/opencode/opencode-go-api-key-store.test.ts
T
OrcaWinandNeil 16eaceb6a3 fix(orcad): orca serve owns its Claude and Codex accounts again (#29209)
* fix(orcad): orca serve owns its Claude and Codex accounts again (#28962)

orcad never wired account services, so every accounts.* RPC failed once
orca serve moved onto orcad. Build the same account services the desktop
builds (now in provider-account-services.ts), made Electron-free, and
install them in orcad except on a WSL host, whose desktop owns its accounts.

* test(orcad): keep account-services test off the real userData when XDG_CONFIG_HOME is set

* fix(orcad): structured Codex chats and session discovery follow this host's managed accounts

Create orcad's account services before the runtime so its structured Codex launch/read resolvers and catalog-probe prep are constructor deps, as on the desktop. A client-sent account home still wins, and a WSL host keeps them unset so the desktop-pushed CODEX_HOME applies. AI Vault session discovery now includes this host's managed Codex homes.

* fix(orcad): automatic Codex resumes follow the account home their session was recorded in

Share the desktop's resume launch prep as a factory and wire it into orcad's PTY registration and AI Vault resume, so a pane restored after an account switch resumes from its origin home. A client-sent account home keeps resuming from itself.

---------

Co-authored-by: Neil <neil@stably.ai>
2026-10-15 17:38:52 -07:00

102 lines
4.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { setSecretStore } from '../../shared/secret-store'
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import type * as NodeFs from 'node:fs'
import type * as NodeOs from 'node:os'
import { join } from 'node:path'
const home = vi.hoisted(() => {
const state: { directory: string; readError: Error | null } = { directory: '', readError: null }
return state
})
vi.mock('node:os', async (importOriginal) => ({
...(await importOriginal<typeof NodeOs>()),
homedir: () => home.directory
}))
vi.mock('node:fs', async (importOriginal) => {
const actual = await importOriginal<typeof NodeFs>()
return {
...actual,
readFileSync: (...args: Parameters<typeof actual.readFileSync>) => {
if (home.readError) {
throw home.readError
}
return actual.readFileSync(...args)
}
}
})
beforeEach(() =>
setSecretStore({
isEncryptionAvailable: () => true,
encryptString: (key: string) => Buffer.from(`encrypted:${key}`),
decryptString: (bytes: Buffer) => bytes.toString().slice('encrypted:'.length),
describeProtectionGap: () => null
})
)
beforeEach(() => {
home.readError = null
home.directory = mkdtempSync(join(tmpdir(), 'orca-go-key-store-'))
vi.resetModules()
})
afterEach(() => rmSync(home.directory, { recursive: true, force: true }))
describe('OpenCode Go main-owned API key file', () => {
it('persists a versioned encrypted envelope, reads it after restart, and clears it', async () => {
const store = await import('./opencode-go-api-key-store')
expect(store.hasOpenCodeGoApiKey()).toBe(false)
store.saveOpenCodeGoApiKey(' fake-key ')
expect(store.hasOpenCodeGoApiKey()).toBe(true)
const path = join(home.directory, '.orca', 'opencode-go-api-key.enc')
expect(readFileSync(path, 'utf8')).toBe(
`orca-opencode-go-api-key:v1:encrypted:${Buffer.from('encrypted:fake-key').toString('base64')}`
)
vi.resetModules()
const restarted = await import('./opencode-go-api-key-store')
expect(restarted.readOpenCodeGoApiKey()).toBe('fake-key')
restarted.clearOpenCodeGoApiKey()
expect(restarted.hasOpenCodeGoApiKey()).toBe(false)
expect(restarted.readOpenCodeGoApiKey()).toBeNull()
})
it('keeps the MiniMax cache and file independent', async () => {
const go = await import('./opencode-go-api-key-store')
const miniMax = await import('../minimax/minimax-api-key-store')
go.saveOpenCodeGoApiKey('fake-go')
miniMax.saveMiniMaxApiKey('fake-minimax')
expect(go.readOpenCodeGoApiKey()).toBe('fake-go')
expect(miniMax.readMiniMaxApiKey()).toBe('fake-minimax')
go.clearOpenCodeGoApiKey()
expect(miniMax.hasMiniMaxApiKey()).toBe(true)
expect(miniMax.readMiniMaxApiKey()).toBe('fake-minimax')
})
it('rejects empty keys and malformed envelopes without returning the key', async () => {
const store = await import('./opencode-go-api-key-store')
expect(() => store.saveOpenCodeGoApiKey(' ')).toThrow('required')
store.saveOpenCodeGoApiKey('fake-key')
writeFileSync(join(home.directory, '.orca', 'opencode-go-api-key.enc'), 'fake-invalid-envelope')
vi.resetModules()
const restarted = await import('./opencode-go-api-key-store')
expect(() => restarted.readOpenCodeGoApiKey()).toThrow(
'OpenCode Go API key could not be decrypted'
)
})
it('throws a distinct unreadable error for a transient read failure', async () => {
const store = await import('./opencode-go-api-key-store')
store.saveOpenCodeGoApiKey('fake-key')
vi.resetModules()
const restarted = await import('./opencode-go-api-key-store')
vi.spyOn(console, 'warn').mockImplementation(() => {})
home.readError = Object.assign(new Error('resource busy'), { code: 'EBUSY' })
expect(() => restarted.readOpenCodeGoApiKey()).toThrow(
'OpenCode Go API key file could not be read'
)
home.readError = null
expect(restarted.readOpenCodeGoApiKey()).toBe('fake-key')
})
})