testTheLibraryAsAnAppUsesIt

tcmts.atproto.test.testTheLibraryAsAnAppUsesIt · #igx5jbdu · function

The library as an app uses it: scopes built from their parts; told where the network is (configure), anyone's records and profile read without signing in; signed in with an app password, kept in a store, records written, listed, read and deleted as them ($type their collection), and the sign-in found again in the store, for the scope it covers.

import {
  atUri, blob, configure, getRecord, listRecords, memorySessions, profile, repo, resumeSession, rpc, scope,
  signInWithAppPassword,
} from "#codebase/tcmts/atproto";
import { fakeNetwork } from "#codebase/tcmts/atproto/testing";
import assert from "node:assert/strict";

export const testTheLibraryAsAnAppUsesIt = async () => {
  assert.equal(
    scope(
      repo("app.example.post"),
      repo("app.example.like", ["create", "delete"]),
      rpc("app.example.call"),
      blob("image/*"),
    ),
    "atproto repo:app.example.post repo:app.example.like?action=create&action=delete rpc:app.example.call?aud=* blob:image/*",
  );
  assert.deepEqual(atUri("at://did:plc:a/app.example.post/3k"), {
    did: "did:plc:a",
    collection: "app.example.post",
    rkey: "3k",
  });
  assert.throws(() => atUri("https://example.com"), /isn't a record's at:\/\/ URI/);

  const alice = {
    did: "did:plc:alice000000000000000000",
    handle: "alice.test",
    password: "a-pass",
  };
  const net = await fakeNetwork([alice]);
  net.seed(alice.did, "app.bsky.actor.profile", "self", {
    displayName: "Alice",
    description: "Writes things.",
  });
  configure(net.config);
  try {
    assert.deepEqual(await profile(alice.did), {
      displayName: "Alice",
      description: "Writes things.",
    });
    assert.equal(await getRecord(`at://${alice.did}/app.example.post/none`), undefined);

    const store = memorySessions();
    const s = await signInWithAppPassword("alice.test", alice.password, { store });
    assert.equal(s.did, alice.did);
    const { uri } = await s.putRecord("app.example.post", "first", { text: "Hello" });
    assert.equal(uri, `at://${alice.did}/app.example.post/first`);
    await s.createRecord("app.example.post", { text: "Again" }, "second");
    assert.deepEqual((await getRecord<{ text: string }>(uri))?.value, {
      $type: "app.example.post",
      text: "Hello",
    });
    assert.equal((await listRecords(alice.did, "app.example.post")).records.length, 2);
    assert.equal((await s.getRecord("app.example.post", "second"))?.value.text, "Again");
    await s.deleteRecord("app.example.post", "second");
    assert.equal(await s.getRecord("app.example.post", "second"), undefined);
    assert.equal((await s.listRecords("app.example.post")).records.length, 1);

    // kept: found again, as the account or the one used last (an app password's covers any scope)
    assert.equal(
      (await resumeSession(store, { scope: scope(repo("app.example.post")) }))?.did,
      alice.did,
    );
    assert.equal(
      (await resumeSession(store, { who: "alice.test", scope: scope(repo("x.y.z")) }))?.did,
      alice.did,
    );
    await s.signOut();
    assert.equal(await resumeSession(store, { scope: "atproto" }), undefined);
  } finally {
    configure({
      plc: undefined,
      appview: undefined,
      allowLocalNetwork: undefined,
      jetstream: undefined,
      relay: undefined,
    });
    await net.close();
  }
}

uses