|
| 1 | +// SPDX-FileCopyrightText: 2025 IEXEC BLOCKCHAIN TECH <[email protected]> |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { BigInt } from '@graphprotocol/graph-ts'; |
| 5 | +import { assert, beforeEach, clearStore, describe, test } from 'matchstick-as/assembly/index'; |
| 6 | +import { Bulk, BulkSlice } from '../../../generated/schema'; |
| 7 | +import { |
| 8 | + createBulkOrderID, |
| 9 | + createBulkSliceID, |
| 10 | + isAddressString, |
| 11 | + isBytes32String, |
| 12 | + isHexString, |
| 13 | + isIntegerString, |
| 14 | +} from '../../../src/utils'; |
| 15 | +import { mockAddress, mockBytes32 } from '../utils/mock'; |
| 16 | + |
| 17 | +const dealId = mockBytes32('dealId'); |
| 18 | + |
| 19 | +describe('Bulk', () => { |
| 20 | + beforeEach(() => { |
| 21 | + clearStore(); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('Validation Functions', () => { |
| 25 | + test('isIntegerString should validate integer strings correctly', () => { |
| 26 | + assert.assertTrue(isIntegerString('123')); |
| 27 | + assert.assertTrue(isIntegerString('1000000000')); |
| 28 | + assert.assertTrue(isIntegerString('0')); |
| 29 | + |
| 30 | + assert.assertTrue(!isIntegerString('')); |
| 31 | + assert.assertTrue(!isIntegerString('01')); // Leading zero |
| 32 | + assert.assertTrue(!isIntegerString('abc')); |
| 33 | + assert.assertTrue(!isIntegerString('123abc')); |
| 34 | + }); |
| 35 | + |
| 36 | + test('isHexString should validate hex strings correctly', () => { |
| 37 | + assert.assertTrue(isHexString('0x1234')); |
| 38 | + assert.assertTrue(isHexString('0xabcdef')); |
| 39 | + assert.assertTrue(isHexString('0x')); // Empty hex string is valid (length >= 2) |
| 40 | + |
| 41 | + // Note: isHexString only accepts lowercase hex chars |
| 42 | + assert.assertTrue(!isHexString('0xABCDEF')); |
| 43 | + assert.assertTrue(!isHexString('1234')); // Missing 0x |
| 44 | + assert.assertTrue(!isHexString('0xgg')); // Invalid hex char |
| 45 | + }); |
| 46 | + |
| 47 | + test('isAddressString should validate address strings correctly', () => { |
| 48 | + const validAddress = mockAddress('test').toHex().toLowerCase(); |
| 49 | + assert.assertTrue(isAddressString(validAddress)); |
| 50 | + |
| 51 | + assert.assertTrue(!isAddressString('0x123')); // Too short |
| 52 | + assert.assertTrue(!isAddressString('not-an-address')); |
| 53 | + }); |
| 54 | + |
| 55 | + test('isBytes32String should validate bytes32 strings correctly', () => { |
| 56 | + const validBytes32 = mockBytes32('test').toHex(); |
| 57 | + assert.assertTrue(isBytes32String(validBytes32)); |
| 58 | + |
| 59 | + assert.assertTrue(!isBytes32String('0x1234')); // Too short |
| 60 | + assert.assertTrue(!isBytes32String('not-bytes32')); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('ID Creation Functions', () => { |
| 65 | + test('createBulkSliceID should create correct ID', () => { |
| 66 | + const index = BigInt.fromI32(5); |
| 67 | + const expectedId = dealId.toHex().concat('-').concat('5'); |
| 68 | + const actualId = createBulkSliceID(dealId.toHex(), index); |
| 69 | + |
| 70 | + assert.stringEquals(expectedId, actualId); |
| 71 | + }); |
| 72 | + |
| 73 | + test('createBulkOrderID should create correct ID', () => { |
| 74 | + const taskId = mockBytes32('task').toHex(); |
| 75 | + const orderIndex = BigInt.fromI32(3); |
| 76 | + const expectedId = taskId.concat('-').concat('3'); |
| 77 | + const actualId = createBulkOrderID(taskId, orderIndex); |
| 78 | + |
| 79 | + assert.stringEquals(expectedId, actualId); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('Bulk Entity', () => { |
| 84 | + test('Should create Bulk entity correctly', () => { |
| 85 | + const bulkId = dealId.toHex(); |
| 86 | + const hash = 'QmBulkHash123'; |
| 87 | + |
| 88 | + let bulk = new Bulk(bulkId); |
| 89 | + bulk.hash = hash; |
| 90 | + bulk.save(); |
| 91 | + |
| 92 | + assert.fieldEquals('Bulk', bulkId, 'id', bulkId); |
| 93 | + assert.fieldEquals('Bulk', bulkId, 'hash', hash); |
| 94 | + }); |
| 95 | + |
| 96 | + test('Bulk entity should be immutable (load returns existing)', () => { |
| 97 | + const bulkId = dealId.toHex(); |
| 98 | + const hash1 = 'QmHash1'; |
| 99 | + const hash2 = 'QmHash2'; |
| 100 | + |
| 101 | + let bulk = new Bulk(bulkId); |
| 102 | + bulk.hash = hash1; |
| 103 | + bulk.save(); |
| 104 | + |
| 105 | + // Try to load and modify |
| 106 | + let loadedBulk = Bulk.load(bulkId); |
| 107 | + if (loadedBulk != null) { |
| 108 | + loadedBulk.hash = hash2; |
| 109 | + loadedBulk.save(); |
| 110 | + } |
| 111 | + |
| 112 | + // Verify it was modified (testing immutability logic should be in handler) |
| 113 | + assert.fieldEquals('Bulk', bulkId, 'hash', hash2); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('BulkSlice Entity', () => { |
| 118 | + test('Should create BulkSlice entity correctly', () => { |
| 119 | + const bulkId = dealId.toHex(); |
| 120 | + const index = BigInt.fromI32(0); |
| 121 | + const sliceId = createBulkSliceID(dealId.toHex(), index); |
| 122 | + const taskId = mockBytes32('task').toHex(); |
| 123 | + const hash = 'QmSliceHash456'; |
| 124 | + |
| 125 | + let bulkSlice = new BulkSlice(sliceId); |
| 126 | + bulkSlice.task = taskId; |
| 127 | + bulkSlice.hash = hash; |
| 128 | + bulkSlice.bulk = bulkId; |
| 129 | + bulkSlice.index = index; |
| 130 | + bulkSlice.datasets = []; |
| 131 | + bulkSlice.datasetOrders = []; |
| 132 | + bulkSlice.save(); |
| 133 | + |
| 134 | + assert.fieldEquals('BulkSlice', sliceId, 'id', sliceId); |
| 135 | + assert.fieldEquals('BulkSlice', sliceId, 'hash', hash); |
| 136 | + assert.fieldEquals('BulkSlice', sliceId, 'bulk', bulkId); |
| 137 | + assert.fieldEquals('BulkSlice', sliceId, 'task', taskId); |
| 138 | + assert.fieldEquals('BulkSlice', sliceId, 'index', '0'); |
| 139 | + }); |
| 140 | + |
| 141 | + test('Should handle BulkSlice with dataset orders', () => { |
| 142 | + const bulkId = dealId.toHex(); |
| 143 | + const index = BigInt.fromI32(0); |
| 144 | + const sliceId = createBulkSliceID(dealId.toHex(), index); |
| 145 | + const taskId = mockBytes32('task').toHex(); |
| 146 | + const datasetAddress1 = mockAddress('dataset1').toHex(); |
| 147 | + const datasetAddress2 = mockAddress('dataset2').toHex(); |
| 148 | + |
| 149 | + let bulkSlice = new BulkSlice(sliceId); |
| 150 | + bulkSlice.task = taskId; |
| 151 | + bulkSlice.hash = 'QmSlice'; |
| 152 | + bulkSlice.bulk = bulkId; |
| 153 | + bulkSlice.index = index; |
| 154 | + |
| 155 | + // Add datasets |
| 156 | + const datasets = [datasetAddress1, datasetAddress2]; |
| 157 | + bulkSlice.datasets = datasets; |
| 158 | + |
| 159 | + // Add dataset order IDs |
| 160 | + const orderIds = [ |
| 161 | + createBulkOrderID(taskId, BigInt.fromI32(0)), |
| 162 | + createBulkOrderID(taskId, BigInt.fromI32(1)), |
| 163 | + ]; |
| 164 | + bulkSlice.datasetOrders = orderIds; |
| 165 | + |
| 166 | + bulkSlice.save(); |
| 167 | + |
| 168 | + assert.fieldEquals('BulkSlice', sliceId, 'id', sliceId); |
| 169 | + // Note: Array field assertions in matchstick are limited |
| 170 | + }); |
| 171 | + |
| 172 | + test('Should create multiple BulkSlice entities for different indices', () => { |
| 173 | + const bulkId = dealId.toHex(); |
| 174 | + |
| 175 | + for (let i = 0; i < 3; i++) { |
| 176 | + const index = BigInt.fromI32(i); |
| 177 | + const sliceId = createBulkSliceID(dealId.toHex(), index); |
| 178 | + const taskId = mockBytes32('task' + i.toString()).toHex(); |
| 179 | + |
| 180 | + let bulkSlice = new BulkSlice(sliceId); |
| 181 | + bulkSlice.task = taskId; |
| 182 | + bulkSlice.hash = 'QmSlice' + i.toString(); |
| 183 | + bulkSlice.bulk = bulkId; |
| 184 | + bulkSlice.index = index; |
| 185 | + bulkSlice.datasets = []; |
| 186 | + bulkSlice.datasetOrders = []; |
| 187 | + bulkSlice.save(); |
| 188 | + |
| 189 | + assert.fieldEquals('BulkSlice', sliceId, 'bulk', bulkId); |
| 190 | + assert.fieldEquals('BulkSlice', sliceId, 'index', i.toString()); |
| 191 | + } |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments