testHubCountsStars

tcmts.hub.test.testHubCountsStars · #pfd4mhry · term

Stars count once they check against their writer's repository (one per person per library, at the library's key); a forged one, one at another key or under a made-up DID, or a second row of one, doesn't count, and one taken back goes. A visitor's follows are the AppView's.

import { startFakeAtproto } from "#codebase/tcmts/cli/test/e2e/examples";
import { tempDir, withChildren } from "#codebase/tcmts/cli/test/e2e/harness";
import { checkStar, followsOf, readJetstream, restar, starCounts, stars } from "#codebase/tcmts/hub";
import { readFromTheStart, withEnv } from "#codebase/tcmts/hub/test";
import { runJobs } from "#codebase/tcmts/server";
import assert from "node:assert/strict";

export const testHubCountsStars = () =>
  withChildren(async (keep) => {
    const alice = {
      did: "did:plc:alice000000000000000000",
      handle: "alice.test",
      password: "a-pass",
    };
    const maya = {
      did: "did:plc:maya0000000000000000000",
      handle: "maya.test",
      password: "m-pass",
    };
    const sam = { did: "did:plc:sam00000000000000000000", handle: "sam.test", password: "s-pass" };
    const net = await startFakeAtproto(keep, [alice, maya, sam]);
    const star = (pkg: string) => ({
      subject: { author: alice.did, package: pkg },
      createdAt: new Date().toISOString(),
    });
    net.seed(maya.did, "codes.tcm.star", `${alice.did}:geo`, star("geo"));
    net.seed(sam.did, "codes.tcm.star", `${alice.did}:geo`, star("geo"));
    net.forge(`at://${sam.did}/codes.tcm.star/${alice.did}:geo`);
    // (at a key that isn't its library's: not a star)
    net.seed(sam.did, "codes.tcm.star", "elsewhere", star("geo"));
    net.seed(maya.did, "app.bsky.graph.follow", "f1", {
      subject: alice.did,
      createdAt: new Date().toISOString(),
    });
    await withEnv(
      {
        TCM_ATPROTO_DIRECTORY: net.url,
        TCM_ATPROTO_LOCAL: "1",
        TCM_JETSTREAM: net.url,
        TCM_RELAY: net.url,
        TCM_APPVIEW: undefined,
        TCM_HUB_CODEBASE: tempDir("hub stars"),
        JETSTREAM_API_KEY: "",
      },
      async () => {
        await readFromTheStart();
        await readJetstream({ idleMs: 500 });
        await runJobs({ all: true });
        assert.deepEqual([...(await starCounts())], [[`${alice.did} geo`, 1]]);
        assert.equal((await stars.find({ did: sam.did })).length, 0);
        // (one real star can't count again: not under a made-up DID, nor at another place, nor as a second row)
        await assert.rejects(
          restar(`${maya.did}/codes.tcm.star/${alice.did}:geo/junk1`, alice.did, "geo"),
          /a star is a DID's/,
        );
        assert.equal(
          await checkStar(`at://${maya.did}/codes.tcm.star/${alice.did}:geo/junk2`),
          false,
        );
        const real = (await stars.findOne({ did: maya.did }))!.value;
        const junk = await stars.insert({ ...real, uri: `${real.uri}/junk3` });
        assert.deepEqual([...(await starCounts())], [[`${alice.did} geo`, 1]]);
        await stars.delete(junk);
        assert.deepEqual([...(await followsOf(maya.did))], [alice.did]);
        // (taken back: the hub is told at once, and checks)
        const del = await fetch(`${net.url}/xrpc/com.atproto.repo.deleteRecord`, {
          method: "POST",
          headers: { authorization: `Bearer acc-${maya.did}`, "content-type": "application/json" },
          body: JSON.stringify({
            repo: maya.did,
            collection: "codes.tcm.star",
            rkey: `${alice.did}:geo`,
          }),
        });
        assert.equal(del.status, 200);
        assert.deepEqual(await restar(maya.did, alice.did, "geo"), { count: 0, starred: false });
        net.seed(maya.did, "codes.tcm.star", `${alice.did}:geo`, star("geo"));
        assert.deepEqual(await restar(maya.did, alice.did, "geo"), { count: 1, starred: true });
      },
    );
  })

uses