Skip to content
Myr

repo-context.test.ts

This file

Path

apps/cli/test/repo-context.test.ts

199 lines · 5.9 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–15why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { AskId, Handle, LayerName, ProjectName } from "@repo/protocol/ids";4import { gitOk } from "@repo/testkit/git";5import { Effect, FileSystem, Layer, Option, Path, Redacted } from "effect";67import { Credentials } from "../src/credentials.ts";8import type { Profile } from "../src/credentials.ts";9import {10 NotARepo,11 parseRemote,12 parseRepoName,13 repoContext,14} from "../src/repo-context.ts";15

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 16why, one step back
16const project = ProjectName.make("oscabriel/datekit");

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 17–26why, one step back
17
18const platform = "https://platform-dev.example.workers.dev";1920describe("a repo's name", () => {21 it("names a project's canonical repo", () => {22 expect(parseRepoName("datekit")).toStrictEqual(23 Option.some({ kind: "canonical", project })24 );25 });26

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 27–40why, one step back
27 it("reads a project's name of two or three segments, in lowercase", () => {
28 expect(parseRepoName("Oscabriel/Datekit")).toStrictEqual(29 Option.some({ kind: "canonical", project })30 );31 expect(parseRepoName("github.com/pallets/click--fix--alice")).toStrictEqual(32 Option.some({33 ask: AskId.make("fix"),34 handle: Handle.make("alice"),35 kind: "claim",36 project: ProjectName.make("github.com/pallets/click"),37 })38 );39 });40

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 41–93why, one step back
41 it("names a claim's fork: project, ask and handle", () => {
42 expect(parseRepoName("datekit--fix-week--alice")).toStrictEqual(43 Option.some({44 ask: AskId.make("fix-week"),45 handle: Handle.make("alice"),46 kind: "claim",47 project,48 })49 );50 });5152 it("names a person's working fork", () => {53 expect(parseRepoName("datekit--me--alice")).toStrictEqual(54 Option.some({ handle: Handle.make("alice"), kind: "working", project })55 );56 });5758 it("names a layer's repo", () => {59 expect(parseRepoName("datekit--maintainers")).toStrictEqual(60 Option.some({61 kind: "layer",62 layer: LayerName.make("maintainers"),63 project,64 })65 );66 });6768 it.each([69 "",70 "datekit--fix-week--Alice",71 "datekit--fix-week--alice--again",72 "datekit--me--",73 "datekit--layer--alice",74 ])("names nothing the platform makes: %j", (name) => {75 expect(parseRepoName(name)).toStrictEqual(Option.none());76 });77});7879describe("a remote's url", () => {80 it("splits into the platform's origin and the repo's name", () => {81 expect(82 parseRemote(`${platform}/datekit--fix-week--alice.git`)83 ).toStrictEqual(84 Option.some({ origin: platform, repo: "datekit--fix-week--alice" })85 );86 });8788 it("leaves out a trailing .git or slash, and any user in the url", () => {89 expect(90 parseRemote("https://someone@platform-dev.example.workers.dev/datekit/")91 ).toStrictEqual(Option.some({ origin: platform, repo: "datekit" }));92 });93

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

lines 94–99why, one step back
94 it("reads a project's name of two or three segments", () => {
95 expect(parseRemote("https://github.com/owner/datekit.git")).toStrictEqual(96 Option.some({ origin: "https://github.com", repo: "owner/datekit" })97 );98 });99

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 100–101why, one step back
100 it.each([
101 "git@github.com:owner/datekit.git",

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 102why, one step back
102 "https://platform.example/a/b/c/d.git",

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 103–199why, one step back
103 "file:///srv/datekit.git",
104 "not a url",105 ])("is no platform remote: %j", (url) => {106 expect(parseRemote(url)).toStrictEqual(Option.none());107 });108});109110const aRepo = (remotes: readonly (readonly [string, string])[]) =>111 Effect.gen(function* () {112 const fs = yield* FileSystem.FileSystem;113 const dir = yield* fs.realPath(yield* fs.makeTempDirectoryScoped());114115 yield* gitOk(dir, "init", "--quiet");116117 for (const [name, url] of remotes) {118 yield* gitOk(dir, "remote", "add", name, url);119 }120121 return dir;122 });123124const signedIn = (url: string) => {125 const profile: Profile = {126 token: Redacted.make("tok-0123456789abcdefghijklmnopqrstuvwxyz"),127 url,128 };129130 return Layer.succeed(Credentials)({131 current: Effect.succeedSome(profile),132 forHost: () => Effect.succeedSome(profile),133 put: () => Effect.void,134 remove: () => Effect.succeed(false),135 });136};137138describe("the repo the CLI stands in", () => {139 it.live(140 "is found from a folder inside it: its root, remote, project, ask and handle",141 () =>142 Effect.gen(function* () {143 const fs = yield* FileSystem.FileSystem;144 const path = yield* Path.Path;145 const url = `${platform}/datekit--fix-week--alice.git`;146 const root = yield* aRepo([["origin", url]]);147 const inside = path.join(root, "src");148149 yield* fs.makeDirectory(inside);150151 const context = yield* repoContext(inside);152153 expect(context).toStrictEqual({154 origin: Option.some(platform),155 remote: Option.some({ name: "origin", url }),156 repo: Option.some({157 ask: AskId.make("fix-week"),158 handle: Handle.make("alice"),159 kind: "claim",160 project,161 }),162 root,163 });164 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))165 );166167 it.live("prefers the remote on the signed-in platform over `origin`", () =>168 Effect.gen(function* () {169 const url = `${platform}/datekit.git`;170171 const root = yield* aRepo([172 ["origin", "https://github.com/owner/datekit.git"],173 ["platform", url],174 ]);175176 const context = yield* repoContext(root).pipe(177 Effect.provide(signedIn(platform))178 );179180 expect(context.remote).toStrictEqual(181 Option.some({ name: "platform", url })182 );183 expect(context.repo).toStrictEqual(184 Option.some({ kind: "canonical", project })185 );186 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))187 );188189 it.live("is NotARepo outside any git repo", () =>190 Effect.gen(function* () {191 const fs = yield* FileSystem.FileSystem;192 const dir = yield* fs.makeTempDirectoryScoped();193194 expect(yield* Effect.flip(repoContext(dir))).toStrictEqual(195 new NotARepo({ cwd: dir })196 );197 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))198 );199});