import { describe, it, expect, mock, afterEach, beforeEach } from 'bun:test';
import { mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
import path, { join } from 'path';
import { tmpdir } from 'os';

mock.module('../../src/utils/logger.js', () => ({
  logger: {
    info: () => {},
    debug: () => {},
    warn: () => {},
    error: () => {},
    formatTool: (toolName: string, toolInput?: any) => toolInput ? `${toolName}(...)` : toolName,
  },
}));

mock.module('../../src/shared/worker-utils.js', () => ({
  getWorkerPort: () => 37777,
  getWorkerHost: () => '127.0.0.1',
  workerHttpRequest: (apiPath: string, options?: any) => {
    const url = `http://127.0.0.1:37777${apiPath}`;
    return globalThis.fetch(url, {
      method: options?.method ?? 'GET',
      headers: options?.headers,
      body: options?.body,
    });
  },
  clearPortCache: () => {},
  ensureWorkerRunning: () => Promise.resolve(true),
  fetchWithTimeout: (url: string, init: any, timeoutMs: number) => globalThis.fetch(url, init),
  buildWorkerUrl: (apiPath: string) => `http://127.0.0.1:37777${apiPath}`,
}));

import {
  replaceTaggedContent,
  formatTimelineForClaudeMd,
  writeClaudeMdToFolder,
  updateFolderClaudeMdFiles,
  getTargetFilename
} from '../../src/utils/claude-md-utils.js';

let tempDir: string;
const originalFetch = global.fetch;

beforeEach(() => {
  tempDir = join(tmpdir(), `test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
  mkdirSync(tempDir, { recursive: true });
});

afterEach(() => {
  mock.restore();
  global.fetch = originalFetch;
  try {
    rmSync(tempDir, { recursive: true, force: true });
  } catch {
    // Ignore cleanup errors
  }
});

describe('replaceTaggedContent', () => {
  it('should wrap new content in tags when existing content is empty', () => {
    const result = replaceTaggedContent('', 'New content here');

    expect(result).toBe('<claude-mem-context>\nNew content here\n</claude-mem-context>');
  });

  it('should replace only tagged section when existing content has tags', () => {
    const existingContent = 'User content before\n<claude-mem-context>\nOld generated content\n</claude-mem-context>\nUser content after';
    const newContent = 'New generated content';

    const result = replaceTaggedContent(existingContent, newContent);

    expect(result).toBe('User content before\n<claude-mem-context>\nNew generated content\n</claude-mem-context>\nUser content after');
  });

  it('should append tagged content with separator when no tags exist in existing content', () => {
    const existingContent = 'User written documentation';
    const newContent = 'Generated timeline';

    const result = replaceTaggedContent(existingContent, newContent);

    expect(result).toBe('User written documentation\n\n<claude-mem-context>\nGenerated timeline\n</claude-mem-context>');
  });

  it('should append when only opening tag exists (no matching end tag)', () => {
    const existingContent = 'Some content\n<claude-mem-context>\nIncomplete tag section';
    const newContent = 'New content';

    const result = replaceTaggedContent(existingContent, newContent);

    expect(result).toBe('Some content\n<claude-mem-context>\nIncomplete tag section\n\n<claude-mem-context>\nNew content\n</claude-mem-context>');
  });

  it('should append when only closing tag exists (no matching start tag)', () => {
    const existingContent = 'Some content\n</claude-mem-context>\nMore content';
    const newContent = 'New content';

    const result = replaceTaggedContent(existingContent, newContent);

    expect(result).toBe('Some content\n</claude-mem-context>\nMore content\n\n<claude-mem-context>\nNew content\n</claude-mem-context>');
  });

  it('should preserve newlines in new content', () => {
    const existingContent = '<claude-mem-context>\nOld content\n</claude-mem-context>';
    const newContent = 'Line 1\nLine 2\nLine 3';

    const result = replaceTaggedContent(existingContent, newContent);

    expect(result).toBe('<claude-mem-context>\nLine 1\nLine 2\nLine 3\n</claude-mem-context>');
  });
});

describe('formatTimelineForClaudeMd', () => {
  it('should return empty string for empty input', () => {
    const result = formatTimelineForClaudeMd('');

    expect(result).toBe('');
  });

  it('should return empty string when no table rows exist', () => {
    const input = 'Just some plain text without table rows';

    const result = formatTimelineForClaudeMd(input);

    expect(result).toBe('');
  });

  it('should parse single observation row correctly', () => {
    const input = '| #123 | 4:30 PM | 🔵 | User logged in | ~100 |';

    const result = formatTimelineForClaudeMd(input);

    expect(result).toContain('#123');
    expect(result).toContain('4:30 PM');
    expect(result).toContain('🔵');
    expect(result).toContain('User logged in');
    expect(result).toContain('~100');
  });

  it('should parse ditto mark for repeated time correctly', () => {
    const input = `| #123 | 4:30 PM | 🔵 | First action | ~100 |
| #124 | ″ | 🔵 | Second action | ~150 |`;

    const result = formatTimelineForClaudeMd(input);

    expect(result).toContain('#123');
    expect(result).toContain('#124');
    expect(result).toContain('4:30 PM');
    expect(result).toContain('"');
  });

  it('should parse session ID format (#S123) correctly', () => {
    const input = '| #S123 | 4:30 PM | 🟣 | Session started | ~200 |';

    const result = formatTimelineForClaudeMd(input);

    expect(result).toContain('#S123');
    expect(result).toContain('4:30 PM');
    expect(result).toContain('🟣');
    expect(result).toContain('Session started');
  });
});

