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

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
49 lines · 1.4 KB Raw History
import { describe, expect, it } from 'vitest';
import {
  formatRecordPath,
  formatSetHashElement,
  parseRecordPath,
} from '../src/path.js';

describe('record paths', () => {
  it('formats a record path', () => {
    expect(formatRecordPath('app.example.post', '3kabc')).toBe(
      'app.example.post/3kabc',
    );
  });

  it('parses a record path', () => {
    expect(parseRecordPath('app.example.post/3kabc')).toEqual({
      collection: 'app.example.post',
      rkey: '3kabc',
    });
  });

  it('round-trips', () => {
    const path = formatRecordPath('com.atmoboards.thread', 'self');
    expect(
      formatRecordPath(
        .../** @type {[string, string]} */ (
          Object.values(parseRecordPath(path))
        ),
      ),
    ).toBe(path);
  });

  it('rejects paths without exactly two segments', () => {
    expect(() => parseRecordPath('only-one')).toThrow(/invalid record path/);
    expect(() => parseRecordPath('a/b/c')).toThrow(/invalid record path/);
    expect(() => parseRecordPath('')).toThrow(/invalid record path/);
  });

  it('rejects paths with an empty segment', () => {
    expect(() => parseRecordPath('a/')).toThrow(/invalid record path/);
    expect(() => parseRecordPath('/b')).toThrow(/invalid record path/);
  });

  it('formats a set hash element', () => {
    expect(
      formatSetHashElement('app.example.post', '3kabc', 'bafyreiabc'),
    ).toBe('app.example.post/3kabc/bafyreiabc');
  });
});