fix: repair ccsx codex profile resources

This commit is contained in:
Tam Nhu Tran committed 2026-05-22 16:27:00 -04:00
1 parent 17e221ac2c
commit 37a14525cc
9 files changed
+346 -17

No files matched your search

@@ -0,0 +1,110 @@
import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
let tempDir: string;
let profileDir: string;
let sharedCodexHome: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-resources-test-'));
profileDir = path.join(tempDir, 'profile');
sharedCodexHome = path.join(tempDir, 'shared-codex');
fs.mkdirSync(path.join(sharedCodexHome, 'agents'), { recursive: true, mode: 0o700 });
fs.writeFileSync(
path.join(sharedCodexHome, 'agents', 'brainstormer.toml'),
'name = "brainstormer"\n'
);
fs.mkdirSync(path.join(sharedCodexHome, 'skills'), { recursive: true, mode: 0o700 });
fs.writeFileSync(path.join(sharedCodexHome, 'skills', 'review.md'), '# Review\n');
});
afterEach(() => {
mock.restore();
fs.rmSync(tempDir, { recursive: true, force: true });
});
describe('ensureCodexProfileResources', () => {
it('links shared agents and skills into a fresh Codex profile', async () => {
const { ensureCodexProfileResources } = await import(
'../../../src/codex-auth/codex-profile-resources'
);
ensureCodexProfileResources(profileDir, { sharedCodexHome });
for (const resourceName of ['agents', 'skills']) {
const resourcePath = path.join(profileDir, resourceName);
expect(fs.lstatSync(resourcePath).isSymbolicLink()).toBe(true);
expect(fs.readlinkSync(resourcePath)).toBe(path.join(sharedCodexHome, resourceName));
}
});
it('repairs a missing resource link without changing existing shared files', async () => {
const { ensureCodexProfileResources } = await import(
'../../../src/codex-auth/codex-profile-resources'
);
ensureCodexProfileResources(profileDir, { sharedCodexHome });
fs.unlinkSync(path.join(profileDir, 'agents'));
ensureCodexProfileResources(profileDir, { sharedCodexHome });
const agentsPath = path.join(profileDir, 'agents');
expect(fs.lstatSync(agentsPath).isSymbolicLink()).toBe(true);
expect(fs.existsSync(path.join(agentsPath, 'brainstormer.toml'))).toBe(true);
});
it('is idempotent for repeated repair calls', async () => {
const { ensureCodexProfileResources } = await import(
'../../../src/codex-auth/codex-profile-resources'
);
ensureCodexProfileResources(profileDir, { sharedCodexHome });
const firstTarget = fs.readlinkSync(path.join(profileDir, 'agents'));
ensureCodexProfileResources(profileDir, { sharedCodexHome });
expect(fs.readlinkSync(path.join(profileDir, 'agents'))).toBe(firstTarget);
expect(fs.readFileSync(path.join(profileDir, 'agents', 'brainstormer.toml'), 'utf8')).toContain(
'brainstormer'
);
});
it('copies missing resource files into an existing profile-local directory', async () => {
const { ensureCodexProfileResources } = await import(
'../../../src/codex-auth/codex-profile-resources'
);
const agentsPath = path.join(profileDir, 'agents');
fs.mkdirSync(agentsPath, { recursive: true, mode: 0o700 });
fs.writeFileSync(path.join(agentsPath, 'local.toml'), 'name = "local"\n');
ensureCodexProfileResources(profileDir, { sharedCodexHome });
expect(fs.lstatSync(agentsPath).isDirectory()).toBe(true);
expect(fs.existsSync(path.join(agentsPath, 'local.toml'))).toBe(true);
expect(fs.existsSync(path.join(agentsPath, 'brainstormer.toml'))).toBe(true);
});
it('falls back to copying resources when directory symlinks are unavailable', async () => {
const { ensureCodexProfileResources } = await import(
'../../../src/codex-auth/codex-profile-resources'
);
const symlinkSpy = spyOn(fs, 'symlinkSync').mockImplementation(() => {
throw Object.assign(new Error('simulated symlink failure'), { code: 'EPERM' });
});
const origWrite = process.stderr.write.bind(process.stderr);
process.stderr.write = () => true;
try {
ensureCodexProfileResources(profileDir, { sharedCodexHome });
} finally {
process.stderr.write = origWrite;
symlinkSpy.mockRestore();
}
const agentsPath = path.join(profileDir, 'agents');
expect(fs.lstatSync(agentsPath).isDirectory()).toBe(true);
expect(fs.existsSync(path.join(agentsPath, 'brainstormer.toml'))).toBe(true);
});
});
@@ -10,13 +10,20 @@ import * as childProcess from 'child_process';
let tempDir: string;
let ccsHome: string;
let homeDir: string;
const ORIG_CCS_HOME = process.env.CCS_HOME;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-create-test-'));
homeDir = path.join(tempDir, 'home');
ccsHome = path.join(tempDir, 'ccs');
fs.mkdirSync(path.join(homeDir, '.codex', 'agents'), { recursive: true });
fs.writeFileSync(path.join(homeDir, '.codex', 'agents', 'brainstormer.toml'), 'name = "b"\n');
fs.mkdirSync(path.join(homeDir, '.codex', 'skills'), { recursive: true });
fs.writeFileSync(path.join(homeDir, '.codex', 'skills', 'review.md'), '# Review\n');
fs.mkdirSync(path.join(ccsHome, '.ccs'), { recursive: true });
process.env.CCS_HOME = ccsHome;
spyOn(os, 'homedir').mockReturnValue(homeDir);
});
afterEach(() => {
@@ -87,6 +94,8 @@ describe('handleCreateCodex — happy path', () => {
expect(ctx.registry.hasProfile('myprofile')).toBe(true);
const instancesDir = path.join(ccsHome, '.ccs', 'codex-instances', 'myprofile');
expect(fs.existsSync(instancesDir)).toBe(true);
expect(fs.lstatSync(path.join(instancesDir, 'agents')).isSymbolicLink()).toBe(true);
expect(fs.lstatSync(path.join(instancesDir, 'skills')).isSymbolicLink()).toBe(true);
});
});
@@ -109,10 +118,14 @@ describe('handleCreateCodex — idempotent re-run', () => {
const profileDir = path.join(ccsHome, '.ccs', 'codex-instances', 'dupprofile');
const configPath = path.join(profileDir, 'config.toml');
const agentsPath = path.join(profileDir, 'agents');
const authJsonPath = path.join(profileDir, 'auth.json');
const authJson = JSON.stringify({ tokens: { id_token: buildToken({ email: 'idempotent@test' }) } });
const authJson = JSON.stringify({
tokens: { id_token: buildToken({ email: 'idempotent@test' }) },
});
fs.writeFileSync(authJsonPath, authJson);
fs.rmSync(configPath, { force: true });
fs.rmSync(agentsPath, { recursive: true, force: true });
const restore2 = silenceConsole();
try {
@@ -124,6 +137,7 @@ describe('handleCreateCodex — idempotent re-run', () => {
// Profile still has exactly one entry
expect(ctx.registry.listProfiles().filter((n) => n === 'dupprofile').length).toBe(1);
expect(fs.existsSync(configPath)).toBe(true);
expect(fs.existsSync(path.join(agentsPath, 'brainstormer.toml'))).toBe(true);
expect(fs.readFileSync(authJsonPath, 'utf8')).toBe(authJson);
});
});