/ opencodehub / tests / integration / pr-state-transition-route.test.ts
swadhinbiswas Add pull-request workflow state transition API · 2/19/2026
249d4ee
103 lines · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { beforeEach, describe, expect, it, vi } from "vitest";

const {
  patchPullRequestMock,
  fakeSchema,
} = vi.hoisted(() => ({
  patchPullRequestMock: vi.fn(async () =>
    new Response(JSON.stringify({ success: true, data: { success: true } }), {
      status: 200,
      headers: { "content-type": "application/json" },
    })
  ),
  fakeSchema: {
    users: { username: {} },
    repositories: { ownerId: {}, name: {}, id: {} },
    prStateDefinitions: { id: {}, repositoryId: {} },
  } as any,
}));

let mockDb: any;

vi.mock("@/db", () => ({
  getDatabase: () => mockDb,
  schema: fakeSchema,
}));

vi.mock("@/pages/api/repos/[owner]/[repo]/pulls/[number]/index", () => ({
  PATCH: patchPullRequestMock,
}));

import { POST as transitionPost } from "@/pages/api/repos/[owner]/[repo]/pulls/[number]/state";

function makeDb() {
  return {
    query: {
      users: {
        findFirst: vi.fn(async () => ({ id: "owner-1" })),
      },
      repositories: {
        findFirst: vi.fn(async () => ({ id: "repo-1" })),
      },
      prStateDefinitions: {
        findFirst: vi.fn(async () => ({ name: "in_review" })),
      },
    },
  };
}

describe("PR state transition route", () => {
  beforeEach(() => {
    mockDb = makeDb();
    patchPullRequestMock.mockClear();
  });

  it("rejects requests with neither state nor stateId", async () => {
    const response = await transitionPost({
      params: { owner: "owner-1", repo: "demo", number: "12" },
      locals: { user: { id: "user-1" } },
      request: new Request("http://localhost/api/repos/owner-1/demo/pulls/12/state", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({}),
      }),
    } as any);

    expect(response.status).toBe(400);
    expect(patchPullRequestMock).not.toHaveBeenCalled();
  });

  it("maps stateId to state name and delegates transition", async () => {
    await transitionPost({
      params: { owner: "owner-1", repo: "demo", number: "12" },
      locals: { user: { id: "user-1" } },
      request: new Request("http://localhost/api/repos/owner-1/demo/pulls/12/state", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ stateId: "state-1" }),
      }),
    } as any);

    expect(patchPullRequestMock).toHaveBeenCalledTimes(1);
    const args = patchPullRequestMock.mock.calls[0][0];
    const forwardedBody = await args.request.json();
    expect(forwardedBody.state).toBe("in_review");
  });

  it("passes explicit state through to patch handler", async () => {
    await transitionPost({
      params: { owner: "owner-1", repo: "demo", number: "12" },
      locals: { user: { id: "user-1" } },
      request: new Request("http://localhost/api/repos/owner-1/demo/pulls/12/state", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ state: "closed" }),
      }),
    } as any);

    const args = patchPullRequestMock.mock.calls[0][0];
    const forwardedBody = await args.request.json();
    expect(forwardedBody.state).toBe("closed");
  });
});