Skip to content

Commit

Permalink
Do not convert MsgPack encoded data to String in tests
Browse files Browse the repository at this point in the history
Also add MsgPack to LinkedAppData tests
  • Loading branch information
danenbm committed Jul 26, 2024
1 parent faa5232 commit 31ca65d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
42 changes: 22 additions & 20 deletions clients/js/test/externalPlugins/appData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type TestContext = {
dataAuthority: PluginAuthority;
wrongDataAuthoritySigner?: Signer;
wrongDataAuthority?: PluginAuthority;
data: string;
otherData: string;
data: Uint8Array;
otherData: Uint8Array;
};

async function generateTestContext(
Expand Down Expand Up @@ -76,19 +76,21 @@ async function generateTestContext(
}
}

let data = '';
let otherData = '';
let data = new Uint8Array();
let otherData = new Uint8Array();

if (schema === ExternalPluginAdapterSchema.Binary) {
data = 'Hello, world!';
otherData = 'Hello, world! Hello, world!';
const binaryData = 'Hello, world!';
const binaryOtherData = 'Hello, world! Hello, world!';
data = Uint8Array.from(Buffer.from(binaryData));
otherData = Uint8Array.from(Buffer.from(binaryOtherData));
} else if (schema === ExternalPluginAdapterSchema.Json) {
data = JSON.stringify({ message: 'Hello', target: 'world' });
otherData = JSON.stringify({
message: 'Hello hello',
target: 'big wide world',
});
const dataJson = { message: 'Hello', target: 'world' };
const otherDataJson = { message: 'Hello hello', target: 'big wide world' };
data = Uint8Array.from(Buffer.from(JSON.stringify(dataJson)));
otherData = Uint8Array.from(Buffer.from(JSON.stringify(otherDataJson)));
} else if (schema === ExternalPluginAdapterSchema.MsgPack) {
data = msgpack.encode({ message: 'Hello', target: 'msgpack' }).toString();
data = msgpack.encode({ message: 'Hello', target: 'msgpack' });
}

if (!dataAuthoritySigner) {
Expand Down Expand Up @@ -181,7 +183,7 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
dataAuthority,
},
authority: dataAuthoritySigner,
data: Uint8Array.from(Buffer.from(data)),
data,
asset: asset.publicKey,
}).sendAndConfirm(umi);

Expand All @@ -190,9 +192,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
schema === ExternalPluginAdapterSchema.Binary ||
schema === ExternalPluginAdapterSchema.MsgPack
) {
assertData = Uint8Array.from(Buffer.from(data));
assertData = data;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(data);
assertData = JSON.parse(Buffer.from(data).toString());
}

await assertAsset(t, umi, {
Expand Down Expand Up @@ -262,9 +264,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
schema === ExternalPluginAdapterSchema.Binary ||
schema === ExternalPluginAdapterSchema.MsgPack
) {
assertData = Uint8Array.from(Buffer.from(data));
assertData = data;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(data);
assertData = JSON.parse(Buffer.from(data).toString());
}

await assertAsset(t, umi, {
Expand Down Expand Up @@ -296,9 +298,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
schema === ExternalPluginAdapterSchema.Binary ||
schema === ExternalPluginAdapterSchema.MsgPack
) {
assertData = Uint8Array.from(Buffer.from(otherData));
assertData = otherData;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(otherData);
assertData = JSON.parse(Buffer.from(otherData).toString());
}

await assertAsset(t, umi, {
Expand Down Expand Up @@ -452,7 +454,7 @@ test(`updating a plugin before a secure app data does not corrupt the data`, asy
asset: asset.publicKey,
}).sendAndConfirm(umi);

const assertData = JSON.parse(data);
const assertData = JSON.parse(Buffer.from(data).toString());

await assertAsset(t, umi, {
...DEFAULT_ASSET,
Expand Down
41 changes: 23 additions & 18 deletions clients/js/test/externalPlugins/linkedAppData.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava';
import { generateSignerWithSol } from '@metaplex-foundation/umi-bundle-tests';
import { Signer, Umi } from '@metaplex-foundation/umi';
import * as msgpack from '@msgpack/msgpack';
import {
assertAsset,
assertCollection,
Expand Down Expand Up @@ -35,8 +36,8 @@ type TestContext = {
dataAuthority: PluginAuthority;
wrongDataAuthoritySigner?: Signer;
wrongDataAuthority?: PluginAuthority;
data: string;
otherData: string;
data: Uint8Array;
otherData: Uint8Array;
};

async function generateTestContext(
Expand Down Expand Up @@ -81,17 +82,21 @@ async function generateTestContext(
}
}

let data = '';
let otherData = '';
let data = new Uint8Array();
let otherData = new Uint8Array();

if (schema === ExternalPluginAdapterSchema.Binary) {
data = 'Hello, world!';
otherData = 'Hello, world! Hello, world!';
const binaryData = 'Hello, world!';
const binaryOtherData = 'Hello, world! Hello, world!';
data = Uint8Array.from(Buffer.from(binaryData));
otherData = Uint8Array.from(Buffer.from(binaryOtherData));
} else if (schema === ExternalPluginAdapterSchema.Json) {
data = JSON.stringify({ message: 'Hello', target: 'world' });
otherData = JSON.stringify({
message: 'Hello hello',
target: 'big wide world',
});
const dataJson = { message: 'Hello', target: 'world' };
const otherDataJson = { message: 'Hello hello', target: 'big wide world' };
data = Uint8Array.from(Buffer.from(JSON.stringify(dataJson)));
otherData = Uint8Array.from(Buffer.from(JSON.stringify(otherDataJson)));
} else if (schema === ExternalPluginAdapterSchema.MsgPack) {
data = msgpack.encode({ message: 'Hello', target: 'msgpack' });
}

if (!dataAuthoritySigner) {
Expand Down Expand Up @@ -202,9 +207,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
schema === ExternalPluginAdapterSchema.Binary ||
schema === ExternalPluginAdapterSchema.MsgPack
) {
assertData = Uint8Array.from(Buffer.from(data));
assertData = data;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(data);
assertData = JSON.parse(Buffer.from(data).toString());
}

// check the derived asset sdk correctly injects the data
Expand Down Expand Up @@ -299,9 +304,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
schema === ExternalPluginAdapterSchema.Binary ||
schema === ExternalPluginAdapterSchema.MsgPack
) {
assertData = Uint8Array.from(Buffer.from(data));
assertData = data;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(data);
assertData = JSON.parse(Buffer.from(data).toString());
}

// check the derived asset sdk correctly injects the data
Expand Down Expand Up @@ -351,9 +356,9 @@ DATA_AUTHORITIES.forEach((dataAuthorityType) => {
}).sendAndConfirm(umi);

if (schema === ExternalPluginAdapterSchema.Binary) {
assertData = Uint8Array.from(Buffer.from(otherData));
assertData = otherData;
} else if (schema === ExternalPluginAdapterSchema.Json) {
assertData = JSON.parse(otherData);
assertData = JSON.parse(Buffer.from(otherData).toString());
}

// check the derived asset sdk correctly injects the data
Expand Down Expand Up @@ -559,7 +564,7 @@ test(`updating a plugin before a secure app data does not corrupt the data`, asy
],
});

const assertData = Uint8Array.from(Buffer.from(data));
const assertData = data;

// check the derived asset sdk correctly injects the data
await assertAsset(
Expand Down

0 comments on commit 31ca65d

Please sign in to comment.