Skip to content
Myr

facts.test.ts

This file

Path

apps/cli/test/local/facts.test.ts

396 lines · 12.3 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–16why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { Fetch, FetchFailed } from "@repo/artifacts/fetch";4import { RepoHost } from "@repo/artifacts/repo-host";5import { RepoHostLocal } from "@repo/artifacts/repo-host/local";6import { Brand } from "@repo/brand";7import { isJudged, judgeClosure } from "@repo/checks/judge";8import { PushTarget } from "@repo/gate/facts";9import { addedLines, treeDiff } from "@repo/git/tree-diff";10import {11 AskId,12 CommitSha,13 Handle,14 LandedSha,15 ProjectName,16 RepoName,

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 17why, one step back
17 keyOf,

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 18–63why, one step back
18 TreeSha,
19} from "@repo/protocol/ids";20import { Person } from "@repo/protocol/principal";21import { defaultPolicyInput } from "@repo/rules/defaults";22import { gitOk } from "@repo/testkit/git";23import { Effect, FileSystem, Layer, Option, Path } from "effect";2425import { localFacts } from "../../src/local/facts.ts";26import { ConfigBundlerBun, localPolicy } from "../../src/local/local-policy.ts";27import {28 aCheckRepo,29 ask,30 commit,31 handle,32 policyFile,33 policyText,34 project,35} from "./fixture.ts";36import type { CheckRepo } from "./fixture.ts";3738const about = {39 initializer: Handle.make(handle),40 project: ProjectName.make(project),41};4243const policyAt = (root: string) =>44 localPolicy(root, "main", about).pipe(Effect.provide(ConfigBundlerBun));4546const pusher = Person.make({ handle: Handle.make(handle) });4748const target = PushTarget.ClaimFork({49 ask: AskId.make(ask),50 claimant: Handle.make(handle),51 project: ProjectName.make(project),52});5354const offline = Layer.succeed(Fetch)({55 fetch: (request) =>56 Effect.fail(new FetchFailed({ reason: "offline", url: request.url })),57});5859const gitSays = (repo: CheckRepo, head: string) =>60 Effect.gen(function* () {61 const fs = yield* FileSystem.FileSystem;62 const path = yield* Path.Path;63 const hosted = yield* fs.makeTempDirectoryScoped();

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 64why, one step back
64 const name = RepoName.make(keyOf(ProjectName.make(project)));

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 65–396why, one step back
65
66 yield* gitOk(67 repo.root,68 "clone",69 "--bare",70 "--quiet",71 repo.root,72 path.join(hosted, `${name}.git`)73 );7475 const base = yield* gitOk(repo.root, "merge-base", "main", head);7677 const treeOf = (sha: string) =>78 RepoHost.use((repos) => repos.readCommit(name, CommitSha.make(sha))).pipe(79 Effect.flatMap(Effect.fromOption),80 Effect.map(({ tree }) => TreeSha.make(tree))81 );8283 return yield* Effect.gen(function* () {84 const changes = yield* treeDiff(85 name,86 yield* treeOf(base),87 yield* treeOf(head)88 );8990 return { addedLines: yield* addedLines(name, changes), changes };91 }).pipe(92 Effect.provide(93 RepoHostLocal.layer({ remoteBase: "http://unused", root: hosted }).pipe(94 Layer.provide(offline)95 )96 )97 );98 });99100describe(localFacts, () => {101 it.live(102 "gives the change list and added lines @repo/git gives from the merge base of main and HEAD",103 () =>104 Effect.gen(function* () {105 const repo = yield* aCheckRepo;106 const policy = yield* policyAt(repo.root);107108 const facts = yield* localFacts({109 ask: Option.none(),110 base: "main",111 claim: Option.none(),112 claimsHeld: 0,113 head: "HEAD",114 policy,115 pusher,116 root: repo.root,117 target,118 });119120 const expected = yield* gitSays(repo, repo.straying);121122 expect(facts.changes).toStrictEqual(expected.changes);123 expect(facts.addedLines).toStrictEqual(expected.addedLines);124 expect(facts.base).toBe(repo.main);125 expect(facts.head).toBe(repo.straying);126 expect(facts.landed).toBe(LandedSha.make(repo.main));127 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))128 );129130 it.live(131 "reads a repo whose objects live in another, through a bare copy",132 () =>133 Effect.gen(function* () {134 const fs = yield* FileSystem.FileSystem;135 const repo = yield* aCheckRepo;136 const policy = yield* policyAt(repo.root);137 const shared = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());138139 // `--shared` keeps the objects in the original: isomorphic-git, under140 // `/local`, reads no alternates.141 yield* gitOk(shared, "clone", "--shared", "--quiet", repo.root, ".");142 yield* gitOk(shared, "checkout", "--quiet", repo.straying);143144 const facts = yield* localFacts({145 ask: Option.none(),146 base: "origin/main",147 claim: Option.none(),148 claimsHeld: 0,149 head: "HEAD",150 policy,151 pusher,152 root: shared,153 target,154 });155156 const expected = yield* gitSays(repo, repo.straying);157158 expect(facts.changes).toStrictEqual(expected.changes);159 expect(facts.addedLines).toStrictEqual(expected.addedLines);160 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))161 );162});163164const wideTree = {165 ".claude/settings.json": JSON.stringify({166 hooks: {167 PreToolUse: [168 {169 hooks: [170 { command: "bun scripts/hooks/shell-policy.ts", type: "command" },171 ],172 },173 ],174 },175 }),176 "lefthook.yml":177 "pre-commit:\n commands:\n guard:\n run: sh scripts/pre-commit.sh\n",178 "oxlint.config.ts":179 'export default { jsPlugins: ["./tools/lint/plugin.ts"] };\n',180 "package.json": JSON.stringify({181 name: "kit",182 scripts: { test: "vitest run" },183 workspaces: ["packages/*"],184 }),185 "packages/a/build.json": JSON.stringify({ compilerOptions: {} }),186 "packages/a/package.json": JSON.stringify({ name: "@kit/a" }),187 "packages/a/src/index.ts": "export const a = 1;\n",188 "packages/a/tsconfig.json": JSON.stringify({189 extends: ["../../tsconfig.base.json", "./build.json"],190 }),191 "scripts/hooks/guard.ts":192 'import { policy } from "./lib/policy.ts";\n\npolicy();\n',193 "scripts/hooks/lib/policy.ts": "export const policy = () => 0;\n",194 "scripts/hooks/shell-policy.ts": "export {};\n",195 "scripts/pre-commit.sh": "#!/bin/sh\nbun scripts/hooks/guard.ts\n",196 "src/app.ts": 'import { a } from "@kit/a";\n\nexport const app = a;\n',197 "test/plugin-cases.test.ts":198 'import plugin from "../tools/lint/plugin.ts";\n\nplugin;\n',199 "tools/lint/helper.ts": "export const rule = {};\n",200 "tools/lint/plugin.ts":201 'import { rule } from "./helper.ts";\n\nexport default { rules: { rule } };\n',202 "tsconfig.base.json": JSON.stringify({ compilerOptions: {} }),203} as const satisfies Readonly<Record<string, string>>;204205describe(localPolicy, () => {206 it.live(207 "judges by the closure every file at main gives: hook scripts, the tsconfig chain, plugin imports and rule fixture tests",208 () =>209 Effect.gen(function* () {210 const fs = yield* FileSystem.FileSystem;211 const root = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());212213 yield* gitOk(root, "init", "--quiet", "--initial-branch", "main");214 yield* commit(root, wideTree, "Start");215216 const policy = yield* policyAt(root);217218 const every = judgeClosure(219 policy.run,220 Object.entries(wideTree).map(([path, content]) => ({ content, path }))221 );222223 expect(policy.judge).toStrictEqual({224 paths: every.paths,225 pointers: every.pointers,226 });227 expect(228 [229 "scripts/pre-commit.sh",230 "scripts/hooks/guard.ts",231 "scripts/hooks/lib/policy.ts",232 "scripts/hooks/shell-policy.ts",233 "packages/a/build.json",234 "tools/lint/helper.ts",235 "test/plugin-cases.test.ts",236 ].filter((path) => !isJudged(policy.judge, path))237 ).toStrictEqual([]);238 expect(policy.modules).toStrictEqual([239 { module: "@kit/a", paths: ["packages/a/**"] },240 ]);241 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))242 );243244 it.live("reads the config folder's policy.json at main, not at HEAD", () =>245 Effect.gen(function* () {246 const repo = yield* aCheckRepo;247248 yield* commit(249 repo.root,250 {251 [policyFile]: (yield* policyText).replace(252 '"bad": "false"',253 '"worse": "false"'254 ),255 },256 "Drop the failing lane"257 );258259 const policy = yield* policyAt(repo.root);260261 expect(policy.at).toBe(repo.main);262 expect(policy.source).toBe("config");263 expect(policy.run).toStrictEqual({264 checks: { bad: "false", ok: "true" },265 test: "true {path}",266 });267 expect(268 policy.asks.map(({ id, project: of, territory }) => ({269 id,270 of,271 territory,272 }))273 ).toStrictEqual([274 {275 id: ask,276 of: project,277 territory: {278 module: "a",279 paths: ["packages/a/**"],280 publicFiles: ["packages/a/src/a.ts"],281 },282 },283 ]);284 expect(policy.modules).toStrictEqual([285 {286 module: "a",287 paths: ["packages/a/**"],288 publicFiles: ["packages/a/src/a.ts"],289 },290 { module: "b", paths: ["packages/b/**"] },291 ]);292 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))293 );294295 it.live("gives the defaults when main has no config folder", () =>296 Effect.gen(function* () {297 const fs = yield* FileSystem.FileSystem;298 const root = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());299300 yield* gitOk(root, "init", "--quiet", "--initial-branch", "main");301302 const main = yield* commit(303 root,304 {305 "package.json": JSON.stringify({306 name: "plain",307 scripts: { test: "vitest run" },308 }),309 },310 "Start"311 );312313 const policy = yield* policyAt(root);314315 expect(policy.at).toBe(main);316 expect(policy.source).toBe("defaults");317 expect(policy.asks).toStrictEqual([]);318 expect({ caps: policy.caps, kinds: policy.kinds }).toStrictEqual({319 caps: defaultPolicyInput.caps,320 kinds: defaultPolicyInput.kinds,321 });322 expect(policy.modules).toStrictEqual([{ module: ".", paths: ["**"] }]);323 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))324 );325326 it.live(327 "holds the config folder as the Lander bundles it: regular files, and no code it leaves out",328 () =>329 Effect.gen(function* () {330 const fs = yield* FileSystem.FileSystem;331 const path = yield* Path.Path;332 const root = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());333334 yield* gitOk(root, "init", "--quiet", "--initial-branch", "main");335 yield* fs.makeDirectory(path.join(root, Brand.configDir));336337 // A policy.json that is a link, and a helper no entry imports: the338 // Lander's bundle holds neither, so the project runs on the defaults.339 yield* fs.symlink("../policy.json", path.join(root, policyFile));340341 yield* commit(342 root,343 {344 [`${Brand.configDir}/helper.tsx`]: "export const helper = 1;\n",345 "package.json": JSON.stringify({346 name: "plain",347 scripts: { test: "vitest run" },348 }),349 "policy.json": "{}\n",350 },351 "Start"352 );353354 const source = yield* policyAt(root).pipe(355 Effect.map((policy) => policy.source),356 Effect.catchTag("RulesFailed", ({ detail }) => Effect.succeed(detail))357 );358359 expect(source).toBe("defaults");360 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))361 );362363 it.live(364 "evaluates a TypeScript config folder at main, bundled with this CLI's own sdk",365 () =>366 Effect.gen(function* () {367 const repo = yield* aCheckRepo;368369 yield* gitOk(repo.root, "checkout", "--quiet", "main");370371 const main = yield* commit(372 repo.root,373 {374 [`${Brand.configDir}/project.ts`]: [375 `import { project } from "${Brand.cliPackage}/rules";`,376 "",377 "export default project({",378 ' run: { checks: { unit: "true" }, test: "true {path}" },',379 "});",380 "",381 ].join("\n"),382 },383 "Write the project in TypeScript"384 );385386 const policy = yield* policyAt(repo.root);387388 expect(policy.at).toBe(main);389 expect(policy.run).toStrictEqual({390 checks: { unit: "true" },391 test: "true {path}",392 });393 expect(policy.asks).toStrictEqual([]);394 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))395 );396});