Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
EventId,
ProjectId,
ProviderInstanceId,
SourceControlProviderKind,
ThreadId,
type OrchestrationProjectShell,
type OrchestrationV2DomainEvent,
type OrchestrationV2ThreadShell,
type OrchestrationV2ServerCommand,
} from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Crypto from "effect/Crypto";
Expand Down Expand Up @@ -169,6 +171,124 @@ describe("ThreadPullRequestServiceV2 reads", () => {
};
};

it.effect.each([
{ state: "closed", sameBranch: false },
{ state: "merged", sameBranch: false },
{ state: "closed", sameBranch: true },
{ state: "merged", sameBranch: true },
] as const)(
"restores a $state automatic PR only for its own branch (sameBranch=$sameBranch)",
({ state, sameBranch }) =>
Effect.scoped(
Effect.gen(function* () {
const savedReference = {
projectId: ProjectId.make("project-1"),
repository: "acme/project",
number: 42,
url: "https://github.com/acme/project/pull/42",
};
const thread = {
...threadShell("branch-pr-thread"),
branch: "main",
branchPullRequest: savedReference,
linkedPullRequest: savedReference,
};
const project: OrchestrationProjectShell = {
id: thread.projectId,
title: "Project",
workspaceRoot: "/workspace/project",
defaultModelSelection: null,
scripts: [],
createdAt: "2026-08-01T00:00:00.000Z",
updatedAt: "2026-08-01T00:00:00.000Z",
};
const activation = yield* Deferred.make<void>();
const sweepStarted = yield* Deferred.make<void>();
const commands: Array<OrchestrationV2ServerCommand> = [];
const dependencies = Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
streamDomainEvents: Stream.never,
getShellSnapshot: () =>
Deferred.succeed(sweepStarted, undefined).pipe(
Effect.as({
schemaVersion: 2,
snapshotSequence: 1,
threads: [thread],
archivedThreads: [],
}),
),
dispatch: (command) =>
Effect.sync(() => {
commands.push(command);
return { sequence: 2, storedEvents: [] };
}),
}),
Layer.mock(ProjectStore.ProjectStoreV2)({
listShells: () => Effect.succeed([project]),
getShell: () => Effect.succeed(Option.some(project)),
}),
Layer.mock(GitManager.GitManager)({
branchPullRequest: () => Effect.succeed(null),
}),
Layer.mock(PullRequestService.PullRequestService)({
summary: () =>
Effect.succeed({
...savedReference,
provider: SourceControlProviderKind.make("github"),
title: "Previous branch work",
state,
headBranch: sameBranch ? thread.branch : "feature/previous-work",
baseBranch: "release",
updatedAt: "2026-09-19T00:00:00.000Z",
}),
}),
Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({
resolve: () =>
Effect.succeed({
canonicalKey: "github.com/acme/project",
locator: {
source: "git-remote" as const,
remoteName: "origin",
remoteUrl: "git@github.com:acme/project.git",
},
provider: "github" as const,
owner: "acme",
name: "project",
}),
}),
Layer.succeed(ServerActivation.ServerActivation, Deferred.await(activation)),
Layer.succeed(
Crypto.Crypto,
Crypto.make({
randomBytes: (size) => new Uint8Array(size).fill(1),
digest: (_algorithm, data) => Effect.succeed(data),
}),
),
FileSystem.layerNoop({}),
);
yield* Effect.gen(function* () {
const service = yield* ThreadPullRequestService.make;
yield* service.start();
yield* Deferred.succeed(activation, undefined);
yield* Deferred.await(sweepStarted);
yield* service.drain;
}).pipe(Effect.provide(dependencies));

if (sameBranch) {
expect(commands).toEqual([]);
} else {
expect(commands).toHaveLength(1);
expect(commands[0]).toMatchObject({
type: "thread.pull-request.sync",
threadId: thread.id,
branchPullRequest: null,
});
expect(commands[0]).not.toHaveProperty("linkedPullRequest");
}
}),
),
);

it.effect("an event for one thread reads that thread's shell, not every thread's", () =>
Effect.scoped(
Effect.gen(function* () {
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/orchestration-v2/ThreadPullRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ export const make = Effect.gen(function* () {
const previous = yield* pullRequests.summary(thread.branchPullRequest, {
recoverTransientFailure: false,
});
if (previous.state === "merged" || previous.state === "closed") {
if (
previous.headBranch === thread.branch &&
(previous.state === "merged" || previous.state === "closed")
) {
branchPullRequest = thread.branchPullRequest;
}
}
Expand Down
Loading