|
| 1 | +import { |
| 2 | + IExecDataProtectorCore, |
| 3 | + ProtectedDataWithSecretProps, |
| 4 | +} from '@iexec/dataprotector'; |
| 5 | +import { beforeAll, describe, expect, it } from '@jest/globals'; |
| 6 | +import { HDNodeWallet } from 'ethers'; |
| 7 | +import { |
| 8 | + DEFAULT_CHAIN_ID, |
| 9 | + getChainDefaultConfig, |
| 10 | +} from '../../src/config/config.js'; |
| 11 | +import { Contact, IExecWeb3telegram } from '../../src/index.js'; |
| 12 | +import { |
| 13 | + MAX_EXPECTED_BLOCKTIME, |
| 14 | + MAX_EXPECTED_WEB2_SERVICES_TIME, |
| 15 | + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME, |
| 16 | + TEST_CHAIN, |
| 17 | + createAndPublishAppOrders, |
| 18 | + getRandomWallet, |
| 19 | + getTestConfig, |
| 20 | + getTestIExecOption, |
| 21 | + getTestWeb3SignerProvider, |
| 22 | + waitSubgraphIndexing, |
| 23 | +} from '../test-utils.js'; |
| 24 | +import { IExec } from 'iexec'; |
| 25 | + |
| 26 | +describe('web3telegram.prepareTelegramCampaign()', () => { |
| 27 | + let consumerWallet: HDNodeWallet; |
| 28 | + let providerWallet: HDNodeWallet; |
| 29 | + let web3telegram: IExecWeb3telegram; |
| 30 | + let dataProtector: IExecDataProtectorCore; |
| 31 | + let validProtectedData1: ProtectedDataWithSecretProps; |
| 32 | + let validProtectedData2: ProtectedDataWithSecretProps; |
| 33 | + let validProtectedData3: ProtectedDataWithSecretProps; |
| 34 | + const iexecOptions = getTestIExecOption(); |
| 35 | + const prodWorkerpoolPublicPrice = 1000; |
| 36 | + const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID); |
| 37 | + |
| 38 | + beforeAll(async () => { |
| 39 | + // Create app orders |
| 40 | + providerWallet = getRandomWallet(); |
| 41 | + const ethProvider = getTestWeb3SignerProvider( |
| 42 | + TEST_CHAIN.appOwnerWallet.privateKey |
| 43 | + ); |
| 44 | + const resourceProvider = new IExec({ ethProvider }, iexecOptions); |
| 45 | + await createAndPublishAppOrders( |
| 46 | + resourceProvider, |
| 47 | + defaultConfig!.dappAddress |
| 48 | + ); |
| 49 | + |
| 50 | + dataProtector = new IExecDataProtectorCore( |
| 51 | + ...getTestConfig(providerWallet.privateKey) |
| 52 | + ); |
| 53 | + |
| 54 | + // create valid protected data |
| 55 | + validProtectedData1 = await dataProtector.protectData({ |
| 56 | + data: { telegram_chatId: '12345' }, |
| 57 | + name: 'bulk test 1', |
| 58 | + }); |
| 59 | + |
| 60 | + validProtectedData2 = await dataProtector.protectData({ |
| 61 | + data: { telegram_chatId: '67890' }, |
| 62 | + name: 'bulk test 2', |
| 63 | + }); |
| 64 | + |
| 65 | + validProtectedData3 = await dataProtector.protectData({ |
| 66 | + data: { telegram_chatId: '11111' }, |
| 67 | + name: 'bulk test 3', |
| 68 | + }); |
| 69 | + |
| 70 | + await waitSubgraphIndexing(); |
| 71 | + }, 5 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + 5_000); |
| 72 | + |
| 73 | + beforeEach(async () => { |
| 74 | + consumerWallet = getRandomWallet(); |
| 75 | + |
| 76 | + // Grant access with allowBulk for bulk processing |
| 77 | + await dataProtector.grantAccess({ |
| 78 | + authorizedApp: defaultConfig.dappAddress, |
| 79 | + protectedData: validProtectedData1.address, |
| 80 | + authorizedUser: consumerWallet.address, |
| 81 | + allowBulk: true, |
| 82 | + }); |
| 83 | + |
| 84 | + await dataProtector.grantAccess({ |
| 85 | + authorizedApp: defaultConfig.dappAddress, |
| 86 | + protectedData: validProtectedData2.address, |
| 87 | + authorizedUser: consumerWallet.address, |
| 88 | + allowBulk: true, |
| 89 | + }); |
| 90 | + |
| 91 | + await dataProtector.grantAccess({ |
| 92 | + authorizedApp: defaultConfig.dappAddress, |
| 93 | + protectedData: validProtectedData3.address, |
| 94 | + authorizedUser: consumerWallet.address, |
| 95 | + allowBulk: true, |
| 96 | + }); |
| 97 | + |
| 98 | + await waitSubgraphIndexing(); |
| 99 | + web3telegram = new IExecWeb3telegram( |
| 100 | + ...getTestConfig(consumerWallet.privateKey) |
| 101 | + ); |
| 102 | + }, MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_SUBGRAPH_INDEXING_TIME); |
| 103 | + |
| 104 | + it( |
| 105 | + 'should prepare a telegram campaignRequest', |
| 106 | + async () => { |
| 107 | + // Fetch contacts with allowBulk access |
| 108 | + const contacts: Contact[] = await web3telegram.fetchMyContacts(); |
| 109 | + expect(contacts.length).toBeGreaterThanOrEqual(3); |
| 110 | + |
| 111 | + const bulkOrders = contacts.map((contact) => contact.grantedAccess); |
| 112 | + |
| 113 | + // Process the bulk request |
| 114 | + const result = await web3telegram.prepareTelegramCampaign({ |
| 115 | + telegramContent: 'Bulk test message', |
| 116 | + senderName: 'Bulk test sender', |
| 117 | + // protectedData is optional when grantedAccess is provided |
| 118 | + grantedAccess: bulkOrders, |
| 119 | + maxProtectedDataPerTask: 3, |
| 120 | + workerpoolMaxPrice: prodWorkerpoolPublicPrice, |
| 121 | + }); |
| 122 | + |
| 123 | + // Verify the result |
| 124 | + expect(result).toBeDefined(); |
| 125 | + expect(result.campaignRequest).toEqual({ |
| 126 | + app: expect.any(String), |
| 127 | + appmaxprice: expect.any(String), |
| 128 | + workerpool: expect.any(String), |
| 129 | + workerpoolmaxprice: expect.any(String), |
| 130 | + dataset: '0x0000000000000000000000000000000000000000', |
| 131 | + datasetmaxprice: '0', |
| 132 | + callback: '0x0000000000000000000000000000000000000000', |
| 133 | + params: expect.any(String), |
| 134 | + beneficiary: consumerWallet.address, |
| 135 | + category: '0', |
| 136 | + requester: consumerWallet.address, |
| 137 | + salt: expect.any(String), |
| 138 | + sign: expect.any(String), |
| 139 | + tag: '0x0000000000000000000000000000000000000000000000000000000000000003', |
| 140 | + trust: '0', |
| 141 | + volume: '1', |
| 142 | + }); |
| 143 | + }, |
| 144 | + 30 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME + 60_000 |
| 145 | + ); |
| 146 | +}); |
0 commit comments