|
| 1 | +import { isSolanaError, SolanaErrorLike, SVM_SLOT_SKIPPED } from "../src/arch/svm/provider"; |
| 2 | +import { expect } from "./utils"; |
| 3 | + |
| 4 | +describe("isSolanaError type guard", () => { |
| 5 | + it("should detect a properly structured SolanaError object", () => { |
| 6 | + const error = { |
| 7 | + name: "SolanaError", |
| 8 | + context: { |
| 9 | + __code: SVM_SLOT_SKIPPED, |
| 10 | + __serverMessage: "Slot was skipped", |
| 11 | + }, |
| 12 | + }; |
| 13 | + |
| 14 | + expect(isSolanaError(error)).to.be.true; |
| 15 | + }); |
| 16 | + |
| 17 | + it("should detect a flattened/serialized SolanaError with null prototype", () => { |
| 18 | + // Create a proper SolanaError-like object |
| 19 | + const error = { |
| 20 | + name: "SolanaError", |
| 21 | + context: { |
| 22 | + __code: SVM_SLOT_SKIPPED, |
| 23 | + __serverMessage: "Slot was skipped", |
| 24 | + }, |
| 25 | + cause: undefined, |
| 26 | + }; |
| 27 | + |
| 28 | + // Simulate serialization/deserialization which creates null prototype objects |
| 29 | + const serialized = JSON.stringify(error); |
| 30 | + const flattened = JSON.parse(serialized); |
| 31 | + |
| 32 | + // Verify the flattened error has lost its prototype chain |
| 33 | + expect(Object.getPrototypeOf(flattened)).to.not.equal(Error.prototype); |
| 34 | + |
| 35 | + // The type guard should still detect it as a SolanaError |
| 36 | + expect(isSolanaError(flattened)).to.be.true; |
| 37 | + }); |
| 38 | + |
| 39 | + it("should provide proper type inference for detected errors", () => { |
| 40 | + const error = { |
| 41 | + name: "SolanaError", |
| 42 | + context: { |
| 43 | + __code: -32009, |
| 44 | + __serverMessage: "Test error", |
| 45 | + statusCode: 429, |
| 46 | + }, |
| 47 | + }; |
| 48 | + |
| 49 | + if (isSolanaError(error)) { |
| 50 | + // These should all be accessible without type assertions |
| 51 | + const code: number = error.context.__code; |
| 52 | + const message: string | undefined = error.context.__serverMessage; |
| 53 | + const status: number | undefined = error.context.statusCode; |
| 54 | + |
| 55 | + expect(code).to.equal(-32009); |
| 56 | + expect(message).to.equal("Test error"); |
| 57 | + expect(status).to.equal(429); |
| 58 | + } else { |
| 59 | + throw new Error("Error should have been detected as SolanaError"); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it("should reject objects missing the required name field", () => { |
| 64 | + const error = { |
| 65 | + context: { |
| 66 | + __code: SVM_SLOT_SKIPPED, |
| 67 | + }, |
| 68 | + }; |
| 69 | + |
| 70 | + expect(isSolanaError(error)).to.be.false; |
| 71 | + }); |
| 72 | + |
| 73 | + it("should reject objects missing the required __code field", () => { |
| 74 | + const error = { |
| 75 | + name: "SolanaError", |
| 76 | + context: {}, |
| 77 | + }; |
| 78 | + |
| 79 | + expect(isSolanaError(error)).to.be.false; |
| 80 | + }); |
| 81 | + |
| 82 | + it("should reject objects with incorrect field types", () => { |
| 83 | + const error = { |
| 84 | + name: "SolanaError", |
| 85 | + context: { |
| 86 | + __code: "not a number", // Should be number |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + expect(isSolanaError(error)).to.be.false; |
| 91 | + }); |
| 92 | + |
| 93 | + it("should reject non-SolanaError objects", () => { |
| 94 | + const regularError = new Error("Regular error"); |
| 95 | + expect(isSolanaError(regularError)).to.be.false; |
| 96 | + |
| 97 | + const randomObject = { foo: "bar" }; |
| 98 | + expect(isSolanaError(randomObject)).to.be.false; |
| 99 | + |
| 100 | + expect(isSolanaError(null)).to.be.false; |
| 101 | + expect(isSolanaError(undefined)).to.be.false; |
| 102 | + expect(isSolanaError("string")).to.be.false; |
| 103 | + expect(isSolanaError(123)).to.be.false; |
| 104 | + }); |
| 105 | + |
| 106 | + it("should allow additional properties in context object", () => { |
| 107 | + const error = { |
| 108 | + name: "SolanaError", |
| 109 | + context: { |
| 110 | + __code: SVM_SLOT_SKIPPED, |
| 111 | + __serverMessage: "Slot was skipped", |
| 112 | + statusCode: 500, |
| 113 | + customField: "custom value", |
| 114 | + anotherField: { nested: "object" }, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + expect(isSolanaError(error)).to.be.true; |
| 119 | + }); |
| 120 | + |
| 121 | + it("should handle errors with cause property", () => { |
| 122 | + const innerError = new Error("Inner error"); |
| 123 | + const error = { |
| 124 | + name: "SolanaError", |
| 125 | + context: { |
| 126 | + __code: SVM_SLOT_SKIPPED, |
| 127 | + }, |
| 128 | + cause: innerError, |
| 129 | + }; |
| 130 | + |
| 131 | + expect(isSolanaError(error)).to.be.true; |
| 132 | + |
| 133 | + if (isSolanaError(error)) { |
| 134 | + expect(error.cause).to.equal(innerError); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it("should detect errors that have been serialized and deserialized multiple times", () => { |
| 139 | + const originalError: SolanaErrorLike = { |
| 140 | + name: "SolanaError", |
| 141 | + context: { |
| 142 | + __code: -32009, |
| 143 | + __serverMessage: "Block not available", |
| 144 | + statusCode: 500, |
| 145 | + }, |
| 146 | + }; |
| 147 | + |
| 148 | + // Serialize and deserialize multiple times |
| 149 | + let error: unknown = originalError; |
| 150 | + for (let i = 0; i < 3; i++) { |
| 151 | + error = JSON.parse(JSON.stringify(error)); |
| 152 | + } |
| 153 | + |
| 154 | + // Should still be detected as a SolanaError |
| 155 | + expect(isSolanaError(error)).to.be.true; |
| 156 | + |
| 157 | + if (isSolanaError(error)) { |
| 158 | + expect(error.context.__code).to.equal(-32009); |
| 159 | + expect(error.context.__serverMessage).to.equal("Block not available"); |
| 160 | + expect(error.context.statusCode).to.equal(500); |
| 161 | + } |
| 162 | + }); |
| 163 | +}); |
0 commit comments