1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect} from '../util';19import {UniqueBaseCollection} from '../util/playgrounds/unique';2021describe('Integration Test: Collection Properties', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 25 before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({filename: __filename});28 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);29 });30 });31 32 itSub('Properties are initially empty', async ({helper}) => {33 const collection = await helper.nft.mintCollection(alice);34 expect(await collection.getProperties()).to.be.empty;35 });36 37 async function testSetsPropertiesForCollection(collection: UniqueBaseCollection) {38 39 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;40 41 await collection.addAdmin(alice, {Substrate: bob.address});42 43 44 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;45 46 const properties = await collection.getProperties();47 expect(properties).to.include.deep.members([48 {key: 'electron', value: 'come bond'},49 {key: 'black_hole', value: ''},50 ]);51 }52 53 itSub('Sets properties for a NFT collection', async ({helper}) => {54 await testSetsPropertiesForCollection(await helper.nft.mintCollection(alice));55 });56 57 itSub.ifWithPallets('Sets properties for a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {58 await testSetsPropertiesForCollection(await helper.rft.mintCollection(alice));59 });60 61 async function testCheckValidNames(collection: UniqueBaseCollection) {62 63 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;64 65 66 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;67 68 69 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;70 71 72 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;73 74 75 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;76 77 const properties = await collection.getProperties();78 expect(properties).to.include.deep.members([79 {key: 'answer', value: ''},80 {key: '451', value: ''},81 {key: 'black_hole', value: ''},82 {key: '-', value: ''},83 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},84 ]);85 }86 87 itSub('Check valid names for NFT collection properties keys', async ({helper}) => {88 await testCheckValidNames(await helper.nft.mintCollection(alice));89 });90 91 itSub.ifWithPallets('Check valid names for ReFungible collection properties keys', [Pallets.ReFungible], async ({helper}) => {92 await testCheckValidNames(await helper.rft.mintCollection(alice));93 });94 95 async function testChangesProperties(collection: UniqueBaseCollection) {96 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;97 98 99 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;100 101 const properties = await collection.getProperties();102 expect(properties).to.include.deep.members([103 {key: 'electron', value: 'come bond'},104 {key: 'black_hole', value: 'LIGO'},105 ]);106 }107 108 itSub('Changes properties of a NFT collection', async ({helper}) => {109 await testChangesProperties(await helper.nft.mintCollection(alice));110 });111 112 itSub.ifWithPallets('Changes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {113 await testChangesProperties(await helper.rft.mintCollection(alice));114 });115 116 async function testDeleteProperties(collection: UniqueBaseCollection) {117 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;118 119 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;120 121 const properties = await collection.getProperties(['black_hole', 'electron']);122 expect(properties).to.be.deep.equal([123 {key: 'black_hole', value: 'LIGO'},124 ]);125 }126 127 itSub('Deletes properties of a NFT collection', async ({helper}) => {128 await testDeleteProperties(await helper.nft.mintCollection(alice));129 });130 131 itSub.ifWithPallets('Deletes properties of a ReFungible collection', [Pallets.ReFungible], async ({helper}) => {132 await testDeleteProperties(await helper.rft.mintCollection(alice));133 });134135 [136 {mode: 'nft' as const, requiredPallets: []},137 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 138 ].map(testCase =>139 itSub.ifWithPallets(`Allows modifying a collection property multiple times (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {140 const propKey = 'tok-prop';141142 const collection = await helper[testCase.mode].mintCollection(alice);143144 const maxCollectionPropertiesSize = 40960;145146 const propDataSize = 4096;147148 let propDataChar = 'a';149 const makeNewPropData = () => {150 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);151 return `${propDataChar}`.repeat(propDataSize);152 };153154 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);155 const originalSpace = await collection.getPropertiesConsumedSpace();156 expect(originalSpace).to.be.equal(propDataSize);157158 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;159160 161 162 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {163 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);164 const consumedSpace = await collection.getPropertiesConsumedSpace();165 expect(consumedSpace).to.be.equal(originalSpace);166 }167 }));168169 [170 {mode: 'nft' as const, requiredPallets: []},171 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, 172 ].map(testCase =>173 itSub.ifWithPallets(`Adding then removing a collection property doesn't change the consumed space (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {174 const propKey = 'tok-prop';175176 const collection = await helper[testCase.mode].mintCollection(alice);177 const originalSpace = await collection.getPropertiesConsumedSpace();178179 const propDataSize = 4096;180 const propData = 'a'.repeat(propDataSize);181182 await collection.setProperties(alice, [{key: propKey, value: propData}]);183 let consumedSpace = await collection.getPropertiesConsumedSpace();184 expect(consumedSpace).to.be.equal(propDataSize);185186 await collection.deleteProperties(alice, [propKey]);187 consumedSpace = await collection.getPropertiesConsumedSpace();188 expect(consumedSpace).to.be.equal(originalSpace);189 }));190});191 192describe('Negative Integration Test: Collection Properties', () => {193 let alice: IKeyringPair;194 let bob: IKeyringPair;195 196 before(async () => {197 await usingPlaygrounds(async (helper, privateKey) => {198 const donor = await privateKey({filename: __filename});199 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);200 });201 });202 203 async function testFailsSetPropertiesIfNotOwnerOrAdmin(collection: UniqueBaseCollection) { 204 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))205 .to.be.rejectedWith(/common\.NoPermission/);206 207 expect(await collection.getProperties()).to.be.empty;208 }209 210 itSub('Fails to set properties in a NFT collection if not its onwer/administrator', async ({helper}) => {211 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.nft.mintCollection(alice));212 });213 214 itSub.ifWithPallets('Fails to set properties in a ReFungible collection if not its onwer/administrator', [Pallets.ReFungible], async ({helper}) => {215 await testFailsSetPropertiesIfNotOwnerOrAdmin(await helper.rft.mintCollection(alice));216 });217 218 async function testFailsSetPropertiesThatExeedLimits(collection: UniqueBaseCollection) {219 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();220 221 222 {223 console.error = () => {};224 await expect(collection.setProperties(alice, [225 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},226 ])).to.be.rejected;227 }228 229 expect(await collection.getProperties(['electron'])).to.be.empty;230 231 await expect(collection.setProperties(alice, [232 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))}, 233 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))}, 234 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);235 236 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;237 }238 239 itSub('Fails to set properties that exceed the limits (NFT)', async ({helper}) => {240 await testFailsSetPropertiesThatExeedLimits(await helper.nft.mintCollection(alice));241 });242 243 itSub.ifWithPallets('Fails to set properties that exceed the limits (ReFungible)', [Pallets.ReFungible], async ({helper}) => {244 await testFailsSetPropertiesThatExeedLimits(await helper.rft.mintCollection(alice));245 });246 247 async function testFailsSetMorePropertiesThanAllowed(collection: UniqueBaseCollection) {248 const propertiesToBeSet = [];249 for (let i = 0; i < 65; i++) {250 propertiesToBeSet.push({251 key: 'electron_' + i,252 value: Math.random() > 0.5 ? 'high' : 'low',253 });254 }255 256 await expect(collection.setProperties(alice, propertiesToBeSet)).257 to.be.rejectedWith(/common\.PropertyLimitReached/);258 259 expect(await collection.getProperties()).to.be.empty;260 }261 262 itSub('Fails to set more properties than it is allowed (NFT)', async ({helper}) => {263 await testFailsSetMorePropertiesThanAllowed(await helper.nft.mintCollection(alice));264 });265 266 itSub.ifWithPallets('Fails to set more properties than it is allowed (ReFungible)', [Pallets.ReFungible], async ({helper}) => {267 await testFailsSetMorePropertiesThanAllowed(await helper.rft.mintCollection(alice));268 });269 270 async function testFailsSetPropertiesWithInvalidNames(collection: UniqueBaseCollection) {271 const invalidProperties = [272 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],273 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],274 [{key: 'déjà vu', value: 'hmm...'}],275 ];276 277 for (let i = 0; i < invalidProperties.length; i++) {278 await expect(279 collection.setProperties(alice, invalidProperties[i]), 280 `on rejecting the new badly-named property #${i}`,281 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);282 }283 284 await expect(285 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]), 286 'on rejecting an unnamed property',287 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);288 289 await expect(290 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]), 291 'on setting the correctly-but-still-badly-named property',292 ).to.be.fulfilled;293 294 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');295 296 const properties = await collection.getProperties(keys);297 expect(properties).to.be.deep.equal([298 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},299 ]);300 301 for (let i = 0; i < invalidProperties.length; i++) {302 await expect(303 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)), 304 `on trying to delete the non-existent badly-named property #${i}`,305 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);306 }307 }308 309 itSub('Fails to set properties with invalid names (NFT)', async ({helper}) => {310 await testFailsSetPropertiesWithInvalidNames(await helper.nft.mintCollection(alice));311 });312 313 itSub.ifWithPallets('Fails to set properties with invalid names (ReFungible)', [Pallets.ReFungible], async ({helper}) => {314 await testFailsSetPropertiesWithInvalidNames(await helper.rft.mintCollection(alice));315 });316});317