// allow-test-rule: pending-migration-to-typed-ir [#2974] // Tracked in #2974 for migration to typed-IR assertions per CONTRIBUTING.md // "Prohibited: Raw Text Matching on Test Outputs". Per-file review may // reclassify some entries as source-text-is-the-product during migration. /** * Regression test for #2698: CRLF line endings break agent-block strip regexes * * The legacy `gsd-update-check` hook migration in bin/install.js uses two * separate .replace() calls: * 1. LF-only regex: /\n# GSD Hooks\n\[\[hooks\]\]\nevent = ...\n/ * 2. CRLF-only regex: /\r\n# GSD Hooks\r\n\[\[hooks\]\]\r\nevent = ...\r\n/ * * These patterns fail when config.toml has mixed line endings — e.g. the * "# GSD Hooks" header uses LF but the body uses CRLF, or vice versa. This * can happen when the file is created cross-platform (Windows/Linux), when * editors convert only part of the file, or when a previous GSD version wrote * the block with different EOL than the file's dominant EOL. * * Fix: consolidate to a single \r?\n-aware regex that handles LF, CRLF, and * any mix in a single pass, making the migration robust regardless of the * platform the file was last written on. * * Test approach: write a `.codex/config.toml` with a stale gsd-update-check * block that uses mixed line endings (header in LF, body in CRLF), then run * install() and assert the stale block is gone. * * Note: The local Codex install writes to `.codex/` in the current directory. * Tests `process.chdir(tmpDir)` and write fixtures to `tmpDir/.codex/`. */ 'use strict'; process.env.GSD_TEST_MODE = '1'; const { describe, test, before, beforeEach, afterEach } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const os = require('os'); const { execFileSync } = require('child_process'); const INSTALL_SRC = path.join(__dirname, '..', 'bin', 'install.js'); const BUILD_SCRIPT = path.join(__dirname, '..', 'scripts', 'build-hooks.js'); const { install, GSD_CODEX_MARKER } = require(INSTALL_SRC); // Ensure hooks/dist/ is populated before install tests before(() => { execFileSync(process.execPath, [BUILD_SCRIPT], { encoding: 'utf-8', stdio: 'pipe', }); }); describe('#2698: CRLF stale gsd-update-check block is removed on Codex reinstall', () => { let tmpDir; beforeEach(() => { tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-crlf-install-2698-')); }); afterEach(() => { fs.rmSync(tmpDir, { recursive: true, force: true }); }); // Helper: pre-populate .codex/config.toml with a GSD marker + stale hooks block // using the given line ending for the stale hooks block header, and a potentially // different EOL for the hooks body. This exercises the cross-platform mixed scenario. function writeCodexConfigWithStaleHooks(dir, headerEol, bodyEol) { // Build the stale block with header EOL for the "# GSD Hooks" line, but body EOL // for the content lines (simulates a file edited by two different platforms). const staleBlock = [ '# GSD Hooks', // line that starts the stale section '[[hooks]]', 'event = "SessionStart"', 'command = "node /old/path/gsd-update-check.js"', ].join(bodyEol); // Put the stale block in user content BEFORE the GSD marker. The GSD marker area // will be regenerated by mergeCodexConfig during install(); the stale block in // the user area is what the hooks migration must remove. const content = [ '[features]', 'codex_hooks = true', '', ].join(headerEol) + headerEol + staleBlock + headerEol + headerEol + GSD_CODEX_MARKER + headerEol; const codexDir = path.join(dir, '.codex'); fs.mkdirSync(codexDir, { recursive: true }); const configPath = path.join(codexDir, 'config.toml'); fs.writeFileSync(configPath, content, 'utf-8'); return configPath; } test('LF config.toml: stale gsd-update-check block removed on reinstall', (t) => { const origCwd = process.cwd(); t.after(() => { process.chdir(origCwd); }); process.chdir(tmpDir); writeCodexConfigWithStaleHooks(tmpDir, '\n', '\n'); install(false, 'codex'); const configPath = path.join(tmpDir, '.codex', 'config.toml'); const content = fs.readFileSync(configPath, 'utf-8'); assert.ok( !content.includes('gsd-update-check'), 'Stale gsd-update-check entry must be removed from LF config.toml (#2698)' ); assert.ok( content.includes('gsd-check-update'), 'New gsd-check-update hook must appear after reinstall' ); }); test('CRLF config.toml: stale gsd-update-check block removed on reinstall', (t) => { const origCwd = process.cwd(); t.after(() => { process.chdir(origCwd); }); process.chdir(tmpDir); writeCodexConfigWithStaleHooks(tmpDir, '\r\n', '\r\n'); install(false, 'codex'); const configPath = path.join(tmpDir, '.codex', 'config.toml'); const content = fs.readFileSync(configPath, 'utf-8'); assert.ok( !content.includes('gsd-update-check'), 'Stale gsd-update-check entry must be removed from CRLF config.toml (#2698)' ); assert.ok( content.includes('gsd-check-update'), 'New gsd-check-update hook must appear after reinstall' ); }); test('mixed-EOL config.toml: stale block with LF header but CRLF body removed on reinstall', (t) => { // This is the primary failure case: header line uses LF but the body uses CRLF. // The old LF-only regex requires all-\n separators; the old CRLF-only regex requires // all-\r\n separators. Neither matches a block with mixed endings, so the stale // block survives reinstall with the old code (#2698). const origCwd = process.cwd(); t.after(() => { process.chdir(origCwd); }); process.chdir(tmpDir); // headerEol='\n' (file dominant), bodyEol='\r\n' (hook block from another platform) writeCodexConfigWithStaleHooks(tmpDir, '\n', '\r\n'); install(false, 'codex'); const configPath = path.join(tmpDir, '.codex', 'config.toml'); const content = fs.readFileSync(configPath, 'utf-8'); assert.ok( !content.includes('gsd-update-check'), [ 'Stale gsd-update-check block with mixed LF/CRLF endings must be removed (#2698).', 'Old code used two separate LF-only and CRLF-only regexes; neither matched mixed content.', 'Fix consolidates to a single \\r?\\n-aware regex.', ].join(' ') ); }); });