|
| 1 | +/** Tests the Feedback category boundary and resolution output contract. */ |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 4 | +import { resolveCommand } from "../../../src/commands/feedback/resolve.js"; |
| 5 | +import { ApiError } from "../../../src/lib/errors.js"; |
| 6 | +import { resetCacheState } from "../../../src/lib/response-cache.js"; |
| 7 | +import type { SentryFeedback } from "../../../src/types/index.js"; |
| 8 | + |
| 9 | +vi.mock("../../../src/commands/issue/utils.js", async (importOriginal) => { |
| 10 | + const actual = |
| 11 | + await importOriginal< |
| 12 | + typeof import("../../../src/commands/issue/utils.js") |
| 13 | + >(); |
| 14 | + return { ...actual, resolveIssue: vi.fn() }; |
| 15 | +}); |
| 16 | + |
| 17 | +vi.mock("../../../src/lib/api-client.js", async (importOriginal) => { |
| 18 | + const actual = |
| 19 | + await importOriginal<typeof import("../../../src/lib/api-client.js")>(); |
| 20 | + return { ...actual, updateIssueStatus: vi.fn() }; |
| 21 | +}); |
| 22 | + |
| 23 | +import { resolveIssue } from "../../../src/commands/issue/utils.js"; |
| 24 | +import { updateIssueStatus } from "../../../src/lib/api-client.js"; |
| 25 | + |
| 26 | +function feedback(): SentryFeedback { |
| 27 | + return { |
| 28 | + id: "123", |
| 29 | + shortId: "TEST-PROJECT-1A", |
| 30 | + title: "User Feedback", |
| 31 | + issueCategory: "feedback", |
| 32 | + issueType: "feedback", |
| 33 | + status: "unresolved", |
| 34 | + metadata: { message: "Checkout is broken" }, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function createMockContext() { |
| 39 | + const stdoutWrite = vi.fn(() => true); |
| 40 | + return { |
| 41 | + context: { |
| 42 | + stdout: { write: stdoutWrite }, |
| 43 | + stderr: { write: vi.fn(() => true) }, |
| 44 | + cwd: "/tmp", |
| 45 | + }, |
| 46 | + output: () => stdoutWrite.mock.calls.map((call) => call[0]).join(""), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +describe("feedback resolve", () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.mocked(resolveIssue).mockResolvedValue({ |
| 53 | + org: "test-org", |
| 54 | + issue: feedback(), |
| 55 | + }); |
| 56 | + vi.mocked(updateIssueStatus).mockResolvedValue({ |
| 57 | + ...feedback(), |
| 58 | + status: "resolved", |
| 59 | + metadata: { message: "Updated by the server" }, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + vi.resetAllMocks(); |
| 65 | + resetCacheState(); |
| 66 | + }); |
| 67 | + |
| 68 | + test("resolves the checked Feedback and emits the server response as JSON", async () => { |
| 69 | + const { context, output } = createMockContext(); |
| 70 | + const func = await resolveCommand.loader(); |
| 71 | + await func.call(context, { json: true }, "TEST-PROJECT-1A"); |
| 72 | + |
| 73 | + expect(resolveIssue).toHaveBeenCalledWith({ |
| 74 | + issueArg: "TEST-PROJECT-1A", |
| 75 | + cwd: "/tmp", |
| 76 | + command: "resolve", |
| 77 | + commandBase: "sentry feedback", |
| 78 | + }); |
| 79 | + expect(updateIssueStatus).toHaveBeenCalledExactlyOnceWith( |
| 80 | + "123", |
| 81 | + "resolved", |
| 82 | + { orgSlug: "test-org" } |
| 83 | + ); |
| 84 | + expect(JSON.parse(output())).toEqual({ |
| 85 | + ...feedback(), |
| 86 | + status: "resolved", |
| 87 | + metadata: { message: "Updated by the server" }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + test("rejects an ordinary issue before any status mutation", async () => { |
| 92 | + vi.mocked(resolveIssue).mockResolvedValue({ |
| 93 | + org: "test-org", |
| 94 | + issue: { ...feedback(), issueCategory: "error", issueType: "error" }, |
| 95 | + }); |
| 96 | + const { context, output } = createMockContext(); |
| 97 | + const func = await resolveCommand.loader(); |
| 98 | + |
| 99 | + await expect( |
| 100 | + func.call(context, { json: false }, "TEST-PROJECT-1A") |
| 101 | + ).rejects.toMatchObject({ |
| 102 | + name: "ResolutionError", |
| 103 | + hint: "sentry issue view test-org/TEST-PROJECT-1A", |
| 104 | + }); |
| 105 | + expect(updateIssueStatus).not.toHaveBeenCalled(); |
| 106 | + expect(output()).toBe(""); |
| 107 | + }); |
| 108 | + |
| 109 | + test("does not report success when the update fails", async () => { |
| 110 | + const error = new ApiError("Permission denied", 403); |
| 111 | + vi.mocked(updateIssueStatus).mockRejectedValue(error); |
| 112 | + const { context, output } = createMockContext(); |
| 113 | + const func = await resolveCommand.loader(); |
| 114 | + |
| 115 | + await expect( |
| 116 | + func.call(context, { json: false }, "TEST-PROJECT-1A") |
| 117 | + ).rejects.toBe(error); |
| 118 | + expect(output()).toBe(""); |
| 119 | + }); |
| 120 | +}); |
0 commit comments