Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

1.9 KB
Raw Blame
import { symlinkSync } from 'node:fs';
import { basename, join } from 'node:path';
import { afterAll, describe, expect, test } from 'vitest';
import { createTempDirFactory } from '../../../../../../test-support/temp-dir.test-helper.ts';
import { walkFiles } from './fs-walk.ts';
import { makeTree as populateTree } from './mktree.test-helper.ts';

const makeTempDir = createTempDirFactory(afterAll);

function makeTree(files: Parameters<typeof populateTree>[1]): string {
  return populateTree(makeTempDir('ok-migrate-'), files);
}

describe('walkFiles', () => {
  test('lists files recursively', () => {
    const root = makeTree({
      'a.md': 'A',
      'sub/b.md': 'B',
      'sub/deep/c.md': 'C',
    });
    const names = walkFiles(root)
      .map((f) => basename(f))
      .sort();
    expect(names).toEqual(['a.md', 'b.md', 'c.md']);
  });

  test('skips dotfiles and dot-directories (.ok, .git)', () => {
    const root = makeTree({
      'keep.md': 'K',
      '.okignore': 'x',
      '.ok/local/server.lock': 'lock',
      '.git/HEAD': 'ref',
    });
    const names = walkFiles(root).map((f) => basename(f));
    expect(names).toEqual(['keep.md']);
  });

  test('returns empty for a missing directory without throwing', () => {
    expect(walkFiles('/no/such/dir/ok-migrate-xyz')).toEqual([]);
  });

  test('does not descend into directory symlinks (ancestor link would cycle forever)', () => {
    const root = makeTree({ 'a.md': 'A', 'sub/b.md': 'B' });
    symlinkSync(root, join(root, 'sub', 'loop'));
    const names = walkFiles(root)
      .map((f) => basename(f))
      .sort();
    expect(names).toEqual(['a.md', 'b.md']);
  });

  test('includes symlinks to files', () => {
    const root = makeTree({ 'real.md': 'R' });
    symlinkSync(join(root, 'real.md'), join(root, 'alias.md'));
    const names = walkFiles(root)
      .map((f) => basename(f))
      .sort();
    expect(names).toEqual(['alias.md', 'real.md']);
  });
});