sailfork / t3code-github / packages/ client-runtime/ src/ pullRequestEditing.test.ts

import type {
  PullRequestActor,
  PullRequestCapabilities,
  PullRequestComment,
  PullRequestDetail,
  PullRequestViewerPermissions,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import {
  canEditPullRequestChangeRequest,
  canEditPullRequestComment,
  canResolvePullRequestThread,
  descriptionDraftInForce,
  settleDescriptionSave,
  startDescriptionSave,
  type DescriptionDraft,
} from "./pullRequestEditing.ts";

type Subject = Pick<PullRequestDetail, "author" | "capabilities" | "viewer" | "viewerPermissions">;

function actor(login: string): PullRequestActor {
  return { login, name: null, avatarUrl: null };
}

function capabilities(edit?: PullRequestCapabilities["edit"]): PullRequestCapabilities {
  return {
    diff: true,
    comment: true,
    actions: [],
    mergeMethods: [],
    search: true,
    reactions: true,
    review: { inlineComment: true, reply: true, resolve: true, verdicts: [] },
    reviewers: { request: true, listCandidates: true },
    ...(edit === undefined ? {} : { edit }),
  };
}

function permissions(
  overrides: Partial<PullRequestViewerPermissions> = {},
): PullRequestViewerPermissions {
  return {
    actions: [],
    comment: true,
    resolve: true,
    verdicts: [],
    requestReviewers: true,
    ...overrides,
  };
}

function subject(overrides: Partial<Subject> = {}): Subject {
  return {
    author: actor("octocat"),
    capabilities: capabilities({ changeRequest: true, comment: true }),
    viewer: "octocat",
    viewerPermissions: permissions(),
    ...overrides,
  };
}

function comment(overrides: Partial<PullRequestComment> = {}): PullRequestComment {
  return {
    id: "c1",
    kind: "issue-comment",
    author: actor("octocat"),
    body: "words",
    createdAt: "2026-01-01T00:00:00.000Z",
    url: null,
    path: null,
    reviewState: null,
    ...overrides,
  };
}

describe("canEditPullRequestChangeRequest", () => {
  it("matches the author regardless of case", () => {
    expect(canEditPullRequestChangeRequest(subject({ viewer: "OctoCat" }))).toBe(true);
  });

  it("lets somebody who may merge rewrite a change request they did not write", () => {
    expect(
      canEditPullRequestChangeRequest(
        subject({
          author: actor("someone-else"),
          viewerPermissions: permissions({ actions: ["merge"] }),
        }),
      ),
    ).toBe(true);
  });

  it("follows the host's own answer about the reader over authorship and merge access", () => {
    expect(
      canEditPullRequestChangeRequest(
        subject({
          author: actor("someone-else"),
          viewerPermissions: permissions({ editChangeRequest: true }),
        }),
      ),
    ).toBe(true);
    expect(
      canEditPullRequestChangeRequest(
        subject({
          viewerPermissions: permissions({ actions: ["merge"], editChangeRequest: false }),
        }),
      ),
    ).toBe(false);
    expect(
      canEditPullRequestChangeRequest(
        subject({
          capabilities: capabilities({ changeRequest: false, comment: true }),
          viewerPermissions: permissions({ editChangeRequest: true }),
        }),
      ),
    ).toBe(false);
  });

  it("refuses a reader who neither wrote it nor may merge it", () => {
    expect(
      canEditPullRequestChangeRequest(
        subject({
          author: actor("someone-else"),
          viewerPermissions: permissions({ actions: ["close"] }),
        }),
      ),
    ).toBe(false);
  });

  it("refuses where the host cannot rewrite a change request at all", () => {
    expect(
      canEditPullRequestChangeRequest(
        subject({ capabilities: capabilities({ changeRequest: false, comment: true }) }),
      ),
    ).toBe(false);
    expect(canEditPullRequestChangeRequest(subject({ capabilities: capabilities() }))).toBe(false);
  });

  it("still allows a merger the host could not name", () => {
    expect(
      canEditPullRequestChangeRequest(
        subject({ viewer: undefined, viewerPermissions: permissions({ actions: ["merge"] }) }),
      ),
    ).toBe(true);
  });
});

describe("canEditPullRequestComment", () => {
  it("lets the reader rewrite their own remark on a line", () => {
    expect(canEditPullRequestComment(subject(), comment({ kind: "review-comment" }))).toBe(true);
  });

  it("matches the author regardless of case", () => {
    expect(
      canEditPullRequestComment(
        subject({ viewer: "OCTOCAT" }),
        comment({ author: actor("octocat") }),
      ),
    ).toBe(true);
  });

  it("refuses somebody else's remark", () => {
    expect(canEditPullRequestComment(subject(), comment({ author: actor("someone-else") }))).toBe(
      false,
    );
  });

  it("follows the host's own answer about a remark over authorship", () => {
    expect(canEditPullRequestComment(subject(), comment({ canEdit: false }))).toBe(false);
    expect(
      canEditPullRequestComment(
        subject(),
        comment({ author: actor("someone-else"), canEdit: true }),
      ),
    ).toBe(true);
    expect(
      canEditPullRequestComment(
        subject({ capabilities: capabilities({ changeRequest: true, comment: false }) }),
        comment({ canEdit: true }),
      ),
    ).toBe(false);
  });

  it("refuses a review's own summary", () => {
    expect(canEditPullRequestComment(subject(), comment({ kind: "review" }))).toBe(false);
  });
});

describe("canResolvePullRequestThread", () => {
  it("uses the reader's repository-wide permission where the host says nothing per thread", () => {
    expect(canResolvePullRequestThread(subject(), {})).toBe(true);
    expect(
      canResolvePullRequestThread(
        subject({ viewerPermissions: permissions({ resolve: false }) }),
        {},
      ),
    ).toBe(false);
  });

  it("only narrows the reader's permission with the host's answer for one thread", () => {
    expect(canResolvePullRequestThread(subject(), { canResolve: false })).toBe(false);
    expect(
      canResolvePullRequestThread(subject({ viewerPermissions: permissions({ resolve: false }) }), {
        canResolve: true,
      }),
    ).toBe(false);
  });
});

describe("description drafts", () => {
  const url = "https://github.com/owner/repo/pull/1";
  const unticked = "- [ ] Ship it";
  const ticked = "- [x] Ship it";

  it("hides readings taken during the saves and ends at the first one after them", () => {
    // Ticking and unticking the same box saves the host's own body last.
    let draft: DescriptionDraft | null = startDescriptionSave(null, { url, observedAt: 1 }, ticked);
    draft = startDescriptionSave(draft, { url, observedAt: 1 }, unticked);
    expect(descriptionDraftInForce(draft, url, 2)?.body).toBe(unticked);
    draft = descriptionDraftInForce(draft, url, 2);
    draft = settleDescriptionSave(draft, url, true);
    expect(descriptionDraftInForce(draft, url, 2)?.body).toBe(unticked);
    draft = settleDescriptionSave(draft, url, true);
    expect(descriptionDraftInForce(draft, url, 2)?.body).toBe(unticked);
    // The acknowledging reading ends the draft, so the host's later ticks show.
    expect(descriptionDraftInForce(draft, url, 3)).toBeNull();
  });

  it("outlives failed saves until a reading after the last one, losing the body only with it", () => {
    let draft: DescriptionDraft | null = startDescriptionSave(null, { url, observedAt: 1 }, ticked);
    draft = startDescriptionSave(draft, { url, observedAt: 1 }, unticked);
    // The save still running writes the whole body again.
    draft = settleDescriptionSave(draft, url, false);
    expect(descriptionDraftInForce(draft, url, 2)).toMatchObject({ body: unticked, saves: 1 });
    draft = descriptionDraftInForce(draft, url, 2);
    draft = settleDescriptionSave(draft, url, false);
    expect(descriptionDraftInForce(draft, url, 2)).toMatchObject({ body: null, saves: 0 });
    expect(settleDescriptionSave(draft, url, true)).toBe(draft);
    expect(descriptionDraftInForce(draft, url, 3)).toBeNull();
  });

  it("belongs to one change request", () => {
    const draft = startDescriptionSave(null, { url, observedAt: 1 }, ticked);
    const other = "https://github.com/owner/repo/pull/2";
    expect(descriptionDraftInForce(draft, other, 1)).toBeNull();
    expect(settleDescriptionSave(draft, other, false)).toBe(draft);
    expect(startDescriptionSave(draft, { url: other, observedAt: 5 }, unticked).saves).toBe(1);
  });
});