Skip to content

Commit

Permalink
Add unit tests about getLicenseTermByType
Browse files Browse the repository at this point in the history
  • Loading branch information
bonnie57 committed Apr 30, 2024
1 parent 1a9f3ed commit 590ea24
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 8 deletions.
29 changes: 21 additions & 8 deletions packages/core-sdk/src/utils/getLicenseTermsByType.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, zeroAddress } from "viem";
import { Hex, getAddress, zeroAddress } from "viem";

import { PIL_TYPE, LicenseTerms } from "../types/resources/license";

Expand Down Expand Up @@ -33,21 +33,34 @@ export function getLicenseTermByType(
if (type === PIL_TYPE.NON_COMMERCIAL_REMIX) {
return licenseTerms;
} else if (type === PIL_TYPE.COMMERCIAL_USE) {
licenseTerms.royaltyPolicy = term!.royaltyPolicyLAPAddress;
licenseTerms.mintingFee = BigInt(term!.mintingFee);
if (!term || term.mintingFee === undefined || term.currency === undefined) {
throw new Error("mintingFee currency are required for commercial use PIL.");
}
licenseTerms.royaltyPolicy = getAddress(term.royaltyPolicyLAPAddress);
licenseTerms.mintingFee = BigInt(term.mintingFee);
licenseTerms.commercialUse = true;
licenseTerms.commercialAttribution = true;
licenseTerms.derivativesReciprocal = false;
licenseTerms.currency = term!.currency;
licenseTerms.currency = getAddress(term.currency);
return licenseTerms;
} else {
licenseTerms.royaltyPolicy = term!.royaltyPolicyLAPAddress;
licenseTerms.mintingFee = BigInt(term!.mintingFee);
if (
!term ||
term.mintingFee === undefined ||
term.currency === undefined ||
term.commercialRevShare === undefined
) {
throw new Error(
"mintingFee, currency and commercialRevShare are required for commercial remix PIL.",
);
}
licenseTerms.royaltyPolicy = getAddress(term.royaltyPolicyLAPAddress);
licenseTerms.mintingFee = BigInt(term.mintingFee!);
licenseTerms.commercialUse = true;
licenseTerms.commercialAttribution = true;
licenseTerms.commercialRevShare = term!.commercialRevShare!;
licenseTerms.commercialRevShare = term.commercialRevShare;
licenseTerms.derivativesReciprocal = true;
licenseTerms.currency = term!.currency;
licenseTerms.currency = getAddress(term.currency);
return licenseTerms;
}
}
175 changes: 175 additions & 0 deletions packages/core-sdk/test/unit/utils/getLicenseTermsByType.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { Hex, zeroAddress } from "viem";
import { PIL_TYPE } from "../../../src/types/resources/license";
import { getLicenseTermByType } from "../../../src/utils/getLicenseTermsByType";
import { expect } from "chai";

describe("Get License Terms By Type", () => {
it("it should return no commercial license terms when call getLicenseTermByType given NON_COMMERCIAL_REMIX", async () => {
const result = getLicenseTermByType(PIL_TYPE.NON_COMMERCIAL_REMIX);
expect(result).to.deep.include({
transferable: true,
commercialAttribution: false,
commercialRevCelling: 0n,
commercialRevShare: 0,
commercialUse: false,
commercializerChecker: "0x0000000000000000000000000000000000000000",
commercializerCheckerData: "0x0000000000000000000000000000000000000000",
currency: "0x0000000000000000000000000000000000000000",
derivativeRevCelling: 0n,
derivativesAllowed: true,
derivativesApproval: false,
derivativesAttribution: true,
derivativesReciprocal: true,
expiration: 0n,
mintingFee: 0n,
royaltyPolicy: "0x0000000000000000000000000000000000000000",
uri: "",
});
});

describe("Get Commercial License Terms", () => {
it("it should throw when call getLicenseTermByType given COMMERCIAL_USE without terms", async () => {
expect(() => getLicenseTermByType(PIL_TYPE.COMMERCIAL_USE)).to.throw(
"mintingFee currency are required for commercial use PIL.",
);
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_USE without mintFee", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_USE, {
currency: zeroAddress,
royaltyPolicyLAPAddress: zeroAddress,
}),
).to.throw("mintingFee currency are required for commercial use PIL.");
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_USE without currency", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_USE, {
royaltyPolicyLAPAddress: zeroAddress,
mintingFee: "1",
}),
).to.throw("mintingFee currency are required for commercial use PIL.");
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_USE and wrong royaltyAddress", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_USE, {
royaltyPolicyLAPAddress: "wrong" as Hex,
mintingFee: "1",
currency: zeroAddress,
}),
).to.throw(`Address "wrong" is invalid.`);
});

