1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';1920describe('Integration Test: Collection Properties', () => {21 let superuser: IKeyringPair;22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 25 before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 superuser = await privateKey('//Alice');28 const donor = await privateKey({filename: __filename});29 [alice, bob] = await helper.arrange.createAccounts([200n, 10n], donor);30 });31 });32 33 itSub('Properties are initially empty', async ({helper}) => {34 const collection = await helper.nft.mintCollection(alice);35 expect(await collection.getProperties()).to.be.empty;36 });3738 [39 {mode: 'nft' as const, requiredPallets: []},40 {mode: 'ft' as const, requiredPallets: []},41 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 42 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {43 before(async function() {44 45 await usingPlaygrounds(async helper => {46 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);47 });48 });4950 itSub('Sets properties for a collection', async ({helper}) => {51 const collection = await helper[testSuite.mode].mintCollection(alice);5253 54 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;55 56 await collection.addAdmin(alice, {Substrate: bob.address});57 58 59 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;60 61 const properties = await collection.getProperties();62 expect(properties).to.include.deep.members([63 {key: 'electron', value: 'come bond'},64 {key: 'black_hole', value: ''},65 ]);66 });67 68 itSub('Check valid names for collection properties keys', async ({helper}) => {69 const collection = await helper[testSuite.mode].mintCollection(alice);7071 72 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;73 74 75 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;76 77 78 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;79 80 81 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;82 83 84 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;85 86 const properties = await collection.getProperties();87 expect(properties).to.include.deep.members([88 {key: 'answer', value: ''},89 {key: '451', value: ''},90 {key: 'black_hole', value: ''},91 {key: '-', value: ''},92 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},93 ]);94 });95 96 itSub('Changes properties of a collection', async ({helper}) => {97 const collection = await helper[testSuite.mode].mintCollection(alice);9899 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;100 101 102 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;103 104 const properties = await collection.getProperties();105 expect(properties).to.include.deep.members([106 {key: 'electron', value: 'come bond'},107 {key: 'black_hole', value: 'LIGO'},108 ]);109 });110 111 itSub('Deletes properties of a collection', async ({helper}) => {112 const collection = await helper[testSuite.mode].mintCollection(alice);113114 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;115 116 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;117 118 const properties = await collection.getProperties(['black_hole', 'electron']);119 expect(properties).to.be.deep.equal([120 {key: 'black_hole', value: 'LIGO'},121 ]);122 });123124 itSub('Allows modifying a collection property multiple times with the same size', async({helper}) => {125 const propKey = 'tok-prop';126127 const collection = await helper[testSuite.mode].mintCollection(alice);128129 const maxCollectionPropertiesSize = 40960;130131 const propDataSize = 4096;132133 let propDataChar = 'a';134 const makeNewPropData = () => {135 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);136 return `${propDataChar}`.repeat(propDataSize);137 };138139 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);140 const originalSpace = await collection.getPropertiesConsumedSpace();141 expect(originalSpace).to.be.equal(propDataSize);142143 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;144145 146 147 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {148 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);149 const consumedSpace = await collection.getPropertiesConsumedSpace();150 expect(consumedSpace).to.be.equal(originalSpace);151 }152 });153154 itSub('Adding then removing a collection property doesn\'t change the consumed space', async({helper}) => {155 const propKey = 'tok-prop';156157 const collection = await helper[testSuite.mode].mintCollection(alice);158 const originalSpace = await collection.getPropertiesConsumedSpace();159160 const propDataSize = 4096;161 const propData = 'a'.repeat(propDataSize);162163 await collection.setProperties(alice, [{key: propKey, value: propData}]);164 let consumedSpace = await collection.getPropertiesConsumedSpace();165 expect(consumedSpace).to.be.equal(propDataSize);166167 await collection.deleteProperties(alice, [propKey]);168 consumedSpace = await collection.getPropertiesConsumedSpace();169 expect(consumedSpace).to.be.equal(originalSpace);170 });171172 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {173 const propKey = 'tok-prop';174175 const collection = await helper[testSuite.mode].mintCollection(alice);176 const originalSpace = await collection.getPropertiesConsumedSpace();177178 const initPropDataSize = 4096;179 const biggerPropDataSize = 5000;180 const smallerPropDataSize = 4000;181182 const initPropData = 'a'.repeat(initPropDataSize);183 const biggerPropData = 'b'.repeat(biggerPropDataSize);184 const smallerPropData = 'c'.repeat(smallerPropDataSize);185186 let consumedSpace;187 let expectedConsumedSpaceDiff;188189 await collection.setProperties(alice, [{key: propKey, value: initPropData}]);190 consumedSpace = await collection.getPropertiesConsumedSpace();191 expectedConsumedSpaceDiff = initPropDataSize - originalSpace;192 expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);193194 await collection.setProperties(alice, [{key: propKey, value: biggerPropData}]);195 consumedSpace = await collection.getPropertiesConsumedSpace();196 expectedConsumedSpaceDiff = biggerPropDataSize - initPropDataSize;197 expect(consumedSpace).to.be.equal(initPropDataSize + expectedConsumedSpaceDiff);198199 await collection.setProperties(alice, [{key: propKey, value: smallerPropData}]);200 consumedSpace = await collection.getPropertiesConsumedSpace();201 expectedConsumedSpaceDiff = biggerPropDataSize - smallerPropDataSize;202 expect(consumedSpace).to.be.equal(biggerPropDataSize - expectedConsumedSpaceDiff);203 });204205 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {206 const properties = [207 {key: 'sea-creatures', value: 'mermaids'},208 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},209 ];210 const collection = await helper[testSuite.mode].mintCollection(alice, {properties});211212 const newProperty = ' '.repeat(4096);213 await collection.setProperties(alice, [{key: 'space', value: newProperty}]);214 const originalSpace = await collection.getPropertiesConsumedSpace();215 expect(originalSpace).to.be.equal(properties[0].value.length + properties[1].value.length + newProperty.length);216217 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true);218 const recomputedSpace = await collection.getPropertiesConsumedSpace();219 expect(recomputedSpace).to.be.equal(originalSpace);220 });221 }));222});223 224describe('Negative Integration Test: Collection Properties', () => {225 let alice: IKeyringPair;226 let bob: IKeyringPair;227 228 before(async () => {229 await usingPlaygrounds(async (helper, privateKey) => {230 const donor = await privateKey({filename: __filename});231 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);232 });233 });234235 [236 {mode: 'nft' as const, requiredPallets: []},237 {mode: 'ft' as const, requiredPallets: []},238 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 239 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {240 before(async function() {241 242 await usingPlaygrounds(async helper => {243 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);244 });245 });246 247 itSub('Fails to set properties in a collection if not its onwer/administrator', async ({helper}) => {248 const collection = await helper[testSuite.mode].mintCollection(alice);249250 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))251 .to.be.rejectedWith(/common\.NoPermission/);252 253 expect(await collection.getProperties()).to.be.empty;254 });255 256 itSub('Fails to set properties that exceed the limits', async ({helper}) => {257 const collection = await helper[testSuite.mode].mintCollection(alice);258259 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();260 261 262 {263 console.error = () => {};264 await expect(collection.setProperties(alice, [265 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},266 ])).to.be.rejected;267 }268 269 expect(await collection.getProperties(['electron'])).to.be.empty;270 271 await expect(collection.setProperties(alice, [272 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))}, 273 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))}, 274 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);275 276 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;277 });278 279 itSub('Fails to set more properties than it is allowed', async ({helper}) => {280 const collection = await helper[testSuite.mode].mintCollection(alice);281282 const propertiesToBeSet = [];283 for (let i = 0; i < 65; i++) {284 propertiesToBeSet.push({285 key: 'electron_' + i,286 value: Math.random() > 0.5 ? 'high' : 'low',287 });288 }289 290 await expect(collection.setProperties(alice, propertiesToBeSet)).291 to.be.rejectedWith(/common\.PropertyLimitReached/);292 293 expect(await collection.getProperties()).to.be.empty;294 });295296 itSub('Fails to set properties with invalid names', async ({helper}) => {297 const collection = await helper[testSuite.mode].mintCollection(alice);298299 const invalidProperties = [300 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],301 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],302 [{key: 'déjà vu', value: 'hmm...'}],303 ];304 305 for (let i = 0; i < invalidProperties.length; i++) {306 await expect(307 collection.setProperties(alice, invalidProperties[i]), 308 `on rejecting the new badly-named property #${i}`,309 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);310 }311 312 await expect(313 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]), 314 'on rejecting an unnamed property',315 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);316 317 await expect(318 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]), 319 'on setting the correctly-but-still-badly-named property',320 ).to.be.fulfilled;321 322 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');323 324 const properties = await collection.getProperties(keys);325 expect(properties).to.be.deep.equal([326 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},327 ]);328 329 for (let i = 0; i < invalidProperties.length; i++) {330 await expect(331 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)), 332 `on trying to delete the non-existent badly-named property #${i}`,333 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);334 }335 });336337 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {338 const collection = await helper[testSuite.mode].mintCollection(alice, {properties: [339 {key: 'sea-creatures', value: 'mermaids'},340 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},341 ]});342343 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true))344 .to.be.rejectedWith(/BadOrigin/);345 });346 }));347});348