|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Regression tests for verifier-bridge. |
| 5 | + * |
| 6 | + * These test the REAL verifier-bridge source against a mocked WASM module, |
| 7 | + * unlike verifier-bridge.test.ts which mocks the entire bridge. |
| 8 | + * |
| 9 | + * Covers: |
| 10 | + * - Bug #1: loadWasm crashes when WASM module has no .default() export |
| 11 | + * (auto-initialized by vite-plugin-wasm) |
| 12 | + * - Bug #2: WASM functions return Promises but were not awaited, |
| 13 | + * causing JSON.parse(Promise) → SyntaxError |
| 14 | + */ |
| 15 | + |
| 16 | +const mockVerifyAttestationWithResult = vi.fn(); |
| 17 | +const mockVerifyChainJson = vi.fn(); |
| 18 | + |
| 19 | +// Mock the underlying WASM module — NOT the bridge itself. |
| 20 | +// This exercises the real verifier-bridge logic. |
| 21 | +vi.mock('auths-verifier-wasm', () => ({ |
| 22 | + // Explicitly set default to undefined — simulates vite-plugin-wasm |
| 23 | + // auto-initialized module that has no init function. |
| 24 | + default: undefined, |
| 25 | + verifyAttestationWithResult: mockVerifyAttestationWithResult, |
| 26 | + verifyChainJson: mockVerifyChainJson, |
| 27 | +})); |
| 28 | + |
| 29 | +// Import the REAL verifier-bridge (it will dynamically import our mock above) |
| 30 | +import { ensureInit, verifyAttestation, verifyChain } from '../src/verifier-bridge'; |
| 31 | + |
| 32 | +describe('verifier-bridge regressions', () => { |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('Bug #1: WASM module without .default export', () => { |
| 38 | + it('ensureInit succeeds when module has no .default()', async () => { |
| 39 | + // vite-plugin-wasm auto-initializes WASM and produces a module |
| 40 | + // without .default(). Before the fix, this threw: |
| 41 | + // TypeError: wasm.default is not a function |
| 42 | + await expect(ensureInit()).resolves.toBeUndefined(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('Bug #2: async WASM functions must be awaited', () => { |
| 47 | + it('verifyChain awaits a Promise-returning verifyChainJson', async () => { |
| 48 | + // WASM functions compiled with async Rust return Promises via externref. |
| 49 | + // Before the fix, the Promise was passed directly to JSON.parse(), |
| 50 | + // which stringified it to "[object Promise]" and threw SyntaxError. |
| 51 | + mockVerifyChainJson.mockResolvedValue( |
| 52 | + JSON.stringify({ |
| 53 | + status: { type: 'Valid' }, |
| 54 | + chain: [{ issuer: 'did:keri:a', subject: 'did:key:b', valid: true }], |
| 55 | + warnings: [], |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + const report = await verifyChain([{ test: true }], 'aabbccdd'); |
| 60 | + expect(report.status.type).toBe('Valid'); |
| 61 | + expect(report.chain).toHaveLength(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it('verifyAttestation awaits a Promise-returning verifyAttestationWithResult', async () => { |
| 65 | + mockVerifyAttestationWithResult.mockResolvedValue( |
| 66 | + JSON.stringify({ valid: true }), |
| 67 | + ); |
| 68 | + |
| 69 | + const result = await verifyAttestation('{"test":true}', 'aabbccdd'); |
| 70 | + expect(result.valid).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('verifyChain does not produce BrokenChain from un-awaited Promise', async () => { |
| 74 | + // The specific symptom: JSON.parse(Promise) throws, catch block returns |
| 75 | + // { status: { type: 'BrokenChain' } }. If this test gets BrokenChain |
| 76 | + // with a valid mock, the await is missing. |
| 77 | + mockVerifyChainJson.mockResolvedValue( |
| 78 | + JSON.stringify({ |
| 79 | + status: { type: 'Valid' }, |
| 80 | + chain: [], |
| 81 | + warnings: [], |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + const report = await verifyChain([], 'aabb'); |
| 86 | + expect(report.status.type).not.toBe('BrokenChain'); |
| 87 | + expect(report.status.type).toBe('Valid'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('verifyChain still handles sync return values', async () => { |
| 91 | + // If WASM functions return a plain string (not a Promise), |
| 92 | + // await on a non-thenable just passes it through. |
| 93 | + mockVerifyChainJson.mockReturnValue( |
| 94 | + JSON.stringify({ |
| 95 | + status: { type: 'Valid' }, |
| 96 | + chain: [], |
| 97 | + warnings: [], |
| 98 | + }), |
| 99 | + ); |
| 100 | + |
| 101 | + const report = await verifyChain([], 'aabb'); |
| 102 | + expect(report.status.type).toBe('Valid'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('verifyAttestation still handles sync return values', async () => { |
| 106 | + mockVerifyAttestationWithResult.mockReturnValue( |
| 107 | + JSON.stringify({ valid: true }), |
| 108 | + ); |
| 109 | + |
| 110 | + const result = await verifyAttestation('{"test":true}', 'aabb'); |
| 111 | + expect(result.valid).toBe(true); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments