/* * This file is part of the Scandit Data Capture SDK * * Copyright (C) 2025- Scandit AG. All rights reserved. */ import { describe, it, expect, jest, beforeEach } from '@jest/globals'; import { CoreProxyAdapter } from '../../src/generated/CoreProxyAdapter'; import { CoreProxy } from '../../src/generated/CoreProxy'; import { NativeCallResult } from '../../src/common'; import { FrameSourceState } from '../../src/frame'; describe('CameraProxyAdapter', () => { let mockProxy: jest.Mocked; let adapter: CoreProxyAdapter; beforeEach(() => { mockProxy = { $executeCore: jest.fn(), } as unknown as jest.Mocked; adapter = new CoreProxyAdapter(mockProxy); }); describe('getCameraState', () => { it('should call $executeCore with correct parameters', async () => { const cameraPosition = '{"position":"worldFacing"}'; const mockResult: NativeCallResult = { data: JSON.stringify({ state: FrameSourceState.On }), }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getCameraState({ cameraPosition }); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'getCameraState', isEventRegistration: false, cameraPosition, }); expect(mockProxy.$executeCore).toHaveBeenCalledTimes(1); }); it('should parse JSON result to FrameSourceState', async () => { const mockResult: NativeCallResult = { data: JSON.stringify({ state: FrameSourceState.On }), }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getCameraState({ cameraPosition: '{"position":"worldFacing"}', }); expect(result).toEqual({ state: FrameSourceState.On }); }); it('should validate parameter is passed correctly', async () => { const cameraPosition = '{"position":"userFacing"}'; const mockResult: NativeCallResult = { data: JSON.stringify({ state: FrameSourceState.Off }), }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getCameraState({ cameraPosition }); const call = mockProxy.$executeCore.mock.calls[0][0]; expect(call.cameraPosition).toBe(cameraPosition); expect(call.methodName).toBe('getCameraState'); }); }); describe('switchCameraToDesiredState', () => { it('should call $executeCore with correct parameters', async () => { const stateJson = '{"state":"on"}'; mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.switchCameraToDesiredState({ stateJson }); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'switchCameraToDesiredState', isEventRegistration: false, stateJson, }); }); it('should return void', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); const result = await adapter.switchCameraToDesiredState({ stateJson: '{"state":"on"}', }); expect(result).toBeUndefined(); }); it('should validate stateJson parameter', async () => { const stateJson = '{"state":"off"}'; mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.switchCameraToDesiredState({ stateJson }); const call = mockProxy.$executeCore.mock.calls[0][0]; expect(call.stateJson).toBe(stateJson); }); }); describe('isTorchAvailable', () => { it('should call $executeCore with correct parameters', async () => { const cameraPosition = '{"position":"worldFacing"}'; const mockResult: NativeCallResult = { data: 'true' }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.isTorchAvailable({ cameraPosition }); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'isTorchAvailable', isEventRegistration: false, cameraPosition, }); }); it('should parse "true" string to boolean true', async () => { const mockResult: NativeCallResult = { data: 'true' }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.isTorchAvailable({ cameraPosition: '{"position":"worldFacing"}', }); expect(result).toBe(true); expect(typeof result).toBe('boolean'); }); it('should parse "false" string to boolean false', async () => { const mockResult: NativeCallResult = { data: 'false' }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.isTorchAvailable({ cameraPosition: '{"position":"userFacing"}', }); expect(result).toBe(false); expect(typeof result).toBe('boolean'); }); it('should handle any non-"true" string as false', async () => { const mockResult: NativeCallResult = { data: 'anything' }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.isTorchAvailable({ cameraPosition: '{"position":"worldFacing"}', }); expect(result).toBe(false); }); }); describe('registerFrameSourceListener', () => { it('should call $executeCore with correct method name', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.registerFrameSourceListener(); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'registerFrameSourceListener', isEventRegistration: true, }); }); it('should not pass any additional parameters', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.registerFrameSourceListener(); const call = mockProxy.$executeCore.mock.calls[0][0]; expect(Object.keys(call)).toEqual(['moduleName', 'methodName', 'isEventRegistration']); }); it('should return void', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); const result = await adapter.registerFrameSourceListener(); expect(result).toBeUndefined(); }); }); describe('unregisterFrameSourceListener', () => { it('should call $executeCore with correct method name', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.unregisterFrameSourceListener(); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'unregisterFrameSourceListener', isEventRegistration: false, }); }); it('should not pass any additional parameters', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.unregisterFrameSourceListener(); const call = mockProxy.$executeCore.mock.calls[0][0]; expect(Object.keys(call)).toEqual(['moduleName', 'methodName', 'isEventRegistration']); }); it('should return void', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); const result = await adapter.unregisterFrameSourceListener(); expect(result).toBeUndefined(); }); }); describe('getLastFrameAsJson', () => { it('should call $executeCore with correct parameters', async () => { const frameId = 'frame-123'; const mockResult: NativeCallResult = { data: '{"frameId":"frame-123","data":"..."}', }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getLastFrameAsJson({ frameId }); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'getLastFrameAsJson', isEventRegistration: false, frameId, }); }); it('should return string result directly', async () => { const expectedJson = '{"frameId":"frame-123","timestamp":1234567890}'; const mockResult: NativeCallResult = { data: expectedJson }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameAsJson({ frameId: 'frame-123' }); expect(result).toBe(expectedJson); expect(typeof result).toBe('string'); }); it('should handle special characters in frameId', async () => { const frameId = 'frame-123-abc_!@#'; const mockResult: NativeCallResult = { data: '{"frameId":"frame-123-abc_!@#"}' }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getLastFrameAsJson({ frameId }); const call = mockProxy.$executeCore.mock.calls[0][0]; expect(call.frameId).toBe(frameId); }); }); describe('getLastFrameOrNullAsJson', () => { it('should call $executeCore with correct parameters', async () => { const frameId = 'frame-456'; const mockResult: NativeCallResult = { data: '{"frameId":"frame-456"}' }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getLastFrameOrNullAsJson({ frameId }); expect(mockProxy.$executeCore).toHaveBeenCalledWith({ moduleName: 'CoreModule', methodName: 'getLastFrameOrNullAsJson', isEventRegistration: false, frameId, }); }); it('should return string result when frame exists', async () => { const expectedJson = '{"frameId":"frame-456","timestamp":1234567890}'; const mockResult: NativeCallResult = { data: expectedJson }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameOrNullAsJson({ frameId: 'frame-456' }); expect(result).toBe(expectedJson); expect(typeof result).toBe('string'); }); it('should return null when frame does not exist', async () => { const mockResult: NativeCallResult | null = null; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameOrNullAsJson({ frameId: 'non-existent' }); expect(result).toBeNull(); }); it('should handle nullable return type correctly', async () => { const mockResult: NativeCallResult | null = null; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameOrNullAsJson({ frameId: 'test' }); expect(result).toBeNull(); }); }); describe('Error Handling', () => { it('should propagate errors from proxy', async () => { const error = new Error('Native error'); mockProxy.$executeCore.mockRejectedValue(error); await expect(adapter.getCameraState({ cameraPosition: '{"position":"worldFacing"}' })).rejects.toThrow( 'Native error' ); }); it('should handle JSON parse errors in getCameraState', async () => { const mockResult: NativeCallResult = { data: 'invalid json{' }; mockProxy.$executeCore.mockResolvedValue(mockResult); await expect(adapter.getCameraState({ cameraPosition: '{"position":"worldFacing"}' })).rejects.toThrow(); }); }); describe('Type Safety', () => { it('getCameraState should return FrameSourceState type', async () => { const mockResult: NativeCallResult = { data: JSON.stringify({ state: FrameSourceState.On }), }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getCameraState({ cameraPosition: '{"position":"worldFacing"}', }); // Type assertion check - this will fail at compile time if types don't match const _typeCheck: FrameSourceState = result; expect(_typeCheck).toBeDefined(); }); it('isTorchAvailable should return boolean type', async () => { const mockResult: NativeCallResult = { data: 'true' }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.isTorchAvailable({ cameraPosition: '{"position":"worldFacing"}', }); // Type assertion check const _typeCheck: boolean = result; expect(_typeCheck).toBeDefined(); }); it('getLastFrameAsJson should return string type', async () => { const mockResult: NativeCallResult = { data: '{"test":"data"}' }; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameAsJson({ frameId: 'test' }); // Type assertion check const _typeCheck: string = result; expect(_typeCheck).toBeDefined(); }); it('getLastFrameOrNullAsJson should return string | null type', async () => { const mockResult: NativeCallResult | null = null; mockProxy.$executeCore.mockResolvedValue(mockResult); const result = await adapter.getLastFrameOrNullAsJson({ frameId: 'test' }); // Type assertion check const _typeCheck: string | null = result; expect(_typeCheck).toBeNull(); }); }); describe('Method Call Verification', () => { it('should call all methods exactly once per invocation', async () => { const mockResult: NativeCallResult = { data: 'test' }; mockProxy.$executeCore.mockResolvedValue(mockResult); await adapter.getLastFrameAsJson({ frameId: 'test' }); await adapter.getLastFrameAsJson({ frameId: 'test2' }); expect(mockProxy.$executeCore).toHaveBeenCalledTimes(2); }); it('should pass parameters in correct object structure', async () => { mockProxy.$executeCore.mockResolvedValue(undefined); await adapter.switchCameraToDesiredState({ stateJson: '{"state":"on"}' }); expect(mockProxy.$executeCore).toHaveBeenCalledWith( expect.objectContaining({ moduleName: expect.any(String), methodName: expect.any(String), stateJson: expect.any(String), }) ); }); }); });