|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { |
| 4 | + ChangeCustomerArgumentsSchema, |
| 5 | + handleChangeCustomerRequest, |
| 6 | +} from "../changeCustomer.js"; |
| 7 | +import { |
| 8 | + createErrorResponse, |
| 9 | + createSuccessResponse, |
| 10 | + formatZodError, |
| 11 | + handleGeneralError, |
| 12 | +} from "../../utils/util.js"; |
| 13 | + |
| 14 | +// Mock the utility functions |
| 15 | +vi.mock("../../utils/util.js", () => ({ |
| 16 | + createErrorResponse: vi.fn((message) => ({ |
| 17 | + content: [{ type: "text", text: message }], |
| 18 | + })), |
| 19 | + createSuccessResponse: vi.fn((text) => ({ |
| 20 | + content: [{ type: "text", text }], |
| 21 | + })), |
| 22 | + formatZodError: vi.fn((error) => `Validation error: ${error.message}`), |
| 23 | + handleGeneralError: vi.fn((error, context) => ({ |
| 24 | + content: [{ type: "text", text: `Error in ${context}: ${error.message}` }], |
| 25 | + })), |
| 26 | +})); |
| 27 | + |
| 28 | +describe("changeCustomer", () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + describe("ChangeCustomerArgumentsSchema", () => { |
| 34 | + it("should validate valid arguments", () => { |
| 35 | + const validArgs = { |
| 36 | + customerContext: "new-customer-123", |
| 37 | + }; |
| 38 | + |
| 39 | + const result = ChangeCustomerArgumentsSchema.parse(validArgs); |
| 40 | + expect(result).toEqual(validArgs); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should reject invalid arguments", () => { |
| 44 | + const invalidArgs = { |
| 45 | + // missing customerContext |
| 46 | + }; |
| 47 | + |
| 48 | + expect(() => ChangeCustomerArgumentsSchema.parse(invalidArgs)).toThrow(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should reject non-string customerContext", () => { |
| 52 | + const invalidArgs = { |
| 53 | + customerContext: 123, |
| 54 | + }; |
| 55 | + |
| 56 | + expect(() => ChangeCustomerArgumentsSchema.parse(invalidArgs)).toThrow(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("handleChangeCustomerRequest", () => { |
| 61 | + it("should successfully change customer context", async () => { |
| 62 | + const args = { |
| 63 | + customerContext: "old-customer", |
| 64 | + }; |
| 65 | + const newContext = "new-customer-123"; |
| 66 | + const token = "mock-token"; |
| 67 | + const updateCallback = vi.fn(); |
| 68 | + |
| 69 | + const result = await handleChangeCustomerRequest( |
| 70 | + { ...args, customerContext: newContext }, |
| 71 | + token, |
| 72 | + updateCallback |
| 73 | + ); |
| 74 | + |
| 75 | + expect(updateCallback).toHaveBeenCalledWith(newContext); |
| 76 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 77 | + "Customer context successfully changed from 'old-customer' to 'new-customer-123'" |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should handle missing previous context", async () => { |
| 82 | + const args = {}; |
| 83 | + const newContext = "new-customer-123"; |
| 84 | + const token = "mock-token"; |
| 85 | + const updateCallback = vi.fn(); |
| 86 | + |
| 87 | + const result = await handleChangeCustomerRequest( |
| 88 | + { ...args, customerContext: newContext }, |
| 89 | + token, |
| 90 | + updateCallback |
| 91 | + ); |
| 92 | + |
| 93 | + expect(updateCallback).toHaveBeenCalledWith(newContext); |
| 94 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 95 | + "Customer context successfully changed to 'new-customer-123'" |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should work without update callback", async () => { |
| 100 | + const args = { |
| 101 | + customerContext: "old-customer", |
| 102 | + }; |
| 103 | + const newContext = "new-customer-123"; |
| 104 | + const token = "mock-token"; |
| 105 | + |
| 106 | + const result = await handleChangeCustomerRequest( |
| 107 | + { ...args, customerContext: newContext }, |
| 108 | + token |
| 109 | + ); |
| 110 | + |
| 111 | + expect(createSuccessResponse).toHaveBeenCalledWith( |
| 112 | + "Customer context successfully changed from 'old-customer' to 'new-customer-123'" |
| 113 | + ); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should handle validation errors", async () => { |
| 117 | + const invalidArgs = { |
| 118 | + customerContext: 123, // invalid type |
| 119 | + }; |
| 120 | + const token = "mock-token"; |
| 121 | + |
| 122 | + const result = await handleChangeCustomerRequest(invalidArgs, token); |
| 123 | + |
| 124 | + expect(createErrorResponse).toHaveBeenCalled(); |
| 125 | + expect(formatZodError).toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should handle general errors", async () => { |
| 129 | + // Mock ChangeCustomerArgumentsSchema.parse to throw a non-Zod error |
| 130 | + const originalParse = ChangeCustomerArgumentsSchema.parse; |
| 131 | + vi.spyOn(ChangeCustomerArgumentsSchema, "parse").mockImplementation( |
| 132 | + () => { |
| 133 | + throw new Error("Unexpected error"); |
| 134 | + } |
| 135 | + ); |
| 136 | + |
| 137 | + const args = { |
| 138 | + customerContext: "new-customer-123", |
| 139 | + }; |
| 140 | + const token = "mock-token"; |
| 141 | + |
| 142 | + const result = await handleChangeCustomerRequest(args, token); |
| 143 | + |
| 144 | + expect(handleGeneralError).toHaveBeenCalledWith( |
| 145 | + expect.any(Error), |
| 146 | + "handling change customer request" |
| 147 | + ); |
| 148 | + |
| 149 | + // Restore original implementation |
| 150 | + ChangeCustomerArgumentsSchema.parse = originalParse; |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments