|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + errorMessage, |
| 4 | + getAblyErrorCode, |
| 5 | + enhanceErrorMessage, |
| 6 | +} from "../../../src/utils/errors.js"; |
| 7 | + |
| 8 | +describe("errorMessage", () => { |
| 9 | + it("should extract message from Error object", () => { |
| 10 | + const error = new Error("Test error message"); |
| 11 | + expect(errorMessage(error)).toBe("Test error message"); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should convert non-Error to string", () => { |
| 15 | + expect(errorMessage("string error")).toBe("string error"); |
| 16 | + expect(errorMessage(123)).toBe("123"); |
| 17 | + expect(errorMessage(null)).toBe("null"); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should handle undefined", () => { |
| 21 | + let undefinedValue: unknown; |
| 22 | + expect(errorMessage(undefinedValue)).toBe("undefined"); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("getAblyErrorCode", () => { |
| 27 | + it("should extract code from Ably-style error object", () => { |
| 28 | + const error = { code: 93002, message: "Test error" }; |
| 29 | + expect(getAblyErrorCode(error)).toBe(93002); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return undefined for Error without code", () => { |
| 33 | + const error = new Error("Test error"); |
| 34 | + expect(getAblyErrorCode(error)).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should return undefined for non-object", () => { |
| 38 | + expect(getAblyErrorCode("string")).toBeUndefined(); |
| 39 | + expect(getAblyErrorCode(123)).toBeUndefined(); |
| 40 | + expect(getAblyErrorCode(null)).toBeUndefined(); |
| 41 | + let undefinedValue: unknown; |
| 42 | + expect(getAblyErrorCode(undefinedValue)).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should return undefined for object with non-numeric code", () => { |
| 46 | + const error = { code: "not-a-number", message: "Test error" }; |
| 47 | + expect(getAblyErrorCode(error)).toBeUndefined(); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe("enhanceErrorMessage", () => { |
| 52 | + describe("error code 93002 (mutable messages not enabled)", () => { |
| 53 | + it("should enhance error message with channel rule hint", () => { |
| 54 | + const error = { code: 93002, message: "Mutable messages not enabled" }; |
| 55 | + const baseMessage = "Mutable messages not enabled"; |
| 56 | + const enhanced = enhanceErrorMessage(error, baseMessage); |
| 57 | + |
| 58 | + expect(enhanced).toContain(baseMessage); |
| 59 | + expect(enhanced).toContain("Hint:"); |
| 60 | + expect(enhanced).toContain( |
| 61 | + "Message annotations, updates, deletes, and appends", |
| 62 | + ); |
| 63 | + expect(enhanced).toContain("Channel rules"); |
| 64 | + expect(enhanced).toContain( |
| 65 | + "https://ably.com/docs/messages/annotations#enable-annotations", |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should include step-by-step instructions", () => { |
| 70 | + const error = { code: 93002, message: "Test" }; |
| 71 | + const enhanced = enhanceErrorMessage(error, "Test"); |
| 72 | + |
| 73 | + expect(enhanced).toContain("Settings tab"); |
| 74 | + expect(enhanced).toContain("Add new rule"); |
| 75 | + expect(enhanced).toContain("Create channel rule"); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe("unknown error codes", () => { |
| 80 | + it("should return original message for unknown error codes", () => { |
| 81 | + const error = { code: 99999, message: "Unknown error" }; |
| 82 | + const baseMessage = "Unknown error"; |
| 83 | + const enhanced = enhanceErrorMessage(error, baseMessage); |
| 84 | + |
| 85 | + expect(enhanced).toBe(baseMessage); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return original message for errors without code", () => { |
| 89 | + const error = new Error("Regular error"); |
| 90 | + const baseMessage = "Regular error"; |
| 91 | + const enhanced = enhanceErrorMessage(error, baseMessage); |
| 92 | + |
| 93 | + expect(enhanced).toBe(baseMessage); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should return original message for non-object errors", () => { |
| 97 | + const enhanced = enhanceErrorMessage("string error", "string error"); |
| 98 | + expect(enhanced).toBe("string error"); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments