Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

4.1 KB
Raw Blame
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import simpleGit, { type SimpleGit } from 'simple-git';
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import { configureTestGitRepository } from '../../../test-support/configure-git-fixture.test-helper.ts';
import { resolveUpstreamChanges } from './server-factory.ts';

describe('resolveUpstreamChanges', () => {
  let dir: string;
  let git: SimpleGit;

  async function commitAs(name: string, email: string, file: string, body: string) {
    writeFileSync(join(dir, file), body);
    await git.add(file);
    await git
      .env({
        GIT_AUTHOR_NAME: name,
        GIT_AUTHOR_EMAIL: email,
        GIT_COMMITTER_NAME: name,
        GIT_COMMITTER_EMAIL: email,
      })
      .commit(`edit ${file}`);
    return (await git.revparse('HEAD')).trim();
  }

  beforeEach(async () => {
    dir = mkdtempSync(resolve(tmpdir(), 'ok-upstream-changes-'));
    git = simpleGit(dir);
    await git.init(['-b', 'main']);
    configureTestGitRepository(dir);
    await git.addConfig('user.name', 'Seed');
    await git.addConfig('user.email', '[email protected]');
  });

  afterEach(() => {
    rmSync(dir, { recursive: true, force: true });
  });

  test('maps a single incoming commit to its author', async () => {
    const oldHead = await commitAs('Seed', '[email protected]', 'bugs.md', 'v0');
    const newHead = await commitAs('Ana Dev', '[email protected]', 'bugs.md', 'v1');

    const changes = resolveUpstreamChanges(dir, dir, oldHead, newHead);
    expect(changes.get('bugs')).toEqual({ name: 'Ana Dev', email: '[email protected]' });
  });

  test('maps a non-ASCII (Unicode) filename to its author', async () => {
    const oldHead = await commitAs('Seed', '[email protected]', 'café.md', 'v0');
    const newHead = await commitAs('Ana Dev', '[email protected]', 'café.md', 'v1');

    const changes = resolveUpstreamChanges(dir, dir, oldHead, newHead);
    expect(changes.get('café')).toEqual({ name: 'Ana Dev', email: '[email protected]' });
  });

  test('maps each doc to the newest author that touched it', async () => {
    const oldHead = await commitAs('Seed', '[email protected]', 'bugs.md', 'v0');
    await commitAs('Ana Dev', '[email protected]', 'bugs.md', 'v1');
    const newHead = await commitAs('Ben Dev', '[email protected]', 'notes.md', 'n1');

    const changes = resolveUpstreamChanges(dir, dir, oldHead, newHead);
    expect(changes.get('bugs')).toEqual({ name: 'Ana Dev', email: '[email protected]' });
    expect(changes.get('notes')).toEqual({ name: 'Ben Dev', email: '[email protected]' });
  });

  test('skips the merge commit and attributes to the content author', async () => {
    const base = await commitAs('Seed', '[email protected]', 'bugs.md', 'v0');
    const startBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();
    await git.checkoutLocalBranch('feature');
    await commitAs('Ana Dev', '[email protected]', 'feature.md', 'f1');
    await git.checkout(startBranch);
    await git
      .env({
        GIT_AUTHOR_NAME: 'Merger',
        GIT_AUTHOR_EMAIL: '[email protected]',
        GIT_COMMITTER_NAME: 'Merger',
        GIT_COMMITTER_EMAIL: '[email protected]',
      })
      .merge(['--no-ff', 'feature']);
    const newHead = (await git.revparse('HEAD')).trim();

    const changes = resolveUpstreamChanges(dir, dir, base, newHead);
    expect(changes.get('feature')).toEqual({ name: 'Ana Dev', email: '[email protected]' });
  });

  test('ignores non-markdown files', async () => {
    const oldHead = await commitAs('Seed', '[email protected]', 'bugs.md', 'v0');
    const newHead = await commitAs('Ana Dev', '[email protected]', 'data.json', '{}');

    const changes = resolveUpstreamChanges(dir, dir, oldHead, newHead);
    expect(changes.has('data')).toBe(false);
    expect(changes.size).toBe(0);
  });

  test('returns an empty map when oldHead is absent', async () => {
    const newHead = await commitAs('Ana Dev', '[email protected]', 'bugs.md', 'v1');
    expect(resolveUpstreamChanges(dir, dir, null, newHead).size).toBe(0);
  });
});