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

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
58 lines · 2.0 KB Raw History
/**
 * The issue statements a repository's page reads from the server, with the
 * visitor's own just-filed issues folded in.
 */

import { afterEach, describe, expect, it } from 'vitest';
import { profileFor } from '../src/lib/authors.js';
import { account } from '../src/lib/git.js';
import { loadIssueStatements } from '../src/lib/issues.js';

describe('loadIssueStatements', () => {
  const original = globalThis.fetch;
  const originalLocation = globalThis.location;

  afterEach(() => {
    globalThis.fetch = original;
    globalThis.location = originalLocation;
    account.did = '';
    account.pds = '';
    account.handle = '';
  });

  it('reads the statements from the server, with the client and not its promise', async () => {
    // serverClient() is async. Calling a method on it without awaiting it
    // first asks the promise, which holds no methods.
    globalThis.location = new URL('https://forge.example/');
    account.pds = 'https://pds.example';
    /** @type {string[]} */
    const asked = [];
    globalThis.fetch = async (/** @type {string|URL|Request} */ input) => {
      const url = String(input);
      asked.push(url);
      if (url.includes('dev.pdsjs.git.listIssues')) {
        return Response.json({
          statements: [
            {
              uri: 'at://did:plc:friend/dev.pdsjs.git.issue/3',
              by: { did: 'did:plc:friend' },
            },
          ],
        });
      }
      if (url.includes('app.bsky.actor.getProfiles')) {
        return Response.json({
          profiles: [{ did: 'did:plc:friend', handle: 'friend.example' }],
        });
      }
      return Response.json({ repos: [] });
    };
    const statements = await loadIssueStatements('demo-app');
    expect(statements).toHaveLength(1);
    expect(statements[0].uri).toBe('at://did:plc:friend/dev.pdsjs.git.issue/3');
    expect(profileFor('did:plc:friend')?.handle).toBe('friend.example');
    expect(asked.some((url) => url.includes('dev.pdsjs.git.listIssues'))).toBe(
      true,
    );
  });
});