it("it should return commercial license terms when call getLicenseTermByType given COMMERCIAL_USE and correct args", async () => {
const result = getLicenseTermByType(PIL_TYPE.COMMERCIAL_USE, {
royaltyPolicyLAPAddress: zeroAddress,
mintingFee: "1",
currency: zeroAddress,
});
expect(result).to.deep.contain({
commercialAttribution: true,
commercialRevCelling: 0n,
commercialRevShare: 0,
commercialUse: true,
commercializerChecker: "0x0000000000000000000000000000000000000000",
commercializerCheckerData: "0x0000000000000000000000000000000000000000",
currency: "0x0000000000000000000000000000000000000000",
derivativeRevCelling: 0n,
derivativesAllowed: true,
derivativesApproval: false,
derivativesAttribution: true,
derivativesReciprocal: false,
expiration: 0n,
mintingFee: 1n,
royaltyPolicy: "0x0000000000000000000000000000000000000000",
transferable: true,
uri: "",
});
});
});

describe("Get Commercial remix License Terms", () => {
it("it should throw when call getLicenseTermByType given COMMERCIAL_REMIX without terms", async () => {
expect(() => getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX)).to.throw(
"mintingFee, currency and commercialRevShare are required for commercial remix PIL.",
);
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_REMIX without mintFee", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX, {
currency: zeroAddress,
royaltyPolicyLAPAddress: zeroAddress,
commercialRevShare: 100,
}),
).to.throw(
"mintingFee, currency and commercialRevShare are required for commercial remix PIL.",
);
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_REMIX without currency", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX, {
royaltyPolicyLAPAddress: zeroAddress,
mintingFee: "1",
commercialRevShare: 100,
}),
).to.throw(
"mintingFee, currency and commercialRevShare are required for commercial remix PIL.",
);
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_REMIX and wrong royaltyAddress", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX, {
royaltyPolicyLAPAddress: "wrong" as Hex,
mintingFee: "1",
currency: zeroAddress,
commercialRevShare: 100,
}),
).to.throw(`Address "wrong" is invalid.`);
});

it("it should throw when call getLicenseTermByType given COMMERCIAL_REMIX without commercialRevShare ", async () => {
expect(() =>
getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX, {
royaltyPolicyLAPAddress: "wrong" as Hex,
mintingFee: "1",
currency: zeroAddress,
}),
).to.throw(
`mintingFee, currency and commercialRevShare are required for commercial remix PIL.`,
);
});

it("it should return commercial license terms when call getLicenseTermByType given COMMERCIAL_REMIX and correct args", async () => {
const result = getLicenseTermByType(PIL_TYPE.COMMERCIAL_REMIX, {
royaltyPolicyLAPAddress: zeroAddress,
mintingFee: "1",
currency: zeroAddress,
commercialRevShare: 100,
});
expect(result).to.deep.contain({
commercialAttribution: true,
commercialRevCelling: 0n,
commercialRevShare: 100,
commercialUse: true,
commercializerChecker: "0x0000000000000000000000000000000000000000",
commercializerCheckerData: "0x0000000000000000000000000000000000000000",
currency: "0x0000000000000000000000000000000000000000",
derivativeRevCelling: 0n,
derivativesAllowed: true,
derivativesApproval: false,
derivativesAttribution: true,
derivativesReciprocal: true,
expiration: 0n,
mintingFee: 1n,
royaltyPolicy: "0x0000000000000000000000000000000000000000",
transferable: true,
uri: "",
});
});
});
});

0 comments on commit 590ea24

Please sign in to comment.