import { expect } from "@playwright/test"; import { knotRecords } from "../harness/api"; import { bobbin, commentId, component, type Ctx, discussion, flow, hydrant, knot, link, not, page, pds, refusedTicketId, refusal, replacing, staleEdit, starCount, starId, subscriptionId, text, testid, through, ticketId, within } from "../harness/flow"; import { sleep, until, type Written } from "../harness/net"; import { answerWith, commented, jsonError, note } from "../harness/shared"; const stateChangeAlert = testid("stateChangeError"); const bobbinWait = "**/xrpc/org.tangled.bobbin.awaitRecord*"; const bobbinFails = answerWith(bobbinWait, 504, "UpstreamTimeout", "bobbin took too long"); const bobbinRecovers = (ctx: Ctx) => ctx.page.unroute(bobbinWait); const probeFails = answerWith( "**/xrpc/org.tangled.knot.describeKnot*", 503, "ServiceUnavailable", "the e2e flow cut this read" ); // a swapCommit the pds no longer has, so it refuses the star write const staleCommit = "bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvygrxj2r4gy"; const racedByAnotherWrite = (ctx: Ctx) => ctx.page.route("**/xrpc/com.atproto.repo.createRecord", async (route) => { const body = route.request().postDataJSON(); if (body?.collection !== starId.nsid) return route.fallback(); await route.fallback({ postData: JSON.stringify({ ...body, swapCommit: staleCommit }) }); }); // the failure clears after 2s, so check it right after the refusal const failedStar = testid("star", { failed: "true" }); flow("a star the pds refuses rolls back and the button says so") .actor("owner") .repo() .actor("fan") .open(page.repo) .step("the pds will refuse the next star", racedByAnotherWrite) .click(testid("star", { starred: "false" })) .sends( starId, pds, (ctx) => ({ record: { subject: { $type: "org.tangled.feed.star#repo", identity: ctx.repo.did } } }), "create" ) .sees(testid("star", { starred: "true" })) .sees(starCount, "1") .refused(starId, 400) .sees(failedStar) .sees(testid("star", { starred: "false" })) .sees(starCount, "0") .step("bobbin counts no stars", async (ctx) => expect(await bobbin.countStars(ctx.repo.did)).toBe(0) ) .reload() .sees(testid("star", { starred: "false" })) .sees(starCount, "0"); // a real burst is hard to hit on purpose, so the 429 is faked for one comment const rateLimited = "too many writes, slow down"; const limitsTheNextComment = async (ctx: Ctx) => { let limited = false; await ctx.page.route("**/xrpc/com.atproto.repo.createRecord", async (route) => { if (limited || route.request().postDataJSON()?.collection !== commentId.nsid) return route.fallback(); limited = true; await route.fulfill(jsonError(429, "RateLimitExceeded", rateLimited)); }); }; flow("a comment the knot rate limits keeps its text and can be sent again") .actor("owner") .repo() .ticket(ticketId, { title: commented }) .open(page.issue(ticketId)) .step("the knot will rate limit the next comment", limitsTheNextComment) .fill(discussion, note) .click(testid("submit")) .sees(refusal) .step("the box still holds the text", async (ctx) => expect(discussion.locate(ctx.page, ctx)).toHaveValue(note) ) .sendsNothing(commentId) .click(testid("submit")) .sends( commentId, knot, (ctx) => ({ record: { subject: { $type: "com.atproto.repo.strongRef", ...ctx.get(ticketId) } } }), "create" ) .trace(commentId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .sees(text(note)) .sees(not(refusal)) .reload() .sees(text(note)); flow("two submits of the comment form in one task send one write") .actor("owner") .repo() .ticket(ticketId, { title: commented }) .open(page.issue(ticketId)) .fill(discussion, note) .step("the form is submitted twice before the page can react", (ctx) => discussion.locate(ctx.page, ctx).evaluate((box) => { const form = (box as HTMLTextAreaElement).form!; form.requestSubmit(); form.requestSubmit(); }) ) .sends(commentId, knot, { record: { body: { text: note } } }, "create") .trace(commentId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .sendsNothing(commentId) .sees(text(note)); // the knot saves the comment and answers late, so the page reloads mid-send const slowAnswer = async (ctx: Ctx) => { await ctx.page.route("**/xrpc/com.atproto.repo.createRecord", async (route) => { if (route.request().postDataJSON()?.collection !== commentId.nsid) return route.fallback(); const response = await route.fetch(); await sleep(4_000); // the page this answer was for is gone by then await route.fulfill({ response }).catch(() => undefined); }); }; const sentComment = new WeakMap(); flow("reloading while a comment is still sending leaves it posted once") .actor("owner") .repo() .ticket(ticketId, { title: commented }) .open(page.issue(ticketId)) .step("the knot is slow to answer", slowAnswer) .fill(discussion, note) .click(testid("submit")) .step("the knot has the comment and the page is still waiting", async (ctx) => { const [stored] = await until("the knot should have the comment", 10_000, async () => { const records = await knotRecords(ctx.repo.did, commentId.nsid); return [records.length === 1, `it lists ${records.length}`, records]; }); sentComment.set(ctx, stored); await expect(testid("submit").locate(ctx.page, ctx)).toBeDisabled(); }) .step("the page is reloaded", async (ctx) => { await ctx.page.reload({ waitUntil: "networkidle" }); }) .step("every service has the comment", async (ctx) => { const written = sentComment.get(ctx)!; await Promise.all([ knot.present(written, ctx.actor), hydrant.present(written), bobbin.indexed(written) ]); }) .reload() .sees(text(note)) .step("there is one comment", async (ctx) => { await expect(ctx.page.getByText(note, { exact: true })).toHaveCount(1); expect(await knotRecords(ctx.repo.did, commentId.nsid)).toHaveLength(1); }); // 60 family emoji are about 1500 bytes, over the title's 1024 byte max const familyOf = (graphemes: number) => "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ".repeat(graphemes); const tooLong = familyOf(60); const trimmed = familyOf(40); const titleField = testid("title"); flow("a title over the byte limit is refused with a reason and a shorter one saves") .actor("owner") .repo() .open(page.newIssue) .fill(titleField, tooLong) .click(testid("submit")) .sends(refusedTicketId, knot, { record: { title: { text: tooLong } } }, "create") .refused(refusedTicketId, 400) .sees(refusal) .step("the form still holds the title", async (ctx) => expect(titleField.locate(ctx.page, ctx)).toHaveValue(tooLong) ) .fill(titleField, trimmed) .click(testid("submit")) .sends(ticketId, knot, { record: { title: { text: trimmed } } }, "create") .trace(ticketId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .at(page.issues) .sees(link(trimmed)); const slowTitle = "saved while bobbin was slow"; flow("creating an issue the knot saved while bobbin was slow does not make a second one") .actor("owner") .repo() .open(page.newIssue) .step("bobbin fails the page's wait", bobbinFails) .fill(titleField, slowTitle) .click(testid("submit")) .sends(ticketId, knot, { record: { title: { text: slowTitle } } }, "create") .trace(ticketId, through(knot, hydrant, bobbin)) .sees(refusal) .step("bobbin answers the page again", bobbinRecovers) .click(testid("submit")) .sendsNothing(ticketId) .trace(subscriptionId, through(pds)) .step("the knot has one issue with that title", async (ctx) => { const stored = await knotRecords(ctx.repo.did, ticketId.nsid); expect(stored.filter((record) => record.value.title.text === slowTitle)).toHaveLength(1); }); flow("closing an issue the knot took while bobbin was slow shows it closed") .actor("owner") .repo() .ticket(ticketId, { title: "close this one" }) .open(page.issue(ticketId)) .step("bobbin fails the page's wait", bobbinFails) .click(testid("requestStateChange", { state: "open" })) .sends(ticketId, knot, replacing(ticketId, { state: "closed" }), "put") .trace(ticketId, through(knot, hydrant, bobbin)) .knotStored(ticketId, "closed") .sees(stateChangeAlert) .sees(testid("requestStateChange", { state: "closed" })) .step("bobbin answers the page again", bobbinRecovers) .reload() .sees(testid("requestStateChange", { state: "closed" })); // guards openTicket and openComment against probing, since a failed probe falls back to the pds const probedTitle = "written while the probe fails"; flow("an issue opened while the knot's capability probe fails still lands on the knot") .actor("owner") .repo() .open(page.newIssue) .step("the knot's capability probe fails", probeFails) .fill(titleField, probedTitle) .click(testid("submit")) .sends( ticketId, knot, (ctx) => ({ repo: ctx.repo.did, record: { $type: ticketId.nsid, title: { text: probedTitle } } }), "create" ) .trace(ticketId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .at(page.issues) .sees(link(probedTitle)); flow("a comment sent while the knot's capability probe fails still lands on the knot") .actor("owner") .repo() .ticket(ticketId, { title: commented }) .open(page.issue(ticketId)) .step("the knot's capability probe fails", probeFails) .fill(discussion, note) .click(testid("submit")) .sends( commentId, knot, (ctx) => ({ repo: ctx.repo.did, record: { $type: commentId.nsid, body: { text: note } } }), "create" ) .trace(commentId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .sees(text(note)); const firstTitle = "a title before anyone edits it"; const secondTitle = "retitled from the second window"; const staleTitle = "retitled from the stale window"; const moreActions = testid("moreMenu"); flow("an edit from a page that loaded before another edit is refused") .actor("owner") .repo() .ticket(ticketId, { title: firstTitle }) .open(page.issue(ticketId)) .click(moreActions) .click(testid("setEditing")) .fill(titleField, staleTitle) .window("second") .open(page.issue(ticketId)) .click(moreActions) .click(testid("setEditing")) .fill(titleField, secondTitle) .click(within(component("TicketForm"), testid("submit"))) .sends(ticketId, knot, replacing(ticketId, { title: { text: secondTitle } }), "put") .trace(ticketId, through(knot, hydrant, bobbin)) .sees(within(testid("ticketTitle"), text(secondTitle))) .window() .click(within(component("TicketForm"), testid("submit"))) .sees(staleEdit) .sendsNothing(refusedTicketId) .step("the form still holds the stale window's title", async (ctx) => expect(titleField.locate(ctx.page, ctx)).toHaveValue(staleTitle) ) .step("the knot still has the second title", async (ctx) => { const stored = await knot.has(ctx.get(ticketId)); expect(stored.title.text).toBe(secondTitle); });