Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
import {
existsSync,
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { parse } from '@iarna/toml';
import { afterEach, describe, expect, it } from 'vitest';
import {
createCodexIsolatedHome,
createCodexProbeHome,
importLocalCodexConfiguration,
createFollowingLocalCodexHome,
synchronizeLocalCodexConfiguration
} from '../../src/codex/probe-home.js';
let tempDir = '';
afterEach(() => {
if (tempDir.length > 0) rmSync(tempDir, { recursive: true, force: true });
tempDir = '';
});
describe('Codex Probe 临时 Home', () => {
it('refreshes automatic model credentials only when local configuration changes', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-follow-local-'));
const source = join(tempDir, 'source');
const isolated = join(tempDir, 'isolated');
mkdirSync(source);
writeFileSync(join(source, 'auth.json'), '{"OPENAI_API_KEY":"local-secret"}');
writeFileSync(join(source, 'config.toml'), 'model = "first-model"\n');
const read = createFollowingLocalCodexHome(source, isolated);
const initial = read();
writeFileSync(join(isolated, 'auth.json'), '{"OPENAI_API_KEY":"refreshed-secret"}');
expect(read()).toEqual({ ...initial, repaired: false });
expect(readFileSync(join(isolated, 'auth.json'), 'utf8')).toContain('refreshed-secret');
writeFileSync(join(source, 'config.toml'), 'model = "second-model"\n');
expect(read().version).not.toBe(initial.version);
expect(readFileSync(join(isolated, 'config.toml'), 'utf8')).toContain('second-model');
expect(readFileSync(join(source, 'auth.json'), 'utf8')).toContain('local-secret');
rmSync(join(source, 'auth.json'));
read();
expect(existsSync(join(isolated, 'auth.json'))).toBe(false);
});
it('checks an existing runtime copy on every startup even when the source did not change', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-startup-sync-'));
const source = join(tempDir, 'source');
const runtime = join(tempDir, 'runtime');
mkdirSync(source); mkdirSync(runtime);
writeFileSync(join(source, 'config.toml'), 'model = "current"\n');
writeFileSync(join(runtime, 'config.toml'), 'model = "stale"\n');
writeFileSync(join(runtime, 'auth.json'), '{"OPENAI_API_KEY":"stale-key"}');
writeFileSync(join(runtime, 'rollout.jsonl'), 'existing session');
expect(synchronizeLocalCodexConfiguration(source, runtime)).toBe(true);
expect(parse(readFileSync(join(runtime, 'config.toml'), 'utf8')).model).toBe('current');
expect(existsSync(join(runtime, 'auth.json'))).toBe(false);
expect(readFileSync(join(runtime, 'rollout.jsonl'), 'utf8')).toBe('existing session');
writeFileSync(join(runtime, 'config.toml'), 'model = "drifted"\n');
expect(synchronizeLocalCodexConfiguration(source, runtime)).toBe(true);
expect(synchronizeLocalCodexConfiguration(source, runtime)).toBe(false);
expect(readFileSync(join(source, 'config.toml'), 'utf8')).toBe('model = "current"\n');
});
it('repairs runtime drift after a failure and changes the host version without changing the local source', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-error-sync-'));
const source = join(tempDir, 'source');
const runtime = join(tempDir, 'runtime');
mkdirSync(source);
writeFileSync(join(source, 'config.toml'), 'model = "current"\n');
const read = createFollowingLocalCodexHome(source, runtime);
const initial = read();
writeFileSync(join(runtime, 'config.toml'), 'model = "drifted"\n');
const checked = read({ verifyRuntime: true });
expect(checked.repaired).toBe(true);
expect(checked.version).not.toBe(initial.version);
expect(parse(readFileSync(join(runtime, 'config.toml'), 'utf8')).model).toBe('current');
expect(read({ verifyRuntime: true })).toEqual({ ...checked, repaired: false });
});
it('validates local files before changing the runtime and never rewrites the source home', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-invalid-sync-'));
const source = join(tempDir, 'source');
const runtime = join(tempDir, 'runtime');
mkdirSync(source); mkdirSync(runtime);
writeFileSync(join(source, 'config.toml'), 'invalid = [');
writeFileSync(join(runtime, 'config.toml'), 'model = "working"\n');
writeFileSync(join(runtime, 'auth.json'), '{"OPENAI_API_KEY":"working-key"}');
expect(() => synchronizeLocalCodexConfiguration(source, runtime)).toThrow('Local Codex configuration or credentials are invalid');
expect(readFileSync(join(runtime, 'config.toml'), 'utf8')).toContain('working');
expect(readFileSync(join(runtime, 'auth.json'), 'utf8')).toContain('working-key');
expect(synchronizeLocalCodexConfiguration(source, source)).toBe(false);
expect(readFileSync(join(source, 'config.toml'), 'utf8')).toBe('invalid = [');
});
it('imports a local login once and does not overwrite an existing OpenCreator selection', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-codex-import-test-'));
const sourceHome = join(tempDir, 'source');
const isolatedHome = join(tempDir, 'isolated');
mkdirSync(sourceHome);
writeFileSync(join(sourceHome, 'auth.json'), '{"OPENAI_API_KEY":"local-secret"}');
writeFileSync(join(sourceHome, 'config.toml'), 'model = "local-model"\n');
expect(importLocalCodexConfiguration(sourceHome, isolatedHome)).toBe(true);
expect(readFileSync(join(isolatedHome, 'auth.json'), 'utf8')).toContain('local-secret');
writeFileSync(join(isolatedHome, 'config.toml'), 'model = "user-choice"\n');
expect(importLocalCodexConfiguration(sourceHome, isolatedHome)).toBe(false);
expect(readFileSync(join(isolatedHome, 'config.toml'), 'utf8')).toContain('user-choice');
});
it('refreshes the isolated provider from the local Codex home', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-isolated-provider-test-'));
const sourceHome = join(tempDir, 'source');
const isolatedPath = join(tempDir, 'isolated');
mkdirSync(sourceHome, { recursive: true });
mkdirSync(isolatedPath, { recursive: true });
writeFileSync(join(sourceHome, 'config.toml'), [
'model = "gpt-5.6-sol"',
'model_provider = "gateway"',
'',
'[model_providers.gateway]',
'base_url = "https://forward.example.test/v1"',
'experimental_bearer_token = "provider-secret"',
''
].join('\n'));
writeFileSync(join(isolatedPath, 'config.toml'), [
'model = "stale-model"',
'openai_base_url = "https://stale.example.test/v1"',
''
].join('\n'));
createCodexIsolatedHome(sourceHome, isolatedPath);
expect(parse(readFileSync(join(isolatedPath, 'config.toml'), 'utf8'))).toMatchObject({
model: 'gpt-5.6-sol',
model_provider: 'gateway',
model_providers: {
gateway: {
base_url: 'https://forward.example.test/v1',
experimental_bearer_token: 'provider-secret'
}
}
});
});
it('为知识会话保留隔离 Home 中的 Codex rollout', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-isolated-home-test-'));
const sourceHome = join(tempDir, 'source');
const isolatedPath = join(tempDir, 'knowledge', 'codex-home');
mkdirSync(sourceHome, { recursive: true });
writeFileSync(join(sourceHome, 'auth.json'), '{"token":"secret"}\n');
const first = createCodexIsolatedHome(sourceHome, isolatedPath);
mkdirSync(join(first.path, 'sessions'), { recursive: true });
writeFileSync(join(first.path, 'sessions', 'rollout.jsonl'), 'turn\n');
first.cleanup();
const second = createCodexIsolatedHome(sourceHome, isolatedPath);
expect(second.path).toBe(isolatedPath);
expect(readFileSync(join(second.path, 'sessions', 'rollout.jsonl'), 'utf8')).toBe('turn\n');
second.cleanup();
expect(existsSync(isolatedPath)).toBe(true);
});
it('只保留模型调用所需配置,不复制 Skills、Plugins 或 MCP 凭据', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-probe-home-test-'));
const sourceHome = join(tempDir, 'source');
const temporaryRoot = join(tempDir, 'temporary');
mkdirSync(join(sourceHome, 'model-catalogs'), { recursive: true });
mkdirSync(join(sourceHome, 'skills', 'example'), { recursive: true });
mkdirSync(join(sourceHome, 'plugins', 'example'), { recursive: true });
mkdirSync(temporaryRoot, { recursive: true });
const sourceCatalog = join(sourceHome, 'model-catalogs', 'custom.json');
writeFileSync(join(sourceHome, 'config.toml'), [
'model = "test-model"',
'model_provider = "custom"',
`model_catalog_json = ${JSON.stringify(sourceCatalog)}`,
'openai_base_url = "https://api.example.test/v1"',
'cli_auth_credentials_store = "file"',
'forced_login_method = "api"',
'notify = ["/tmp/slow-notifier"]',
'check_for_update_on_startup = true',
'',
'[model_providers.custom]',
'name = "Custom Provider"',
'base_url = "https://provider.example.test/v1"',
'wire_api = "responses"',
'requires_openai_auth = true',
'http_headers = { X_Probe = "enabled" }',
'',
'[projects."/tmp/project"]',
'trust_level = "trusted"',
'',
'[mcp_servers.slow]',
'command = "slow-server"',
'',
'[plugins.example]',
'enabled = true',
''
].join('\n'));
writeFileSync(join(sourceHome, 'auth.json'), '{"token":"secret"}\n');
writeFileSync(sourceCatalog, '{}\n');
writeFileSync(join(sourceHome, 'model-catalogs', 'unused.json'), '{"unused":true}\n');
writeFileSync(join(sourceHome, 'skills', 'example', 'SKILL.md'), 'skill\n');
writeFileSync(join(sourceHome, 'plugins', 'example', 'plugin.json'), '{}\n');
writeFileSync(join(sourceHome, 'mcp_oauth_credentials.json'), '{"secret":true}\n');
const probeHome = createCodexProbeHome(sourceHome, temporaryRoot);
try {
const config = parse(readFileSync(join(probeHome.path, 'config.toml'), 'utf8'));
expect(config).toMatchObject({
model: 'test-model',
model_provider: 'custom',
openai_base_url: 'https://api.example.test/v1',
cli_auth_credentials_store: 'file',
forced_login_method: 'api',
model_providers: {
custom: {
name: 'Custom Provider',
base_url: 'https://provider.example.test/v1',
wire_api: 'responses',
requires_openai_auth: true,
http_headers: { X_Probe: 'enabled' }
}
}
});
expect(config).not.toHaveProperty('projects');
expect(config).not.toHaveProperty('mcp_servers');
expect(config).not.toHaveProperty('plugins');
expect(config).not.toHaveProperty('notify');
expect(config).not.toHaveProperty('check_for_update_on_startup');
expect(config.model_catalog_json).toBe(
join(probeHome.path, 'model-catalogs', 'custom.json')
);
expect(readFileSync(join(probeHome.path, 'auth.json'), 'utf8'))
.toBe('{"token":"secret"}\n');
expect(existsSync(join(probeHome.path, 'model-catalogs', 'custom.json'))).toBe(true);
expect(existsSync(join(probeHome.path, 'model-catalogs', 'unused.json'))).toBe(false);
expect(existsSync(join(probeHome.path, 'skills'))).toBe(false);
expect(existsSync(join(probeHome.path, 'plugins'))).toBe(false);
expect(existsSync(join(probeHome.path, 'mcp_oauth_credentials.json'))).toBe(false);
} finally {
probeHome.cleanup();
}
expect(existsSync(probeHome.path)).toBe(false);
});
it('没有 config.toml 时仍创建可用的空 Probe Home', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-probe-home-empty-test-'));
const sourceHome = join(tempDir, 'source');
const temporaryRoot = join(tempDir, 'temporary');
mkdirSync(sourceHome, { recursive: true });
mkdirSync(temporaryRoot, { recursive: true });
writeFileSync(join(sourceHome, 'auth.json'), '{"token":"secret"}\n');
const probeHome = createCodexProbeHome(sourceHome, temporaryRoot);
try {
expect(existsSync(join(probeHome.path, 'config.toml'))).toBe(false);
expect(readFileSync(join(probeHome.path, 'auth.json'), 'utf8'))
.toBe('{"token":"secret"}\n');
} finally {
probeHome.cleanup();
}
});
it('忽略 Codex 已废弃的顶层 profile 选择器和内联 profile', () => {
tempDir = mkdtempSync(join(tmpdir(), 'opencreator-probe-home-profile-test-'));
const sourceHome = join(tempDir, 'source');
const temporaryRoot = join(tempDir, 'temporary');
mkdirSync(sourceHome, { recursive: true });
mkdirSync(temporaryRoot, { recursive: true });
writeFileSync(join(sourceHome, 'config.toml'), [
'profile = "work"',
'model = "base-model"',
'model_provider = "base"',
'',
'[model_providers.base]',
'name = "Base"',
'base_url = "https://base.example.test/v1"',
'',
'[model_providers.work]',
'name = "Work"',
'base_url = "https://work.example.test/v1"',
'',
'[profiles.work]',
'model = "inline-work-model"',
'model_provider = "work"',
''
].join('\n'));
writeFileSync(join(sourceHome, 'work.config.toml'), [
'model = "work-model"',
'model_provider = "work"',
'notify = ["/tmp/profile-notifier"]',
''
].join('\n'));
const probeHome = createCodexProbeHome(sourceHome, temporaryRoot);
try {
const config = parse(readFileSync(join(probeHome.path, 'config.toml'), 'utf8'));
expect(config).toMatchObject({
model: 'base-model',
model_provider: 'base',
model_providers: {
base: {
name: 'Base',
base_url: 'https://base.example.test/v1'
}
}
});
expect(config).not.toHaveProperty('profile');
expect(config).not.toHaveProperty('profiles');
expect(config).not.toHaveProperty('notify');
expect(config.model_providers).not.toHaveProperty('work');
expect(existsSync(join(probeHome.path, 'work.config.toml'))).toBe(false);
} finally {
probeHome.cleanup();
}
});
});