|
| 1 | +import type { ManagementApiClient } from "@prisma/management-api-sdk"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { resolveReadBranch } from "../src/lib/app/read-branch"; |
| 5 | + |
| 6 | +type RawBranch = { |
| 7 | + id: string; |
| 8 | + gitName: string; |
| 9 | + isDefault: boolean; |
| 10 | + role: "production" | "preview"; |
| 11 | +}; |
| 12 | + |
| 13 | +function pageResponse(branches: RawBranch[], nextCursor: string | null) { |
| 14 | + return { |
| 15 | + data: { |
| 16 | + data: branches, |
| 17 | + pagination: { hasMore: nextCursor !== null, nextCursor }, |
| 18 | + }, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function clientReturning(branches: RawBranch[]): ManagementApiClient { |
| 23 | + return { |
| 24 | + GET: vi.fn().mockResolvedValue(pageResponse(branches, null)), |
| 25 | + } as unknown as ManagementApiClient; |
| 26 | +} |
| 27 | + |
| 28 | +describe("resolveReadBranch", () => { |
| 29 | + it("returns the branch whose gitName matches the request", async () => { |
| 30 | + const client = clientReturning([ |
| 31 | + { |
| 32 | + id: "b_master", |
| 33 | + gitName: "master", |
| 34 | + isDefault: true, |
| 35 | + role: "production", |
| 36 | + }, |
| 37 | + { id: "b_feat", gitName: "feat/x", isDefault: false, role: "preview" }, |
| 38 | + ]); |
| 39 | + |
| 40 | + const result = await resolveReadBranch(client, { |
| 41 | + projectId: "proj_1", |
| 42 | + branchName: "feat/x", |
| 43 | + }); |
| 44 | + |
| 45 | + expect(result).toEqual({ id: "b_feat", name: "feat/x", kind: "preview" }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("falls back to the default branch when the requested branch does not exist", async () => { |
| 49 | + const client = clientReturning([ |
| 50 | + { |
| 51 | + id: "b_master", |
| 52 | + gitName: "master", |
| 53 | + isDefault: true, |
| 54 | + role: "production", |
| 55 | + }, |
| 56 | + ]); |
| 57 | + |
| 58 | + const result = await resolveReadBranch(client, { |
| 59 | + projectId: "proj_1", |
| 60 | + branchName: "main", |
| 61 | + }); |
| 62 | + |
| 63 | + expect(result).toEqual({ |
| 64 | + id: "b_master", |
| 65 | + name: "master", |
| 66 | + kind: "production", |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("returns null when the project has no branches", async () => { |
| 71 | + const client = clientReturning([]); |
| 72 | + |
| 73 | + const result = await resolveReadBranch(client, { |
| 74 | + projectId: "proj_1", |
| 75 | + branchName: "main", |
| 76 | + }); |
| 77 | + |
| 78 | + expect(result).toBeNull(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("throws when the branches request fails", async () => { |
| 82 | + const client = { |
| 83 | + GET: vi.fn().mockResolvedValue({ |
| 84 | + error: { message: "Unauthorized" }, |
| 85 | + response: { status: 401 }, |
| 86 | + }), |
| 87 | + } as unknown as ManagementApiClient; |
| 88 | + |
| 89 | + await expect( |
| 90 | + resolveReadBranch(client, { projectId: "proj_1", branchName: "main" }), |
| 91 | + ).rejects.toThrow(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("follows pagination to a branch on a later page", async () => { |
| 95 | + const GET = vi |
| 96 | + .fn() |
| 97 | + .mockResolvedValueOnce( |
| 98 | + pageResponse( |
| 99 | + [ |
| 100 | + { |
| 101 | + id: "b_main", |
| 102 | + gitName: "main", |
| 103 | + isDefault: true, |
| 104 | + role: "production", |
| 105 | + }, |
| 106 | + ], |
| 107 | + "cursor_1", |
| 108 | + ), |
| 109 | + ) |
| 110 | + .mockResolvedValueOnce( |
| 111 | + pageResponse( |
| 112 | + [ |
| 113 | + { |
| 114 | + id: "b_feat", |
| 115 | + gitName: "feat/x", |
| 116 | + isDefault: false, |
| 117 | + role: "preview", |
| 118 | + }, |
| 119 | + ], |
| 120 | + null, |
| 121 | + ), |
| 122 | + ); |
| 123 | + const client = { GET } as unknown as ManagementApiClient; |
| 124 | + |
| 125 | + const result = await resolveReadBranch(client, { |
| 126 | + projectId: "proj_1", |
| 127 | + branchName: "feat/x", |
| 128 | + }); |
| 129 | + |
| 130 | + expect(result).toEqual({ id: "b_feat", name: "feat/x", kind: "preview" }); |
| 131 | + expect(GET).toHaveBeenCalledTimes(2); |
| 132 | + expect(GET).toHaveBeenNthCalledWith( |
| 133 | + 1, |
| 134 | + "/v1/projects/{projectId}/branches", |
| 135 | + expect.objectContaining({ |
| 136 | + params: { path: { projectId: "proj_1" }, query: { cursor: undefined } }, |
| 137 | + }), |
| 138 | + ); |
| 139 | + expect(GET).toHaveBeenNthCalledWith( |
| 140 | + 2, |
| 141 | + "/v1/projects/{projectId}/branches", |
| 142 | + expect.objectContaining({ |
| 143 | + params: { |
| 144 | + path: { projectId: "proj_1" }, |
| 145 | + query: { cursor: "cursor_1" }, |
| 146 | + }, |
| 147 | + }), |
| 148 | + ); |
| 149 | + }); |
| 150 | +}); |
0 commit comments