describe('writeClaudeMdToFolder', () => {
  it('should skip non-existent folders (fix for spurious directory creation)', () => {
    const folderPath = join(tempDir, 'non-existent-folder');
    const content = '# Recent Activity\n\nTest content';

    writeClaudeMdToFolder(folderPath, content);

    expect(existsSync(folderPath)).toBe(false);
    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should create CLAUDE.md in existing folder', () => {
    const folderPath = join(tempDir, 'existing-folder');
    mkdirSync(folderPath, { recursive: true });
    const content = '# Recent Activity\n\nTest content';

    writeClaudeMdToFolder(folderPath, content);

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(true);

    const fileContent = readFileSync(claudeMdPath, 'utf-8');
    expect(fileContent).toContain('<claude-mem-context>');
    expect(fileContent).toContain('Test content');
    expect(fileContent).toContain('</claude-mem-context>');
  });

  it('should preserve user content outside tags', () => {
    const folderPath = join(tempDir, 'preserve-test');
    mkdirSync(folderPath, { recursive: true });

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    const userContent = 'User-written docs\n<claude-mem-context>\nOld content\n</claude-mem-context>\nMore user docs';
    writeFileSync(claudeMdPath, userContent);

    const newContent = 'New generated content';
    writeClaudeMdToFolder(folderPath, newContent);

    const fileContent = readFileSync(claudeMdPath, 'utf-8');
    expect(fileContent).toContain('User-written docs');
    expect(fileContent).toContain('New generated content');
    expect(fileContent).toContain('More user docs');
    expect(fileContent).not.toContain('Old content');
  });

  it('should not create nested directories (fix for spurious directory creation)', () => {
    const folderPath = join(tempDir, 'deep', 'nested', 'folder');
    const content = 'Nested content';

    writeClaudeMdToFolder(folderPath, content);

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
    expect(existsSync(join(tempDir, 'deep'))).toBe(false);
  });

  it('should not leave .tmp file after write (atomic write)', () => {
    const folderPath = join(tempDir, 'atomic-test');
    mkdirSync(folderPath, { recursive: true });
    const content = 'Atomic write test';

    writeClaudeMdToFolder(folderPath, content);

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    const tempFilePath = `${claudeMdPath}.tmp`;

    expect(existsSync(claudeMdPath)).toBe(true);
    expect(existsSync(tempFilePath)).toBe(false);
  });
});

