1import {expect} from 'chai';2import {getApiConnection} from '../substrate/substrate-api';3import {NftIdTuple} from './util/fetch';4import {expectTxFailure, getResourceById} from './util/helpers';5import {6 addNftBasicResource,7 acceptNftResource,8 createCollection,9 mintNft,10 sendNft,11 addNftSlotResource,12 addNftComposableResource,13} from './util/tx';14import {RmrkTraitsResourceResourceInfo as ResourceInfo} from '@polkadot/types/lookup';15import {requirePallets, Pallets} from '../util/helpers';1617describe('integration test: add NFT resource', () => {18 const Alice = '//Alice';19 const Bob = '//Bob';20 const src = 'test-res-src';21 const metadata = 'test-res-metadata';22 const license = 'test-res-license';23 const thumb = 'test-res-thumb';2425 const nonexistentId = 99999;2627 let api: any;28 before(async function() {29 api = await getApiConnection();30 await requirePallets(this, [Pallets.RmrkCore]);31 });3233 it('add resource', async () => {34 const collectionIdAlice = await createCollection(35 api,36 Alice,37 'test-metadata',38 null,39 'test-symbol',40 );4142 const nftAlice = await mintNft(43 api,44 Alice,45 Alice,46 collectionIdAlice,47 'nft-metadata',48 );4950 await addNftBasicResource(51 api,52 Alice,53 'added',54 collectionIdAlice,55 nftAlice,56 src,57 metadata,58 license,59 thumb,60 );61 });6263 it('add a resource to the nested NFT', async () => {64 const collectionIdAlice = await createCollection(65 api,66 Alice,67 'test-metadata',68 null,69 'test-symbol',70 );7172 const parentNftId = await mintNft(api, Alice, Alice, collectionIdAlice, 'parent-nft-metadata');73 const childNftId = await mintNft(api, Alice, Alice, collectionIdAlice, 'child-nft-metadata');7475 const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];7677 await sendNft(api, 'sent', Alice, collectionIdAlice, childNftId, newOwnerNFT);7879 await addNftBasicResource(80 api,81 Alice,82 'added',83 collectionIdAlice,84 childNftId,85 src,86 metadata,87 license,88 thumb,89 );90 });9192 it('add multiple resources', async () => {93 const collectionIdAlice = await createCollection(94 api,95 Alice,96 'test-metadata',97 null,98 'test-symbol',99 );100101 const nftAlice = await mintNft(102 api,103 Alice,104 Alice,105 collectionIdAlice,106 'nft-metadata',107 );108109 const baseId = 42;110 const slotId = 10;111 const parts = [0, 5, 2];112113 const resourcesInfo = [];114 const resourceNum = 4;115116 const checkResource = async (resource: ResourceInfo, resType: string, expectedId: number, expected: {117 src: string,118 metadata: string,119 license: string,120 thumb: string121 }) => {122123 124 125 126 127 128 129 const resourceJson = (resource.resource.toHuman() as any)[resType];130131 expect(resource.id.toNumber(), 'Error: Invalid resource Id')132 .to.be.eq(expectedId);133134 expect(resourceJson.src, 'Error: Invalid resource src')135 .to.be.eq(expected.src);136 expect(resourceJson.metadata, 'Error: Invalid resource metadata')137 .to.be.eq(expected.metadata);138 expect(resourceJson.license, 'Error: Invalid resource license')139 .to.be.eq(expected.license);140 expect(resourceJson.thumb, 'Error: Invalid resource thumb')141 .to.be.eq(expected.thumb);142 };143144 for (let i = 0; i < resourceNum; i++) {145 resourcesInfo.push({146 src: src + 'r-' + i,147 metadata: metadata + 'r-' + i,148 license: license + 'r-' + i,149 thumb: thumb + 'r-' + i,150 });151 }152153 const firstBasicResourceId = await addNftBasicResource(154 api,155 Alice,156 'added',157 collectionIdAlice,158 nftAlice,159 resourcesInfo[0].src,160 resourcesInfo[0].metadata,161 resourcesInfo[0].license,162 resourcesInfo[0].thumb,163 );164165 const secondBasicResourceId = await addNftBasicResource(166 api,167 Alice,168 'added',169 collectionIdAlice,170 nftAlice,171 resourcesInfo[1].src,172 resourcesInfo[1].metadata,173 resourcesInfo[1].license,174 resourcesInfo[1].thumb,175 );176177 const composableResourceId = await addNftComposableResource(178 api,179 Alice,180 'added',181 collectionIdAlice,182 nftAlice,183 parts,184 baseId,185 resourcesInfo[2].src,186 resourcesInfo[2].metadata,187 resourcesInfo[2].license,188 resourcesInfo[2].thumb,189 );190191 const slotResourceId = await addNftSlotResource(192 api,193 Alice,194 'added',195 collectionIdAlice,196 nftAlice,197 baseId,198 slotId,199 resourcesInfo[3].src,200 resourcesInfo[3].metadata,201 resourcesInfo[3].license,202 resourcesInfo[3].thumb,203 );204205 const firstResource = await getResourceById(api, collectionIdAlice, nftAlice, firstBasicResourceId);206 await checkResource(firstResource, 'Basic', firstBasicResourceId, resourcesInfo[0]);207208 const secondResource = await getResourceById(api, collectionIdAlice, nftAlice, secondBasicResourceId);209 await checkResource(secondResource, 'Basic', secondBasicResourceId, resourcesInfo[1]);210211 const composableResource = await getResourceById(api, collectionIdAlice, nftAlice, composableResourceId);212 await checkResource(composableResource, 'Composable', composableResourceId, resourcesInfo[2]);213214 const slotResource = await getResourceById(api, collectionIdAlice, nftAlice, slotResourceId);215 await checkResource(slotResource, 'Slot', slotResourceId, resourcesInfo[3]);216 });217218 it('[negative]: unable to add a resource to the non-existing NFT', async () => {219 const collectionIdAlice = await createCollection(220 api,221 Alice,222 'test-metadata',223 null,224 'test-symbol',225 );226227 const tx = addNftBasicResource(228 api,229 Alice,230 'added',231 collectionIdAlice,232 nonexistentId,233 src,234 metadata,235 license,236 thumb,237 );238 239 await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);240 });241242 it('[negative]: unable to add a resource by a not-an-owner user', async () => {243 const collectionIdAlice = await createCollection(244 api,245 Alice,246 'test-metadata',247 null,248 'test-symbol',249 );250251 const nftAlice = await mintNft(252 api,253 Alice,254 Alice,255 collectionIdAlice,256 'nft-metadata',257 );258259 const tx = addNftBasicResource(260 api,261 Bob,262 'added',263 collectionIdAlice,264 nftAlice,265 src,266 metadata,267 license,268 thumb,269 );270 271 await expectTxFailure(/rmrkCore\.NoPermission/, tx);272 });273274 it('[negative]: unable to add a resource to the nested NFT if it isnt root owned by the caller', async () => {275 const collectionIdAlice = await createCollection(276 api,277 Alice,278 'test-metadata',279 null,280 'test-symbol',281 );282283 const parentNftId = await mintNft(api, Alice, Alice, collectionIdAlice, 'parent-nft-metadata');284 const childNftId = await mintNft(api, Alice, Alice, collectionIdAlice, 'child-nft-metadata');285286 const newOwnerNFT: NftIdTuple = [collectionIdAlice, parentNftId];287288 await sendNft(api, 'sent', Alice, collectionIdAlice, childNftId, newOwnerNFT);289290 const tx = addNftBasicResource(291 api,292 Bob,293 'added',294 collectionIdAlice,295 childNftId,296 src,297 metadata,298 license,299 thumb,300 );301 302 await expectTxFailure(/rmrkCore\.NoPermission/, tx);303 });304305 it('accept resource', async () => {306 const collectionIdBob = await createCollection(307 api,308 Bob,309 'test-metadata',310 null,311 'test-symbol',312 );313314 const nftAlice = await mintNft(315 api,316 Bob,317 Alice,318 collectionIdBob,319 'nft-metadata',320 );321322 const resourceId = await addNftBasicResource(323 api,324 Bob,325 'pending',326 collectionIdBob,327 nftAlice,328 src,329 metadata,330 license,331 thumb,332 );333334 await acceptNftResource(api, Alice, collectionIdBob, nftAlice, resourceId);335 });336337 it('[negative]: unable to accept a non-existing resource', async () => {338 const collectionIdBob = await createCollection(339 api,340 Bob,341 'test-metadata',342 null,343 'test-symbol',344 );345346 const nftAlice = await mintNft(347 api,348 Bob,349 Alice,350 collectionIdBob,351 'nft-metadata',352 );353354 const tx = acceptNftResource(api, Alice, collectionIdBob, nftAlice, nonexistentId);355 await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);356 });357358 it('[negative]: unable to accept a resource by a not-an-NFT-owner user', async () => {359 const collectionIdBob = await createCollection(360 api,361 Bob,362 'test-metadata',363 null,364 'test-symbol',365 );366367 const nftAlice = await mintNft(368 api,369 Bob,370 Alice,371 collectionIdBob,372 'nft-metadata',373 );374375 const resourceId = await addNftBasicResource(376 api,377 Bob,378 'pending',379 collectionIdBob,380 nftAlice,381 src,382 metadata,383 license,384 thumb,385 );386387 const tx = acceptNftResource(api, Bob, collectionIdBob, nftAlice, resourceId);388389 await expectTxFailure(/rmrkCore\.NoPermission/, tx);390 });391392 it('[negative]: unable to accept a resource to a non-target NFT', async () => {393 const collectionIdBob = await createCollection(394 api,395 Bob,396 'test-metadata',397 null,398 'test-symbol',399 );400401 const nftAlice = await mintNft(402 api,403 Bob,404 Alice,405 collectionIdBob,406 'nft-metadata',407 );408409 const wrongNft = await mintNft(410 api,411 Bob,412 Alice,413 collectionIdBob,414 'nft-metadata',415 );416 417 const resourceId = await addNftBasicResource(418 api,419 Bob,420 'pending',421 collectionIdBob,422 nftAlice,423 src,424 metadata,425 license,426 thumb,427 );428429 const tx = acceptNftResource(api, Bob, collectionIdBob, wrongNft, resourceId);430431 await expectTxFailure(/rmrkCore\.ResourceDoesntExist/, tx);432 });433434435 after(() => {436 api.disconnect();437 });438});