import { expect } from "@playwright/test";
import { comments, editCob } from "../harness/api";
import { seeded } from "../harness/config";
import {
bobbin,
commentId,
commitRow,
component,
type Ctx,
type Flow,
hydrant,
knot,
link,
not,
page,
patchId,
pds,
pullId,
record,
refusal,
replacing,
rkeyOf,
subscriptionId,
target,
testid,
text,
through,
triage,
unique,
within
} from "../harness/flow";
import { lsRemote } from "../harness/git";
import {
branches,
defineLabel,
labelOff,
labelOn,
merges,
onBranches,
pipelineFor,
recall,
remember,
sendsPatch,
stillStored,
subscribesToPull
} from "../harness/shared";
const title = unique("add a feature");
onBranches("opening a pull request", { ci: true })
.open(page.newPull(branches))
.fill(within(component("PullCompose"), testid("title")), title)
.click(within(component("PullCompose"), testid("submit")))
.sends(
pullId,
knot,
(ctx) => ({
repo: ctx.repo.did,
record: {
state: "open",
title: { text: title },
targetBranch: branches.into
}
}),
"create"
)
.trace(pullId, through(knot, hydrant, bobbin))
.use(sendsPatch())
.use(subscribesToPull)
.at(page.pulls)
.sees(link(title))
.checkpoint("the pull is listed")
.step(
"spindle has a pipeline for the pull",
pipelineFor("pullRequest", (ctx) => ctx.repo.heads[branches.from])
)
.open(page.pull(pullId))
.sees(testid("workflowName", { name: "ci.yml" }));
onBranches("closing and reopening a pull request")
.pull(pullId, patchId, branches)
.open(page.pull(pullId))
.changeState(pullId, "closed", testid("requestClose"), testid("changeState"))
.changeState(pullId, "open", testid("changeState"), testid("requestClose"))
.reload()
.sees(testid("requestClose"));
// the diff lists a file only once the mirror can read the commits between the branches
const diffedFile = testid("diffFileName", { path: "e2e/feature.txt" });
onBranches("the new pull form lists the branch's file in its diff")
.open(page.newPull(branches))
.sees(diffedFile)
.sees(within(component("PullCompose"), testid("submit")));
const viewedTitle = unique("a pull to view");
onBranches("the pull page shows the pull's title")
.pull(pullId, patchId, { ...branches, title: viewedTitle })
.open(page.pull(pullId))
.sees(not(testid("errorTitle")))
.sees(within(testid("ticketTitle"), text(viewedTitle)));
const otherPull = record("org.tangled.track.ticket");
const otherPatch = record("org.tangled.review.patch");
const searchPulls = within(component("TicketSearch"), testid("q"));
const pullNeedle = unique("search needle");
const pullHay = unique("unrelated");
const onTwoPulls = (name: string) =>
onBranches(name)
.pull(pullId, patchId, { ...branches, title: pullNeedle })
.pull(otherPull, otherPatch, { ...branches, title: pullHay });
onTwoPulls("searching the pull list finds the pull you named")
.open(page.pulls)
.sees(link(pullNeedle))
.sees(link(pullHay))
.fill(searchPulls, pullNeedle)
.press("Enter")
.sees(link(pullNeedle))
.sees(not(link(pullHay)));
const note = "first thoughts";
const secondThoughts = "second thoughts";
const commentMenu = within(component("PullReviewComment"), testid("moreMenu"));
const editor = within(component("PullReviewCommentEditor"), testid("body"));
const commentField = within(component("PullReviewCommentForm"), testid("reviewComment"));
const commented = (name: string) =>
onBranches(name)
.pull(pullId, patchId, branches)
.open(page.pull(pullId))
.fill(commentField, note)
.click(within(component("PullReviewCommentForm"), testid("submit")))
.sends(
commentId,
knot,
(ctx) => ({ record: { subject: { uri: ctx.get(pullId).uri } } }),
"create"
)
.trace(commentId, through(knot, hydrant, bobbin))
.trace(subscriptionId, through(pds))
.sees(within(testid("reviewCommentBody"), text(note)));
commented("commenting on a pull request")
.reload()
.sees(within(testid("reviewCommentBody"), text(note)));
const startEdit = <T extends ReturnType<typeof commented>>(flowSoFar: T) =>
flowSoFar
.click(commentMenu)
.click(testid("setEditing"))
.fill(editor, secondThoughts)
.click(within(component("PullReviewCommentEditor"), testid("submit")))
.sends(
commentId,
knot,
replacing(commentId, (ctx) => ({
subject: { uri: ctx.get(pullId).uri },
body: { text: secondThoughts }
})),
"put"
)
.trace(commentId, through(knot, hydrant, bobbin));
startEdit(commented("editing a comment on a pull request"))
.reload()
.sees(within(testid("reviewCommentBody"), text(secondThoughts)))
.sees(not(within(testid("reviewCommentBody"), text(note))));
startEdit(commented("an edited pull comment shows without a reload")).sees(
within(testid("reviewCommentBody"), text(secondThoughts))
);
const bugTitle = unique("a pull to resubmit");
const pushedFile = testid("diffFileName", { path: "e2e/more.txt" });
// round labels only head comments, so commit rows are how to tell rounds apart here
const twoCommitRows = (flow: Flow) => flow.sees(commitRow(2)).sees(not(commitRow(3)));
onBranches("resubmitting a pull request after pushing to its source branch", { ci: true })
.pull(pullId, patchId, { ...branches, title: bugTitle })
.push({ "e2e/more.txt": "more\n" }, { branch: branches.from })
.mirrored()
.open(page.pull(pullId))
.sees(not(pushedFile))
.sees(testid("resubmit"))
.click(testid("resubmit"))
.sends(patchId, knot, (ctx) => ({ patch: { ticket: rkeyOf(ctx, pullId) } }), "create")
.trace(patchId, through(knot, hydrant, bobbin))
.reload()
.sees(pushedFile)
.use(twoCommitRows)
.checkpoint("the resubmitted round shows in the diff")
.step(
"spindle has a pipeline for the new commit",
pipelineFor("pullRequest", (ctx) => ctx.repo.heads[branches.from])
);
const moreActions = within(component("Ticket"), testid("moreMenu"));
const editPull = within(component("Ticket"), testid("setEditing"));
const strangerTitle = "a pull from someone who can't contribute";
onBranches(
"a stranger's pull is refused when only collaborators may contribute and the page says so"
)
.policy("collaborators")
.actor(seeded.charlie)
.open(page.newPull(branches))
.fill(within(component("PullCompose"), testid("title")), strangerTitle)
.click(within(component("PullCompose"), testid("submit")))
.refused(pullId, 403)
.sees(refusal)
.open(page.pulls)
.sees(not(link(strangerTitle)));
const otherBranch = "other";
// keep it in its own folder, a shared file makes the knot refuse the merge
const withOtherBranch = (name: string) =>
onBranches(name)
.push({ "other/other.txt": "other\n" }, { branch: otherBranch, base: branches.into })
.mirrored()
.pull(pullId, patchId, branches);
const targetBranchPicker = within(
component("TicketForm"),
within(testid("targetBranch"), testid("trigger"))
);
const otherOption = testid("option", { value: otherBranch });
const targeting = (branch: string) => testid("targetBranchTag", { branch });
withOtherBranch("retargeting a pull request to another branch")
.open(page.pull(pullId))
.sees(targeting(branches.into))
.click(moreActions)
.click(editPull)
.click(targetBranchPicker)
.click(otherOption)
.click(within(component("TicketForm"), testid("submit")))
.sends(pullId, knot, replacing(pullId, { targetBranch: otherBranch }), "put")
.trace(pullId, through(knot, hydrant, bobbin))
.sees(targeting(otherBranch))
.step("the knot's copy targets the other branch", async (ctx) =>
expect((await knot.has(ctx.get(pullId))).targetBranch).toBe(otherBranch)
)
.reload()
.sees(targeting(otherBranch))
.sees(not(targeting(branches.into)));
// the page won't see this, so the knot's copy is edited directly
const retargetOnKnot = async (ctx: Ctx) => {
const reply = await editCob(ctx.actor, ctx.get(pullId).uri, "retarget", (stored) => ({
...stored,
targetBranch: otherBranch
}));
expect(reply.status, `the retarget answered ${JSON.stringify(reply.body)}`).toBe(200);
ctx.set(pullId, { uri: reply.body.uri, cid: reply.body.cid });
};
withOtherBranch("a retargeted pull merges into its new branch and leaves the old one alone")
.step("the pull is retargeted", retargetOnKnot)
.open(page.pull(pullId))
.sees(targeting(otherBranch))
.use(merges)
.knotStored(pullId, "completed")
.step("only the other branch moved", async (ctx) => {
const heads = await lsRemote(ctx.actor, ctx.repo.did, "refs/heads/");
expect(heads[`refs/heads/${branches.into}`]).toBe(ctx.repo.heads[branches.into]);
expect(heads[`refs/heads/${otherBranch}`]).not.toBe(ctx.repo.heads[otherBranch]);
});
onBranches("putting a label on a pull request and taking it off")
.pull(pullId, patchId, branches)
.step("define the label", defineLabel(triage))
.open(page.pull(pullId))
.use(labelOn(pullId, "pull"))
.sees(targeting(branches.into))
.use(labelOff(pullId, "pull"));
const reviewNote = "looks fine to me";
const peerSays = async (ctx: Ctx) => {
const reply = await comments.add(ctx.actor, ctx.repo.did, ctx.get(pullId), reviewNote);
expect(reply.status, `the review comment answered ${JSON.stringify(reply.body)}`).toBe(200);
await bobbin.indexed({ uri: reply.body.uri, cid: reply.body.cid });
};
// the wrapper has the same testid, so match the button
const assigneePicker = target("the assignee picker", (tab) =>
tab
.locator('[data-component="TicketForm"] button[data-testid="assignee"]')
.filter({ visible: true })
);
const peerOption = target("the peer in the picker", (tab, ctx) =>
testid("option", { value: recall(ctx, "peer").did }).locate(tab, ctx)
);
const assignedTo = target("the assignee in the pull's header", (tab, ctx) =>
testid("assignee", { handle: recall(ctx, "peer").handle }).locate(tab, ctx)
);
onBranches("assigning a pull request to a peer keeps the assignee after a reload")
.pull(pullId, patchId, branches)
.actor("peer")
.step("remember the peer", remember("peer"))
.member()
.step("the peer comments on the pull", peerSays)
.actor("owner")
.open(page.pull(pullId))
.click(moreActions)
.click(editPull)
.click(assigneePicker)
.click(peerOption)
.click(within(component("TicketForm"), testid("submit")))
.sends(pullId, knot, replacing(pullId), "put")
.trace(pullId, through(knot, hydrant, bobbin))
.sees(assignedTo)
.reload()
.sees(assignedTo);
const afterWords = "a note on the second round";
const inRound = (round: string, words: string) =>
within(
testid("roundGroup", { version: round.replace(/^v/, "") }),
within(testid("reviewCommentBody"), text(words))
);
onBranches("a comment written after a resubmit files under the new round")
.pull(pullId, patchId, branches)
.push({ "e2e/more.txt": "more\n" }, { branch: branches.from })
.mirrored()
.open(page.pull(pullId))
.sees(testid("resubmit"))
.click(testid("resubmit"))
.use(sendsPatch())
.use(twoCommitRows)
.fill(commentField, afterWords)
.click(within(component("PullReviewCommentForm"), testid("submit")))
.sends(
commentId,
knot,
(ctx) => ({ record: { subject: { uri: ctx.get(pullId).uri } } }),
"create"
)
.trace(commentId, through(knot, hydrant, bobbin))
.trace(subscriptionId, through(pds))
.sees(inRound("v1", afterWords))
.sees(not(inRound("v0", afterWords)))
.reload()
.sees(inRound("v1", afterWords))
.sees(not(inRound("v0", afterWords)));
const edited = "same here, again";
const cancel = within(component("PullReviewCommentEditor"), testid("cancel"));
onBranches("cancelling a pull comment edit leaves the comment as it was")
.pull(pullId, patchId, branches)
.open(page.pull(pullId))
.fill(commentField, note)
.click(within(component("PullReviewCommentForm"), testid("submit")))
.trace(commentId, through(knot, hydrant, bobbin))
.trace(subscriptionId, through(pds))
.sees(within(testid("reviewCommentBody"), text(note)))
.click(commentMenu)
.click(testid("setEditing"))
.fill(editor, edited)
.click(cancel)
.sendsNothing(commentId)
.sees(within(testid("reviewCommentBody"), text(note)))
.sees(not(within(testid("reviewCommentBody"), text(edited))))
.reload()
.sees(within(testid("reviewCommentBody"), text(note)))
.sees(not(within(testid("reviewCommentBody"), text(edited))))
.step(
"the knot and hydrant still have the first version",
stillStored(commentId, (value) => value.body.text, note)
);