Files
t3code/apps/server/src/pullRequest/GitLabPullRequestProvider.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

215 lines
7.0 KiB
TypeScript

import { assert, describe, expect, it, vi } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
import { gitLabViewerPermissions, make } from "./GitLabPullRequestProvider.ts";
describe("gitLabViewerPermissions", () => {
it("offers everything to a viewer GitLab says can merge", () => {
expect(gitLabViewerPermissions({ viewerCanMerge: true })).toEqual({
// Arming a merge for later and taking the arming back answer to the same `can_merge`.
actions: [
"merge",
"ready",
"draft",
"close",
"reopen",
"update-branch",
"enable-auto-merge",
"disable-auto-merge",
],
comment: true,
resolve: true,
verdicts: ["comment", "approve"],
// GitLab says nothing about who may set a reviewer, and an unreported permission is granted.
requestReviewers: true,
// Rebase and nothing else: GitLab cannot merge a target branch into a source branch, so
// offering the choice would be offering something no request could carry out.
updateMethods: ["rebase"],
});
});
it("keeps merge, now and later, from a viewer GitLab says cannot", () => {
// `user.can_merge` already accounts for the role, the approval rules and a protected target
// branch, so it is the one answer here that does not have to be inferred.
expect(gitLabViewerPermissions({ viewerCanMerge: false })).toEqual({
actions: ["ready", "draft", "close", "reopen"],
comment: true,
resolve: true,
verdicts: ["comment", "approve"],
requestReviewers: true,
});
});
it("names no way of updating a branch it will not let this viewer update", () => {
// The action and the strategy behind it go together: a button offered with nothing to press
// it with, or a strategy left standing next to a withheld button, is a half-refusal.
expect(gitLabViewerPermissions({ viewerCanMerge: false }).updateMethods).toBeUndefined();
});
it("treats an author with read access as any other reader, which is all GitLab says", () => {
// Its REST API names no relationship between the viewer and the merge request beyond
// `can_merge`, so the four an author keeps stay offered to everyone rather than being taken
// from the one person entitled to them.
expect(gitLabViewerPermissions({ viewerCanMerge: false }).actions).toEqual([
"ready",
"draft",
"close",
"reopen",
]);
});
});
describe("getChangeRequest base freshness", () => {
const detail = {
number: 7,
title: "Merge request 7",
url: "https://gitlab.com/acme/web/-/merge_requests/7",
author: null,
headBranch: "feat/page",
baseBranch: "main",
state: "open" as const,
isDraft: false,
mergeability: "mergeable" as const,
additions: 0,
deletions: 0,
createdAt: "2026-07-01T00:00:00Z",
updatedAt: "2026-07-02T00:00:00Z",
reviewRequestLogins: [],
labels: [],
body: "",
changedFiles: 1,
mergedAt: null,
closedAt: null,
reviewers: [],
checks: [],
viewerCanMerge: true,
reviewerIds: [],
};
it.effect("reads checks without fetching project settings", () =>
Effect.gen(function* () {
const provider = yield* make.pipe(
Effect.provide(
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
getMergeRequestDetail: () => Effect.succeed(detail),
}),
),
);
const read = provider.getChangeRequestChecks;
if (read === undefined) return yield* Effect.die("checks read missing");
expect(
yield* read({ cwd: "/w", repository: "group/subgroup/web", host: "gitlab.com", number: 7 }),
).toEqual({ state: "open", checks: [] });
}),
);
const readWith = (divergence: { readonly divergedCommits?: number }) =>
Effect.gen(function* () {
const provider = yield* make;
return yield* provider.getChangeRequest({
cwd: "/w",
repository: "acme/web",
host: "gitlab.com",
number: 7,
});
}).pipe(
Effect.provide(
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
getProjectMergeCapabilities: () =>
Effect.succeed({ merge: true, squash: true, rebase: true }),
}),
),
);
it.effect("reads a counted divergence as a branch that has fallen behind", () =>
Effect.gen(function* () {
const changeRequest = yield* readWith({ divergedCommits: 3 });
expect(changeRequest.baseComparison).toBe("behind");
expect(changeRequest.behindBy).toBe(3);
}),
);
it.effect("reads a divergence of none as a branch that is current", () =>
Effect.gen(function* () {
const changeRequest = yield* readWith({ divergedCommits: 0 });
expect(changeRequest.baseComparison).toBe("up-to-date");
expect(changeRequest.behindBy).toBe(0);
}),
);
it.effect("says nothing at all where GitLab counted nothing", () =>
Effect.gen(function* () {
// An install too old to answer has to leave the page silent rather than let it claim the
// branch is current, which is the one wrong thing this banner could say.
const changeRequest = yield* readWith({});
expect(changeRequest.baseComparison).toBe("unknown");
expect(changeRequest.behindBy).toBeUndefined();
}),
);
});
describe("rewriting what has already been said", () => {
const updateMergeRequest = vi.fn(() => Effect.void);
const updateNote = vi.fn(() => Effect.void);
const providerWith = make.pipe(
Effect.provide(
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ updateMergeRequest, updateNote }),
),
);
it.effect("sends only the half of the merge request the reader rewrote", () =>
Effect.gen(function* () {
const provider = yield* providerWith;
assert.isDefined(provider.updateChangeRequest);
yield* provider.updateChangeRequest({
cwd: "/w",
repository: "acme/web",
host: "gitlab.com",
number: 7,
body: "What this changes.",
});
// GitLab calls it the description, and the title stays out of the request entirely.
expect(updateMergeRequest).toHaveBeenCalledWith({
cwd: "/w",
repository: "acme/web",
number: 7,
description: "What this changes.",
});
}),
);
it.effect("rewrites a positioned comment through the same note as any other", () =>
Effect.gen(function* () {
const provider = yield* providerWith;
assert.isDefined(provider.updateComment);
yield* provider.updateComment({
cwd: "/w",
repository: "acme/web",
host: "gitlab.com",
number: 7,
commentId: "42",
kind: "review-comment",
body: "Reworded.",
});
expect(updateNote).toHaveBeenCalledWith({
cwd: "/w",
repository: "acme/web",
number: 7,
noteId: "42",
body: "Reworded.",
});
}),
);
});