|
| 1 | +import { client } from "./setup"; |
| 2 | + |
| 3 | +describe("Error Scope", () => { |
| 4 | + it("should capture and return error messages from popErrorScope", async () => { |
| 5 | + const result = await client.eval(({ device }) => { |
| 6 | + // Invalid WGSL shader with syntax error (missing closing parenthesis) |
| 7 | + const invalidShaderWGSL = `@fragment |
| 8 | +fn main() -> @location(0) vec4f { |
| 9 | + return vec4(1.0, 0.0, 0.0, 1.0; |
| 10 | +}`; |
| 11 | + |
| 12 | + device.pushErrorScope("validation"); |
| 13 | + |
| 14 | + // This should generate a validation error due to syntax error |
| 15 | + device.createShaderModule({ |
| 16 | + code: invalidShaderWGSL, |
| 17 | + }); |
| 18 | + |
| 19 | + return device.popErrorScope().then((error) => { |
| 20 | + if (error) { |
| 21 | + return { |
| 22 | + hasError: true, |
| 23 | + message: error.message, |
| 24 | + messageLength: error.message.length, |
| 25 | + messageNotEmpty: error.message.length > 0, |
| 26 | + messageContainsExpected: |
| 27 | + error.message.includes("expected") || |
| 28 | + error.message.includes("error") || |
| 29 | + error.message.includes("parsing"), |
| 30 | + }; |
| 31 | + } else { |
| 32 | + return { |
| 33 | + hasError: false, |
| 34 | + message: "", |
| 35 | + messageLength: 0, |
| 36 | + messageNotEmpty: false, |
| 37 | + messageContainsExpected: false, |
| 38 | + }; |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + expect(result.hasError).toBe(true); |
| 44 | + expect(result.messageNotEmpty).toBe(true); |
| 45 | + expect(result.messageLength).toBeGreaterThan(0); |
| 46 | + // The error message should contain some indication that it's a parsing error |
| 47 | + expect(result.messageContainsExpected).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return null when no error occurs", async () => { |
| 51 | + const result = await client.eval(({ device, shaders: { redFragWGSL } }) => { |
| 52 | + device.pushErrorScope("validation"); |
| 53 | + |
| 54 | + // This should not generate any errors |
| 55 | + device.createShaderModule({ |
| 56 | + code: redFragWGSL, |
| 57 | + }); |
| 58 | + |
| 59 | + return device.popErrorScope().then((error) => { |
| 60 | + return { |
| 61 | + hasError: error !== null, |
| 62 | + error: error, |
| 63 | + }; |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + expect(result.hasError).toBe(false); |
| 68 | + expect(result.error).toBe(null); |
| 69 | + }); |
| 70 | +}); |
0 commit comments