From e2e226e0d48abf648d346229feeb614ede80dbf8 Mon Sep 17 00:00:00 2001 From: abretonc7s Date: Wed, 15 Jan 2025 10:08:03 +0800 Subject: [PATCH] feat: unit tests --- .../services/MobilePortStream/write.test.ts | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/packages/sdk/src/services/MobilePortStream/write.test.ts b/packages/sdk/src/services/MobilePortStream/write.test.ts index ee09552f3..97676c8a5 100644 --- a/packages/sdk/src/services/MobilePortStream/write.test.ts +++ b/packages/sdk/src/services/MobilePortStream/write.test.ts @@ -1,4 +1,5 @@ import { Buffer } from 'buffer'; +import { MAX_MESSAGE_LENGTH } from '../../config'; import { write } from './write'; describe('write function', () => { @@ -77,4 +78,50 @@ describe('write function', () => { new Error('MobilePortStream - disconnected'), ); }); + + describe('Message Size Validation', () => { + beforeEach(() => { + jest.clearAllMocks(); + global.window = { + location: { href: 'http://example.com' }, + ReactNativeWebView: { postMessage: mockPostMessage }, + } as any; + }); + + it('should reject messages exceeding MAX_MESSAGE_LENGTH', () => { + const largeData = { + data: { + jsonrpc: '2.0', + method: 'test_method', + params: ['x'.repeat(MAX_MESSAGE_LENGTH)], + }, + }; + + write(largeData, 'utf-8', cb); + + expect(cb).toHaveBeenCalledWith( + expect.objectContaining({ + message: expect.stringMatching( + /Message size \d+ exceeds maximum allowed size of \d+ bytes/u, + ), + }), + ); + expect(mockPostMessage).not.toHaveBeenCalled(); + }); + + it('should accept messages within MAX_MESSAGE_LENGTH', () => { + const validData = { + data: { + jsonrpc: '2.0', + method: 'test_method', + params: ['x'.repeat(100)], + }, + }; + + write(validData, 'utf-8', cb); + + expect(cb).toHaveBeenCalledWith(); + expect(mockPostMessage).toHaveBeenCalled(); + }); + }); });