Skip to content
Myr

shape.test.ts

This file

Path

packages/gate/test/shape.test.ts

529 lines · 15.9 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 { Glob } from "@repo/protocol/domain/common";3import { Territory } from "@repo/protocol/domain/territory";4import { RepoPath } from "@repo/protocol/ids";5import {6  groups,7  OutOfScope,8  PrivatePathInPublicTree,9  ReachedInternals,10  TouchedJudge,11  Weakened,12} from "@repo/protocol/refusal";13import type { Refusal } from "@repo/protocol/refusal";14import { Array, Effect, Option, Schema } from "effect";1516import type { PushFacts } from "../src/facts.ts";17import { scan } from "../src/stages/shape.ts";18import {19  addedLines,20  ask,21  checkPath,22  configDir,23  modified,24  policy,25  testFacts,26  wire,27} from "./facts.ts";2829const paths = (...each: readonly [string, ...string[]]) =>30  Array.map(each, (path) => RepoPath.make(path));3132const at = (path: string, line: number) => ({33  line,34  path: RepoPath.make(path),35});3637const week = "packages/parse/src/week.ts";3839const weekTest = "packages/parse/test/week.test.ts";4041const layerHolds = (...each: readonly string[]): ReadonlySet<RepoPath> =>42  new Set(each.map((path) => RepoPath.make(path)));4344const push = (45  files: readonly string[],46  lines: readonly (readonly [string, number, string])[] = [],47  overrides: Partial<PushFacts> = {}48) =>49  testFacts({50    addedLines: addedLines(...lines),51    changes: modified(...files),52    ...overrides,53  });5455const everyProblem = push(56  [week, "docs/guide.md", ".github/workflows/ci.yml"],57  [58    [week, 1, 'export { pad } from "../../format/src/pad";'],59    [week, 4, "// @ts-ignore"],60    [week, 9, "it.only("],61  ],62  { layerPaths: new Set([RepoPath.make("docs/guide.md")]) }63);6465// The language service reads its directive anywhere in a source file, so66// the marker is joined at run time.67const effectDiagnostics = ["@effect", "-diagnostics"].join("");6869const [moduleA, moduleB] = Schema.decodeSync(70  Schema.Tuple([Territory, Territory])71)([72  {73    module: "a",74    paths: ["packages/a/**"],75    publicFiles: ["packages/a/src/index.ts"],76  },77  {78    module: "b",79    paths: ["packages/b/**"],80    publicFiles: ["packages/b/src/index.ts"],81  },82]);8384const x = "packages/a/src/x.ts";8586const xTest = "packages/a/test/x.test.ts";8788const pushToA = (89  files: readonly string[],90  lines: readonly (readonly [string, number, string])[] = []91) =>92  push(files, lines, {93    ask: Option.some({ ...ask, territory: moduleA }),94    policy: {95      ...policy,96      judge: {97        ...policy.judge,98        paths: [...policy.judge.paths, Glob.make("oxlint.config.*")],99      },100      modules: [moduleA, moduleB],101    },102  });103104const markerLines: readonly (readonly [string, string])[] = [105  [".skip(", '  it.skip("parses week 53", () => {'],106  [".only(", '  describe.only("weeks", () => {'],107  [".todo(", '  it.todo("parses week 54");'],108  ["eslint-disable", "/* eslint-disable no-console */"],109  ["oxlint-disable", "// oxlint-disable-next-line no-explicit-any"],110  ["biome-ignore", "// biome-ignore lint/style: generated"],111  ["@ts-ignore", "// @ts-ignore"],112  ["@ts-expect-error", "// @ts-expect-error"],113  ["@ts-nocheck", "// @ts-nocheck"],114  [effectDiagnostics, `// ${effectDiagnostics} floatingEffect:off`],115  ["# noqa", "import os  # noqa"],116  ["# type: ignore", "x = parse(y)  # type: ignore"],117  ["#[allow(", "#[allow(dead_code)]"],118  ["//nolint", "func parse() { //nolint"],119  ["istanbul ignore", "/* istanbul ignore next */"],120  ["c8 ignore", "/* c8 ignore next */"],121];122123const pad = "../../format/src/pad.ts";124125const reachedFromWeek = (line: number, to: string) =>126  new ReachedInternals({ at: at(week, line), from: RepoPath.make(week), to });127128const rows: readonly {129  readonly name: string;130  readonly facts: PushFacts;131  readonly refusals: readonly Refusal[];132}[] = [133  { facts: push([week]), name: "a change inside the territory", refusals: [] },134  {135    facts: push([week, "packages/format/src/pad.ts", "docs/guide.md"]),136    name: "changes outside the territory, every one named",137    refusals: [138      new OutOfScope({139        paths: paths("docs/guide.md", "packages/format/src/pad.ts"),140      }),141    ],142  },143  {144    facts: push([".github/workflows/ci.yml"]),145    name: "a change to a CI workflow",146    refusals: [new TouchedJudge({ paths: paths(".github/workflows/ci.yml") })],147  },148  {149    facts: push([`${configDir}/project.ts`]),150    name: "a change to the config folder",151    refusals: [new TouchedJudge({ paths: paths(`${configDir}/project.ts`) })],152  },153  {154    facts: push(["vitest.config.ts", "packages/parse/vitest.config.ts"]),155    name: "a change to a test runner's config, at the root or in the territory",156    refusals: [157      new TouchedJudge({158        paths: paths("packages/parse/vitest.config.ts", "vitest.config.ts"),159      }),160    ],161  },162  {163    facts: push([".env.production", "apps/web/.env.production.local"]),164    name: "a committed env file is judged, and a local one is only out of scope",165    refusals: [166      new OutOfScope({ paths: paths("apps/web/.env.production.local") }),167      new TouchedJudge({ paths: paths(".env.production") }),168    ],169  },170  {171    facts: push(["packages/format/package.json"]),172    name: "the territory's root manifest is no other package's manifest",173    refusals: [174      new OutOfScope({ paths: paths("packages/format/package.json") }),175    ],176  },177  {178    facts: push(["package.json"]),179    name: "a change to package.json's dependencies only",180    refusals: [],181  },182  {183    facts: push(["package.json"], [], { scriptsChanged: true }),184    name: "a change to package.json's scripts",185    refusals: [new TouchedJudge({ paths: paths("package.json#/scripts") })],186  },187  {188    facts: push([checkPath]),189    name: "an answer's change to its own ask's check, inside the territory",190    refusals: [new TouchedJudge({ paths: paths(checkPath) })],191  },192  {193    facts: push([ask.file, checkPath], [], { opensAsk: true }),194    name: "a maintainer opening the ask writes its file and its check",195    refusals: [],196  },197  {198    facts: push([ask.file, `${configDir}/project.ts`], [], { opensAsk: true }),199    name: "opening an ask still may not touch the rest of the judge",200    refusals: [new TouchedJudge({ paths: paths(`${configDir}/project.ts`) })],201  },202  {203    facts: push(204      [weekTest],205      [[weekTest, 12, '  it.skip("parses week 53", () => {']]206    ),207    name: "an added .skip( with no reason",208    refusals: [209      new Weakened({ at: at(weekTest, 12), weakening: { marker: ".skip(" } }),210    ],211  },212  {213    facts: push(214      [week],215      [[week, 7, "// oxlint-disable-next-line no-x -- reason given"]]216    ),217    name: "an added escape with a reason (counted by escapeDecisions)",218    refusals: [],219  },220  {221    facts: push([weekTest], [[weekTest, 2, 'vi.mock("../src/week.ts");']], {222      policy: { ...policy, weakening: { markers: ["vi.mock("] } },223    }),224    name: "a marker the project adds to the defaults",225    refusals: [226      new Weakened({ at: at(weekTest, 2), weakening: { marker: "vi.mock(" } }),227    ],228  },229  {230    facts: push([week], [], {231      layerPaths: new Set([RepoPath.make(week)]),232    }),233    name: "a path a layer holds",234    refusals: [new PrivatePathInPublicTree({ paths: paths(week) })],235  },236  {237    facts: push([week, weekTest], [], { layerPaths: layerHolds(weekTest) }),238    name: "a path a layer holds, pushed by someone who reads no layer: every path they wrote, not which collide",239    refusals: [new PrivatePathInPublicTree({ paths: paths(week, weekTest) })],240  },241  {242    facts: push([week, weekTest], [], {243      canReadLayers: false,244      layerPaths: layerHolds(weekTest),245    }),246    name: "a path a layer holds, pushed by someone told they read no layer: every path they wrote",247    refusals: [new PrivatePathInPublicTree({ paths: paths(week, weekTest) })],248  },249  {250    facts: push([week, weekTest], [], {251      canReadLayers: true,252      layerPaths: layerHolds(weekTest),253    }),254    name: "a path a layer holds, pushed by someone who reads the layers: only the path that collides",255    refusals: [new PrivatePathInPublicTree({ paths: paths(weekTest) })],256  },257  {258    facts: push([week, weekTest], [], {259      layerPaths: layerHolds("packages/parse/test"),260    }),261    name: "a path under a folder a layer holds, pushed by someone who reads no layer: every path they wrote",262    refusals: [new PrivatePathInPublicTree({ paths: paths(week, weekTest) })],263  },264  {265    facts: push([week, weekTest], [], {266      canReadLayers: true,267      layerPaths: layerHolds("packages/parse/test"),268    }),269    name: "a path under a folder a layer holds, pushed by someone who reads the layers: only the path under it",270    refusals: [new PrivatePathInPublicTree({ paths: paths(weekTest) })],271  },272  {273    facts: push([week, weekTest], [], {274      layerPaths: layerHolds("packages/parse/tes", "packages/parse/test/week"),275    }),276    name: "a layer path that only begins a path's name holds no path",277    refusals: [],278  },279  {280    facts: push([week], [], { layerPaths: layerHolds(weekTest) }),281    name: "a push by someone who reads no layer, with nothing in a layer, is not refused",282    refusals: [],283  },284  {285    facts: testFacts({286      changes: [287        { path: RepoPath.make(week), status: "added" },288        { path: RepoPath.make(weekTest), status: "deleted" },289      ],290      layerPaths: layerHolds(weekTest),291    }),292    name: "deleting a path a layer holds writes nothing into the public tree",293    refusals: [],294  },295  {296    facts: testFacts({297      changes: [298        { path: RepoPath.make(week), status: "added" },299        { path: RepoPath.make(weekTest), status: "modified" },300        { path: RepoPath.make("packages/parse/src/old.ts"), status: "deleted" },301      ],302      layerPaths: layerHolds(weekTest),303    }),304    name: "to someone who reads no layer, every path added or modified, never one deleted",305    refusals: [new PrivatePathInPublicTree({ paths: paths(week, weekTest) })],306  },307  {308    facts: push(309      [week],310      [[week, 1, 'import { pad } from "../../format/src/pad.ts";']]311    ),312    name: "an import of another module's internal file",313    refusals: [314      new ReachedInternals({315        at: at(week, 1),316        from: RepoPath.make(week),317        to: "../../format/src/pad.ts",318      }),319    ],320  },321  {322    facts: push(323      [week],324      [325        [week, 1, "import {"],326        [week, 2, "  pad,"],327        [week, 3, '  trim } from "../../format/src/pad.ts";'],328      ]329    ),330    name: "an import of another module's internal file, over several lines",331    refusals: [332      new ReachedInternals({333        at: at(week, 3),334        from: RepoPath.make(week),335        to: "../../format/src/pad.ts",336      }),337    ],338  },339  {340    facts: push(341      [week],342      [[week, 1, 'export * from "../../format/src/pad.ts";']]343    ),344    name: "a re-export of another module's internal file",345    refusals: [346      new ReachedInternals({347        at: at(week, 1),348        from: RepoPath.make(week),349        to: "../../format/src/pad.ts",350      }),351    ],352  },353  {354    facts: push(355      [week],356      [357        [week, 1, 'import { pad } from "../../format/src/index.ts";'],358        [week, 2, 'import { lex } from "./lex.ts";'],359        [week, 3, 'import { Array, Effect, Option, Schema } from "effect";'],360      ]361    ),362    name: "imports of another module's public file, its own files and a package",363    refusals: [],364  },365  {366    facts: pushToA(367      [x],368      [[x, 12, `// ${effectDiagnostics} floatingEffect:off`]]369    ),370    name: "an added Effect diagnostics directive with no reason",371    refusals: [372      new Weakened({373        at: at(x, 12),374        weakening: { marker: effectDiagnostics },375      }),376    ],377  },378  {379    facts: pushToA(380      [x],381      [382        [383          x,384          12,385          `// ${effectDiagnostics} floatingEffect:off -- the Worker's fetch is the one HTTP client`,386        ],387      ]388    ),389    name: "an added Effect diagnostics directive with a reason",390    refusals: [],391  },392  {393    facts: pushToA(394      ["packages/b/src/y.ts", xTest, "oxlint.config.ts", x],395      [396        [xTest, 9, '  it.only("reads y", () => {'],397        [x, 1, 'import { y } from "../../b/src/internal.ts";'],398        [xTest, 3, '  it.skip("reads x", () => {'],399      ]400    ),401    name: "a push out of scope, touching the judge, weakening twice and reaching into internals, all at once",402    refusals: [403      new OutOfScope({ paths: paths("packages/b/src/y.ts") }),404      new TouchedJudge({ paths: paths("oxlint.config.ts") }),405      new Weakened({ at: at(xTest, 3), weakening: { marker: ".skip(" } }),406      new Weakened({ at: at(xTest, 9), weakening: { marker: ".only(" } }),407      new ReachedInternals({408        at: at(x, 1),409        from: RepoPath.make(x),410        to: "../../b/src/internal.ts",411      }),412    ],413  },414  ...markerLines.map(([marker, text]) => ({415    facts: push([week], [[week, 5, text]]),416    name: `the default marker ${marker}`,417    refusals: [new Weakened({ at: at(week, 5), weakening: { marker } })],418  })),419  {420    facts: push(421      ["packages/parse/README.md"],422      [["packages/parse/README.md", 40, 'Focus one test with `it.only("…")`.']]423    ),424    name: "a marker inside a string in a README",425    refusals: [426      new Weakened({427        at: at("packages/parse/README.md", 40),428        weakening: { marker: ".only(" },429      }),430    ],431  },432  {433    facts: push(434      [week],435      [436        [week, 2, `const pad = await import("${pad}");`],437        [week, 6, `const { trim } = require("${pad}");`],438      ]439    ),440    name: "a dynamic import and a require of another module's internal file",441    refusals: [reachedFromWeek(2, pad), reachedFromWeek(6, pad)],442  },443  {444    facts: push([week], [[week, 4, `const pad = await import(\`${pad}\`);`]]),445    name: "a dynamic import of another module's internal file, its specifier a template",446    refusals: [reachedFromWeek(4, pad)],447  },448  {449    facts: push(450      [week],451      [452        [453          week,454          1,455          `import a from "../../format/src/a.ts"; import b from "${pad}";`,456        ],457      ]458    ),459    name: "two imports of internal files on one line",460    refusals: [461      reachedFromWeek(1, "../../format/src/a.ts"),462      reachedFromWeek(1, pad),463    ],464  },465  {466    facts: push(467      [week],468      [[week, 1, 'import { pad } from "@datekit/format/src/pad";']]469    ),470    name: "a package subpath into another module's src folder",471    refusals: [reachedFromWeek(1, "@datekit/format/src/pad")],472  },473  {474    facts: push(475      [week, "packages/parse/README.md"],476      [477        [week, 1, `// import { pad } from "${pad}";`],478        [week, 2, 'import { pad } from "../../format";'],479        [week, 3, 'import { trim } from "@datekit/format";'],480        ["packages/parse/README.md", 1, `import { pad } from "${pad}";`],481      ]482    ),483    name: "an import in a comment, of a module's root or package, or in prose",484    refusals: [],485  },486  {487    facts: everyProblem,488    name: "every shape problem at once",489    refusals: [490      new OutOfScope({ paths: paths("docs/guide.md") }),491      new TouchedJudge({ paths: paths(".github/workflows/ci.yml") }),492      new Weakened({ at: at(week, 4), weakening: { marker: "@ts-ignore" } }),493      new Weakened({ at: at(week, 9), weakening: { marker: ".only(" } }),494      // bob reads no layer, so he hears every path he wrote.495      new PrivatePathInPublicTree({496        paths: paths(".github/workflows/ci.yml", "docs/guide.md", week),497      }),498      new ReachedInternals({499        at: at(week, 1),500        from: RepoPath.make(week),501        to: "../../format/src/pad",502      }),503    ],504  },505];506507describe("shape: every problem a push's changes have", () => {508  it.effect.each(rows)("$name", ({ facts, refusals }) =>509    Effect.gen(function* () {510      const found = yield* scan.run(facts);511512      expect(wire(found)).toStrictEqual(wire(refusals));513    })514  );515516  it.effect("reports in the order of the union's Shape group", () =>517    Effect.gen(function* () {518      const found = yield* scan.run(everyProblem);519520      const order = groups521        .find((group) => group.name === "Shape")522        ?.members.map((member) => member.identifier);523524      expect([...new Set(found.map((refusal) => refusal._tag))]).toStrictEqual(525        order526      );527    })528  );529});