describe('issue #1165 - prevent CLAUDE.md inside .git directories', () => {
  it('should not write CLAUDE.md when folder is inside .git/', () => {
    const gitRefsFolder = join(tempDir, '.git', 'refs');
    mkdirSync(gitRefsFolder, { recursive: true });

    writeClaudeMdToFolder(gitRefsFolder, 'Should not be written');

    const claudeMdPath = join(gitRefsFolder, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should not write CLAUDE.md when folder is .git itself', () => {
    const gitFolder = join(tempDir, '.git');
    mkdirSync(gitFolder, { recursive: true });

    writeClaudeMdToFolder(gitFolder, 'Should not be written');

    const claudeMdPath = join(gitFolder, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should not write CLAUDE.md to deeply nested .git path', () => {
    const deepGitPath = join(tempDir, 'project', '.git', 'hooks');
    mkdirSync(deepGitPath, { recursive: true });

    writeClaudeMdToFolder(deepGitPath, 'Should not be written');

    const claudeMdPath = join(deepGitPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should still write CLAUDE.md to normal folders', () => {
    const normalFolder = join(tempDir, 'src', 'git-utils');
    mkdirSync(normalFolder, { recursive: true });

    writeClaudeMdToFolder(normalFolder, 'Should be written');

    const claudeMdPath = join(normalFolder, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(true);
  });
});

describe('updateFolderClaudeMdFiles', () => {
  it('should skip when filePaths is empty', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles([], 'test-project', 37777);

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should fetch timeline and write CLAUDE.md', async () => {
    const folderPath = join(tempDir, 'api-test');
    mkdirSync(folderPath, { recursive: true }); 
    const filePath = join(folderPath, 'test.ts');

    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test observation | ~100 |'
      }]
    };

    global.fetch = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));

    await updateFolderClaudeMdFiles([filePath], 'test-project', 37777);

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(true);

    const content = readFileSync(claudeMdPath, 'utf-8');
    expect(content).toContain('Recent Activity');
    expect(content).toContain('#123');
    expect(content).toContain('Test observation');
  });

  it('should deduplicate folders from multiple files', async () => {
    const folderPath = join(tempDir, 'dedup-test');
    const file1 = join(folderPath, 'file1.ts');
    const file2 = join(folderPath, 'file2.ts');

    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles([file1, file2], 'test-project', 37777);

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  it('should handle API errors gracefully (404 response)', async () => {
    const folderPath = join(tempDir, 'error-test');
    const filePath = join(folderPath, 'test.ts');

    global.fetch = mock(() => Promise.resolve({
      ok: false,
      status: 404
    } as Response));

    await expect(updateFolderClaudeMdFiles([filePath], 'test-project', 37777)).resolves.toBeUndefined();

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should handle network errors gracefully (fetch throws)', async () => {
    const folderPath = join(tempDir, 'network-error-test');
    const filePath = join(folderPath, 'test.ts');

    global.fetch = mock(() => Promise.reject(new Error('Network error')));

    await expect(updateFolderClaudeMdFiles([filePath], 'test-project', 37777)).resolves.toBeUndefined();

    const claudeMdPath = join(folderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(false);
  });

  it('should resolve relative paths using projectRoot', async () => {
    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test observation | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/utils/file.ts'],  // relative path
      'test-project',
      37777,
      '/home/user/my-project'  
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/home/user/my-project/src/utils'));
  });

  it('should accept absolute paths within projectRoot and use them directly', async () => {
    const folderPath = join(tempDir, 'absolute-path-test');
    const filePath = join(folderPath, 'file.ts');

    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test observation | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      [filePath],  // absolute path within tempDir
      'test-project',
      37777,
      tempDir  
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent(folderPath));
  });

  it('should work without projectRoot for backward compatibility', async () => {
    const folderPath = join(tempDir, 'backward-compat-test');
    const filePath = join(folderPath, 'file.ts');

    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test observation | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      [filePath],  // absolute path
      'test-project',
      37777
      // No projectRoot - backward compatibility
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent(folderPath));
  });

  it('should handle projectRoot with trailing slash correctly', async () => {
    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test observation | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/utils/file.ts'],
      'test-project',
      37777,
      '/home/user/my-project/'  
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/home/user/my-project/src/utils'));
    expect(callUrl.replace('http://', '')).not.toContain('//');
  });

  it('should write CLAUDE.md to resolved projectRoot path', async () => {
    const subfolderPath = join(tempDir, 'project-root-write-test', 'src', 'utils');
    mkdirSync(subfolderPath, { recursive: true }); 

    const apiResponse = {
      content: [{
        text: '| #456 | 5:00 PM | 🔵 | Written to correct path | ~200 |'
      }]
    };

    global.fetch = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));

    await updateFolderClaudeMdFiles(
      ['src/utils/file.ts'],
      'test-project',
      37777,
      join(tempDir, 'project-root-write-test')
    );

    const claudeMdPath = join(subfolderPath, 'CLAUDE.md');
    expect(existsSync(claudeMdPath)).toBe(true);

    const content = readFileSync(claudeMdPath, 'utf-8');
    expect(content).toContain('Written to correct path');
    expect(content).toContain('#456');
  });

  it('should deduplicate relative paths from same folder with projectRoot', async () => {
    const apiResponse = {
      content: [{
        text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |'
      }]
    };

    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/utils/file1.ts', 'src/utils/file2.ts', 'src/utils/file3.ts'],
      'test-project',
      37777,
      '/home/user/project'
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/home/user/project/src/utils'));
  });

  it('should handle empty string paths gracefully with projectRoot', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['', 'src/file.ts', ''],  // includes empty strings
      'test-project',
      37777,
      '/home/user/project'
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/home/user/project/src'));
  });
});

