fix(codex): remove duplicate skill copies and consolidate hook payloads
This commit is contained in:
123
tests/bug-3427-3433-codex-install-shape.test.cjs
Normal file
123
tests/bug-3427-3433-codex-install-shape.test.cjs
Normal file
@@ -0,0 +1,123 @@
|
||||
'use strict';
|
||||
|
||||
process.env.GSD_TEST_MODE = '1';
|
||||
|
||||
const { describe, test, beforeEach, afterEach } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const crypto = require('node:crypto');
|
||||
const { execFileSync } = require('node:child_process');
|
||||
|
||||
const { install, parseTomlToObject } = require('../bin/install.js');
|
||||
const { createTempDir, cleanup } = require('./helpers.cjs');
|
||||
|
||||
const HOOKS_DIST = path.join(__dirname, '..', 'hooks', 'dist');
|
||||
const BUILD_HOOKS_SCRIPT = path.join(__dirname, '..', 'scripts', 'build-hooks.js');
|
||||
|
||||
function withCodexHome(codexHome, fn) {
|
||||
const prev = process.env.CODEX_HOME;
|
||||
process.env.CODEX_HOME = codexHome;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
if (prev == null) delete process.env.CODEX_HOME;
|
||||
else process.env.CODEX_HOME = prev;
|
||||
}
|
||||
}
|
||||
|
||||
function extractSessionStartCommandsFromHooksJson(value) {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) return [];
|
||||
const table = (value.hooks && typeof value.hooks === 'object' && !Array.isArray(value.hooks))
|
||||
? value.hooks
|
||||
: value;
|
||||
const sessionStart = Array.isArray(table.SessionStart) ? table.SessionStart : [];
|
||||
return sessionStart.flatMap((entry) => {
|
||||
const hooks = entry && Array.isArray(entry.hooks) ? entry.hooks : [];
|
||||
return hooks.map((h) => h && h.command).filter((cmd) => typeof cmd === 'string');
|
||||
});
|
||||
}
|
||||
|
||||
describe('#3427 + #3433 — Codex installer avoids duplicate skills and mixed hook representation', { concurrency: false }, () => {
|
||||
let tmpRoot;
|
||||
let codexHome;
|
||||
|
||||
beforeEach(() => {
|
||||
if (!fs.existsSync(HOOKS_DIST) || fs.readdirSync(HOOKS_DIST).length === 0) {
|
||||
execFileSync(process.execPath, [BUILD_HOOKS_SCRIPT], { stdio: 'pipe' });
|
||||
}
|
||||
tmpRoot = createTempDir('gsd-3427-3433-');
|
||||
codexHome = path.join(tmpRoot, '.codex');
|
||||
fs.mkdirSync(codexHome, { recursive: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup(tmpRoot);
|
||||
});
|
||||
|
||||
test('removes legacy gsd-* copies from ~/.codex/skills and does not regenerate them', () => {
|
||||
const legacySkillBody = '# old managed\n';
|
||||
fs.mkdirSync(path.join(codexHome, 'skills', 'gsd-help'), { recursive: true });
|
||||
fs.writeFileSync(path.join(codexHome, 'skills', 'gsd-help', 'SKILL.md'), legacySkillBody);
|
||||
const legacyHash = crypto.createHash('sha256').update(legacySkillBody).digest('hex');
|
||||
fs.writeFileSync(path.join(codexHome, 'gsd-file-manifest.json'), JSON.stringify({
|
||||
version: 1,
|
||||
files: {
|
||||
'skills/gsd-help/SKILL.md': legacyHash,
|
||||
},
|
||||
}, null, 2));
|
||||
|
||||
fs.mkdirSync(path.join(codexHome, 'skills', 'custom-user-skill'), { recursive: true });
|
||||
fs.writeFileSync(path.join(codexHome, 'skills', 'custom-user-skill', 'SKILL.md'), '# user skill\n');
|
||||
|
||||
withCodexHome(codexHome, () => install(true, 'codex'));
|
||||
|
||||
const skillsDir = path.join(codexHome, 'skills');
|
||||
const entries = fs.existsSync(skillsDir)
|
||||
? fs.readdirSync(skillsDir, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name)
|
||||
: [];
|
||||
|
||||
assert.equal(entries.some((name) => name.startsWith('gsd-')), false);
|
||||
assert.equal(entries.includes('custom-user-skill'), true);
|
||||
});
|
||||
|
||||
test('stores managed SessionStart update hook in hooks.json and removes inline gsd hook from config.toml', () => {
|
||||
const configToml = [
|
||||
'[features]',
|
||||
'codex_hooks = true',
|
||||
'',
|
||||
'[[hooks.SessionStart]]',
|
||||
'[[hooks.SessionStart.hooks]]',
|
||||
'type = "command"',
|
||||
'command = "node /tmp/legacy/.codex/hooks/gsd-check-update.js"',
|
||||
'',
|
||||
].join('\n');
|
||||
fs.writeFileSync(path.join(codexHome, 'config.toml'), configToml);
|
||||
|
||||
fs.writeFileSync(path.join(codexHome, 'hooks.json'), JSON.stringify({
|
||||
SessionStart: [
|
||||
{
|
||||
hooks: [
|
||||
{ type: 'command', command: 'node "/Users/example/bin/user-hook.js"' },
|
||||
],
|
||||
},
|
||||
],
|
||||
}, null, 2));
|
||||
|
||||
withCodexHome(codexHome, () => install(true, 'codex'));
|
||||
|
||||
const parsedToml = parseTomlToObject(fs.readFileSync(path.join(codexHome, 'config.toml'), 'utf8'));
|
||||
const tomlSessionStart = parsedToml.hooks?.SessionStart ?? [];
|
||||
const tomlCommands = tomlSessionStart.flatMap((entry) =>
|
||||
(Array.isArray(entry?.hooks) ? entry.hooks : []).map((hook) => hook.command).filter((cmd) => typeof cmd === 'string')
|
||||
);
|
||||
assert.equal(tomlCommands.some((cmd) => cmd.includes('gsd-check-update.js')), false);
|
||||
|
||||
const hooksJson = JSON.parse(fs.readFileSync(path.join(codexHome, 'hooks.json'), 'utf8'));
|
||||
const sessionStartCommands = extractSessionStartCommandsFromHooksJson(hooksJson);
|
||||
const gsdCommands = sessionStartCommands.filter((cmd) => cmd.includes('gsd-check-update.js'));
|
||||
|
||||
assert.equal(gsdCommands.length, 1);
|
||||
assert.equal(sessionStartCommands.includes('node "/Users/example/bin/user-hook.js"'), true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user