import.test.ts
This file
Path
packages/project/test/import.test.ts
319 lines · 10.4 KiB · TypeScript
Last changed by
Merge build/comments-project into mainoscabrielbefore adoption49efd2a
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320import { describe, expect, it } from "@effect/vitest";import { RefName, RepoHost } from "@repo/artifacts/repo-host";import { memoryFs } from "@repo/artifacts/repo-host/memory-fs";import { ProjectEvent } from "@repo/protocol/domain/event";import { Handle, keyOf, LandedSha, RepoName } from "@repo/protocol/ids";import { Person } from "@repo/protocol/principal";import { ProjectExists } from "@repo/protocol/refusal";import { advanceTo } from "@repo/testkit/clock";import { gitClient } from "@repo/testkit/git-client";import { aProject } from "@repo/testkit/scenarios";import { DateTime, Effect, Exit, Option, Ref, Struct } from "effect";
import { ProjectStore } from "../src/project-store.ts";import { projectService } from "../src/project.ts";import { worldOf } from "./world.ts";
const scenario = aProject({ people: ["bob"] });
const { project } = scenario;
const repo = RepoName.make(keyOf(project));
const alice = Person.make({ handle: scenario.initializer });
const bob = Person.make({ handle: Handle.make("bob") });
const mainRef = RefName.make("refs/heads/main");
const forgetSource = ProjectStore.use((store) => Effect.flatMap(store.load(), ({ project: row }) => Option.match(row, { onNone: () => Effect.void, onSome: (kept) => store.commit({ project: Struct.omit(kept, ["importedFrom"]) }), }) ));
const masterRef = RefName.make("refs/heads/master");
const upstreamOnMaster = Effect.gen(function* () { const host = yield* RepoHost; const upstream = RepoName.make("upstream"); const client = yield* gitClient(memoryFs());
const tip = yield* client.commit("Start", [ ["Cargo.toml", '[package]\nname = "upstream"\n'], ["src/lib.rs", "pub fn parse() {}\n"], ]);
yield* host.create(upstream).pipe(Effect.orDie); yield* host .push(upstream, client.source, [ { from: Option.none(), ref: masterRef, to: tip }, ]) .pipe(Effect.orDie);
return { tip, url: "https://example.com/upstream.git" };});
const settled = Effect.gen(function* () { const api = yield* projectService(project); const snapshot = yield* ProjectStore.use((store) => store.load()); const refs = yield* (yield* RepoHost).refs(repo).pipe(Effect.orDie);
return { head: (yield* api.head()).landed, main: refs.get(mainRef), state: Option.map(snapshot.project, (row) => row.state), };});
/** Reads of the imported repo die once, as the binding does while Artifacts answers `IMPORT_IN_PROGRESS`. */const stillCopying = Effect.gen(function* () { const host = yield* RepoHost; const copying = yield* Ref.make(false);
return RepoHost.of({ ...host, importFrom: (name, source) => host .importFrom(name, source) .pipe(Effect.tap(() => Ref.set(copying, true))), refs: (name) => Effect.flatMap(Ref.getAndSet(copying, false), (busy) => busy ? Effect.die(new Error(`Import in progress: ${name}`)) : host.refs(name) ), });});
describe("importing a public repo", () => { it.effect( "adopts a remote whose only branch is master, with no branch given, on a main at its tip", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { tip, url } = yield* upstreamOnMaster; const api = yield* projectService(project); const made = yield* api.initProject(alice, { import: { url } });
expect(made.landed).toStrictEqual(LandedSha.make(tip)); expect(yield* settled).toStrictEqual({ head: LandedSha.make(tip), main: tip, state: Option.some("live"), }); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect( "adopts on a retry the repo a first import left when it died, not ProjectExists", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { tip, url } = yield* upstreamOnMaster; const host = yield* stillCopying;
const api = yield* projectService(project).pipe( Effect.provideService(RepoHost, host) );
const first = yield* Effect.exit( api.initProject(alice, { import: { url } }) );
const retried = yield* Effect.exit( api.initProject(alice, { import: { url } }) );
expect(Exit.hasDies(first)).toBeTruthy(); expect(Exit.map(retried, ({ landed }) => landed)).toStrictEqual( Exit.succeed(LandedSha.make(tip)) ); expect(yield* settled).toStrictEqual({ head: LandedSha.make(tip), main: tip, state: Option.some("live"), }); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect( "is heard as its adoption's landing, so a project whose policy never evaluates is listed", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { tip, url } = yield* upstreamOnMaster; const api = yield* projectService(project);
yield* api.initProject(alice, { import: { url } });
const heard = yield* api.events(0, 10);
expect(heard.map(({ event }) => event._tag)).toStrictEqual(["Landed"]); expect( heard.flatMap(({ event }) => ProjectEvent.guards.Landed(event) ? [event.landing] : [] ) ).toStrictEqual([ { assisted: [], author: alice.handle, credits: [], landedAt: DateTime.makeUnsafe("2026-10-08T12:00:00Z"), project, sha: LandedSha.make(tip), }, ]); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect( "keeps where it came from on its row, and projectPolicy names it", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { url } = yield* upstreamOnMaster; const api = yield* projectService(project);
yield* api.initProject(alice, { import: { branch: "master", url } });
const snapshot = yield* ProjectStore.use((store) => store.load());
expect( Option.map(snapshot.project, (row) => row.importedFrom) ).toStrictEqual(Option.some({ branch: "master", url })); expect((yield* api.policy()).importedFrom).toStrictEqual({ branch: "master", url, }); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect( "answers a repeat with the same source as the first call did, and ProjectExists for another source", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { tip, url } = yield* upstreamOnMaster; const api = yield* projectService(project);
const made = yield* api.initProject(alice, { import: { url } }); const again = yield* api.initProject(alice, { import: { url } });
expect(again).toStrictEqual(made); expect(made.landed).toStrictEqual(LandedSha.make(tip)); expect( yield* Effect.flip( api.initProject(alice, { import: { url: "https://example.com/other.git" }, }) ) ).toStrictEqual(new ProjectExists({ project })); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect( "sets the source on a repeat by the initializer for an import adopted before sources were kept, hearing no second landing", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { tip, url } = yield* upstreamOnMaster; const api = yield* projectService(project);
yield* api.initProject(alice, { import: { url } }); yield* forgetSource;
const retried = yield* api.initProject(alice, { import: { url } }); const snapshot = yield* ProjectStore.use((store) => store.load());
expect(retried.landed).toStrictEqual(LandedSha.make(tip)); expect( Option.map(snapshot.project, (row) => row.importedFrom) ).toStrictEqual(Option.some({ url })); expect( (yield* api.events(0, 10)).map(({ event }) => event._tag) ).toStrictEqual(["Landed"]); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect("refuses a source from anyone but the initializer", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { url } = yield* upstreamOnMaster; const api = yield* projectService(project);
yield* api.initProject(alice, { import: { url } }); yield* forgetSource;
expect( yield* Effect.flip(api.initProject(bob, { import: { url } })) ).toStrictEqual(new ProjectExists({ project })); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );
it.effect("refuses a source for a project that started empty", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const { url } = yield* upstreamOnMaster; const api = yield* projectService(project); const client = yield* gitClient(memoryFs());
const first = yield* client.commit("Start", [["README.md", "# start\n"]]);
yield* api.initProject(alice, {}); yield* (yield* RepoHost) .push(repo, client.source, [ { from: Option.none(), ref: mainRef, to: first }, ]) .pipe(Effect.orDie); yield* advanceTo("2026-10-08T12:01:00Z"); yield* api.recordFirstPush(alice, first);
expect( yield* Effect.flip(api.initProject(alice, { import: { url } })) ).toStrictEqual(new ProjectExists({ project })); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );});
describe("a first push", () => { it.effect( "is heard once as the adoption's landing, however often it is recorded", () => Effect.gen(function* () { yield* advanceTo("2026-10-08T12:00:00Z");
const api = yield* projectService(project); const client = yield* gitClient(memoryFs());
const first = yield* client.commit("Start", [ ["README.md", "# start\n"], ]);
yield* api.initProject(alice, {}); yield* (yield* RepoHost) .push(repo, client.source, [ { from: Option.none(), ref: mainRef, to: first }, ]) .pipe(Effect.orDie); yield* api.recordFirstPush(alice, first); yield* api.recordFirstPush(alice, first);
const heard = yield* api.events(0, 10);
expect(heard.map(({ event }) => event._tag)).toStrictEqual(["Landed"]); }).pipe(Effect.provide(worldOf(scenario, "memory"))) );});