Commit fabd57f5 authored by r-4bb1t's avatar r-4bb1t
Browse files

test: implement lookupObject tests with request cancellation scenarios

parent 396e90b0
Loading
Loading
Loading
Loading
+91 −1
Original line number Diff line number Diff line
@@ -5,7 +5,10 @@ import { test } from "../testing/mod.ts";
import { lookupObject, traverseCollection } from "./lookup.ts";
import { Collection, Note, Object, Person } from "./vocab.ts";

test("lookupObject()", async (t) => {
test("lookupObject()", {
  sanitizeResources: false,
  sanitizeOps: false,
}, async (t) => {
  fetchMock.spyGlobal();

  fetchMock.get(
@@ -89,6 +92,93 @@ test("lookupObject()", async (t) => {
    assertEquals(await lookupObject("https://example.com/404", options), null);
  });

  fetchMock.removeRoutes();
  fetchMock.get(
    "begin:https://example.com/.well-known/webfinger",
    () =>
      new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            subject: "acct:johndoe@example.com",
            links: [
              {
                rel: "self",
                href: "https://example.com/person",
                type: "application/activity+json",
              },
            ],
          });
        }, 1000);
      }),
  );

  await t.step("request cancellation", async () => {
    const controller = new AbortController();
    const promise = lookupObject("johndoe@example.com", {
      ...options,
      signal: controller.signal,
    });

    controller.abort();
    assertEquals(await promise, null);
  });

  fetchMock.removeRoutes();
  fetchMock.get(
    "begin:https://example.com/.well-known/webfinger",
    {
      subject: "acct:johndoe@example.com",
      links: [
        {
          rel: "self",
          href: "https://example.com/person",
          type: "application/activity+json",
        },
      ],
    },
  );

  await t.step("successful request with signal", async () => {
    const controller = new AbortController();
    const person = await lookupObject("johndoe@example.com", {
      ...options,
      signal: controller.signal,
    });
    assertInstanceOf(person, Person);
    assertEquals(person.id, new URL("https://example.com/person"));
  });

  fetchMock.removeRoutes();
  fetchMock.get(
    "begin:https://example.com/.well-known/webfinger",
    () =>
      new Promise((resolve) => {
        setTimeout(() => {
          resolve({
            subject: "acct:johndoe@example.com",
            links: [
              {
                rel: "self",
                href: "https://example.com/person",
                type: "application/activity+json",
              },
            ],
          });
        }, 500);
      }),
  );

  await t.step("cancellation with immediate abort", async () => {
    const controller = new AbortController();
    controller.abort();

    const result = await lookupObject("johndoe@example.com", {
      ...options,
      signal: controller.signal,
    });
    assertEquals(result, null);
  });

  fetchMock.hardReset();
});