Skip to content
Myr

local.test.ts

This file

Path

packages/artifacts/test/local.test.ts

286 lines · 9.4 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

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–70why, one step back
1import { NodeHttpServer, NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { gitOk } from "@repo/testkit/git";4import { gitClient } from "@repo/testkit/git-client";5import { httpBackend } from "@repo/testkit/http-backend";6import { Context, Duration, Effect, FileSystem, Layer, Option } from "effect";7import { HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";8import type { ChildProcessSpawner } from "effect/process";910import { Fetch, FetchFailed } from "../src/fetch.ts";11import {12 BranchName,13 ImportRefused,14 RefName,15 RepoHost,16 RepoName,17} from "../src/repo-host.ts";18import { RepoHostLocal } from "../src/repo-host/local.ts";19import { memoryFs } from "../src/repo-host/memory-fs.ts";20import { describeRepoHost } from "./repo-host-contract.ts";21import { temporaryRoot } from "./temporary-root.ts";2223const main = RefName.make("refs/heads/main");2425const offline = Layer.succeed(26 Fetch,27 Fetch.of({28 fetch: (request) =>29 Effect.fail(new FetchFailed({ reason: "offline", url: request.url })),30 })31);3233const storeAt = (root: string) =>34 RepoHostLocal.layer({ remoteBase: `file://${root}`, root }).pipe(35 Layer.provide(offline)36 );3738const freshStore = Layer.unwrap(Effect.map(temporaryRoot, storeAt));3940const served = (root: string) =>41 Effect.gen(function* () {42 const spawner =43 yield* Effect.context<ChildProcessSpawner.ChildProcessSpawner>();4445 const backend = httpBackend(root);4647 const app = Effect.gen(function* () {48 const request = yield* HttpServerRequest.toWeb(49 yield* HttpServerRequest.HttpServerRequest50 );5152 return HttpServerResponse.fromWeb(53 yield* Effect.scoped(backend(request)).pipe(Effect.provide(spawner))54 );55 }).pipe(Effect.orDie);5657 const server = yield* Layer.build(58 HttpServer.serve(app).pipe(Layer.provideMerge(NodeHttpServer.layerTest))59 ).pipe(Effect.orDie);6061 const { address } = Context.get(server, HttpServer.HttpServer);6263 return "port" in address64 ? `http://127.0.0.1:${address.port}`65 : yield* Effect.die("the test server listens on a socket, not a port");66 });6768// Its test-only meaning of a public remote: another bare repo on disk, named69// by its `file://` URL.70describeRepoHost("local", freshStore, {

Merge branch 'build/913-import-any-branch-and-examples'oscabrielbefore adoptiona4543f6

line 71why, one step back
71 danglingHead: true,

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 72–76why, one step back
72 importUrl: (name) =>
73 RepoHost.use((repos) => repos.remote(name)).pipe(Effect.orDie),74});7576describe("bare repos on local disk", () => {

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

line 77why, one step back
77 // What the adapter stores is a repo real git can clone over smart HTTP.

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 78–286why, one step back
78 it.live(
79 "serves a pushed file to a real git clone of its remote",80 () =>81 Effect.gen(function* () {82 const fs = yield* FileSystem.FileSystem;83 const root = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);84 const work = yield* fs.makeTempDirectoryScoped().pipe(Effect.orDie);85 const client = yield* gitClient(memoryFs());86 const first = yield* client.commit("first", [["src/a.ts", "v1\n"]]);87 const name = RepoName.make("demo");8889 const remote = yield* RepoHost.use((repos) =>90 Effect.gen(function* () {91 yield* repos.create(name);92 yield* repos.push(name, client.source, [93 { from: Option.none(), ref: main, to: first },94 ]);9596 return yield* repos.remote(name);97 })98 ).pipe(99 Effect.provide(100 RepoHostLocal.layer({101 remoteBase: yield* served(root),102 root,103 }).pipe(Layer.provide(offline))104 )105 );106107 yield* gitOk(work, "clone", "--quiet", remote, "clone");108109 expect(110 yield* fs.readFileString(`${work}/clone/src/a.ts`).pipe(Effect.orDie)111 ).toBe("v1\n");112 }).pipe(Effect.provide(NodeServices.layer)),113 30_000114 );115116 it.effect("names each repo's remote under the base it was given", () =>117 Effect.gen(function* () {118 const root = yield* temporaryRoot;119 const name = RepoName.make("demo");120121 const remote = yield* RepoHost.use((repos) =>122 Effect.andThen(repos.create(name), repos.remote(name))123 ).pipe(124 Effect.provide(125 RepoHostLocal.layer({126 remoteBase: "http://127.0.0.1:8080/git",127 root,128 }).pipe(Layer.provide(offline))129 )130 );131132 expect(remote).toBe("http://127.0.0.1:8080/git/demo.git");133 })134 );135136 it.effect(137 "keeps repos, refs and tokens for the next layer over the same root",138 () =>139 Effect.gen(function* () {140 const root = yield* temporaryRoot;141 const client = yield* gitClient(memoryFs());142 const first = yield* client.commit("first", [["a", "1"]]);143 const name = RepoName.make("kept");144145 const minted = yield* RepoHost.use((repos) =>146 Effect.gen(function* () {147 yield* repos.create(name);148 yield* repos.push(name, client.source, [149 { from: Option.none(), ref: main, to: first },150 ]);151152 return yield* repos.mintToken(name, "write", Duration.hours(1));153 })154 ).pipe(Effect.provide(storeAt(root)));155156 const later = yield* RepoHost.use((repos) =>157 Effect.gen(function* () {158 const refs = yield* repos.refs(name);159 const revoked = yield* repos.revokeToken(name, minted.id);160161 const next = yield* repos.mintToken(162 name,163 "read",164 Duration.hours(1)165 );166167 return { next, refs, revoked };168 })169 ).pipe(Effect.provide(storeAt(root)));170171 expect(later.refs).toStrictEqual(new Map([[main, first]]));172 expect(later.revoked).toBeTruthy();173 expect(later.next.id).not.toBe(minted.id);174 expect(175 yield* RepoHost.use((repos) =>176 Effect.all([177 repos.revokeToken(name, minted.id),178 repos.revokeToken(name, later.next.id),179 ])180 ).pipe(Effect.provide(storeAt(root)))181 ).toStrictEqual([false, true]);182 })183 );184});185186describe("importing onto local disk", () => {187 it.effect("imports the branch it names, under that name", () =>188 Effect.gen(function* () {189 const repos = yield* RepoHost;190 const client = yield* gitClient(memoryFs());191 const first = yield* client.commit("first", [["a", "1"]]);192 const release = RefName.make("refs/heads/release");193 const remote = RepoName.make("branches");194 const name = RepoName.make("from-a-branch");195196 yield* repos.create(remote);197 yield* repos.push(remote, client.source, [198 { from: Option.none(), ref: release, to: first },199 ]);200201 yield* repos.importFrom(name, {202 branch: BranchName.make("release"),203 url: yield* repos.remote(remote),204 });205206 expect(yield* repos.refs(name)).toStrictEqual(207 new Map([[release, first]])208 );209 }).pipe(Effect.provide(freshStore))210 );211212 it.effect("refuses a path that holds no repo, and makes no repo", () =>213 Effect.gen(function* () {214 const repos = yield* RepoHost;215 const name = RepoName.make("nowhere");216 const url = "file:///nonexistent/demo.git";217218 expect(yield* Effect.flip(repos.importFrom(name, { url }))).toStrictEqual(219 new ImportRefused({ name, reason: `no repo at ${url}` })220 );221 expect(yield* repos.remove(name)).toBeFalsy();222 }).pipe(Effect.provide(freshStore))223 );224225 it.effect("refuses a file URL on another host, and makes no repo", () =>226 Effect.gen(function* () {227 const repos = yield* RepoHost;228 const name = RepoName.make("elsewhere");229 const url = "file://example.com/demo.git";230231 expect(yield* Effect.flip(repos.importFrom(name, { url }))).toStrictEqual(232 new ImportRefused({ name, reason: `no repo at ${url}` })233 );234 expect(yield* repos.remove(name)).toBeFalsy();235 }).pipe(Effect.provide(freshStore))236 );237238 it.effect("refuses a branch the remote lacks, and makes no repo", () =>239 Effect.gen(function* () {240 const repos = yield* RepoHost;241 const remote = RepoName.make("empty-remote");242 const name = RepoName.make("from-nothing");243244 yield* repos.create(remote);245246 const url = yield* repos.remote(remote);247248 expect(249 yield* Effect.flip(250 repos.importFrom(name, { branch: BranchName.make("absent"), url })251 )252 ).toStrictEqual(253 new ImportRefused({ name, reason: `${url} has no branch absent` })254 );255 expect(yield* repos.remove(name)).toBeFalsy();256 }).pipe(Effect.provide(freshStore))257 );258259 it.effect("refuses an HTTPS remote it cannot reach, and makes no repo", () =>260 Effect.gen(function* () {261 const repos = yield* RepoHost;262 const name = RepoName.make("unreachable");263 const url = "https://example.com/demo.git";264265 expect(yield* Effect.flip(repos.importFrom(name, { url }))).toStrictEqual(266 new ImportRefused({267 name,268 reason: `${url} could not be fetched: offline`,269 })270 );271 expect(yield* repos.remove(name)).toBeFalsy();272 }).pipe(Effect.provide(freshStore))273 );274275 it.effect("refuses a remote that is neither a file nor HTTP", () =>276 Effect.gen(function* () {277 const repos = yield* RepoHost;278 const name = RepoName.make("over-ssh");279 const url = "ssh://example.com/demo.git";280281 expect(yield* Effect.flip(repos.importFrom(name, { url }))).toStrictEqual(282 new ImportRefused({ name, reason: `${url} is not a file or HTTP URL` })283 );284 }).pipe(Effect.provide(freshStore))285 );286});