import test from 'node:test';
import assert from 'node:assert/strict';
import { normalizeRollbackDiffText } from './chat-runtime-rollback-service.js';

test('normalizeRollbackDiffText strips chat resource prefixes from stored diff paths', () => {
  const normalized = normalizeRollbackDiffText([
    'diff --git a/api/chat/resources/.codex_chat/chat-room/resource/src/app/main/MainHeader.tsx b/api/chat/resources/.codex_chat/chat-room/resource/src/app/main/MainHeader.tsx',
    '--- a/api/chat/resources/.codex_chat/chat-room/resource/src/app/main/MainHeader.tsx',
    '+++ b/api/chat/resources/.codex_chat/chat-room/resource/src/app/main/MainHeader.tsx',
    '@@',
    '+const value = 1;',
    '',
  ].join('\n'));

  assert.match(normalized, /^diff --git a\/src\/app\/main\/MainHeader\.tsx b\/src\/app\/main\/MainHeader\.tsx/m);
  assert.match(normalized, /^--- a\/src\/app\/main\/MainHeader\.tsx$/m);
  assert.match(normalized, /^\+\+\+ b\/src\/app\/main\/MainHeader\.tsx$/m);
});

test('normalizeRollbackDiffText keeps the final repo path when resource prefixes were duplicated', () => {
  const normalized = normalizeRollbackDiffText(
    'diff --git a/api/chat/resources/.codex_chat/chat-room/resource/etc/servers/work-server/api/chat/resources/.codex_chat/chat-room/resource/README.md b/api/chat/resources/.codex_chat/chat-room/resource/etc/servers/work-server/api/chat/resources/.codex_chat/chat-room/resource/README.md\n',
  );

  assert.equal(normalized.trim(), 'diff --git a/README.md b/README.md');
});