describe('path validation in updateFolderClaudeMdFiles', () => {
  it('should reject tilde paths', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['~/.claude-mem/logs/worker.log'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject URLs', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['https://example.com/file.ts'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject paths with spaces', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['PR #610 on thedotmack/CLAUDE.md'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject paths with hash symbols', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['issue#123/file.ts'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject path traversal outside project', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['../../../etc/passwd'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject absolute paths outside project root', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['/etc/passwd'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should accept absolute paths within project root', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    const absolutePathInProject = path.join(tempDir, 'src', 'utils', 'file.ts');

    await updateFolderClaudeMdFiles(
      [absolutePathInProject],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  it('should accept absolute paths when no projectRoot is provided', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['/home/user/valid/file.ts'],
      'test-project',
      37777
      // No projectRoot provided
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  it('should accept valid relative paths', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/utils/logger.ts'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });
});

describe('issue #814 - reject consecutive duplicate path segments', () => {
  it('should reject paths with consecutive duplicate segments like frontend/frontend/', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['frontend/src/file.ts'],
      'test-project',
      37777,
      path.join(tempDir, 'frontend')  
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should reject paths with consecutive duplicate segments like src/src/', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/components/file.ts'],
      'test-project',
      37777,
      path.join(tempDir, 'src')  
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should allow paths with non-consecutive duplicate segments', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/components/src/utils/file.ts'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });
});

describe('issue #859 - skip folders with active CLAUDE.md', () => {
  it('should skip folder when CLAUDE.md was read in observation', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['/project/src/utils/CLAUDE.md'],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip folder when CLAUDE.md was modified in observation', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['/project/src/CLAUDE.md'],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should process other folders even when one has active CLAUDE.md', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      [
        '/project/src/utils/CLAUDE.md',  // Should skip /project/src/utils
        '/project/src/services/api.ts'   
      ],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/project/src/services'));
    expect(callUrl).not.toContain(encodeURIComponent('/project/src/utils'));
  });

  it('should handle relative CLAUDE.md paths with projectRoot', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/components/CLAUDE.md'],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip only the specific folder containing active CLAUDE.md', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      [
        '/project/src/a/CLAUDE.md',
        '/project/src/b/CLAUDE.md',
        '/project/src/c/file.ts'
      ],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/project/src/c'));
  });

  it('should still exclude project root even when CLAUDE.md filter would allow it', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    const projectRoot = join(tempDir, 'git-project');
    const gitDir = join(projectRoot, '.git');
    mkdirSync(gitDir, { recursive: true });

    await updateFolderClaudeMdFiles(
      [join(projectRoot, 'file.ts')],
      'test-project',
      37777,
      projectRoot
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });
});

describe('issue #912 - skip unsafe directories for CLAUDE.md generation', () => {
  it('should skip node_modules directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['node_modules/lodash/index.js'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip .git directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['.git/refs/heads/main'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip Android res/ directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['app/src/main/res/layout/activity_main.xml'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip build/ directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['build/outputs/apk/debug/app-debug.apk'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip __pycache__/ directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/__pycache__/module.cpython-311.pyc'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should allow safe directories like src/', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['src/utils/file.ts'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
  });

  it('should skip deeply nested unsafe directories', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['packages/frontend/node_modules/react/index.js'],
      'test-project',
      37777,
      tempDir
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });
});

