Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

13.3 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('./execGit.js', () => ({
  execGit: vi.fn()
}));

vi.mock('./fileUtils.js', () => ({
  PATHS: { root: '/mock' }
}));

import { execGit } from './execGit.js';
import {
  classifyOriginRemote,
  parseGitRemoteUrl,
  getOriginInfo,
  readOriginRemoteUrl,
  redactRemoteUrlCredentials,
  UPSTREAM_OWNER,
  UPSTREAM_REPO,
  UPSTREAM_FULL_NAME,
  isUpstreamOwner,
  isUpstreamFullName
} from './gitRemote.js';

describe('upstream identity helpers', () => {
  it('matches upstream owners and full names case-insensitively', () => {
    expect(isUpstreamOwner('Atomantic/PortOS')).toBe(true);
    expect(isUpstreamOwner('ATOMANTIC')).toBe(true);
    expect(isUpstreamOwner('other/PortOS')).toBe(false);
    expect(isUpstreamFullName('Atomantic/PortOS')).toBe(true);
    expect(isUpstreamFullName('  ATOMANTIC/PORTOS  ')).toBe(true);
    expect(isUpstreamFullName('atomantic/other')).toBe(false);
  });
});

describe('parseGitRemoteUrl', () => {
  it('parses SCP-style SSH URLs', () => {
    expect(parseGitRemoteUrl('[email protected]:atomantic/PortOS.git')).toEqual({
      host: 'github.com', owner: 'atomantic', repo: 'PortOS'
    });
    expect(parseGitRemoteUrl('[email protected]:alice/my-fork')).toEqual({
      host: 'github.com', owner: 'alice', repo: 'my-fork'
    });
  });

  it('parses HTTPS URLs', () => {
    expect(parseGitRemoteUrl('https://github.com/atomantic/PortOS.git')).toEqual({
      host: 'github.com', owner: 'atomantic', repo: 'PortOS'
    });
    expect(parseGitRemoteUrl('https://github.com/alice/my-fork')).toEqual({
      host: 'github.com', owner: 'alice', repo: 'my-fork'
    });
  });

  it('parses HTTPS URLs with embedded credentials', () => {
    expect(parseGitRemoteUrl('https://user:[email protected]/alice/my-fork.git')).toEqual({
      host: 'github.com', owner: 'alice', repo: 'my-fork'
    });
  });

  it('parses ssh:// URLs', () => {
    expect(parseGitRemoteUrl('ssh://[email protected]/atomantic/PortOS.git')).toEqual({
      host: 'github.com', owner: 'atomantic', repo: 'PortOS'
    });
  });

  it('handles enterprise/non-github hosts', () => {
    expect(parseGitRemoteUrl('[email protected]:team/repo.git')).toEqual({
      host: 'git.example.com', owner: 'team', repo: 'repo'
    });
    expect(parseGitRemoteUrl('https://gitlab.com/group/proj.git')).toEqual({
      host: 'gitlab.com', owner: 'group', repo: 'proj'
    });
  });

  it('strips trailing slashes and .git suffix only', () => {
    expect(parseGitRemoteUrl('https://github.com/alice/PortOS/')).toEqual({
      host: 'github.com', owner: 'alice', repo: 'PortOS'
    });
    expect(parseGitRemoteUrl('https://github.com/alice/portos.git.backup')).toEqual({
      host: 'github.com', owner: 'alice', repo: 'portos.git.backup'
    });
  });

  it('returns null for invalid input', () => {
    expect(parseGitRemoteUrl('')).toBeNull();
    expect(parseGitRemoteUrl(null)).toBeNull();
    expect(parseGitRemoteUrl(undefined)).toBeNull();
    expect(parseGitRemoteUrl(123)).toBeNull();
    expect(parseGitRemoteUrl('not-a-url')).toBeNull();
  });

  it('rejects URLs with extra path segments beyond owner/repo', () => {
    // SCP-style with extra segment would otherwise produce repo="repo/extra"
    expect(parseGitRemoteUrl('[email protected]:owner/repo/extra')).toBeNull();
    expect(parseGitRemoteUrl('[email protected]:owner/repo/extra.git')).toBeNull();
    // HTTPS with extra path segment
    expect(parseGitRemoteUrl('https://github.com/owner/repo/extra')).toBeNull();
    expect(parseGitRemoteUrl('https://github.com/org/team/repo.git')).toBeNull();
    // ssh:// with extra
    expect(parseGitRemoteUrl('ssh://[email protected]/owner/repo/extra.git')).toBeNull();
  });

  it('strips ports from host so github.com:443 still classifies as GitHub', () => {
    // ssh:// with explicit port
    expect(parseGitRemoteUrl('ssh://[email protected]:443/atomantic/PortOS.git')).toEqual({
      host: 'github.com', owner: 'atomantic', repo: 'PortOS'
    });
    // HTTPS with explicit port
    expect(parseGitRemoteUrl('https://github.com:443/atomantic/PortOS.git')).toEqual({
      host: 'github.com', owner: 'atomantic', repo: 'PortOS'
    });
  });

  it('accepts the SCP `host:port/owner/repo` GitHub variant', () => {
    // GitHub publishes `[email protected]:443/owner/repo.git` for users
    // tunneling SSH over 443 — must classify the same as the standard form.
    expect(parseGitRemoteUrl('[email protected]:443/atomantic/PortOS.git')).toEqual({
      host: 'ssh.github.com', owner: 'atomantic', repo: 'PortOS'
    });
  });
});

