context.test.ts
This file
Path
packages/drafting/test/context.test.ts
180 lines · 5.7 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
Merge branch 'build/908-drafter-workers-ai'oscabrielbefore adoption004e40e
lines 1–180why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { RepoPath } from "@repo/protocol/ids";4import { Effect, Option } from "effect";56import { askTestFolderOf, pickFiles } from "../src/context.ts";78const askFile = (id: string) => `${Brand.configDir}/asks/${id}.ask.ts`;910const askSource = (id: string) =>11 `export default kinds.fix.ask({\n path: "test/asks/${id}.test.ts",\n want: "w",\n});\n`;1213const files = {14 ".github/workflows/ci.yml": "steps: []\n",15 "README.md": "# datekit\n",16 [askFile("add-months-clamp")]: askSource("add-months-clamp"),17 [askFile("format-ordinal")]: askSource("format-ordinal"),18 [`${Brand.configDir}/layers/maintainers/notes.md`]: "sealed notes\n",19 [`${Brand.configDir}/project.ts`]: "export default project({});\n",20 "checks/held-back/export/offsets.test.ts": "sealed\n",21 "docs/README.md": "# docs\n",22 "package.json": '{ "name": "datekit" }\n',23 "src/add.ts": "export const addMonths = () => 0;\n",24 "src/format.ts": "export const format = () => '';\n",25 "src/parse.ts": "export const parseISO = () => 0;\n",26 "src/tz.ts": "export const zone = () => 0;\n",27 "test/add.test.ts": "it('adds')\n",28 "test/asks/add-months-clamp.test.ts": "it('clamps')\n",29 "test/asks/format-ordinal.test.ts": "it('ordinals')\n",30 "test/format.test.ts": "it('formats')\n",31 "test/parse.test.ts": "it('parses')\n",32} as const satisfies Readonly<Record<string, string>>;3334const tree = Object.keys(files).map((path) => RepoPath.make(path));3536const read = (contents: Readonly<Record<string, string>>) => (path: RepoPath) =>37 Effect.succeed(contents[path] ?? "");3839const pathsOf = (picked: readonly { readonly path: string }[]) =>40 picked.map(({ path }) => path);4142describe("the files a draft reads", () => {43 it.effect("picks the same files for the same tree and want, twice", () =>44 Effect.gen(function* () {45 const want = { want: "addMonths clamps to the end of a short month" };46 const first = yield* pickFiles(tree, want, read(files));4748 const second = yield* pickFiles(49 [...tree].toReversed(),50 want,51 read(files)52 );5354 expect(first).toStrictEqual(second);55 expect(pathsOf(first)).toStrictEqual([56 askFile("format-ordinal"),57 "test/asks/format-ordinal.test.ts",58 "README.md",59 "package.json",60 `${Brand.configDir}/project.ts`,61 "src/add.ts",62 "test/add.test.ts",63 "docs/README.md",64 ]);65 })66 );6768 it.effect("reads the territory's files when the want names one", () =>69 Effect.gen(function* () {70 const picked = yield* pickFiles(71 tree,72 { territory: [RepoPath.make("src/parse.ts")], want: "Week dates" },73 read(files)74 );7576 expect(pathsOf(picked)).toContain("src/parse.ts");77 expect(pathsOf(picked)).toContain("test/parse.test.ts");78 expect(pathsOf(picked)).not.toContain("src/add.ts");79 })80 );8182 it.effect("never picks a path under a layer or a held-back folder", () =>83 Effect.gen(function* () {84 const picked = yield* pickFiles(85 tree,86 {87 territory: [88 RepoPath.make("checks"),89 RepoPath.make(`${Brand.configDir}/layers`),90 ],91 want: "held back offsets notes maintainers export",92 },93 read(files)94 );9596 expect(97 pathsOf(picked).filter(98 (path) =>99 path.includes("held-back/") ||100 path.startsWith(`${Brand.configDir}/layers/`)101 )102 ).toStrictEqual([]);103 })104 );105106 it.effect(107 "cuts a 300 KB file at 40 KB with its marker, within the budget",108 () =>109 Effect.gen(function* () {110 const big = "x".repeat(300 * 1024);111112 const many = Object.fromEntries(113 Array.from({ length: 12 }, (_, index) => [114 `src/add-${index}.ts`,115 "y".repeat(30 * 1024),116 ])117 );118119 const contents = { ...files, ...many, "src/add-big.ts": big };120121 const picked = yield* pickFiles(122 Object.keys(contents).map((path) => RepoPath.make(path)),123 { want: "the big add" },124 read(contents)125 );126127 const bytes = picked.reduce(128 (sum, { content }) => sum + new TextEncoder().encode(content).length,129 0130 );131132 expect(bytes).toBeLessThanOrEqual(200 * 1024);133134 const cut = picked.find(({ path }) => path === "src/add-big.ts");135136 expect(cut?.content.startsWith("x".repeat(40 * 1024))).toBeTruthy();137 expect(cut?.content.slice(40 * 1024)).toBe(138 "\n[cut: the first 40960 of 307200 bytes]\n"139 );140 })141 );142143 it.effect(144 "always shows an existing ask and its test as the style example",145 () =>146 Effect.gen(function* () {147 const picked = yield* pickFiles(148 tree,149 { want: "time zones" },150 read(files),151 1024152 );153154 expect(pathsOf(picked)).toContain(askFile("format-ordinal"));155 expect(pathsOf(picked)).toContain("test/asks/format-ordinal.test.ts");156 expect(askTestFolderOf(picked)).toStrictEqual(Option.some("test/asks"));157 })158 );159160 it.effect(161 "keeps the style example when a long README would spend the budget first",162 () =>163 Effect.gen(function* () {164 const contents = { ...files, "README.md": "r".repeat(300 * 1024) };165166 const picked = yield* pickFiles(167 tree,168 { want: "time zones" },169 read(contents),170 8 * 1024171 );172173 expect(pathsOf(picked).slice(0, 2)).toStrictEqual([174 askFile("format-ordinal"),175 "test/asks/format-ordinal.test.ts",176 ]);177 expect(askTestFolderOf(picked)).toStrictEqual(Option.some("test/asks"));178 })179 );180});