Skip to content
Myr

refs.test.tsx

This file

Path

apps/web/test/refs.test.tsx

424 lines · 13.4 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 { RefsView as Refs } from "@repo/protocol/domain/code";4import { defaultParseSearch } from "@tanstack/react-router";5import { Effect, Option, Schema } from "effect";6import type { ReactElement } from "react";7import { renderToStaticMarkup } from "react-dom/server";89import { RefChoices } from "../src/features/code/ref-switcher.tsx";10import { packageParamOf } from "../src/features/code/refs-param.ts";11import { RefsView, TAGS_PAGE } from "../src/features/code/refs-view.tsx";12import { plainText } from "./ask-fixtures.ts";13import { elementWith, factsOf, mainOf } from "./records.ts";14import { renderAt } from "./render.tsx";1516const markupOf = (element: ReactElement) => renderToStaticMarkup(element);1718const textOf = (element: ReactElement) => plainText(markupOf(element));1920const hrefsOf = (markup: string) =>21  [...markup.matchAll(/href="(?<href>[^"]*)"/gu)].map((match) =>22    (match.groups?.href ?? "").replaceAll("&amp;", "&")23  );2425const slice = (markup: string, marker: string, value: string) => {26  const start = markup.indexOf(`${marker}="${value}"`);27  const next = markup.indexOf(`${marker}="`, start + 1);2829  return start === -130    ? ""31    : markup.slice(start, next === -1 ? undefined : next);32};3334const platformUrl = "https://platform-test.example.workers.dev";3536const LANDED = "1".repeat(40);3738const IMPORTED = "2".repeat(40);3940const OLDER = "3".repeat(40);4142const TAG_SEGMENT = "%40datekit%2Fcore%401.0.0";4344const pickNothing = () => {};4546describe(RefsView, () => {47  const refs = Schema.decodeSync(Refs)({48    forks: [49      {50        ask: "parse-iso",51        handle: "ines-v",52        kind: "claim",53        repo: "datekit--parse-iso--ines-v",54        state: "live",55      },56      { handle: "bram", kind: "working", repo: "datekit--me--bram" },57    ],58    landed: LANDED,59    project: "datekit",60    tags: [61      { name: "v1.1.0", sha: IMPORTED },62      { name: "v1.0.0", sha: OLDER },63    ],64  });6566  const page = <RefsView platformUrl={platformUrl} refs={refs} />;6768  it("is titled Refs", () => {69    expect(markupOf(page)).toMatch(/<h1\b[^>]*>Refs<\/h1>/u);70  });7172  it("shows main as a row that moves only by a landing, at its landed commit, linked", () => {73    const main = elementWith(mainOf(markupOf(page)), 'data-ref="main"');7475    expect(plainText(main)).toContain("moves only by a landing");76    expect(main).toContain(LANDED.slice(0, 7));77    expect(main).toContain(`title="${LANDED}"`);78    expect(hrefsOf(main)).toContain(`/oscabriel/datekit/commit/${LANDED}`);79  });8081  it("offers main's files from its row", () => {82    const main = elementWith(mainOf(markupOf(page)), 'data-ref="main"');8384    expect(hrefsOf(main)).toContain("/oscabriel/datekit/tree/main");85    expect(plainText(main)).toContain("Browse files");86  });8788  it("says plainly when nothing has landed on main", () => {89    const empty = Schema.decodeSync(Refs)({90      forks: [],91      project: "datekit",92      tags: [],93    });9495    expect(96      textOf(<RefsView platformUrl={platformUrl} refs={empty} />)97    ).toContain("Nothing has landed on main yet.");98  });99100  it("lists the tags newest first, each linking its tree first and its commit", () => {101    const markup = mainOf(markupOf(page));102    const newest = slice(markup, "data-tag", "v1.1.0");103104    expect(markup.indexOf('data-tag="v1.1.0"')).toBeGreaterThan(-1);105    expect(markup.indexOf('data-tag="v1.1.0"')).toBeLessThan(106      markup.indexOf('data-tag="v1.0.0"')107    );108    expect(hrefsOf(newest).slice(0, 2)).toStrictEqual([109      "/oscabriel/datekit/tree/v1.1.0",110      `/oscabriel/datekit/commit/${IMPORTED}`,111    ]);112  });113114  it("draws a claim fork as one line: its name in mono, its answer, its claimant and the claim's state in a word", () => {115    const fork = elementWith(116      mainOf(markupOf(page)),117      'data-fork="datekit--parse-iso--ines-v"'118    );119120    expect(fork).toContain('data-state="live"');121    expect(fork).toMatch(122      /<p\b[^>]*font-mono[^>]*><a\b[^>]*>datekit--parse-iso--ines-v</u123    );124    expect(hrefsOf(fork)).toStrictEqual([125      "/oscabriel/datekit/answers/parse-iso",126      "/people/ines-v",127    ]);128    expect(plainText(fork)).toContain("Claimed");129    // One line: the row holds one line of text, not two.130    expect(fork.match(/<p\b/gu)).toHaveLength(1);131  });132133  it("names a working fork's person, apart from the claims", () => {134    const fork = elementWith(135      mainOf(markupOf(page)),136      'data-fork="datekit--me--bram"'137    );138139    expect(plainText(fork)).toContain("datekit--me--bram");140    expect(plainText(fork)).toContain("Working fork of");141    expect(hrefsOf(fork)).toContain("/people/bram");142  });143144  it("puts the clone command and what a claim fork is in its facts", () => {145    const facts = factsOf(markupOf(page));146147    expect(plainText(facts)).toContain(148      `git clone ${platformUrl}/oscabriel/datekit.git`149    );150    // The command is the text of the one code element a tap selects whole.151    expect(152      plainText(153        /<code\b[^>]*select-all[^>]*>.*?<\/code>/u.exec(facts)?.[0] ?? ""154      )155    ).toMatch(/^ ?git clone /u);156    expect(plainText(facts)).toContain("claim fork");157  });158159  it("never names the platform's own refs, and says tags, not releases", () => {160    const text = textOf(page);161162    expect(text).not.toContain(Brand.refsPrefix);163    expect(text).not.toContain("refs/notes");164    expect(text.toLowerCase()).not.toContain("release");165  });166167  it("lists no packages when the tags version none or one", () => {168    const one = Schema.decodeSync(Refs)({169      forks: [],170      project: "datekit",171      tags: [172        { name: "v1.1.0", sha: IMPORTED },173        { name: "@datekit/core@2.0.0", sha: LANDED },174        { name: "@datekit/core@1.0.0", sha: OLDER },175      ],176    });177178    const view = <RefsView platformUrl={platformUrl} refs={one} />;179180    expect(markupOf(page)).not.toContain("data-package=");181    expect(markupOf(view)).not.toContain("data-package=");182    expect(textOf(view)).not.toContain("Packages");183  });184185  it.effect("sits under the project's crumbs: datekit, then Refs", () =>186    Effect.gen(function* () {187      const markup = yield* renderAt("/oscabriel/datekit/refs", page);188      const crumbs = elementWith(markup, 'aria-label="Breadcrumb"');189190      expect(crumbs).toMatch(191        /<a\b[^>]*href="\/oscabriel\/datekit"[^>]*>oscabriel\/datekit<\/a>/u192      );193      expect(plainText(crumbs)).toContain("Refs");194    })195  );196});197198const viewOf = (refs: Refs, only?: string, from?: number) => (199  <RefsView from={from} package={only} platformUrl={platformUrl} refs={refs} />200);201202describe("a monorepo's refs page", () => {203  const monorepo = Schema.decodeSync(Refs)({204    forks: [],205    landed: LANDED,206    project: "datekit",207    tags: [208      { name: "v1.1.0", sha: IMPORTED },209      { name: "@datekit/core@2.0.0", sha: LANDED },210      { name: "@datekit/core@1.0.0", sha: OLDER },211      { name: "fmt@0.1.0", sha: OLDER },212    ],213  });214215  const CORE = "/oscabriel/datekit/refs?package=%40datekit%2Fcore";216217  it("lists each package above the tags, with its newest tag, linked to its tree, and its count", () => {218    const markup = mainOf(markupOf(viewOf(monorepo)));219    const core = slice(markup, "data-package", "@datekit/core");220    const fmt = slice(markup, "data-package", "fmt");221222    expect(textOf(viewOf(monorepo))).toContain("Packages");223    expect(hrefsOf(core)).toStrictEqual([224      CORE,225      `/oscabriel/datekit/tree/${TAG_SEGMENT.replace("1.0.0", "2.0.0")}`,226    ]);227    expect(plainText(core)).toContain("2 tags");228    // The last package's slice runs on to the page's end: its first two links.229    expect(hrefsOf(fmt).slice(0, 2)).toStrictEqual([230      "/oscabriel/datekit/refs?package=fmt",231      "/oscabriel/datekit/tree/fmt%400.1.0",232    ]);233    expect(fmt).toContain("1 tag<");234  });235236  it("puts the packages above the tags", () => {237    const markup = markupOf(viewOf(monorepo));238239    expect(markup.indexOf('data-package="@datekit/core"')).toBeGreaterThan(-1);240    expect(markup.indexOf('data-package="@datekit/core"')).toBeLessThan(241      markup.indexOf('data-tag="v1.1.0"')242    );243  });244245  it("lists only one package's tags, newest first, with a way back to all", () => {246    const element = viewOf(monorepo, "@datekit/core");247    const markup = markupOf(element);248249    expect(250      [...markup.matchAll(/data-tag="(?<tag>[^"]*)"/gu)].map(251        (match) => match.groups?.tag252      )253    ).toStrictEqual(["@datekit/core@2.0.0", "@datekit/core@1.0.0"]);254    expect(markup).not.toContain("data-package=");255    expect(hrefsOf(markup)).toContain("/oscabriel/datekit/refs");256    expect(textOf(element)).toContain("All tags");257  });258259  it("pages one package's tags, keeping the package in the address", () => {260    const many = Schema.decodeSync(Refs)({261      forks: [],262      project: "datekit",263      tags: [264        ...Array.from({ length: TAGS_PAGE + 1 }, (_, at) => ({265          name: `@datekit/core@1.0.${TAGS_PAGE - at}`,266          sha: OLDER,267        })),268        { name: "fmt@0.1.0", sha: OLDER },269      ],270    });271272    expect(hrefsOf(markupOf(viewOf(many, "@datekit/core")))).toContain(273      `${CORE}&from=${TAGS_PAGE}`274    );275    expect(276      hrefsOf(markupOf(viewOf(many, "@datekit/core", TAGS_PAGE)))277    ).toContain(CORE);278  });279280  it("says plainly that a package it does not know has no tags", () => {281    const element = viewOf(monorepo, "@datekit/none");282283    expect(markupOf(element)).not.toContain("data-tag=");284    expect(textOf(element)).toContain("No tags for @datekit/none.");285    expect(hrefsOf(markupOf(element))).toContain("/oscabriel/datekit/refs");286  });287288  it("links each package so the router reads its name back, even one that reads as a number", () => {289    const odd = Schema.decodeSync(Refs)({290      forks: [],291      project: "datekit",292      tags: [293        { name: "2048@1.0.0", sha: OLDER },294        { name: "true@1.0.0", sha: OLDER },295        { name: "@datekit/core@1.0.0", sha: OLDER },296      ],297    });298299    const read = hrefsOf(markupOf(viewOf(odd)))300      .filter((href) => href.includes("package="))301      .map((href) => {302        // What the router hands the route's `validateSearch`.303        const search = defaultParseSearch(new URL(href, "https://web").search);304305        return packageParamOf("package" in search ? search.package : undefined);306      });307308    expect(read).toStrictEqual([309      Option.some("2048"),310      Option.some("true"),311      Option.some("@datekit/core"),312    ]);313  });314315  it("reads a package from the address only when it names one", () => {316    expect(packageParamOf("@datekit/core")).toStrictEqual(317      Option.some("@datekit/core")318    );319    expect(320      [undefined, "", 3, "x".repeat(256)].map((value) => packageParamOf(value))321    ).toStrictEqual([322      Option.none(),323      Option.none(),324      Option.none(),325      Option.none(),326    ]);327  });328});329330describe(RefChoices, () => {331  const refs = Schema.decodeSync(Refs)({332    forks: [333      {334        ask: "parse-iso",335        handle: "ines-v",336        kind: "claim",337        repo: "datekit--parse-iso--ines-v",338        state: "warned",339      },340      { handle: "bram", kind: "working", repo: "datekit--me--bram" },341    ],342    landed: LANDED,343    project: "oscabriel/datekit",344    tags: [345      { name: "v1.1.0", sha: IMPORTED },346      { name: "v1.0.0", sha: OLDER },347    ],348  });349350  const choices = (filter = "") =>351    markupOf(352      <RefChoices353        filter={filter}354        hrefAt={(rev) => `/oscabriel/datekit/tree/${rev}`}355        onPick={pickNothing}356        refs={refs}357        rev="main"358      />359    );360361  /** Ines's fork entry, as the list draws it while `rev` is read. */362  const forkAt = (rev: string) =>363    elementWith(364      markupOf(365        <RefChoices366          hrefAt={(to) => `/oscabriel/datekit/tree/${to}`}367          onPick={pickNothing}368          refs={refs}369          rev={rev}370        />371      ),372      'data-fork="ines-v/parse-iso"'373    );374375  it("lists main as current, then the tags, then the answers in forks", () => {376    const markup = choices();377378    expect(379      [380        ...markup.matchAll(/data-(?:ref-choice|tag|fork)="(?<name>[^"]*)"/gu),381      ].map(({ groups }) => groups?.name)382    ).toStrictEqual(["main", "v1.1.0", "v1.0.0", "ines-v/parse-iso"]);383    expect(elementWith(markup, 'data-ref-choice="main"')).toContain(384      'aria-current="true"'385    );386  });387388  it("names each answer's ask, claimant and claim state, and opens the fork at its address", () => {389    const fork = elementWith(choices(), 'data-fork="ines-v/parse-iso"');390391    expect(hrefsOf(fork)).toStrictEqual([392      "/oscabriel/datekit/tree/@ines-v/parse-iso",393    ]);394    expect(plainText(fork)).toContain("parse-iso");395    expect(plainText(fork)).toContain("ines-v · Warned, no push lately");396  });397398  it("marks the fork being read as current, at its main or at one of its commits", () => {399    expect(400      [401        "@ines-v/parse-iso",402        `@ines-v/parse-iso:${LANDED}`,403        "main",404        "@bram/parse-iso",405      ].map((rev) => forkAt(rev).includes('aria-current="true"'))406    ).toStrictEqual([true, true, false, false]);407  });408409  it("lists no working fork and names no fork's repo", () => {410    const markup = choices();411412    expect(markup).not.toContain("datekit--me--bram");413    expect(markup).not.toContain("datekit--parse-iso--ines-v");414  });415416  it("finds a fork by its claimant or its ask, and a tag by its name", () => {417    expect(choices("ines").match(/data-(?:tag|fork)="[^"]*"/gu)).toStrictEqual([418      'data-fork="ines-v/parse-iso"',419    ]);420    expect(choices("v1.0").match(/data-(?:tag|fork)="[^"]*"/gu)).toStrictEqual([421      'data-tag="v1.0.0"',422    ]);423  });424});