describe('getTargetFilename', () => {
  it('should return CLAUDE.md by default', () => {
    const settings = { CLAUDE_MEM_FOLDER_USE_LOCAL_MD: 'false' } as any;
    expect(getTargetFilename(settings)).toBe('CLAUDE.md');
  });

  it('should return CLAUDE.local.md when USE_LOCAL_MD is true', () => {
    const settings = { CLAUDE_MEM_FOLDER_USE_LOCAL_MD: 'true' } as any;
    expect(getTargetFilename(settings)).toBe('CLAUDE.local.md');
  });

  it('should return CLAUDE.md when USE_LOCAL_MD is undefined', () => {
    const settings = {} as any;
    expect(getTargetFilename(settings)).toBe('CLAUDE.md');
  });
});

describe('CLAUDE.local.md support', () => {
  it('should write CLAUDE.local.md when targetFilename is specified', () => {
    const folderPath = join(tempDir, 'local-md-test');
    mkdirSync(folderPath, { recursive: true });
    const content = '# Recent Activity\n\nTest content';

    writeClaudeMdToFolder(folderPath, content, 'CLAUDE.local.md');

    const localMdPath = join(folderPath, 'CLAUDE.local.md');
    const regularMdPath = join(folderPath, 'CLAUDE.md');

    expect(existsSync(localMdPath)).toBe(true);
    expect(existsSync(regularMdPath)).toBe(false);

    const fileContent = readFileSync(localMdPath, 'utf-8');
    expect(fileContent).toContain('<claude-mem-context>');
    expect(fileContent).toContain('Test content');
    expect(fileContent).toContain('</claude-mem-context>');
  });

  it('should preserve user content in CLAUDE.local.md outside tags', () => {
    const folderPath = join(tempDir, 'local-preserve-test');
    mkdirSync(folderPath, { recursive: true });

    const localMdPath = join(folderPath, 'CLAUDE.local.md');
    const userContent = 'My personal notes\n<claude-mem-context>\nOld content\n</claude-mem-context>\nMore notes';
    writeFileSync(localMdPath, userContent);

    writeClaudeMdToFolder(folderPath, 'New generated content', 'CLAUDE.local.md');

    const fileContent = readFileSync(localMdPath, 'utf-8');
    expect(fileContent).toContain('My personal notes');
    expect(fileContent).toContain('New generated content');
    expect(fileContent).toContain('More notes');
    expect(fileContent).not.toContain('Old content');
  });

  it('should not leave .tmp file after writing CLAUDE.local.md', () => {
    const folderPath = join(tempDir, 'local-atomic-test');
    mkdirSync(folderPath, { recursive: true });

    writeClaudeMdToFolder(folderPath, 'Atomic write test', 'CLAUDE.local.md');

    const localMdPath = join(folderPath, 'CLAUDE.local.md');
    const tempFilePath = `${localMdPath}.tmp`;

    expect(existsSync(localMdPath)).toBe(true);
    expect(existsSync(tempFilePath)).toBe(false);
  });

  it('should skip folder when CLAUDE.local.md was read in observation', async () => {
    const fetchMock = mock(() => Promise.resolve({ ok: true } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      ['/project/src/utils/CLAUDE.local.md'],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).not.toHaveBeenCalled();
  });

  it('should skip folder when either CLAUDE.md or CLAUDE.local.md was read', async () => {
    const apiResponse = {
      content: [{ text: '| #123 | 4:30 PM | 🔵 | Test | ~100 |' }]
    };
    const fetchMock = mock(() => Promise.resolve({
      ok: true,
      json: () => Promise.resolve(apiResponse)
    } as Response));
    global.fetch = fetchMock;

    await updateFolderClaudeMdFiles(
      [
        '/project/src/a/CLAUDE.md',          // Skip folder a (regular)
        '/project/src/b/CLAUDE.local.md',    // Skip folder b (local)
        '/project/src/c/file.ts'             
      ],
      'test-project',
      37777,
      '/project'
    );

    expect(fetchMock).toHaveBeenCalledTimes(1);
    const callUrl = (fetchMock.mock.calls[0] as unknown[])[0] as string;
    expect(callUrl).toContain(encodeURIComponent('/project/src/c'));
    expect(callUrl).not.toContain(encodeURIComponent('/project/src/a'));
    expect(callUrl).not.toContain(encodeURIComponent('/project/src/b'));
  });
});
