import * as NodeServices from "@effect/platform-node/NodeServices";
import { assert, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Redacted from "effect/Redacted";
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
import * as GitHubAppAccess from "./GitHubAppAccess.ts";
import { cascadeRebaseStack } from "./githubStackRebase.ts";
const layer = Layer.mergeAll(
Layer.mock(GitHubAppAccess.GitHubAppAccess)({
credentialFor: () => Effect.succeed({ token: Redacted.make("token"), fingerprint: "fp" }),
}),
TestSourceControlHost.layerWithRealGit(),
).pipe(Layer.provideMerge(NodeServices.layer));
const setup = Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const { process } = yield* SourceControlHost.SourceControlHost;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-test-" });
const remote = `${root}/remote.git`;
const work = `${root}/work`;
const git = (cwd: string, ...args: string[]) =>
process
.run({
operation: "test",
command: "git",
args,
cwd,
env: {
GIT_AUTHOR_NAME: "t",
GIT_AUTHOR_EMAIL: "t@t",
GIT_COMMITTER_NAME: "t",
GIT_COMMITTER_EMAIL: "t@t",
},
})
.pipe(Effect.map((out) => out.stdout.trim()));
const write = (path: string, text: string) => fs.writeFileString(`${work}/${path}`, text);
yield* git(root, "init", "--quiet", "--bare", "-b", "main", remote);
yield* git(root, "clone", "--quiet", remote, work);
yield* write("index.ts", "export const answer = 41;\n");
yield* git(work, "add", "-A");
yield* git(work, "commit", "--quiet", "-m", "Initial");
yield* git(work, "switch", "--quiet", "-c", "feat/answer-42");
yield* write("index.ts", "export const answer = 42;\n");
yield* git(work, "commit", "--quiet", "-am", "A: set the answer to 42");
yield* git(work, "switch", "--quiet", "-c", "feat/answer-doc");
yield* write("index.ts", "/** The answer. */\nexport const answer = 42;\n");
yield* git(work, "commit", "--quiet", "-am", "B: document the answer");
yield* git(work, "switch", "--quiet", "main");
yield* write("README.md", "main moved ahead\n");
yield* git(work, "add", "-A");
yield* git(work, "commit", "--quiet", "-m", "Docs");
yield* git(work, "push", "--quiet", "origin", "main", "feat/answer-42", "feat/answer-doc");
const head = (branch: string) => git(remote, "--git-dir=.", "rev-parse", `refs/heads/${branch}`);
return {
remote,
head,
git: (...args: string[]) => git(remote, "--git-dir=.", ...args),
layers: [
{ number: 1, headBranch: "feat/answer-42", headSha: yield* head("feat/answer-42") },
{ number: 2, headBranch: "feat/answer-doc", headSha: yield* head("feat/answer-doc") },
],
};
});
it.layer(layer)("cascadeRebaseStack", (it) => {
it.effect("moves each layer's own commits onto the rebased layer below it", () =>
Effect.gen(function* () {
const repo = yield* setup;
const completed = yield* cascadeRebaseStack({
host: "github.com",
repository: "acme/web",
base: "main",
layers: repo.layers,
remote: repo.remote,
});
assert.strictEqual(completed, 2);
const main = yield* repo.head("main");
const bottom = yield* repo.head("feat/answer-42");
const top = yield* repo.head("feat/answer-doc");
expect(yield* repo.git("rev-parse", `${bottom}~1`)).toBe(main);
expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(bottom);
expect(yield* repo.git("log", "--format=%s", `${bottom}..${top}`)).toBe(
"B: document the answer",
);
}).pipe(Effect.scoped),
);
it.effect("drops a squash-merged lower layer's commits from the layer above it", () =>
Effect.gen(function* () {
const repo = yield* setup;
const fs = yield* FileSystem.FileSystem;
const { process } = yield* SourceControlHost.SourceControlHost;
const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-squash-" });
const run = (...args: string[]) =>
process
.run({
operation: "test",
command: "git",
args,
cwd: scratch,
env: {
GIT_AUTHOR_NAME: "t",
GIT_AUTHOR_EMAIL: "t@t",
GIT_COMMITTER_NAME: "t",
GIT_COMMITTER_EMAIL: "t@t",
},
})
.pipe(Effect.map((out) => out.stdout.trim()));
const write = (text: string) => fs.writeFileString(`${scratch}/index.ts`, text);
yield* run("clone", "--quiet", repo.remote, ".");
yield* run("switch", "--quiet", "-c", "feat/two-steps");
yield* write("export const answer = 40;\n");
yield* run("commit", "--quiet", "-am", "A1");
yield* write("export const answer = 42;\n");
yield* run("commit", "--quiet", "-am", "A2");
const mergedHeadSha = yield* run("rev-parse", "HEAD");
yield* run("switch", "--quiet", "-c", "feat/on-top");
yield* write("/** The answer. */\nexport const answer = 42;\n");
yield* run("commit", "--quiet", "-am", "B");
const headSha = yield* run("rev-parse", "HEAD");
yield* run("switch", "--quiet", "main");
yield* run("merge", "--quiet", "--squash", "feat/two-steps");
yield* run("commit", "--quiet", "-m", "A (#1)");
yield* run("push", "--quiet", "origin", "main", "feat/on-top");
const completed = yield* cascadeRebaseStack({
host: "github.com",
repository: "acme/web",
base: "main",
layers: [{ number: 2, headBranch: "feat/on-top", headSha }],
mergedHeadSha,
remote: repo.remote,
});
assert.strictEqual(completed, 1);
const top = yield* repo.head("feat/on-top");
expect(yield* repo.git("rev-parse", `${top}~1`)).toBe(yield* repo.head("main"));
expect(yield* repo.git("log", "--format=%s", `main..${top}`)).toBe("B");
}).pipe(Effect.scoped),
);
it.effect("refuses to overwrite a layer pushed after it was reviewed", () =>
Effect.gen(function* () {
const repo = yield* setup;
const stale = [repo.layers[0]!, { ...repo.layers[1]!, headSha: repo.layers[0]!.headSha }];
const error = yield* Effect.flip(
cascadeRebaseStack({
host: "github.com",
repository: "acme/web",
base: "main",
layers: stale,
remote: repo.remote,
}),
);
expect(error).toMatchObject({ _tag: "GitHubStackRebaseGitError", number: 2, completed: 1 });
expect(yield* repo.head("feat/answer-doc")).toBe(repo.layers[1]!.headSha);
}).pipe(Effect.scoped),
);
it.effect("stops at a conflicting layer and leaves it untouched", () =>
Effect.gen(function* () {
const repo = yield* setup;
const fs = yield* FileSystem.FileSystem;
const { process } = yield* SourceControlHost.SourceControlHost;
const scratch = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cascade-conflict-" });
const run = (...args: string[]) =>
process.run({
operation: "test",
command: "git",
args,
cwd: scratch,
env: {
GIT_AUTHOR_NAME: "t",
GIT_AUTHOR_EMAIL: "t@t",
GIT_COMMITTER_NAME: "t",
GIT_COMMITTER_EMAIL: "t@t",
},
});
yield* run("clone", "--quiet", repo.remote, ".");
yield* fs.writeFileString(`${scratch}/index.ts`, "export const answer = 43;\n");
yield* run("commit", "--quiet", "-am", "Conflict");
yield* run("push", "--quiet", "origin", "main");
const error = yield* Effect.flip(
cascadeRebaseStack({
host: "github.com",
repository: "acme/web",
base: "main",
layers: repo.layers,
remote: repo.remote,
}),
);
expect(error).toMatchObject({
_tag: "GitHubStackRebaseConflictError",
number: 1,
completed: 0,
});
expect(yield* repo.head("feat/answer-42")).toBe(repo.layers[0]!.headSha);
}).pipe(Effect.scoped),
);
});