Skip to content

Commit

Permalink
[GUILD-2017] - Requirement types (#25)
Browse files Browse the repository at this point in the history
* test improvements

* update @guildxyz/types

* fix some tests

* use randomly generated wallets

* use proper update type

* upgrade @guildxyz/types

* 2.3.0
  • Loading branch information
cs-balazs authored Jan 11, 2024
1 parent 0087a3c commit fd7ffa0
Show file tree
Hide file tree
Showing 19 changed files with 464 additions and 483 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guildxyz/sdk",
"version": "2.2.0",
"version": "2.3.0",
"description": "SDK for Guild.xyz Public API ",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -43,7 +43,7 @@
"testWithPrivileged": "npx dotenv-cli -e .test.privileged.env -- npm run test"
},
"dependencies": {
"@guildxyz/types": "^1.3.2",
"@guildxyz/types": "^1.3.9",
"ethers": "^6.7.1",
"randombytes": "^2.1.0"
},
Expand Down
10 changes: 7 additions & 3 deletions src/clients/requirement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Requirement, Schemas } from "@guildxyz/types";
import {
Requirement,
RequirementUpdatePayload,
Schemas,
} from "@guildxyz/types";
import { SignerFunction, callGuildAPI } from "../utils";

const requirement = {
Expand Down Expand Up @@ -31,7 +35,7 @@ const requirement = {
requirementCreationParams: Schemas["RequirementCreationPayload"],
signer: SignerFunction
) =>
callGuildAPI<Requirement>({
callGuildAPI<Schemas["RequirementCreateResponse"]>({
url: `/guilds/${guildIdOrUrlName}/roles/${roleId}/requirements`,
method: "POST",
body: {
Expand All @@ -45,7 +49,7 @@ const requirement = {
guildIdOrUrlName: string | number,
roleId: number,
requirementId: number,
requirementUpdateParams: Schemas["RequirementUpdatePayload"],
requirementUpdateParams: RequirementUpdatePayload,
signer: SignerFunction
) =>
callGuildAPI<Requirement>({
Expand Down
2 changes: 1 addition & 1 deletion tests/callGuildAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe.concurrent("callGuildAPI", () => {
method: "POST",
body: {
schema: "RequirementCreationPayloadSchema",
data: { type: "ALLOWLIST" },
data: { type: "ALLOWLIST", data: { addresses: [] } },
},
signer,
});
Expand Down
2 changes: 1 addition & 1 deletion tests/clients/actions/accessCheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TEST_WALLET_SIGNER = createSigner.fromEthersWallet(

const { guild } = createGuildClient("vitest");

describe("Access check action", () => {
describe.skip("Access check action", () => {
it("can check access", async () => {
// const onPoll = vi.fn();

Expand Down
2 changes: 1 addition & 1 deletion tests/clients/actions/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TEST_WALLET_SIGNER = createSigner.fromEthersWallet(

const { guild } = createGuildClient("vitest");

describe("Join action", () => {
describe.skip("Join action", () => {
it("can join", async () => {
// const onPoll = vi.fn();
const result = await guild.join(
Expand Down
154 changes: 39 additions & 115 deletions tests/clients/guild.test.ts
Original file line number Diff line number Diff line change
@@ -1,140 +1,64 @@
import { Guild } from "@guildxyz/types";
import { Wallet } from "ethers";
import { assert, describe, expect, it } from "vitest";
import { createGuildClient } from "../../src";
import { GuildSDKValidationError } from "../../src/error";
import { createSigner } from "../../src/utils";
import { CLIENT, TEST_SIGNER, TEST_USER } from "../common";
import { createTestGuild, pick } from "../utils";

// TODO Create test guilds for these instead of using data like our-guild
const createdGuild = await createTestGuild();
const createdGuild2 = await createTestGuild();

const TEST_WALLET_SIGNER = createSigner.fromEthersWallet(
new Wallet(process.env.PRIVATE_KEY!)
);

const { guild } = createGuildClient("vitest");

describe.concurrent("Guild client", () => {
describe("Guild client", () => {
it("Can get a guild by id", async () => {
const response = await guild.get(1985);
const response = await CLIENT.guild.get(createdGuild.id);

expect(response).toMatchObject({
urlName: "our-guild",
name: "Our Guild",
} satisfies Partial<Guild>);
expect(response).toMatchObject(pick(createdGuild, ["id", "urlName"]));
});

it("Can get multiple guilds by ids", async () => {
const response = await guild.getMany([1985, 20111]);

expect(response).toMatchObject([
{
urlName: "our-guild",
name: "Our Guild",
} satisfies Partial<Guild>,
{
urlName: "buildonbase",
name: "Base Guild",
} satisfies Partial<Guild>,
const response = await CLIENT.guild.getMany([
createdGuild.id,
createdGuild2.id,
]);
});

it("Can get multiple guilds with pagination", async () => {
const [
[firstGuild, , , , fifthGuild],
[fifthGuildWithPagination],
[firstGuildWithPagination],
] = await Promise.all([
guild.search({ limit: 5, offset: 0 }),
guild.search({
limit: 1,
offset: 4,
}),
guild.search({
limit: 1,
offset: 0,
}),
expect(response).toMatchObject([
pick(createdGuild, ["id", "urlName"]),
pick(createdGuild2, ["id", "urlName"]),
]);

expect(fifthGuildWithPagination.id).toEqual(fifthGuild.id);
expect(firstGuildWithPagination.id).toEqual(firstGuild.id);
});

it("Can get guild members", async () => {
const numberOfPublicRoles = await guild.role
.getAll(1985)
.then((res) => res.length);
const response = await CLIENT.guild.getMembers(createdGuild.id);

const response = await guild.getMembers(1985);

expect(response).toHaveLength(numberOfPublicRoles);
expect(response).toHaveLength(1);
});

it("Can get user membership for guild", async () => {
const numberOfPublicRoles = await guild.role
.getAll(1985)
.then((res) => res.length);

const response = await guild.getUserMemberships(1985, 4226297);
const response = await CLIENT.guild.getUserMemberships(
createdGuild.id,
TEST_USER.id
);

expect(response).toHaveLength(numberOfPublicRoles);
expect(response).toHaveLength(1);
});

describe("guild create - update - delete", () => {
let createdGuildId: number;

it("Can create guild", async () => {
const response = await guild.create(
{
name: "SDK Test Guild",
urlName: "sdk-test-guild",
roles: [
{
name: "SDK Test Role",
requirements: [{ type: "FREE" }],
},
],
},
TEST_WALLET_SIGNER
);

createdGuildId = response.id;

expect(response).toMatchObject({
name: "SDK Test Guild",
roles: [
{
name: "SDK Test Role",
requirements: [{ type: "FREE" }],
},
],
});
expect(response.urlName.startsWith("sdk-test-guild")).toBeTruthy();
});

it("Can update guild", async () => {
try {
await guild.update(createdGuildId, {}, TEST_WALLET_SIGNER);
assert(false);
} catch (error) {
expect(error).toBeInstanceOf(GuildSDKValidationError);
}

const updated = await guild.update(
createdGuildId,
{ description: "EDITED" },
TEST_WALLET_SIGNER
);

expect(updated.description).toMatchObject("EDITED");
});

it("Subsequent GET returns updated data", async () => {
const fetchedGuild = await guild.get(createdGuildId);
expect(fetchedGuild.description).toEqual("EDITED");
});
it("Can update guild", async () => {
try {
await CLIENT.guild.update(createdGuild.id, {}, TEST_SIGNER);
assert(false);
} catch (error) {
expect(error).toBeInstanceOf(GuildSDKValidationError);
}

const updated = await CLIENT.guild.update(
createdGuild.id,
{ description: "EDITED" },
TEST_SIGNER
);

expect(updated.description).toMatchObject("EDITED");
});

it("Can delete guild", async () => {
await guild.delete(createdGuildId, TEST_WALLET_SIGNER);
});
it("Subsequent GET returns updated data", async () => {
const fetchedGuild = await CLIENT.guild.get(createdGuild.id);
expect(fetchedGuild.description).toEqual("EDITED");
});
});
34 changes: 16 additions & 18 deletions tests/clients/guildAdmin.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
import { Wallet } from "ethers";
import { describe, expect, it } from "vitest";
import { GuildAPICallFailed, createGuildClient, createSigner } from "../../src";
import { GuildAPICallFailed } from "../../src";
import { CLIENT, TEST_SIGNER, TEST_USER } from "../common";
import { createTestGuild } from "../utils";

const client = createGuildClient("vitest");

const TEST_WALLET_SIGNER = createSigner.fromEthersWallet(
new Wallet(process.env.PRIVATE_KEY!)
);
const GUILD_ID = "sdk-test-guild-62011a";
const adminAddress = Wallet.createRandom().address.toLowerCase();

const guild = await createTestGuild();

describe("Guild admins", () => {
it("get all", async () => {
const admins = await client.guild.admin.getAll(1985);
const admins = await CLIENT.guild.admin.getAll(guild.id);
expect(admins.length).toBeGreaterThan(0);
});

it("get", async () => {
const admin = await client.guild.admin.get(1985, 45);
expect(admin).toMatchObject({ userId: 45 });
const admin = await CLIENT.guild.admin.get(guild.id, TEST_USER.id);
expect(admin).toMatchObject({ userId: TEST_USER.id });
});

let adminUserId: number;

it("create", async () => {
const newAdmin = await client.guild.admin.create(
GUILD_ID,
const newAdmin = await CLIENT.guild.admin.create(
guild.id,
{
address: adminAddress,
isOwner: false,
},
TEST_WALLET_SIGNER
TEST_SIGNER
);

adminUserId = newAdmin.userId;
Expand All @@ -39,22 +37,22 @@ describe("Guild admins", () => {
});

it("get created admin", async () => {
const admin = await client.guild.admin.get(GUILD_ID, adminUserId);
const admin = await CLIENT.guild.admin.get(guild.id, adminUserId);
expect(admin).toMatchObject({ userId: adminUserId, isOwner: false });
});

it("delete", async () => {
await client.guild.admin.delete(
GUILD_ID,
await CLIENT.guild.admin.delete(
guild.id,
adminUserId,

TEST_WALLET_SIGNER
TEST_SIGNER
);
});

it("can't get created admin", async () => {
try {
await client.guild.admin.get(GUILD_ID, adminUserId);
await CLIENT.guild.admin.get(guild.id, adminUserId);
} catch (error) {
expect(error).toBeInstanceOf(GuildAPICallFailed);
expect(error.statusCode).toEqual(404);
Expand Down
Loading

0 comments on commit fd7ffa0

Please sign in to comment.