import { describe, it, expect } from 'vitest' import * as Comlink from 'comlink' import { MessageChannel } from 'node:worker_threads' /** * Real (unmocked) Comlink round-trip for the bip122 wire shape. * * Every other BridgeChild/BridgeParent test mocks `comlink` entirely * (`proxy: obj => obj`, `wrap: () => mockObject`), which never exercises * actual `postMessage` serialization — so a design that "looks right" against * those mocks can still throw in a real browser. This file uses a real * MessageChannel + real Comlink to prove the specific pattern * `tryMarshalBitcoin`/`getBitcoinWallets` rely on: a `Comlink.proxy()`-marked * feature method NESTED inside a larger plain `wallet` object is only * transported correctly if the CHILD resolves that feature as its OWN leaf * GET — not by awaiting the whole `wallet` object in one shot (Comlink checks * the proxy marker once, on the exact value a GET resolves to; nested markers * are invisible to that check, and native structured clone then throws on the * function they wrap). */ describe('bip122 wire shape — real Comlink (no mocks)', () => { const buildParentAPI = () => { const connectFeature = { connect: async (input: unknown) => ({ accounts: [{ address: 'bc1qtest', input }] }) } return { bip122Wallets: [ { uuid: 'mm-1', name: 'MetaMask', features: ['bitcoin:connect'], wallet: { name: 'MetaMask', accounts: [] as unknown[], features: { 'bitcoin:connect': Comlink.proxy({ connect: async (input: unknown) => await connectFeature.connect(input) }) } } } ] } } // Loosely typed on purpose: this test drives the wire protocol directly // (bypassing the strongly-typed ParentAPI interfaces), so `any` here mirrors // how the rest of this test suite treats mocked Comlink remotes. const wrapOverRealChannel = (): { remote: any cleanup: () => void } => { const { port1, port2 } = new MessageChannel() Comlink.expose(buildParentAPI(), port1 as any) const remote = Comlink.wrap(port2 as any) return { remote, cleanup: () => (port1.close(), port2.close()) } } it('throws when the whole wallet object is awaited in one GET (the bug this test guards against)', async () => { const { remote, cleanup } = wrapOverRealChannel() await expect(remote.bip122Wallets[0].wallet).rejects.toThrow() cleanup() }) it('resolves the wallet by GETting each feature individually — the pattern tryMarshalBitcoin uses', async () => { const { remote, cleanup } = wrapOverRealChannel() const entry = remote.bip122Wallets[0] const [uuid, name, features, walletName, accounts, connectFeature] = await Promise.all([ entry.uuid, entry.name, entry.features, entry.wallet.name, entry.wallet.accounts, entry.wallet.features['bitcoin:connect'] ]) expect(uuid).toBe('mm-1') expect(name).toBe('MetaMask') expect(features).toEqual(['bitcoin:connect']) expect(walletName).toBe('MetaMask') expect(accounts).toEqual([]) // The resolved feature must be a live callable proxy, not cloned data — // calling it should round-trip to the REAL parent-side implementation. expect(connectFeature).toBeDefined() const result = await connectFeature.connect({ purposes: ['payment'] }) expect(result).toEqual({ accounts: [{ address: 'bc1qtest', input: { purposes: ['payment'] } }] }) cleanup() }) it('resolves an unsupported feature to undefined rather than a dangling proxy', async () => { const { remote, cleanup } = wrapOverRealChannel() const signMessageFeature = await remote.bip122Wallets[0].wallet.features['bitcoin:signMessage'] expect(signMessageFeature).toBeUndefined() cleanup() }) })