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

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
77 lines · 2.5 KB Raw History
import { getKnownServiceUrl, parseAtprotoProxyHeader } from '@pdsjs/core/pds';
import { describe, expect, test } from 'vitest';

// Internal constant - not exported from pds.js due to Cloudflare Workers limitation
const BSKY_APPVIEW_URL = 'https://api.bsky.app';

describe('Proxy Utilities', () => {
  describe('parseAtprotoProxyHeader', () => {
    test('parses valid header', () => {
      const result = parseAtprotoProxyHeader(
        'did:web:api.bsky.app#bsky_appview',
      );
      expect(result).toEqual({
        did: 'did:web:api.bsky.app',
        serviceId: 'bsky_appview',
      });
    });

    test('parses header with did:plc', () => {
      const result = parseAtprotoProxyHeader(
        'did:plc:z72i7hdynmk6r22z27h6tvur#atproto_labeler',
      );
      expect(result).toEqual({
        did: 'did:plc:z72i7hdynmk6r22z27h6tvur',
        serviceId: 'atproto_labeler',
      });
    });

    test('returns null for null/undefined', () => {
      // @ts-expect-error - Testing runtime handling of null
      expect(parseAtprotoProxyHeader(null)).toBe(null);
      // @ts-expect-error - Testing runtime handling of undefined
      expect(parseAtprotoProxyHeader(undefined)).toBe(null);
      expect(parseAtprotoProxyHeader('')).toBe(null);
    });

    test('returns null for header without fragment', () => {
      expect(parseAtprotoProxyHeader('did:web:api.bsky.app')).toBe(null);
    });

    test('returns null for header with only fragment', () => {
      expect(parseAtprotoProxyHeader('#bsky_appview')).toBe(null);
    });

    test('returns null for header with trailing fragment', () => {
      expect(parseAtprotoProxyHeader('did:web:api.bsky.app#')).toBe(null);
    });
  });

  describe('getKnownServiceUrl', () => {
    test('returns URL for known Bluesky AppView', () => {
      const result = getKnownServiceUrl('did:web:api.bsky.app', 'bsky_appview');
      expect(result).toBe(BSKY_APPVIEW_URL);
    });

    test('returns null for unknown service DID', () => {
      const result = getKnownServiceUrl(
        'did:web:unknown.service',
        'bsky_appview',
      );
      expect(result).toBe(null);
    });

    test('returns null for unknown service ID', () => {
      const result = getKnownServiceUrl(
        'did:web:api.bsky.app',
        'unknown_service',
      );
      expect(result).toBe(null);
    });

    test('returns null for both unknown', () => {
      const result = getKnownServiceUrl('did:web:unknown', 'unknown');
      expect(result).toBe(null);
    });
  });
});