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

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
79 lines · 2.7 KB Raw History
// listAllRecords feeds the blob reap guard, so it has to reach every record in
// every space. A record it misses is a blob the reaper is free to delete.
import { describe, expect, it } from 'vitest';
import { cborEncodeDagCbor } from '../packages/core/src/repo.js';
import { createSpaceBrowser } from '../packages/core/src/space-browser.js';
import { createMemorySpaceStorage } from '../packages/spaces/src/memory-storage.js';
import { makeSpaceRow } from '../packages/spaces/src/space-row.js';

const ONE = 'at://did:plc:aaaa/space/com.example.forum/general';
const TWO = 'at://did:plc:bbbb/space/com.example.forum/other';
const COL = 'com.example.post';

/** @param {any} store @param {string} space @param {string} rkey @param {any} value */
function write(store, space, rkey, value) {
  return store.commitSpaceWrite(space, {
    writes: [
      {
        action: 'create',
        collection: COL,
        rkey,
        cid: `bafy${rkey}`,
        value: cborEncodeDagCbor(value),
        prev: null,
      },
    ],
    setHash: new Uint8Array(32),
    rev: `3lk${rkey}`,
    indexedAt: '2026-08-10T00:00:00.000Z',
  });
}

describe('space browser listAllRecords', () => {
  it('returns every record in every space, decoded', async () => {
    const store = createMemorySpaceStorage();
    await store.putSpace(makeSpaceRow(ONE, { isOwner: true }));
    await store.putSpace(makeSpaceRow(TWO, { isOwner: false }));
    await write(store, ONE, 'a', { text: 'one' });
    await write(store, TWO, 'b', { text: 'two' });

    const all = await createSpaceBrowser(store).listAllRecords();

    expect(all).toEqual([
      { space: ONE, collection: COL, rkey: 'a', value: { text: 'one' } },
      { space: TWO, collection: COL, rkey: 'b', value: { text: 'two' } },
    ]);
  });

  it('is empty for an account holding no spaces', async () => {
    const browser = createSpaceBrowser(createMemorySpaceStorage());
    expect(await browser.listAllRecords()).toEqual([]);
  });

  it('follows the cursor past the first page', async () => {
    let call = 0;
    const paged = /** @type {any} */ ({
      async listSpaces() {
        return { spaces: [{ uri: ONE }], cursor: null };
      },
      async listSpaceRecords() {
        call += 1;
        return {
          records: [
            {
              collection: COL,
              rkey: `r${call}`,
              cid: 'bafy',
              value: cborEncodeDagCbor({ n: call }),
              indexedAt: '2026-08-10T00:00:00.000Z',
            },
          ],
          cursor: call < 3 ? `${COL}/r${call}` : null,
        };
      },
    });

    const all = await createSpaceBrowser(paged).listAllRecords();
    expect(all.map((r) => r.value)).toEqual([{ n: 1 }, { n: 2 }, { n: 3 }]);
  });
});