Skip to content
Myr

render.test.ts

This file

Path

packages/protocol/test/render.test.ts

509 lines · 16.3 KiB · TypeScript

Last changed by

Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571

History of this file

Take it

1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Result } from "effect";45import { Outcome } from "../src/domain/outcome.ts";6import {7  AskId,8  CommitSha,9  DecisionId,10  EnvKey,11  Handle,12  LandedSha,13  ProjectName,14  ProposalId,15  RepoPath,16  RequestId,17} from "../src/ids.ts";18import {19  CapReached,20  ChecksFailed,21  DirectPush,22  DraftInProgress,23  NotDecider,24  OutOfScope,25  PrivatePathInPublicTree,26  ProposalClosed,27  ReachedInternals,28  StaleBase,29  TouchedJudge,30  UnknownPath,31  UnknownProposal,32  UnknownRevision,33  ValueTooShort,34  Weakened,35} from "../src/refusal.ts";36import { explain, headline, receiptLine } from "../src/render.ts";37import { ask, base, examples, main, path } from "./examples.ts";3839const outOfScope = new OutOfScope({ paths: [path] });4041const touchedJudge = new TouchedJudge({ paths: [path] });4243describe(headline, () => {44  it("names the refusal for git's ng line", () => {45    expect(headline([new StaleBase({ base, conflicts: [], main })])).toBe(46      "StaleBase"47    );48  });4950  it("names the first refusal and counts the rest", () => {51    expect(headline([outOfScope, touchedJudge, outOfScope])).toBe(52      "OutOfScope and 2 more"53    );54  });55});5657describe(explain, () => {58  it("gives the reason, then next, then whether retrying can help", () => {59    expect(60      explain([new StaleBase({ base, conflicts: [path], main })])61    ).toStrictEqual([62      "StaleBase: main moved to 2222222, past your base 1111111; conflicts in src/a.ts",63      "  next: git pull --rebase",64      "  retryable: yes",65    ]);66  });6768  it("names the landing that last changed each conflicting path it knows of", () => {69    const landing = LandedSha.make("3333333333333333333333333333333333333333");70    const other = RepoPath.make("src/b.ts");7172    expect(73      explain([74        new StaleBase({75          base,76          conflicts: [path, other, RepoPath.make("src/c.ts")],77          main,78          movedBy: [79            { ask, author: Handle.make("alice"), landing, path },80            { author: Handle.make("dora"), landing, path: other },81          ],82        }),83      ])[0]84    ).toBe(85      "StaleBase: main moved to 2222222, past your base 1111111; conflicts in src/a.ts (last changed by alice in 3333333, for fix-parser), src/b.ts (last changed by dora in 3333333), src/c.ts"86    );87  });8889  it("points at the line when the refusal has one", () => {90    expect(91      explain([92        new Weakened({93          at: { line: 3, path },94          weakening: { marker: "it.skip" },95        }),96      ])97    ).toStrictEqual([98      'Weakened: adds it.skip without a reason; remove it, or give a reason after " -- " for a maintainer to weigh',99      "  at: src/a.ts:3",100      `  next: ${Brand.cli} check`,101      "  retryable: no",102    ]);103  });104105  it.each(examples)("explains $_tag, opening with its tag", (refusal) => {106    expect(explain([refusal])[0]).toMatch(107      new RegExp(`^${refusal._tag}: \\S`, "u")108    );109  });110111  it.each(examples)(112    "explains $_tag with every field it names filled in",113    (refusal) => {114      expect(explain([refusal]).join("\n")).not.toMatch(115        /undefined|null|\[object Object\]/u116      );117    }118  );119120  it.each([121    {122      ends: "; put those files back, or flag the ask if the want needs them",123      refusal: outOfScope,124      starts: "OutOfScope: changes paths outside the ask's territory: src/a.ts",125    },126    {127      ends: "; the checks are fixed for this ask, so put them back, or flag the ask if a check is wrong",128      refusal: touchedJudge,129      starts:130        "TouchedJudge: changes files that decide what the checks do: src/a.ts",131    },132    {133      ends: '; remove it, or give a reason after " -- " for a maintainer to weigh',134      refusal: new Weakened({135        at: { line: 3, path },136        weakening: { marker: ".skip(" },137      }),138      starts: "Weakened: adds .skip( without a reason",139    },140    {141      ends: "; a maintainer declined it, so take the marker out",142      refusal: new Weakened({143        at: { line: 3, path },144        weakening: { marker: "oxlint-disable", reason: "generated code" },145      }),146      starts: 'Weakened: adds oxlint-disable with the reason "generated code"',147    },148    {149      ends: "; import it through that module's public exports instead",150      refusal: new ReachedInternals({151        at: { line: 1, path },152        from: path,153        to: "../../b/src/internal.ts",154      }),155      starts:156        "ReachedInternals: src/a.ts reaches into ../../b/src/internal.ts, which its module does not export",157    },158    {159      ends: "; those paths belong to a layer, so remove them from the answer",160      refusal: new PrivatePathInPublicTree({ paths: [path] }),161      starts:162        "PrivatePathInPublicTree: would put held-back paths in a public tree: src/a.ts",163    },164  ])(165    "ends $refusal._tag's reason with how to repair it",166    ({ ends, refusal, starts }) => {167      expect(explain([refusal])[0]).toBe(`${starts}${ends}`);168    }169  );170171  it("explains every refusal of one push in the gate's order, each with its line", () => {172    const a = RepoPath.make("packages/a/src/x.ts");173    const test = RepoPath.make("packages/a/test/x.test.ts");174175    const refusals = [176      new OutOfScope({ paths: [RepoPath.make("packages/b/src/y.ts")] }),177      new TouchedJudge({ paths: [RepoPath.make("oxlint.config.ts")] }),178      new Weakened({179        at: { line: 3, path: test },180        weakening: { marker: ".skip(" },181      }),182      new Weakened({183        at: { line: 9, path: test },184        weakening: { marker: ".only(" },185      }),186      new ReachedInternals({187        at: { line: 1, path: a },188        from: a,189        to: "../../b/src/internal.ts",190      }),191    ] as const;192193    expect(explain(refusals)).toStrictEqual([194      "OutOfScope: changes paths outside the ask's territory: packages/b/src/y.ts; put those files back, or flag the ask if the want needs them",195      `  next: ${Brand.cli} check`,196      "  retryable: no",197      "TouchedJudge: changes files that decide what the checks do: oxlint.config.ts; the checks are fixed for this ask, so put them back, or flag the ask if a check is wrong",198      `  next: ${Brand.cli} check`,199      "  retryable: no",200      'Weakened: adds .skip( without a reason; remove it, or give a reason after " -- " for a maintainer to weigh',201      "  at: packages/a/test/x.test.ts:3",202      `  next: ${Brand.cli} check`,203      "  retryable: no",204      'Weakened: adds .only( without a reason; remove it, or give a reason after " -- " for a maintainer to weigh',205      "  at: packages/a/test/x.test.ts:9",206      `  next: ${Brand.cli} check`,207      "  retryable: no",208      "ReachedInternals: packages/a/src/x.ts reaches into ../../b/src/internal.ts, which its module does not export; import it through that module's public exports instead",209      "  at: packages/a/src/x.ts:1",210      `  next: ${Brand.cli} check`,211      "  retryable: no",212    ]);213  });214215  it("says a maintainer declined a reasoned marker, and to take it out", () => {216    const lines = explain([217      new Weakened({218        at: { line: 3, path },219        weakening: { marker: ".skip(", reason: "flaky" },220      }),221    ]);222223    expect(lines).toStrictEqual([224      'Weakened: adds .skip( with the reason "flaky"; a maintainer declined it, so take the marker out',225      "  at: src/a.ts:3",226      `  next: ${Brand.cli} check`,227      "  retryable: no",228    ]);229    expect(lines.join("\n")).not.toContain("waits for a maintainer");230  });231232  it("leaves a raised ratchet's reason as it was", () => {233    expect(234      explain([235        new Weakened({236          weakening: {237            after: 4,238            before: 3,239            ratchet: "no-explicit-any",240            territory: "parser",241          },242        }),243      ])[0]244    ).toBe(245      "Weakened: raises the no-explicit-any ratchet in parser from 3 to 4"246    );247  });248249  it("tells a direct push to main where answers go instead", () => {250    expect(explain([new DirectPush({ ref: "refs/heads/main" })])).toStrictEqual(251      [252        "DirectPush: main moves only by a landing, so refs/heads/main takes no direct push; push your answer to your claim's fork",253        `  next: ${Brand.cli} asks`,254        "  retryable: no",255      ]256    );257  });258259  it("tells a push to a ref the platform writes that a fork takes branches only", () => {260    expect(261      explain([new DirectPush({ ref: `refs/notes/${Brand.slug}` })])262    ).toStrictEqual([263      `DirectPush: refs/notes/${Brand.slug} is written by the platform, not pushed; a fork takes branches only, so push your answer with git push ${Brand.slug} HEAD:main`,264      `  next: ${Brand.cli} asks`,265      "  retryable: no",266    ]);267  });268269  it.each([270    {271      reason: "you already hold 3 claims, the most this project allows",272      refusal: new CapReached({ cap: 3 }),273    },274    {275      reason: "you already hold 1 claim, the most this project allows",276      refusal: new CapReached({ cap: 1, limit: "claims" }),277    },278    {279      reason:280        "you made 10 claims in the last minute, the most the platform allows; try again in 60s",281      refusal: new CapReached({ cap: 10, limit: "claims", retryAfter: 60 }),282    },283    {284      reason:285        "you made 5 proposals in the last minute, the most the platform allows; try again in 60s",286      refusal: new CapReached({ cap: 5, limit: "proposals", retryAfter: 60 }),287    },288    {289      reason:290        "you made 1 proposal in the last minute, the most the platform allows; try again in 60s",291      refusal: new CapReached({ cap: 1, limit: "proposals", retryAfter: 60 }),292    },293    {294      reason:295        "you already have 2 open proposals here, the most this project allows",296      refusal: new CapReached({ cap: 2, limit: "proposals" }),297    },298    {299      reason:300        "you already have 1 open proposal here, the most this project allows",301      refusal: new CapReached({ cap: 1, limit: "proposals" }),302    },303    {304      reason:305        "you started 2 drafts in the last minute, the most the platform allows; try again in 60s",306      refusal: new CapReached({ cap: 2, limit: "drafts", retryAfter: 60 }),307    },308    {309      reason:310        "you started 1 draft in the last minute, the most the platform allows; try again in 30s",311      refusal: new CapReached({ cap: 1, limit: "drafts", retryAfter: 30 }),312    },313    {314      reason:315        "this proposal was already drafted 3 times, the most one proposal takes",316      refusal: new CapReached({ cap: 3, limit: "drafts" }),317    },318    {319      reason:320        "this proposal was already drafted once, the most one proposal takes",321      refusal: new CapReached({ cap: 1, limit: "drafts" }),322    },323  ])("reads CapReached as: $reason", ({ refusal, reason }) => {324    expect(explain([refusal])).toStrictEqual([325      `CapReached: ${reason}`,326      `  next: ${Brand.cli} status`,327      "  retryable: yes",328    ]);329  });330331  it.each([332    {333      lines: [334        "UnknownPath: 2222222 has no file or folder at src/a.ts",335        "  next: git ls-tree",336        "  retryable: no",337      ],338      refusal: new UnknownPath({ commit: main, path }),339    },340    {341      lines: [342        "UnknownRevision: v9 is neither main, a commit this project holds, nor one of its tags",343        "  next: git ls-remote",344        "  retryable: no",345      ],346      refusal: new UnknownRevision({ rev: "v9" }),347    },348    {349      lines: [350        "UnknownProposal: this project has no proposal prop-000007",351        `  next: ${Brand.cli} today`,352        "  retryable: no",353      ],354      refusal: new UnknownProposal({355        proposal: ProposalId.make("prop-000007"),356      }),357    },358    {359      lines: [360        "ProposalClosed: prop-000007 was already accepted, declined or expired",361        `  next: ${Brand.cli} today`,362        "  retryable: no",363      ],364      refusal: new ProposalClosed({ proposal: ProposalId.make("prop-000007") }),365    },366    {367      lines: [368        "DraftInProgress: the platform is already drafting prop-000007's ask",369        `  next: ${Brand.cli} proposal prop-000007`,370        "  retryable: yes",371      ],372      refusal: DraftInProgress.of(ProposalId.make("prop-000007")),373    },374  ])("explains $refusal._tag with its next command", ({ lines, refusal }) => {375    expect(explain([refusal])).toStrictEqual(lines);376  });377378  it("names the commit a missing path was looked for at", () => {379    expect(380      explain([381        new UnknownPath({382          commit: CommitSha.make("abcdef0".padEnd(40, "0")),383          path,384        }),385      ])[0]386    ).toBe("UnknownPath: abcdef0 has no file or folder at src/a.ts");387  });388389  it("says a value is too short by the minimum alone", () => {390    expect(391      explain([ValueTooShort.of(EnvKey.make("PAYMENTS_KEY"), 32)])392    ).toStrictEqual([393      "ValueTooShort: the value for PAYMENTS_KEY is shorter than the 32 characters it needs",394      `  next: ${Brand.cli} env set PAYMENTS_KEY --acknowledge-short`,395      "  retryable: no",396    ]);397  });398399  it("says a lane's log was withheld rather than where it is", () => {400    expect(401      explain([402        new ChecksFailed({403          lane: "unit",404          logRef: ChecksFailed.withheld,405          next: "bun test",406        }),407      ])408    ).toStrictEqual([409      "ChecksFailed: the unit checks failed; their log was withheld",410      "  next: bun test",411      "  retryable: no",412    ]);413  });414415  it("says the checks failed on the answer, the command that shows it, and where the log is kept", () => {416    const logRef = "datekit/push-2222222/unit#1";417418    expect(419      explain([new ChecksFailed({ lane: "unit", logRef, next: "bun test" })])420    ).toStrictEqual([421      `ChecksFailed: the unit checks failed on the answer; run bun test in a clone of the fork to see the failure; the log is kept as ${logRef}, which the answerer and the project's maintainers read with ${Brand.cli} evidence`,422      "  next: bun test",423      "  retryable: no",424    ]);425  });426427  it("says a lane the local check ran failed here, with no kept log to read", () => {428    const lines = explain([429      new ChecksFailed({430        lane: "unit",431        logRef: ChecksFailed.localRef("unit"),432        next: "bun test",433      }),434    ]);435436    expect(lines).toStrictEqual([437      "ChecksFailed: the unit checks failed on this commit; run bun test to see the failure",438      "  next: bun test",439      "  retryable: no",440    ]);441    expect(lines.join("\n")).not.toContain("evidence");442  });443444  it("tells a caller they are not a maintainer without naming a project", () => {445    const [line] = explain([new NotDecider({})]);446447    expect(line).toBe(448      "NotDecider: you are not a maintainer here: only the maintainers a project's config names may do this"449    );450    expect(line).not.toContain("this project's");451  });452453  it("says a value below the floor needs another value, not an acknowledgement", () => {454    expect(455      explain([ValueTooShort.belowFloor(EnvKey.make("PAYMENTS_KEY"), 3)])456    ).toStrictEqual([457      "ValueTooShort: the value for PAYMENTS_KEY is shorter than the 3 characters it needs",458      `  next: ${Brand.cli} env set PAYMENTS_KEY`,459      "  retryable: no",460    ]);461  });462});463464const receiptAt = (outcome: Outcome) => ({465  ask,466  base,467  head: main,468  outcome,469  project: ProjectName.make("oscabriel/datekit"),470  requestId: RequestId.make("push-2222222"),471});472473describe(receiptLine, () => {474  const follow = `follow with ${Brand.cli} status`;475476  it("says where the push's answer stands, in one line", () => {477    expect(478      [479        Outcome.cases.Checking.make({ stage: "merge" }),480        Outcome.cases.AwaitingDecision.make({481          decision: DecisionId.make("dec-000001"),482        }),483        Outcome.cases.AwaitingFinish.make({}),484        Outcome.cases.WaitingOnParent.make({ ask: AskId.make("parse-iso") }),485        Outcome.cases.Landed.make({ sha: LandedSha.make("3".repeat(40)) }),486      ].map((outcome) => receiptLine(receiptAt(outcome)))487    ).toStrictEqual([488      Result.succeed(`checking fix-parser at 2222222; ${follow}`),489      Result.succeed(490        `fix-parser at 2222222 waits on decision dec-000001; ${follow}`491      ),492      Result.succeed(493        `fix-parser at 2222222 passed and waits for a maintainer's finish; ${follow}`494      ),495      Result.succeed(496        `fix-parser at 2222222 passed and waits for parse-iso to land; ${follow}`497      ),498      Result.succeed("fix-parser at 2222222 landed as 3333333"),499    ]);500  });501502  it("gives a refused answer's refusals back, for explain", () => {503    expect(504      receiptLine(505        receiptAt(Outcome.cases.Refused.make({ refusals: [outOfScope] }))506      )507    ).toStrictEqual(Result.fail([outOfScope]));508  });509});