-
Notifications
You must be signed in to change notification settings - Fork 188
Add proving machine tests #749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
transphorm
merged 5 commits into
dev
from
codex/add-expert-level-tests-for-provingmachine.ts-and-provingutil
Jul 7, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
|
|
||
| import { jest } from '@jest/globals'; | ||
|
|
||
| // Minimal actor stub used to observe send calls and emit state transitions | ||
|
|
||
| export const actorMock = { | ||
| start: jest.fn(), | ||
| stop: jest.fn(), | ||
| send: jest.fn(), | ||
| subscribe: jest.fn((cb: (state: any) => void) => { | ||
| (actorMock as any)._callback = cb; | ||
| return { unsubscribe: jest.fn() }; | ||
| }), | ||
| }; | ||
|
|
||
| export function emitState(stateValue: string) { | ||
| const cb = (actorMock as any)._callback; | ||
| if (cb) { | ||
| cb({ value: stateValue, matches: (v: string) => v === stateValue }); | ||
| } | ||
| } |
224 changes: 224 additions & 0 deletions
224
app/tests/utils/proving/provingMachine.generatePayload.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
|
|
||
| import { jest } from '@jest/globals'; | ||
|
|
||
| import { useProtocolStore } from '../../../src/stores/protocolStore'; | ||
| import { useSelfAppStore } from '../../../src/stores/selfAppStore'; | ||
| import { useProvingStore } from '../../../src/utils/proving/provingMachine'; | ||
|
|
||
| jest.mock('xstate', () => { | ||
| const actual = jest.requireActual('xstate') as any; | ||
| const { actorMock } = require('./actorMock'); | ||
| return { ...actual, createActor: jest.fn(() => actorMock) }; | ||
| }); | ||
|
|
||
| jest.mock('../../../src/utils/analytics', () => () => ({ | ||
| trackEvent: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@selfxyz/common', () => { | ||
| const actual = jest.requireActual('@selfxyz/common') as any; | ||
| return { | ||
| ...actual, | ||
| getSolidityPackedUserContextData: jest.fn(() => '0x1234'), | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock('../../../src/utils/proving/provingInputs', () => ({ | ||
| generateTEEInputsRegister: jest.fn(() => ({ | ||
| inputs: { r: 1 }, | ||
| circuitName: 'reg', | ||
| endpointType: 'celo', | ||
| endpoint: 'https://reg', | ||
| })), | ||
| generateTEEInputsDSC: jest.fn(() => ({ | ||
| inputs: { d: 1 }, | ||
| circuitName: 'dsc', | ||
| endpointType: 'celo', | ||
| endpoint: 'https://dsc', | ||
| })), | ||
| generateTEEInputsDisclose: jest.fn(() => ({ | ||
| inputs: { s: 1 }, | ||
| circuitName: 'vc_and_disclose', | ||
| endpointType: 'https', | ||
| endpoint: 'https://dis', | ||
| })), | ||
| })); | ||
|
|
||
| jest.mock('../../../src/utils/proving/provingUtils', () => { | ||
| const actual = jest.requireActual( | ||
| '../../../src/utils/proving/provingUtils', | ||
| ) as any; | ||
| return { | ||
| ...actual, | ||
| getPayload: jest.fn(() => ({ mocked: true })), | ||
| encryptAES256GCM: jest.fn(() => ({ | ||
| nonce: [0], | ||
| cipher_text: [1], | ||
| auth_tag: [2], | ||
| })), | ||
| }; | ||
| }); | ||
|
|
||
| const { | ||
| getPayload, | ||
| encryptAES256GCM, | ||
| } = require('../../../src/utils/proving/provingUtils'); | ||
| const { | ||
| generateTEEInputsRegister, | ||
| generateTEEInputsDSC, | ||
| generateTEEInputsDisclose, | ||
| } = require('../../../src/utils/proving/provingInputs'); | ||
|
|
||
| function setupDefaultStores() { | ||
| useProvingStore.setState({ | ||
| circuitType: 'register', | ||
| passportData: { documentCategory: 'passport', mock: false }, | ||
| secret: 'sec', | ||
| uuid: '123', | ||
| sharedKey: Buffer.alloc(32, 1), | ||
| env: 'prod', | ||
| }); | ||
|
|
||
| useSelfAppStore.setState({ | ||
| selfApp: createMockSelfApp(), | ||
| }); | ||
|
|
||
| useProtocolStore.setState({ | ||
| passport: createMockProtocolState(), | ||
| id_card: createMockProtocolState(), | ||
| }); | ||
| } | ||
|
|
||
| function createMockSelfApp() { | ||
| return { | ||
| chainID: 42220 as const, | ||
| userId: 'u', | ||
| userDefinedData: '0x0', | ||
| endpointType: 'https' as const, | ||
| endpoint: 'https://e', | ||
| scope: 's', | ||
| sessionId: '', | ||
| appName: '', | ||
| logoBase64: '', | ||
| header: '', | ||
| userIdType: 'uuid' as const, | ||
| devMode: false, | ||
| disclosures: {}, | ||
| version: 1, | ||
| }; | ||
| } | ||
|
|
||
| function createMockProtocolState() { | ||
| return { | ||
| dsc_tree: 'tree', | ||
| csca_tree: [['a']], | ||
| commitment_tree: null, | ||
| deployed_circuits: null, | ||
| circuits_dns_mapping: null, | ||
| alternative_csca: {}, | ||
| fetch_deployed_circuits: jest.fn(() => Promise.resolve()), | ||
| fetch_circuits_dns_mapping: jest.fn(() => Promise.resolve()), | ||
| fetch_csca_tree: jest.fn(() => Promise.resolve()), | ||
| fetch_dsc_tree: jest.fn(() => Promise.resolve()), | ||
| fetch_identity_tree: jest.fn(() => Promise.resolve()), | ||
| fetch_alternative_csca: jest.fn(() => Promise.resolve()), | ||
| fetch_all: jest.fn(() => Promise.resolve()), | ||
| }; | ||
| } | ||
|
|
||
| describe('_generatePayload', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| setupDefaultStores(); | ||
| }); | ||
|
|
||
| it('register circuit', async () => { | ||
| useProvingStore.setState({ circuitType: 'register' }); | ||
| const payload = await useProvingStore.getState()._generatePayload(); | ||
| expect(generateTEEInputsRegister).toHaveBeenCalled(); | ||
| expect(getPayload).toHaveBeenCalled(); | ||
| expect(encryptAES256GCM).toHaveBeenCalled(); | ||
| expect(useProvingStore.getState().endpointType).toBe('celo'); | ||
| expect(payload.params).toEqual({ | ||
| uuid: '123', | ||
| nonce: [0], | ||
| cipher_text: [1], | ||
| auth_tag: [2], | ||
| }); | ||
| }); | ||
|
|
||
| it('dsc circuit', async () => { | ||
| useProvingStore.setState({ circuitType: 'dsc' }); | ||
| const payload = await useProvingStore.getState()._generatePayload(); | ||
| expect(generateTEEInputsDSC).toHaveBeenCalled(); | ||
| expect(generateTEEInputsDSC).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| documentCategory: 'passport', | ||
| mock: false, | ||
| }), | ||
| [['a']], // csca_tree | ||
| 'prod', // env | ||
| ); | ||
| expect(useProvingStore.getState().endpointType).toBe('celo'); | ||
| expect(payload.params.uuid).toBe('123'); | ||
| expect(payload.params).toEqual({ | ||
| uuid: '123', | ||
| nonce: [0], | ||
| cipher_text: [1], | ||
| auth_tag: [2], | ||
| }); | ||
| }); | ||
|
|
||
| it('disclose circuit', async () => { | ||
| useProvingStore.setState({ circuitType: 'disclose' }); | ||
| const payload = await useProvingStore.getState()._generatePayload(); | ||
| expect(generateTEEInputsDisclose).toHaveBeenCalled(); | ||
| expect(useProvingStore.getState().endpointType).toBe('https'); | ||
| expect(payload.params.uuid).toBe('123'); | ||
| }); | ||
|
|
||
| it('handles missing passport data', async () => { | ||
| useProvingStore.setState({ passportData: null }); | ||
| await expect( | ||
| useProvingStore.getState()._generatePayload(), | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('handles input generation failure', async () => { | ||
| // Reset all mocks first | ||
| jest.clearAllMocks(); | ||
| setupDefaultStores(); | ||
|
|
||
| generateTEEInputsRegister.mockImplementation(() => { | ||
| throw new Error('Input generation failed'); | ||
| }); | ||
| await expect(useProvingStore.getState()._generatePayload()).rejects.toThrow( | ||
| 'Input generation failed', | ||
| ); | ||
|
|
||
| // Restore the mock to its original implementation | ||
| generateTEEInputsRegister.mockRestore(); | ||
| }); | ||
|
|
||
| it('handles encryption failure', async () => { | ||
| // Reset all mocks first | ||
| jest.clearAllMocks(); | ||
| setupDefaultStores(); | ||
|
|
||
| // Restore all original implementations first | ||
| generateTEEInputsRegister.mockImplementation(() => ({ | ||
| inputs: { r: 1 }, | ||
| circuitName: 'reg', | ||
| endpointType: 'celo', | ||
| endpoint: 'https://reg', | ||
| })); | ||
|
|
||
| encryptAES256GCM.mockImplementation(() => { | ||
| throw new Error('Encryption failed'); | ||
| }); | ||
| await expect(useProvingStore.getState()._generatePayload()).rejects.toThrow( | ||
| 'Encryption failed', | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.