Skip to content
Myr

row.test.tsx

This file

Path

apps/web/test/row.test.tsx

432 lines · 12.5 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { AskListing } from "@repo/protocol/domain/ask";4import { Outcome } from "@repo/protocol/domain/outcome";5import { CommitSha, Handle, ProjectName } from "@repo/protocol/ids";6import { StaleBase } from "@repo/protocol/refusal";7import { DateTime, Effect, Schema } from "effect";8import type { ReactElement } from "react";910import { STATE_MARKS } from "../src/features/shared/marks.tsx";11import { AskRow, Row, RowGroup } from "../src/features/shared/row.tsx";12import { attributeOf, tagsWith, textIn } from "./markup.ts";13import { renderAt } from "./render.tsx";1415const burrow = ProjectName.make("oscabriel/burrow");1617const listing = (territory: string) =>18  Schema.decodeSync(AskListing)({19    ask: "fix-week-start",20    blockedBy: [],21    claimed: false,22    kind: "bug",23    territory,24    want: "A week starts on the locale's first day, not always on Sunday.",25  });2627const rowsAt = (element: ReactElement) => renderAt("/", <ul>{element}</ul>);2829const anchors = (markup: string) =>30  [...markup.matchAll(/<a\b(?<attributes>[^>]*)>(?<body>.*?)<\/a>/gsu)].map(31    (match) => [32      attributeOf(match.groups?.attributes ?? "", "href"),33      textIn(match.groups?.body ?? ""),34    ]35  );3637describe(AskRow, () => {38  it.effect("links the want, never the id, to the ask's page", () =>39    Effect.gen(function* () {40      const markup = yield* rowsAt(41        <AskRow listing={listing("@burrow/auth")} project={burrow} takeable />42      );4344      expect(anchors(markup)).toContainEqual([45        "/oscabriel/burrow/asks/fix-week-start",46        "A week starts on the locale's first day, not always on Sunday.",47      ]);48      expect(49        anchors(markup).some(([, text]) => text === "fix-week-start")50      ).toBeFalsy();51      expect(markup).toContain(52        `title="A week starts on the locale&#x27;s first day, not always on Sunday."`53      );54    })55  );5657  it.effect("leaves a whole-repo territory out of the second line", () =>58    Effect.gen(function* () {59      const whole = textIn(60        yield* rowsAt(61          <AskRow listing={listing(".")} project={burrow} takeable />62        )63      );6465      const module = textIn(66        yield* rowsAt(67          <AskRow listing={listing("@burrow/auth")} project={burrow} takeable />68        )69      );7071      expect(whole).not.toContain("Territory");72      expect(whole).not.toMatch(/·\s*\.\s*(?:·|$)/u);73      expect(module).toContain("bug · @burrow/auth · fix-week-start");74    })75  );7677  it.effect(78    "holds the claim command as text inside its copy button, named for what it copies",79    () =>80      Effect.gen(function* () {81        const markup = yield* rowsAt(82          <AskRow listing={listing("@burrow/auth")} project={burrow} takeable />83        );8485        const command = `${Brand.cli} claim fix-week-start --project oscabriel/burrow`;8687        expect(markup).toMatch(88          new RegExp(89            `<button\\b[^>]*aria-label="Copy claim command"[^>]*>.*<code class="sr-only">${command}</code>.*</button>`,90            "su"91          )92        );93      })94  );9596  it.effect(97    "carries the ask, its project and whether the viewer may take it on the row itself",98    () =>99      Effect.gen(function* () {100        const markup = yield* rowsAt(101          <AskRow listing={listing("@burrow/auth")} project={burrow} takeable />102        );103104        const [row] = tagsWith(markup, "data-ask");105106        expect(row).toMatch(/^<li\b/u);107        expect(attributeOf(row ?? "", "data-ask")).toBe("fix-week-start");108        expect(attributeOf(row ?? "", "data-project")).toBe("oscabriel/burrow");109        expect(attributeOf(row ?? "", "data-takeable")).toBe("true");110      })111  );112113  it.effect(114    "says an ask it may take is open to take, and gives one it may not no command",115    () =>116      Effect.gen(function* () {117        const open = textIn(118          yield* rowsAt(119            <AskRow120              listing={listing("@burrow/auth")}121              project={burrow}122              takeable123            />124          )125        );126127        const held = yield* rowsAt(128          <AskRow129            listing={{ ...listing("@burrow/auth"), claimed: true }}130            project={burrow}131            takeable={false}132          />133        );134135        expect(open).toContain("Open to take");136        expect(textIn(held)).toContain("Claimed");137        expect(held).not.toContain("Copy claim command");138      })139  );140  it.effect(141    "draws a refusal's word in the refused role even when it can be retried, its mark apart",142    () =>143      Effect.gen(function* () {144        const markup = yield* rowsAt(145          <AskRow146            listing={listing("@burrow/auth")}147            progress={{148              lastSeq: 2,149              latest: Outcome.cases.Refused.make({150                refusals: [151                  new StaleBase({152                    base: CommitSha.make("a".repeat(40)),153                    conflicts: [],154                    main: CommitSha.make("b".repeat(40)),155                  }),156                ],157              }),158              rounds: [],159            }}160            project={burrow}161          />162        );163164        const words = [165          ...markup.matchAll(/<span class="(?<classes>[^"]*)">Refused</gu),166        ].map((match) => match.groups?.classes ?? "");167168        expect(words.length).toBeGreaterThan(0);169170        for (const classes of words) {171          expect(classes).toContain("text-refused");172          expect(classes).not.toContain("text-waiting");173        }174      })175  );176});177178const wordClasses = (markup: string, word: string) =>179  [180    ...markup.matchAll(181      new RegExp(`<span class="(?<classes>[^"]*)">${word}<`, "gu")182    ),183  ].map((match) => match.groups?.classes ?? "");184185describe("an ask row's standing, word and time", () => {186  const now = DateTime.makeUnsafe("2026-10-11T12:00:00Z");187  const held = { ...listing("@burrow/auth"), reserve: "newcomers" } as const;188189  it.effect(190    "reads an ask held for newcomers as held for them, whether or not the viewer may take it",191    () =>192      Effect.gen(function* () {193        const refused = textIn(194          yield* rowsAt(195            <AskRow listing={held} project={burrow} takeable={false} />196          )197        );198199        const unknown = textIn(200          yield* rowsAt(<AskRow listing={held} project={burrow} />)201        );202203        expect(refused).toContain("Held for newcomers");204        expect(refused).not.toContain("Not open to you");205        expect(unknown).toContain("Held for newcomers");206      })207  );208209  it.effect(210    "sets a waiting word in ink, its colour on the mark alone, and keeps Landed green",211    () =>212      Effect.gen(function* () {213        const waiting = yield* rowsAt(214          <AskRow215            listing={{ ...listing("@burrow/auth"), blockedBy: [held.ask] }}216            project={burrow}217          />218        );219220        const landed = yield* rowsAt(221          <Row222            href="/burrow/asks/a"223            id="a"224            state={{ mark: "landed", word: "Landed" }}225            title="Format ordinals"226          />227        );228229        expect(wordClasses(waiting, "Waiting").length).toBeGreaterThan(0);230        expect(wordClasses(waiting, "Waiting").join(" ")).not.toContain(231          STATE_MARKS.waiting.tone232        );233        expect(waiting).toMatch(/<svg\b[^>]*text-waiting/u);234        expect(wordClasses(landed, "Landed").join(" ")).toContain(235          "text-landed"236        );237      })238  );239240  it.effect(241    "fills the time slot with the last push, and names the round in line 2",242    () =>243      Effect.gen(function* () {244        const at = DateTime.subtract(now, { hours: 3 });245        const head = CommitSha.make("c".repeat(40));246247        const markup = yield* rowsAt(248          <AskRow249            listing={listing("@burrow/auth")}250            now={now}251            progress={{252              claimant: Handle.make("bram"),253              lastPush: { at, head },254              lastSeq: 4,255              rounds: [256                {257                  at,258                  head,259                  outcome: Outcome.cases.Checking.make({ stage: "checks" }),260                  seq: 3,261                },262                {263                  at,264                  head,265                  outcome: Outcome.cases.Checking.make({ stage: "checks" }),266                  seq: 4,267                },268              ],269            }}270            project={burrow}271          />272        );273274        expect(markup).toMatch(275          new RegExp(276            `<time dateTime="${DateTime.formatIso(at)}"[^>]*>3 h ago</time>`,277            "u"278          )279        );280        expect(textIn(markup)).toContain("round 2");281      })282  );283284  it.effect(285    "fills the time slot with the ask's opening when nothing was pushed",286    () =>287      Effect.gen(function* () {288        const opened = DateTime.subtract(now, { days: 2 });289290        const markup = yield* rowsAt(291          <AskRow292            listing={listing("@burrow/auth")}293            now={now}294            progress={{ lastSeq: 1, opened, rounds: [] }}295            project={burrow}296            takeable297          />298        );299300        expect(textIn(markup)).toContain("2 d ago");301      })302  );303304  it.effect(305    "gives a selected row the pressed surface under the pointer, and lets the want take two lines on a phone",306    () =>307      Effect.gen(function* () {308        const markup = yield* rowsAt(309          <Row310            data={{ "data-ask": "a" }}311            href="/burrow/asks/a"312            id="a"313            selected314            title="Format ordinals"315          />316        );317318        const [row] = tagsWith(markup, "data-ask");319320        const want = /<p class="(?<classes>[^"]*)"><a\b/u.exec(markup)?.groups321          ?.classes;322323        expect(attributeOf(row ?? "", "class")?.split(" ")).toStrictEqual(324          expect.arrayContaining(["bg-surface", "hover:bg-surface-2"])325        );326        expect(want?.split(" ")).toStrictEqual(327          expect.arrayContaining(["line-clamp-2", "tablet:line-clamp-1"])328        );329      })330  );331});332333describe(Row, () => {334  it.effect(335    "puts the state's word beside its mark, and keeps the mark from the screen reader",336    () =>337      Effect.gen(function* () {338        const markup = yield* rowsAt(339          <Row340            href="/oscabriel/datekit/asks/a"341            id="a"342            state={{ mark: "landed", word: "Landed" }}343            title="Format ordinals"344          />345        );346347        expect(textIn(markup)).toContain("Landed");348        expect(markup).toMatch(/<svg\b[^>]*aria-hidden="true"/u);349      })350  );351352  it.effect(353    "marks the selected row, and gives j and k its link to move to",354    () =>355      Effect.gen(function* () {356        const markup = yield* rowsAt(357          <Row358            href="/oscabriel/datekit/asks/a"359            id="a"360            selected361            title="Format ordinals"362          />363        );364365        expect(markup).toContain('data-selected="true"');366        expect(markup).toContain('data-list-key="a"');367      })368  );369370  it.effect(371    "keeps the whole row the link's to press at every width, with nothing beside the list to select into",372    () =>373      Effect.gen(function* () {374        const markup = yield* rowsAt(375          <Row376            href="/oscabriel/datekit/asks/a"377            id="a"378            selected379            title="Format ordinals"380          />381        );382383        const [link] = tagsWith(markup, "data-list-key");384385        expect(link).toContain("after:inset-0");386        expect(link).not.toContain("pointer-events-none");387      })388  );389390  it.effect(391    "keeps its raised controls inside its own layer, under the sticky headers",392    () =>393      Effect.gen(function* () {394        const markup = yield* rowsAt(395          <Row396            action={<button type="button">Copy</button>}397            data={{ "data-ask": "a" }}398            href="/oscabriel/datekit/asks/a"399            id="a"400            title="Format ordinals"401          />402        );403404        const [row] = tagsWith(markup, "data-ask");405406        expect(attributeOf(row ?? "", "class")?.split(" ")).toContain(407          "isolate"408        );409      })410  );411412  it.effect("heads a group with its name as a link and its counts", () =>413    Effect.gen(function* () {414      const markup = yield* renderAt(415        "/",416        <RowGroup417          counts="7 open · 6 to take"418          name={<a href="/oscabriel/datekit">datekit</a>}419        >420          <Row421            href="/oscabriel/datekit/asks/a"422            id="a"423            title="Format ordinals"424          />425        </RowGroup>426      );427428      expect(textIn(markup)).toContain("datekit");429      expect(textIn(markup)).toContain("7 open · 6 to take");430    })431  );432});