gateway.test.ts
This file
Path
packages/git/test/gateway.test.ts
389 lines · 11.9 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake 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–338why, one step back1import { NodeServices } from "@effect/platform-node";2import { describe, expect, it } from "@effect/vitest";3import { CommitSha, RepoPath } from "@repo/protocol/ids";4import { OutOfScope, StaleBase, TouchedJudge } from "@repo/protocol/refusal";5import type { Refusal } from "@repo/protocol/refusal";6import { git, gitOk } from "@repo/testkit/git";7import { Effect, Option, Ref } from "effect";89import { Custody } from "../src/custody.ts";10import { gitRoute } from "../src/gateway.ts";11import { admitAll, commit, world } from "./world.ts";1213const refusing = (ref: string, refusals: readonly Refusal[]) =>14 Custody.of({15 admit: (_repo, commands) =>16 Effect.succeed(17 commands.some((command) => command.ref === ref) ? refusals : []18 ),19 review: () => Effect.succeed([]),20 });2122const artifactsBusy = () =>23 new Response("0024\u0003artifacts_git_receive_pack_busy", {24 headers: { "content-type": "application/x-git-receive-pack-result" },25 });2627const remoteLines = (stderr: string) =>28 stderr29 .split("\n")30 .filter((line) => line.startsWith("remote: "))31 .map((line) => line.trimEnd());3233// A credential the client sends; the gateway must keep it from the upstream.34const asClient = ["-c", "http.extraHeader=Authorization: Bearer client-secret"];3536const noise = (length: number) => {37 const digits: string[] = [];38 let state = 20_261_006;3940 while (digits.length < length) {41 state = (state * 16_807) % 2_147_483_647;42 digits.push((state % 16).toString(16));43 }4445 return digits.join("");46};4748describe("the gateway between git and its upstream", () => {49 it.live(50 "clones and fetches, keeping the client's credentials to itself",51 () =>52 Effect.gen(function* () {53 const { root, seed, upstream, url } = yield* world(admitAll);54 const clone = `${root}/clone`;5556 yield* gitOk(root, ...asClient, "clone", "--quiet", url, clone);5758 expect(yield* gitOk(clone, "rev-parse", "HEAD")).toBe(59 yield* gitOk(seed, "rev-parse", "HEAD")60 );6162 const second = yield* commit(seed, "second", { "b.txt": "b\n" });6364 yield* gitOk(seed, "push", "--quiet", upstream, "HEAD:refs/heads/main");65 yield* gitOk(clone, ...asClient, "fetch", "--quiet");6667 expect(yield* gitOk(clone, "rev-parse", "origin/main")).toBe(second);68 }).pipe(Effect.provide(NodeServices.layer)),69 20_00070 );7172 it.live(73 "forwards a push custody admits, and the upstream takes it",74 () =>75 Effect.gen(function* () {76 const { root, upstream, url } = yield* world(admitAll);77 const clone = `${root}/clone`;7879 yield* gitOk(root, "clone", "--quiet", url, clone);8081 const answer = yield* commit(clone, "answer", { "b.txt": "b\n" });8283 const pushed = yield* git(84 clone,85 "push",86 "origin",87 "HEAD:refs/heads/answer"88 );8990 expect(pushed.exitCode).toBe(0);91 expect(yield* gitOk(upstream, "rev-parse", "refs/heads/answer")).toBe(92 answer93 );94 }).pipe(Effect.provide(NodeServices.layer)),95 20_00096 );9798 it.live(99 "forwards a push larger than git's post buffer, sent as a probe and then in chunks",100 () =>101 Effect.gen(function* () {102 const { root, upstream, url } = yield* world(admitAll);103 const clone = `${root}/clone`;104105 yield* gitOk(root, "clone", "--quiet", url, clone);106107 const answer = yield* commit(clone, "large", {108 "large.bin": noise(300_000),109 });110111 const pushed = yield* git(112 clone,113 // git's smallest post buffer; a larger request is probed with an114 // empty one first, then streamed chunked.115 "-c",116 "http.postBuffer=65520",117 "push",118 "origin",119 "HEAD:refs/heads/large"120 );121122 expect(pushed.exitCode).toBe(0);123 expect(yield* gitOk(upstream, "rev-parse", "refs/heads/large")).toBe(124 answer125 );126 }).pipe(Effect.provide(NodeServices.layer)),127 20_000128 );129130 it.live(131 "refuses a push before forwarding it, in git's own output",132 () =>133 Effect.gen(function* () {134 const stale = new StaleBase({135 base: CommitSha.make("1".repeat(40)),136 conflicts: [],137 main: CommitSha.make("2".repeat(40)),138 });139140 const { root, upstream, url } = yield* world(141 refusing("refs/heads/main", [stale])142 );143144 const clone = `${root}/clone`;145146 yield* gitOk(root, "clone", "--quiet", url, clone);147 yield* commit(clone, "answer", { "b.txt": "b\n" });148149 const before = yield* gitOk(upstream, "rev-parse", "main");150 const pushed = yield* git(clone, "push", "origin", "HEAD:main");151152 expect(pushed.exitCode).toBe(1);153 expect(remoteLines(pushed.stderr)).toStrictEqual([154 "remote: StaleBase: main moved to 2222222, past your base 1111111",155 "remote: next: git pull --rebase",156 "remote: retryable: yes",157 ]);158 expect(pushed.stderr).toContain(159 " ! [remote rejected] HEAD -> main (StaleBase)"160 );161 expect(yield* gitOk(upstream, "rev-parse", "main")).toBe(before);162 }).pipe(Effect.provide(NodeServices.layer)),163 20_000164 );165166 it.live(167 "turns a whole push away when custody refuses one of its refs",168 () =>169 Effect.gen(function* () {170 const path = RepoPath.make("src/a.ts");171172 const { root, upstream, url } = yield* world(173 refusing("refs/heads/main", [174 new OutOfScope({ paths: [path] }),175 new TouchedJudge({ paths: [path] }),176 ])177 );178179 const clone = `${root}/clone`;180181 yield* gitOk(root, "clone", "--quiet", url, clone);182 yield* commit(clone, "answer", { "b.txt": "b\n" });183184 const before = yield* gitOk(upstream, "rev-parse", "main");185186 const pushed = yield* git(187 clone,188 "push",189 "origin",190 "HEAD:main",191 "HEAD:refs/heads/answer"192 );193194 expect(pushed.exitCode).toBe(1);195 expect(pushed.stderr).toContain(196 " ! [remote rejected] HEAD -> main (OutOfScope and 1 more)"197 );198 expect(pushed.stderr).toContain(199 " ! [remote rejected] HEAD -> answer (OutOfScope and 1 more)"200 );201 expect(yield* gitOk(upstream, "rev-parse", "main")).toBe(before);202 expect(203 (yield* git(upstream, "rev-parse", "--verify", "--quiet", "answer"))204 .exitCode205 ).toBe(1);206 }).pipe(Effect.provide(NodeServices.layer)),207 20_000208 );209210 it.live(211 "refuses a push after the upstream took it, when custody's review says so",212 () =>213 Effect.gen(function* () {214 const outOfScope = new OutOfScope({215 paths: [RepoPath.make("src/a.ts")],216 });217218 const { root, upstream, url } = yield* world(219 Custody.of({220 admit: () => Effect.succeed([]),221 review: () => Effect.succeed([outOfScope]),222 })223 );224225 const clone = `${root}/clone`;226227 yield* gitOk(root, "clone", "--quiet", url, clone);228229 const answer = yield* commit(clone, "answer", { "b.txt": "b\n" });230231 const pushed = yield* git(232 clone,233 "push",234 "origin",235 "HEAD:refs/heads/answer"236 );237238 expect(pushed.exitCode).toBe(1);239 expect(remoteLines(pushed.stderr)).toContain(240 "remote: OutOfScope: changes paths outside the ask's territory: src/a.ts; put those files back, or flag the ask if the want needs them"241 );242 expect(pushed.stderr).toContain(243 " ! [remote rejected] HEAD -> answer (OutOfScope)"244 );245 expect(yield* gitOk(upstream, "rev-parse", "answer")).toBe(answer);246 }).pipe(Effect.provide(NodeServices.layer)),247 20_000248 );249250 it.live(251 "names a ref the upstream found moved under the push StaleBase",252 () =>253 Effect.gen(function* () {254 const moved = yield* Ref.make(Option.none<string>());255256 const { root, upstream, url } = yield* world(admitAll, (repos) =>257 Effect.gen(function* () {258 const tip = yield* commit(repos.seed, "moved", { "c.txt": "c\n" });259260 yield* gitOk(261 repos.seed,262 "push",263 "--quiet",264 repos.upstream,265 "HEAD:refs/heads/main"266 );267 yield* Ref.set(moved, Option.some(tip));268269 return Option.none();270 })271 );272273 const clone = `${root}/clone`;274275 yield* gitOk(root, "clone", "--quiet", url, clone);276277 const base = yield* gitOk(clone, "rev-parse", "HEAD");278279 yield* commit(clone, "answer", { "b.txt": "b\n" });280281 const pushed = yield* git(clone, "push", "origin", "HEAD:main");282 const tip = Option.getOrThrow(yield* Ref.get(moved));283284 expect(pushed.exitCode).toBe(1);285 expect(remoteLines(pushed.stderr).slice(-3)).toStrictEqual([286 `remote: StaleBase: main moved to ${tip.slice(0, 7)}, past your base ${base.slice(0, 7)}`,287 "remote: next: git pull --rebase",288 "remote: retryable: yes",289 ]);290 expect(pushed.stderr).toContain(291 " ! [remote rejected] HEAD -> main (StaleBase)"292 );293 expect(yield* gitOk(upstream, "rev-parse", "main")).toBe(tip);294 }).pipe(Effect.provide(NodeServices.layer)),295 20_000296 );297298 it.live(299 "names Artifacts' busy refusal RepoBusy",300 () =>301 Effect.gen(function* () {302 const { root, url } = yield* world(admitAll, () =>303 Effect.succeedSome(artifactsBusy())304 );305306 const clone = `${root}/clone`;307308 yield* gitOk(root, "clone", "--quiet", url, clone);309 yield* commit(clone, "answer", { "b.txt": "b\n" });310311 const pushed = yield* git(312 clone,313 "push",314 "origin",315 "HEAD:refs/heads/answer"316 );317318 expect(pushed.exitCode).toBe(1);319 expect(remoteLines(pushed.stderr)).toStrictEqual([320 "remote: RepoBusy: the repo was busy with another push",321 "remote: next: git push",322 "remote: retryable: yes",323 ]);324 expect(pushed.stderr).toContain(325 " ! [remote rejected] HEAD -> answer (RepoBusy)"326 );327 }).pipe(Effect.provide(NodeServices.layer)),328 20_000329 );330});331332describe(gitRoute, () => {333 it("reads a repo and git's endpoint from a smart HTTP path, and nothing else", () => {334 expect(335 [336 "/datekit.git/info/refs",337 "/datekit/git-upload-pack",338 "/datekit--parse-iso--alice.git/git-receive-pack",Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 339–341why, one step back339 "/oscabriel/datekit.git/info/refs",340 "/github.com/pallets/click.git/info/refs",341 "/alice/notes--fix-week--bob.git/git-receive-pack",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
line 342why, one step back342 "/datekit.git/HEAD",Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 343why, one step back343 "/a/b/c/d.git/info/refs",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 344–347why, one step back344 "/rpc",345 ].map(gitRoute)346 ).toStrictEqual([347 Option.some({Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 348–357why, one step back348 endpoint: "info/refs",349 name: "datekit",350 repo: Option.some("datekit"),351 }),352 Option.some({353 endpoint: "git-upload-pack",354 name: "datekit",355 repo: Option.some("datekit"),356 }),357 Option.some({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 358–359why, one step back358 endpoint: "git-receive-pack",359 name: "datekit--parse-iso--alice",Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 360why, one step back360 repo: Option.some("datekit--parse-iso--alice"),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
line 361why, one step back361 }),Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 362–376why, one step back362 Option.some({363 endpoint: "info/refs",364 name: "oscabriel/datekit",365 repo: Option.some("datekit"),366 }),367 Option.some({368 endpoint: "info/refs",369 name: "github.com/pallets/click",370 repo: Option.some("click"),371 }),372 Option.some({373 endpoint: "git-receive-pack",374 name: "alice/notes--fix-week--bob",375 repo: Option.some("alice__notes--fix-week--bob"),376 }),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 377–380why, one step back377 Option.none(),378 Option.none(),379 Option.none(),380 ]);Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
lines 381–387why, one step back381 });382383 // A one-segment address is a legacy key; any other bare word is no project's.384 it("names no repo for an address no project can have", () => {385 expect(gitRoute("/demo.git/info/refs")).toStrictEqual(386 Option.some({ endpoint: "info/refs", name: "demo", repo: Option.none() })387 );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 388–389why, one step back388 });389});