describe('readOriginRemoteUrl', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('returns the trimmed URL on success', async () => {
    execGit.mockResolvedValue({ stdout: 'https://github.com/atomantic/PortOS.git\n', stderr: '', exitCode: 0 });
    const url = await readOriginRemoteUrl();
    expect(url).toBe('https://github.com/atomantic/PortOS.git');
    expect(execGit).toHaveBeenCalledWith(['remote', 'get-url', 'origin'], '/mock', { ignoreExitCode: true });
  });

  it('returns null when origin is missing (non-zero exit)', async () => {
    execGit.mockResolvedValue({ stdout: '', stderr: 'error: No such remote: origin', exitCode: 2 });
    const url = await readOriginRemoteUrl();
    expect(url).toBeNull();
  });

  it('returns null when stdout is empty', async () => {
    execGit.mockResolvedValue({ stdout: '   \n', stderr: '', exitCode: 0 });
    const url = await readOriginRemoteUrl();
    expect(url).toBeNull();
  });
});

describe('getOriginInfo', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('classifies upstream when origin == atomantic/PortOS', async () => {
    execGit.mockResolvedValue({ stdout: '[email protected]:atomantic/PortOS.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info).toEqual({
      hasOrigin: true,
      originUrl: '[email protected]:atomantic/PortOS.git',
      host: 'github.com',
      owner: 'atomantic',
      repo: 'PortOS',
      fullName: 'atomantic/PortOS',
      isUpstream: true,
      isGithub: true,
      isFork: false
    });
  });

  it('is case-insensitive for upstream comparison', async () => {
    execGit.mockResolvedValue({ stdout: 'https://github.com/ATOMANTIC/portos.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.isUpstream).toBe(true);
    expect(info.isFork).toBe(false);
  });

  it('classifies fork when origin is another github user with the same repo name', async () => {
    execGit.mockResolvedValue({ stdout: '[email protected]:alice/PortOS.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.isFork).toBe(true);
    expect(info.isUpstream).toBe(false);
    expect(info.fullName).toBe('alice/PortOS');
  });

  it('classifies managed integration forks against their own canonical upstream', async () => {
    execGit.mockResolvedValue({
      stdout: '[email protected]:alice/eidoverse-worlds.git\n',
      stderr: '',
      exitCode: 0,
    });

    const info = await getOriginInfo('/managed/worlds', {
      upstreamOwner: 'anima-research',
      upstreamRepo: 'eidoverse-worlds',
    });

    expect(execGit).toHaveBeenCalledWith(
      ['remote', 'get-url', 'origin'],
      '/managed/worlds',
      { ignoreExitCode: true },
    );
    expect(info).toMatchObject({
      fullName: 'alice/eidoverse-worlds',
      isUpstream: false,
      isFork: true,
    });
  });

  it('classifies as fork by owner alone, regardless of repo name (#5931)', async () => {
    // A fork renamed on GitHub (`<owner>/PortOS` -> `<owner>/PortOS-Fork`) still IS the
    // PortOS checkout — GitHub's slug redirect keeps `git`/`gh repo sync` working, and a
    // strict repo-name match silently dropped the fork panel, the sync-fork route, and
    // the divergence check the moment someone renamed. The tradeoff: an origin genuinely
    // unrelated to PortOS also classifies as a fork now — an acceptable false positive
    // whose failure mode is a clear `gh repo sync` error, not four silently missing
    // features.
    execGit.mockResolvedValue({ stdout: '[email protected]:alice/MyCustomOS.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.isGithub).toBe(true);
    expect(info.isFork).toBe(true);
    expect(info.isUpstream).toBe(false);
    expect(info.fullName).toBe('alice/MyCustomOS');
  });

  it('classifies a renamed fork of PortOS itself as a fork', async () => {
    execGit.mockResolvedValue({ stdout: '[email protected]:alice/PortOS-Fork.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.isFork).toBe(true);
    expect(info.isUpstream).toBe(false);
  });

  it('classifies upstream even when origin URL carries a port', async () => {
    execGit.mockResolvedValue({ stdout: 'ssh://[email protected]:443/atomantic/PortOS.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.host).toBe('github.com');
    expect(info.isGithub).toBe(true);
    expect(info.isUpstream).toBe(true);
  });

  it('does not flag non-github remotes as fork even when owner/repo differ', async () => {
    execGit.mockResolvedValue({ stdout: '[email protected]:team/PortOS.git\n', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.isGithub).toBe(false);
    expect(info.isFork).toBe(false);
    expect(info.isUpstream).toBe(false);
    expect(info.fullName).toBe('team/PortOS');
  });

  it('returns hasOrigin=false when no origin remote exists', async () => {
    execGit.mockResolvedValue({ stdout: '', stderr: '', exitCode: 2 });
    const info = await getOriginInfo();
    expect(info.hasOrigin).toBe(false);
    expect(info.isFork).toBe(false);
    expect(info.isUpstream).toBe(false);
    expect(info.fullName).toBeNull();
  });

  it('returns hasOrigin=true but unparsed when URL is malformed', async () => {
    execGit.mockResolvedValue({ stdout: 'mumble-mumble', stderr: '', exitCode: 0 });
    const info = await getOriginInfo();
    expect(info.hasOrigin).toBe(true);
    expect(info.originUrl).toBe('mumble-mumble');
    expect(info.fullName).toBeNull();
    expect(info.isFork).toBe(false);
    expect(info.isUpstream).toBe(false);
  });
});

describe('classifyOriginRemote', () => {
  it('keeps credential redaction when classifying a non-PortOS upstream', () => {
    const info = classifyOriginRemote(
      'https://alice:[email protected]/alice/eidoverse-worlds.git',
      { upstreamOwner: 'anima-research', upstreamRepo: 'eidoverse-worlds' },
    );

    expect(info).toMatchObject({
      originUrl: 'https://***@github.com/alice/eidoverse-worlds.git',
      fullName: 'alice/eidoverse-worlds',
      isFork: true,
    });
  });
});

describe('redactRemoteUrlCredentials', () => {
  it('strips https://user:token@ prefix', () => {
    expect(redactRemoteUrlCredentials('https://user:[email protected]/alice/PortOS.git'))
      .toBe('https://***@github.com/alice/PortOS.git');
  });

  it('strips https://token@ prefix (no password)', () => {
    expect(redactRemoteUrlCredentials('https://[email protected]/alice/PortOS.git'))
      .toBe('https://***@github.com/alice/PortOS.git');
  });

  it('strips credentials from ssh:// URLs', () => {
    expect(redactRemoteUrlCredentials('ssh://git:[email protected]/alice/PortOS.git'))
      .toBe('ssh://***@github.com/alice/PortOS.git');
  });

  it('leaves SCP-style remotes untouched (no embedded secret)', () => {
    expect(redactRemoteUrlCredentials('[email protected]:alice/PortOS.git'))
      .toBe('[email protected]:alice/PortOS.git');
  });

  it('leaves URLs without credentials untouched', () => {
    expect(redactRemoteUrlCredentials('https://github.com/alice/PortOS.git'))
      .toBe('https://github.com/alice/PortOS.git');
  });

  it('returns non-string input unchanged', () => {
    expect(redactRemoteUrlCredentials(null)).toBeNull();
    expect(redactRemoteUrlCredentials(undefined)).toBeUndefined();
  });
});

describe('getOriginInfo credential leak regression', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('never returns credentials in originUrl when the remote embeds a PAT', async () => {
    execGit.mockResolvedValue({
      stdout: 'https://alice:[email protected]/alice/PortOS.git\n',
      stderr: '',
      exitCode: 0
    });
    const info = await getOriginInfo();
    expect(info.originUrl).toBe('https://***@github.com/alice/PortOS.git');
    // Belt-and-suspenders: the secret must not appear anywhere in the result
    const serialized = JSON.stringify(info);
    expect(serialized).not.toContain('ghp_secret_abc123');
    expect(serialized).not.toContain('alice:'); // user:pass prefix
    // Classification should still work — credentials don't break the parser
    expect(info.isGithub).toBe(true);
    expect(info.isFork).toBe(true);
    expect(info.fullName).toBe('alice/PortOS');
  });

  it('also redacts when the URL is unparseable', async () => {
    execGit.mockResolvedValue({
      stdout: 'https://user:tok@somethingweirdwithoutslash',
      stderr: '',
      exitCode: 0
    });
    const info = await getOriginInfo();
    expect(info.originUrl).not.toContain('tok');
    expect(info.originUrl).toContain('***@');
  });
});

describe('upstream constants', () => {
  it('exposes UPSTREAM_OWNER, UPSTREAM_REPO, UPSTREAM_FULL_NAME', () => {
    expect(UPSTREAM_OWNER).toBe('atomantic');
    expect(UPSTREAM_REPO).toBe('PortOS');
    expect(UPSTREAM_FULL_NAME).toBe('atomantic/PortOS');
  });
});