Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

3.9 KB
Raw Blame
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  it,
  vi,
} from "vite-plus/test";
import deleteLabel from "./delete-label";

const { request } = vi.hoisted(() => ({ request: vi.fn() }));
vi.mock("@kaneo/libs", () => ({
  client: { label: { ":id": { $delete: request } } },
}));
const root = {
  id: "root",
  name: "bug",
  workspaceId: "workspace",
  taskId: null,
};
const busy = () =>
  Response.json(
    {
      message: "Label deletion is busy; retry this request",
      code: "RATE_LIMITED",
    },
    { status: 429, headers: { "Retry-After": "1" } },
  );
beforeEach(() => {
  request.mockReset();
});
afterEach(() => {
  vi.useRealTimers();
});

describe("resumable label deletion client", () => {
  it("awaits each batch and resolves only after the final response", async () => {
    let release!: (response: Response) => void;
    request
      .mockImplementationOnce(
        () =>
          new Promise<Response>((resolve) => {
            release = resolve;
          }),
      )
      .mockResolvedValueOnce(
        Response.json({ ...root, pendingDeletion: true }, { status: 202 }),
      )
      .mockResolvedValueOnce(Response.json(root));
    let finished = false;
    const operation = deleteLabel({ id: root.id }).then((result) => {
      finished = true;
      return result;
    });
    await Promise.resolve();
    expect(request).toHaveBeenCalledTimes(1);
    expect(finished).toBe(false);
    release(Response.json({ ...root, pendingDeletion: true }, { status: 202 }));
    await expect(operation).resolves.toEqual(root);
    expect(request.mock.calls).toEqual(
      Array.from({ length: 3 }, () => [{ param: { id: root.id } }]),
    );
  });

  it("surfaces an interrupted operation and resumes with the same label ID on retry", async () => {
    request
      .mockResolvedValueOnce(
        Response.json({ ...root, pendingDeletion: true }, { status: 202 }),
      )
      .mockRejectedValueOnce(new TypeError("Network unavailable"));
    await expect(deleteLabel({ id: root.id })).rejects.toThrow(
      "Network unavailable",
    );
    request.mockResolvedValueOnce(Response.json(root));
    await expect(deleteLabel({ id: root.id })).resolves.toEqual(root);
    expect(request).toHaveBeenLastCalledWith({ param: { id: root.id } });
  });

  it("backs off when busy and stops after five retries", async () => {
    vi.useFakeTimers();
    request.mockImplementation(async () => busy());
    const operation = deleteLabel({ id: root.id });
    const rejection = expect(operation).rejects.toMatchObject({
      status: 429,
      code: "RATE_LIMITED",
      message: "Label deletion is busy; retry this request",
    });
    await vi.advanceTimersByTimeAsync(999);
    expect(request).toHaveBeenCalledTimes(1);
    await vi.advanceTimersByTimeAsync(4001);
    await rejection;
    expect(request).toHaveBeenCalledTimes(6);
  });

  it("continues after temporary contention without reporting success early", async () => {
    vi.useFakeTimers();
    request
      .mockResolvedValueOnce(busy())
      .mockResolvedValueOnce(
        Response.json({ ...root, pendingDeletion: true }, { status: 202 }),
      )
      .mockResolvedValueOnce(Response.json(root));
    const operation = deleteLabel({ id: root.id });
    await vi.advanceTimersByTimeAsync(1000);
    await expect(operation).resolves.toEqual(root);
    expect(request).toHaveBeenCalledTimes(3);
  });

  it("does not retry authorization failures", async () => {
    request.mockResolvedValueOnce(
      Response.json(
        {
          message: "Insufficient permissions",
          code: "MISSING_PERMISSION",
          missingPermissions: ["label:delete"],
        },
        { status: 403 },
      ),
    );
    await expect(deleteLabel({ id: root.id })).rejects.toMatchObject({
      status: 403,
      code: "MISSING_PERMISSION",
      missingPermissions: ["label:delete"],
    });
    expect(request).toHaveBeenCalledTimes(1);
  });
});