Files
gbrain/test/sync.test.ts
T
27eb87f1f4 feat: slugify file paths with spaces and special characters (v0.5.1) (#29)
* feat: slugify file paths with spaces and special characters

Apple Notes files (e.g., "2017-05-03 ohmygreen.md") now get clean,
URL-safe slugs instead of raw filenames with spaces. Spaces become
hyphens, special chars are stripped, accented chars normalize to ASCII.

Both import (inferSlug) and sync (pathToSlug) pipelines now use the
same slugifyPath() function, eliminating the case-preservation mismatch.

* feat: one-time migration to slugify existing page slugs

Extends Migration interface with optional TypeScript handler for
application-level data transformations. Adds version 2 migration that
renames all existing slugs to their slugified form, including link
rewriting. Collision handling via try/catch + warning.

* test: slugify unit tests, E2E tests, and updated expectations

22 new unit tests for slugifySegment and slugifyPath covering spaces,
special chars, unicode, dots, empty segments, and all 4 bug report
examples. Updated pathToSlug tests for new lowercase behavior. Updated
E2E tests for slugified Apple Notes slugs. Added 2 new E2E tests for
space-named file import and sync.

* chore: bump version and changelog (v0.5.1)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* docs: fix changelog example to show directory with spaces

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 11:42:32 -07:00

188 lines
6.4 KiB
TypeScript

import { describe, test, expect } from 'bun:test';
import { buildSyncManifest, isSyncable, pathToSlug } from '../src/core/sync.ts';
describe('buildSyncManifest', () => {
test('parses A/M/D entries from single commit', () => {
const output = `A\tpeople/new-person.md\nM\tpeople/existing-person.md\nD\tpeople/deleted-person.md`;
const manifest = buildSyncManifest(output);
expect(manifest.added).toEqual(['people/new-person.md']);
expect(manifest.modified).toEqual(['people/existing-person.md']);
expect(manifest.deleted).toEqual(['people/deleted-person.md']);
expect(manifest.renamed).toEqual([]);
});
test('parses R100 rename entries', () => {
const output = `R100\tpeople/old-name.md\tpeople/new-name.md`;
const manifest = buildSyncManifest(output);
expect(manifest.renamed).toEqual([{ from: 'people/old-name.md', to: 'people/new-name.md' }]);
expect(manifest.added).toEqual([]);
expect(manifest.modified).toEqual([]);
expect(manifest.deleted).toEqual([]);
});
test('parses partial rename (R075)', () => {
const output = `R075\tpeople/old.md\tpeople/new.md`;
const manifest = buildSyncManifest(output);
expect(manifest.renamed).toEqual([{ from: 'people/old.md', to: 'people/new.md' }]);
});
test('handles empty diff', () => {
const manifest = buildSyncManifest('');
expect(manifest.added).toEqual([]);
expect(manifest.modified).toEqual([]);
expect(manifest.deleted).toEqual([]);
expect(manifest.renamed).toEqual([]);
});
test('handles mixed entries with blank lines', () => {
const output = `A\tpeople/a.md\n\nM\tpeople/b.md\n\nD\tpeople/c.md`;
const manifest = buildSyncManifest(output);
expect(manifest.added).toEqual(['people/a.md']);
expect(manifest.modified).toEqual(['people/b.md']);
expect(manifest.deleted).toEqual(['people/c.md']);
});
test('skips malformed lines', () => {
const output = `A\tpeople/a.md\ngarbage line\nM\tpeople/b.md`;
const manifest = buildSyncManifest(output);
expect(manifest.added).toEqual(['people/a.md']);
expect(manifest.modified).toEqual(['people/b.md']);
});
});
describe('isSyncable', () => {
test('accepts normal .md files', () => {
expect(isSyncable('people/pedro-franceschi.md')).toBe(true);
expect(isSyncable('meetings/2026-04-03-lunch.md')).toBe(true);
expect(isSyncable('daily/2026-04-05.md')).toBe(true);
expect(isSyncable('notes.md')).toBe(true);
});
test('rejects non-.md files', () => {
expect(isSyncable('people/photo.jpg')).toBe(false);
expect(isSyncable('config.json')).toBe(false);
expect(isSyncable('src/cli.ts')).toBe(false);
});
test('rejects files in hidden directories', () => {
expect(isSyncable('.git/config')).toBe(false);
expect(isSyncable('.obsidian/plugins.md')).toBe(false);
expect(isSyncable('people/.hidden/secret.md')).toBe(false);
});
test('rejects .raw/ sidecar directories', () => {
expect(isSyncable('people/pedro.raw/source.md')).toBe(false);
expect(isSyncable('dir/.raw/notes.md')).toBe(false);
});
test('rejects skip-list basenames', () => {
expect(isSyncable('schema.md')).toBe(false);
expect(isSyncable('index.md')).toBe(false);
expect(isSyncable('log.md')).toBe(false);
expect(isSyncable('README.md')).toBe(false);
expect(isSyncable('people/README.md')).toBe(false);
});
test('rejects ops/ directory', () => {
expect(isSyncable('ops/deploy-log.md')).toBe(false);
expect(isSyncable('ops/config.md')).toBe(false);
});
});
describe('pathToSlug', () => {
test('strips .md extension and lowercases', () => {
expect(pathToSlug('people/pedro-franceschi.md')).toBe('people/pedro-franceschi');
});
test('lowercases uppercase paths', () => {
expect(pathToSlug('People/Pedro-Franceschi.md')).toBe('people/pedro-franceschi');
});
test('strips leading slash', () => {
expect(pathToSlug('/people/pedro.md')).toBe('people/pedro');
});
test('normalizes backslash separators', () => {
expect(pathToSlug('people\\pedro.md')).toBe('people/pedro');
});
test('handles flat files', () => {
expect(pathToSlug('notes.md')).toBe('notes');
});
test('handles nested paths', () => {
expect(pathToSlug('projects/gbrain/spec.md')).toBe('projects/gbrain/spec');
});
test('adds repo prefix when provided', () => {
expect(pathToSlug('people/pedro.md', 'brain')).toBe('brain/people/pedro');
});
test('no prefix when not provided', () => {
expect(pathToSlug('people/pedro.md')).toBe('people/pedro');
});
test('handles empty string', () => {
expect(pathToSlug('')).toBe('');
});
test('handles file with only extension', () => {
expect(pathToSlug('.md')).toBe('');
});
test('slugifies spaces to hyphens', () => {
expect(pathToSlug('Apple Notes/2017-05-03 ohmygreen.md')).toBe('apple-notes/2017-05-03-ohmygreen');
});
test('strips special characters', () => {
expect(pathToSlug('notes/meeting (march 2024).md')).toBe('notes/meeting-march-2024');
});
});
describe('isSyncable edge cases', () => {
test('rejects uppercase .MD extension', () => {
// isSyncable checks path.endsWith('.md'), so .MD should fail
expect(isSyncable('people/someone.MD')).toBe(false);
});
test('rejects files with no extension', () => {
expect(isSyncable('README')).toBe(false);
});
test('accepts deeply nested .md files', () => {
expect(isSyncable('a/b/c/d/e/f/deep.md')).toBe(true);
});
test('rejects .md files inside nested hidden dirs', () => {
expect(isSyncable('docs/.internal/secret.md')).toBe(false);
});
});
describe('buildSyncManifest edge cases', () => {
test('handles tab-separated fields correctly', () => {
const output = "A\tpath/to/file.md";
const manifest = buildSyncManifest(output);
expect(manifest.added).toEqual(['path/to/file.md']);
});
test('handles multiple renames', () => {
const output = [
'R100\told/a.md\tnew/a.md',
'R095\told/b.md\tnew/b.md',
].join('\n');
const manifest = buildSyncManifest(output);
expect(manifest.renamed).toHaveLength(2);
expect(manifest.renamed[0].from).toBe('old/a.md');
expect(manifest.renamed[1].from).toBe('old/b.md');
});
test('ignores unknown status codes', () => {
const output = "X\tunknown/file.md";
const manifest = buildSyncManifest(output);
expect(manifest.added).toEqual([]);
expect(manifest.modified).toEqual([]);
expect(manifest.deleted).toEqual([]);
expect(manifest.renamed).toEqual([]);
});
});