1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';1920describe('Integration Test: Collection Properties', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;23 24 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({filename: __filename});27 [alice, bob] = await helper.arrange.createAccounts([200n, 10n], donor);28 });29 });30 31 itSub('Properties are initially empty', async ({helper}) => {32 const collection = await helper.nft.mintCollection(alice);33 expect(await collection.getProperties()).to.be.empty;34 });3536 [37 {mode: 'nft' as const, requiredPallets: []},38 {mode: 'ft' as const, requiredPallets: []},39 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 40 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {41 before(async function() {42 43 await usingPlaygrounds(async helper => {44 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);45 });46 });4748 itSub('Sets properties for a collection', async ({helper}) => {49 const collection = await helper[testSuite.mode].mintCollection(alice);5051 52 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;53 54 await collection.addAdmin(alice, {Substrate: bob.address});55 56 57 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;58 59 const properties = await collection.getProperties();60 expect(properties).to.include.deep.members([61 {key: 'electron', value: 'come bond'},62 {key: 'black_hole', value: ''},63 ]);64 });65 66 itSub('Check valid names for collection properties keys', async ({helper}) => {67 const collection = await helper[testSuite.mode].mintCollection(alice);6869 70 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;71 72 73 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;74 75 76 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;77 78 79 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;80 81 82 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;83 84 const properties = await collection.getProperties();85 expect(properties).to.include.deep.members([86 {key: 'answer', value: ''},87 {key: '451', value: ''},88 {key: 'black_hole', value: ''},89 {key: '-', value: ''},90 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},91 ]);92 });93 94 itSub('Changes properties of a collection', async ({helper}) => {95 const collection = await helper[testSuite.mode].mintCollection(alice);9697 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;98 99 100 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;101 102 const properties = await collection.getProperties();103 expect(properties).to.include.deep.members([104 {key: 'electron', value: 'come bond'},105 {key: 'black_hole', value: 'LIGO'},106 ]);107 });108 109 itSub('Deletes properties of a collection', async ({helper}) => {110 const collection = await helper[testSuite.mode].mintCollection(alice);111112 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;113 114 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;115 116 const properties = await collection.getProperties(['black_hole', 'electron']);117 expect(properties).to.be.deep.equal([118 {key: 'black_hole', value: 'LIGO'},119 ]);120 });121122 itSub('Allows modifying a collection property multiple times with the same size', async({helper}) => {123 const propKey = 'tok-prop';124125 const collection = await helper[testSuite.mode].mintCollection(alice);126127 const maxCollectionPropertiesSize = 40960;128129 const propDataSize = 4096;130131 let propDataChar = 'a';132 const makeNewPropData = () => {133 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);134 return `${propDataChar}`.repeat(propDataSize);135 };136137 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);138 const originalSpace = await collection.getPropertiesConsumedSpace();139 expect(originalSpace).to.be.equal(propDataSize);140141 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;142143 144 145 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {146 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);147 const consumedSpace = await collection.getPropertiesConsumedSpace();148 expect(consumedSpace).to.be.equal(originalSpace);149 }150 });151152 itSub('Adding then removing a collection property doesn\'t change the consumed space', async({helper}) => {153 const propKey = 'tok-prop';154155 const collection = await helper[testSuite.mode].mintCollection(alice);156 const originalSpace = await collection.getPropertiesConsumedSpace();157158 const propDataSize = 4096;159 const propData = 'a'.repeat(propDataSize);160161 await collection.setProperties(alice, [{key: propKey, value: propData}]);162 let consumedSpace = await collection.getPropertiesConsumedSpace();163 expect(consumedSpace).to.be.equal(propDataSize);164165 await collection.deleteProperties(alice, [propKey]);166 consumedSpace = await collection.getPropertiesConsumedSpace();167 expect(consumedSpace).to.be.equal(originalSpace);168 });169170 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {171 const propKey = 'tok-prop';172173 const collection = await helper[testSuite.mode].mintCollection(alice);174 const originalSpace = await collection.getPropertiesConsumedSpace();175176 const initPropDataSize = 4096;177 const biggerPropDataSize = 5000;178 const smallerPropDataSize = 4000;179180 const initPropData = 'a'.repeat(initPropDataSize);181 const biggerPropData = 'b'.repeat(biggerPropDataSize);182 const smallerPropData = 'c'.repeat(smallerPropDataSize);183184 let consumedSpace;185 let expectedConsumedSpaceDiff;186187 await collection.setProperties(alice, [{key: propKey, value: initPropData}]);188 consumedSpace = await collection.getPropertiesConsumedSpace();189 expectedConsumedSpaceDiff = initPropDataSize - originalSpace;190 expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);191192 await collection.setProperties(alice, [{key: propKey, value: biggerPropData}]);193 consumedSpace = await collection.getPropertiesConsumedSpace();194 expectedConsumedSpaceDiff = biggerPropDataSize - initPropDataSize;195 expect(consumedSpace).to.be.equal(initPropDataSize + expectedConsumedSpaceDiff);196197 await collection.setProperties(alice, [{key: propKey, value: smallerPropData}]);198 consumedSpace = await collection.getPropertiesConsumedSpace();199 expectedConsumedSpaceDiff = biggerPropDataSize - smallerPropDataSize;200 expect(consumedSpace).to.be.equal(biggerPropDataSize - expectedConsumedSpaceDiff);201 });202 }));203});204 205describe('Negative Integration Test: Collection Properties', () => {206 let alice: IKeyringPair;207 let bob: IKeyringPair;208 209 before(async () => {210 await usingPlaygrounds(async (helper, privateKey) => {211 const donor = await privateKey({filename: __filename});212 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);213 });214 });215216 [217 {mode: 'nft' as const, requiredPallets: []},218 {mode: 'ft' as const, requiredPallets: []},219 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 220 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {221 before(async function() {222 223 await usingPlaygrounds(async helper => {224 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);225 });226 });227 228 itSub('Fails to set properties in a collection if not its onwer/administrator', async ({helper}) => {229 const collection = await helper[testSuite.mode].mintCollection(alice);230231 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))232 .to.be.rejectedWith(/common\.NoPermission/);233 234 expect(await collection.getProperties()).to.be.empty;235 });236 237 itSub('Fails to set properties that exceed the limits', async ({helper}) => {238 const collection = await helper[testSuite.mode].mintCollection(alice);239240 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();241 242 243 {244 console.error = () => {};245 await expect(collection.setProperties(alice, [246 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},247 ])).to.be.rejected;248 }249 250 expect(await collection.getProperties(['electron'])).to.be.empty;251 252 await expect(collection.setProperties(alice, [253 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))}, 254 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))}, 255 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);256 257 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;258 });259 260 itSub('Fails to set more properties than it is allowed', async ({helper}) => {261 const collection = await helper[testSuite.mode].mintCollection(alice);262263 const propertiesToBeSet = [];264 for (let i = 0; i < 65; i++) {265 propertiesToBeSet.push({266 key: 'electron_' + i,267 value: Math.random() > 0.5 ? 'high' : 'low',268 });269 }270 271 await expect(collection.setProperties(alice, propertiesToBeSet)).272 to.be.rejectedWith(/common\.PropertyLimitReached/);273 274 expect(await collection.getProperties()).to.be.empty;275 });276277 itSub('Fails to set properties with invalid names', async ({helper}) => {278 const collection = await helper[testSuite.mode].mintCollection(alice);279280 const invalidProperties = [281 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],282 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],283 [{key: 'déjà vu', value: 'hmm...'}],284 ];285 286 for (let i = 0; i < invalidProperties.length; i++) {287 await expect(288 collection.setProperties(alice, invalidProperties[i]), 289 `on rejecting the new badly-named property #${i}`,290 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);291 }292 293 await expect(294 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]), 295 'on rejecting an unnamed property',296 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);297 298 await expect(299 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]), 300 'on setting the correctly-but-still-badly-named property',301 ).to.be.fulfilled;302 303 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');304 305 const properties = await collection.getProperties(keys);306 expect(properties).to.be.deep.equal([307 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},308 ]);309 310 for (let i = 0; i < invalidProperties.length; i++) {311 await expect(312 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)), 313 `on trying to delete the non-existent badly-named property #${i}`,314 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);315 }316 });317318 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {319 const collection = await helper[testSuite.mode].mintCollection(alice, {properties: [320 {key: 'sea-creatures', value: 'mermaids'},321 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},322 ]});323324 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true))325 .to.be.rejectedWith(/BadOrigin/);326 });327 }));328});329