From 1f190c6cc5708890d6aba24f38efc1963b86f992 Mon Sep 17 00:00:00 2001 From: Nhan Phan Date: Fri, 8 Mar 2024 19:10:23 -0800 Subject: [PATCH] refactor collection update delegate tests --- clients/js/test/_setup.ts | 16 ++- .../collectionUpdateDelegate.test.ts | 112 ++++-------------- 2 files changed, 33 insertions(+), 95 deletions(-) diff --git a/clients/js/test/_setup.ts b/clients/js/test/_setup.ts index c1937580..60b08eca 100644 --- a/clients/js/test/_setup.ts +++ b/clients/js/test/_setup.ts @@ -3,7 +3,6 @@ import { createUmi as basecreateUmi } from '@metaplex-foundation/umi-bundle-test import { Assertions } from 'ava'; import { PublicKey, Signer, Umi, generateSigner, publicKey } from '@metaplex-foundation/umi'; import { - Collection, DataState, Key, create, @@ -27,6 +26,7 @@ export type CreateAssetHelperArgs = { dataState?: DataState; name?: string; uri?: string; + authority?: Signer; updateAuthority?: PublicKey | Signer collection?: PublicKey // TODO use PluginList type here @@ -61,6 +61,7 @@ export const createAsset = async ( uri: input.uri || DEFAULT_ASSET.uri, plugins: input.plugins || [], collection: input.collection, + authority: input.authority }).sendAndConfirm(umi); return fetchAssetWithPlugins(umi, publicKey(asset)); @@ -93,10 +94,10 @@ export const createCollection = async (umi: Umi, input:CreateCollectionHelperArg export const createAssetWithCollection: ( umi: Umi, - assetInput: CreateAssetHelperArgs & { collection?: Collection }, + assetInput: CreateAssetHelperArgs & { collection?: PublicKey | Signer}, collectionInput?: CreateCollectionHelperArgs ) => Promise<{ asset: AssetWithPlugins; collection: CollectionWithPlugins }> = async (umi, assetInput, collectionInput = {}) => { - const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, assetInput.collection.publicKey) : await createCollection(umi, { + const collection = assetInput.collection ? await fetchCollectionWithPlugins(umi, publicKey(assetInput.collection)) : await createCollection(umi, { payer: assetInput.payer, updateAuthority: assetInput.updateAuthority, ...collectionInput @@ -159,7 +160,7 @@ export const assertCollection = async ( umi: Umi, input: { collection: PublicKey | Signer; - updateAuthority: PublicKey | Signer; + updateAuthority?: PublicKey | Signer; name?: string | RegExp; uri?: string | RegExp; numMinted?: number; @@ -169,8 +170,7 @@ export const assertCollection = async ( } ) => { const collectionAddress = publicKey(input.collection); - const updateAuthority = publicKey(input.updateAuthority); - const { name, uri, numMinted, currentSize } = input; + const { name, uri, numMinted, currentSize, updateAuthority } = input; const collection = await fetchCollectionWithPlugins(umi, collectionAddress); // Name. @@ -196,6 +196,10 @@ export const assertCollection = async ( testObj.currentSize = currentSize; } + if(updateAuthority) { + testObj.updateAuthority = publicKey(updateAuthority); + } + t.like(collection, testObj); }; diff --git a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts index 476cbed7..05de2c69 100644 --- a/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts +++ b/clients/js/test/plugins/collection/collectionUpdateDelegate.test.ts @@ -2,88 +2,38 @@ import { generateSigner } from '@metaplex-foundation/umi'; import test from 'ava'; import { generateSignerWithSol } from '@metaplex-foundation/umi-bundle-tests'; import { - AssetWithPlugins, - CollectionWithPlugins, - DataState, PluginType, - create, - createCollection, - fetchAssetWithPlugins, - fetchCollectionWithPlugins, updateAuthority, - addCollectionPlugin, approveCollectionPluginAuthority, + plugin, + authority, } from '../../../src'; -import { createUmi } from '../../_setup'; +import { DEFAULT_ASSET, DEFAULT_COLLECTION, assertAsset, assertCollection, createAsset, createCollection, createUmi } from '../../_setup'; test('it can create a new asset with a collection if it is the collection update delegate', async (t) => { // Given a Umi instance and a new signer. const umi = await createUmi(); - const collectionAddress = generateSigner(umi); - const assetAddress = generateSigner(umi); const updateDelegate = await generateSignerWithSol(umi); // When we create a new account. - await createCollection(umi, { - collection: collectionAddress, - name: 'Test Bread Collection', - uri: 'https://example.com/bread', - plugins: [], - }).sendAndConfirm(umi); - - await addCollectionPlugin(umi, { - collection: collectionAddress.publicKey, - plugin: { - __kind: 'UpdateDelegate', - fields: [{}], - }, - initAuthority: null - }).sendAndConfirm(umi); - - // console.log(JSON.stringify(await fetchCollectionWithPlugins(umi, collectionAddress.publicKey), (_, v) => typeof v === 'bigint' ? v.toString() : v, 2)); + const collection = await createCollection(umi, { + plugins: [plugin('UpdateDelegate', [{}])] + }) await approveCollectionPluginAuthority(umi, { - collection: collectionAddress.publicKey, + collection: collection.publicKey, pluginType: PluginType.UpdateDelegate, - newAuthority: { - __kind: 'Pubkey', - address: updateDelegate.publicKey, - }, + newAuthority: authority('Pubkey', { address: updateDelegate.publicKey }), }).sendAndConfirm(umi); - const collection = await fetchCollectionWithPlugins( - umi, - collectionAddress.publicKey - ); - // console.log("Account State:", collection); - t.like(collection, { - publicKey: collectionAddress.publicKey, + await assertCollection(t, umi, { + ...DEFAULT_COLLECTION, + collection: collection.publicKey, updateAuthority: umi.identity.publicKey, - name: 'Test Bread Collection', - uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(105), - }, - pluginRegistry: { - key: 4, - registry: [ - { - pluginType: PluginType.UpdateDelegate, - offset: BigInt(104), - authority: - { __kind: 'Pubkey', address: updateDelegate.publicKey }, - }, - ], - }, plugins: [ { - authority: - { __kind: 'Pubkey', address: updateDelegate.publicKey }, - plugin: { - __kind: 'UpdateDelegate', - fields: [{}], - }, + authority: authority('Pubkey', { address: updateDelegate.publicKey }), + plugin: plugin('UpdateDelegate', [{}]), }, ], }); @@ -92,33 +42,17 @@ test('it can create a new asset with a collection if it is the collection update umi.payer = updateDelegate; const owner = generateSigner(umi); // When we create a new account. - await create(umi, { - owner: owner.publicKey, - dataState: DataState.AccountState, - asset: assetAddress, - name: 'Test Bread', - uri: 'https://example.com/bread', - collection: collectionAddress.publicKey, - plugins: [], - }).sendAndConfirm(umi); - - const asset = await fetchAssetWithPlugins(umi, assetAddress.publicKey); - // console.log("Asset State:", asset); - t.like(asset, { - publicKey: assetAddress.publicKey, - updateAuthority: updateAuthority('Collection', [ - collectionAddress.publicKey, - ]), + const asset = await createAsset(umi, { + collection: collection.publicKey, + owner, + authority: updateDelegate, + }) + + await assertAsset(t, umi, { + ...DEFAULT_ASSET, + asset: asset.publicKey, owner: owner.publicKey, - name: 'Test Bread', - uri: 'https://example.com/bread', - pluginHeader: { - key: 3, - pluginRegistryOffset: BigInt(118), - }, - pluginRegistry: { - key: 4, - }, + updateAuthority: updateAuthority('Collection', [collection.publicKey]), }); t.assert(asset.pluginRegistry?.registry.length === 0);