~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
94 lines · 2.8 KB Raw History
import { describe, expect, it } from 'vitest';
import { parseRemotes, upstreamRemote } from '../src/upstream.js';

const LISTING = [
  'remote.origin.url atproto://alice.example/demo-app',
  'remote.mine.url atproto://bob.example/demo-app',
  'remote.github.url git@github.com:someone/demo-app.git',
  '',
].join('\n');

describe('parseRemotes', () => {
  it('reads each remote and its URL, and nothing else', () => {
    expect(parseRemotes(LISTING)).toEqual([
      { name: 'origin', url: 'atproto://alice.example/demo-app' },
      { name: 'mine', url: 'atproto://bob.example/demo-app' },
      { name: 'github', url: 'git@github.com:someone/demo-app.git' },
    ]);
    expect(parseRemotes('')).toEqual([]);
    expect(
      parseRemotes('remote.origin.fetch +refs/heads/*:refs/remotes/origin/*'),
    ).toEqual([]);
  });
});

describe('upstreamRemote', () => {
  const remotes = parseRemotes(LISTING);

  it('names the atproto remote the copy came from, when pushing to another account', () => {
    expect(
      upstreamRemote(remotes, {
        authority: 'bob.example',
        repoName: 'demo-app',
      }),
    ).toEqual({
      name: 'origin',
      authority: 'alice.example',
      repoName: 'demo-app',
    });
  });

  it('names nothing when pushing back to where the copy came from', () => {
    expect(
      upstreamRemote(
        [{ name: 'origin', url: 'atproto://alice.example/demo-app' }],
        { authority: 'Alice.Example', repoName: 'demo-app' },
      ),
    ).toBeNull();
  });

  it('ignores a remote under another name, a non-atproto remote, and a space', () => {
    expect(
      upstreamRemote(
        [
          { name: 'origin', url: 'atproto://alice.example/other' },
          { name: 'github', url: 'https://github.com/x/demo-app.git' },
          {
            name: 'team',
            url: 'atproto://alice.example/space/com.example.t/s/demo-app',
          },
        ],
        { authority: 'bob.example', repoName: 'demo-app' },
      ),
    ).toBeNull();
  });

  it('prefers origin where several remotes fit, and takes the first otherwise', () => {
    const several = [
      { name: 'carol', url: 'atproto://carol.example/demo-app' },
      { name: 'origin', url: 'atproto://alice.example/demo-app' },
    ];
    expect(
      upstreamRemote(several, {
        authority: 'bob.example',
        repoName: 'demo-app',
      })?.authority,
    ).toBe('alice.example');
    expect(
      upstreamRemote(several.slice(0, 1), {
        authority: 'bob.example',
        repoName: 'demo-app',
      })?.authority,
    ).toBe('carol.example');
  });

  it('names nothing for a push into a space', () => {
    expect(
      upstreamRemote(remotes, {
        authority: 'bob.example',
        repoName: 'demo-app',
        space: 'at://did:plc:x/com.example.t/s',
      }),
    ).toBeNull();
  });
});