|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../../src/lib/apiClient", () => ({ |
| 4 | + apiClient: { |
| 5 | + patch: vi.fn(), |
| 6 | + }, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("../../src/lib/tm-base-url", () => ({ |
| 10 | + getTMBaseURL: vi.fn().mockResolvedValue("https://test-management.browserstack.com"), |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("../../src/logger", () => ({ |
| 14 | + default: { error: vi.fn(), info: vi.fn(), debug: vi.fn() }, |
| 15 | +})); |
| 16 | + |
| 17 | +import { apiClient } from "../../src/lib/apiClient"; |
| 18 | +import { updateTestRun } from "../../src/tools/testmanagement-utils/update-testrun"; |
| 19 | + |
| 20 | +const mockConfig = { |
| 21 | + "browserstack-username": "config-user", |
| 22 | + "browserstack-access-key": "config-key", |
| 23 | +} as any; |
| 24 | + |
| 25 | +describe("updateTestRun — endpoint dispatch", () => { |
| 26 | + beforeEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("routes metadata (name/run_state) to the /update endpoint", async () => { |
| 31 | + (apiClient.patch as any).mockResolvedValue({ |
| 32 | + data: { success: true, testrun: { name: "New" } }, |
| 33 | + }); |
| 34 | + |
| 35 | + const result = await updateTestRun( |
| 36 | + { |
| 37 | + project_identifier: "PR-1", |
| 38 | + test_run_id: "TR-1", |
| 39 | + test_run: { name: "New", run_state: "in_progress" }, |
| 40 | + }, |
| 41 | + mockConfig, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result.isError).toBeFalsy(); |
| 45 | + const call = (apiClient.patch as any).mock.calls[0][0]; |
| 46 | + expect(call.url).toMatch(/\/test-runs\/TR-1\/update$/); |
| 47 | + expect(call.body).toEqual({ |
| 48 | + test_run: { name: "New", run_state: "in_progress" }, |
| 49 | + }); |
| 50 | + expect(result.content?.[0]?.text).toContain( |
| 51 | + "Successfully updated test run TR-1", |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it("routes add_test_cases to the /test-cases endpoint with preserve default", async () => { |
| 56 | + (apiClient.patch as any).mockResolvedValue({ |
| 57 | + data: { success: true, async: true, unique_id: "abc" }, |
| 58 | + }); |
| 59 | + |
| 60 | + const result = await updateTestRun( |
| 61 | + { |
| 62 | + project_identifier: "PR-1", |
| 63 | + test_run_id: "TR-9", |
| 64 | + test_run: { |
| 65 | + add_test_cases: [{ test_case_ids: ["TC-1", "TC-2"] }], |
| 66 | + }, |
| 67 | + }, |
| 68 | + mockConfig, |
| 69 | + ); |
| 70 | + |
| 71 | + expect(result.isError).toBeFalsy(); |
| 72 | + const call = (apiClient.patch as any).mock.calls[0][0]; |
| 73 | + expect(call.url).toMatch(/\/test-runs\/TR-9\/test-cases$/); |
| 74 | + expect(call.body.test_run.add_test_cases).toEqual([ |
| 75 | + { test_case_ids: ["TC-1", "TC-2"] }, |
| 76 | + ]); |
| 77 | + expect(call.body.test_run.preserve_existing_results).toBe(true); |
| 78 | + expect(result.content?.[0]?.text).toContain("added 2"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("honors the preserve_existing_results flag when adding", async () => { |
| 82 | + (apiClient.patch as any).mockResolvedValue({ |
| 83 | + data: { success: true, async: true, unique_id: "def" }, |
| 84 | + }); |
| 85 | + |
| 86 | + const result = await updateTestRun( |
| 87 | + { |
| 88 | + project_identifier: "PR-1", |
| 89 | + test_run_id: "TR-9", |
| 90 | + test_run: { |
| 91 | + add_test_cases: [{ test_case_ids: ["TC-3"] }], |
| 92 | + preserve_existing_results: false, |
| 93 | + }, |
| 94 | + }, |
| 95 | + mockConfig, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(result.isError).toBeFalsy(); |
| 99 | + const call = (apiClient.patch as any).mock.calls[0][0]; |
| 100 | + expect(call.url).toMatch(/\/test-runs\/TR-9\/test-cases$/); |
| 101 | + expect(call.body.test_run.preserve_existing_results).toBe(false); |
| 102 | + expect(result.content?.[0]?.text).toContain("added 1"); |
| 103 | + }); |
| 104 | + |
| 105 | + it("updates both metadata and test cases when both are provided", async () => { |
| 106 | + (apiClient.patch as any) |
| 107 | + .mockResolvedValueOnce({ data: { success: true, testrun: { name: "New" } } }) |
| 108 | + .mockResolvedValueOnce({ data: { success: true, async: true, unique_id: "z" } }); |
| 109 | + |
| 110 | + const result = await updateTestRun( |
| 111 | + { |
| 112 | + project_identifier: "PR-1", |
| 113 | + test_run_id: "TR-1", |
| 114 | + test_run: { |
| 115 | + name: "New", |
| 116 | + add_test_cases: [{ test_case_ids: ["TC-1"] }], |
| 117 | + }, |
| 118 | + }, |
| 119 | + mockConfig, |
| 120 | + ); |
| 121 | + |
| 122 | + expect(result.isError).toBeFalsy(); |
| 123 | + expect(apiClient.patch).toHaveBeenCalledTimes(2); |
| 124 | + const urls = (apiClient.patch as any).mock.calls.map((c: any) => c[0].url); |
| 125 | + expect(urls.some((u: string) => /\/test-runs\/TR-1\/update$/.test(u))).toBe(true); |
| 126 | + expect(urls.some((u: string) => /\/test-runs\/TR-1\/test-cases$/.test(u))).toBe(true); |
| 127 | + const combined = (result.content ?? []).map((c: any) => c.text).join("\n"); |
| 128 | + expect(combined).toContain("Successfully updated test run TR-1"); |
| 129 | + expect(combined).toContain("added 1"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("reports an error if one of the two updates fails", async () => { |
| 133 | + (apiClient.patch as any) |
| 134 | + .mockResolvedValueOnce({ data: { success: true, testrun: {} } }) |
| 135 | + .mockResolvedValueOnce({ data: { success: false, error: "closed_run" } }); |
| 136 | + |
| 137 | + const result = await updateTestRun( |
| 138 | + { |
| 139 | + project_identifier: "PR-1", |
| 140 | + test_run_id: "TR-1", |
| 141 | + test_run: { |
| 142 | + run_state: "in_progress", |
| 143 | + add_test_cases: [{ test_case_ids: ["TC-1"] }], |
| 144 | + }, |
| 145 | + }, |
| 146 | + mockConfig, |
| 147 | + ); |
| 148 | + |
| 149 | + expect(result.isError).toBe(true); |
| 150 | + expect(apiClient.patch).toHaveBeenCalledTimes(2); |
| 151 | + }); |
| 152 | + |
| 153 | + it("rejects an empty update with no actionable fields", async () => { |
| 154 | + const result = await updateTestRun( |
| 155 | + { |
| 156 | + project_identifier: "PR-1", |
| 157 | + test_run_id: "TR-1", |
| 158 | + test_run: {}, |
| 159 | + }, |
| 160 | + mockConfig, |
| 161 | + ); |
| 162 | + |
| 163 | + expect(result.isError).toBe(true); |
| 164 | + expect(apiClient.patch).not.toHaveBeenCalled(); |
| 165 | + expect(result.content?.[0]?.text).toContain("Nothing to update"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("surfaces an unsuccessful membership response as an error", async () => { |
| 169 | + (apiClient.patch as any).mockResolvedValue({ |
| 170 | + data: { success: false, error: "closed_run" }, |
| 171 | + }); |
| 172 | + |
| 173 | + const result = await updateTestRun( |
| 174 | + { |
| 175 | + project_identifier: "PR-1", |
| 176 | + test_run_id: "TR-9", |
| 177 | + test_run: { add_test_cases: [{ test_case_ids: ["TC-1"] }] }, |
| 178 | + }, |
| 179 | + mockConfig, |
| 180 | + ); |
| 181 | + |
| 182 | + expect(result.isError).toBe(true); |
| 183 | + expect(result.content?.[0]?.text).toContain( |
| 184 | + "Failed to update test run test cases", |
| 185 | + ); |
| 186 | + }); |
| 187 | +}); |
0 commit comments