Skip to content

Commit

Permalink
Merge pull request #2611 from IntersectMBO/fix/dRep-sort-and-govActio…
Browse files Browse the repository at this point in the history
…n-search-test

Improve dRep Search Test and Governance Action Validation Using IDs
  • Loading branch information
kneerose authored Jan 9, 2025
2 parents a5b5dd9 + ef59257 commit 355de00
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
42 changes: 42 additions & 0 deletions tests/govtool-frontend/playwright/lib/helpers/dRep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import DRepDirectoryPage from "@pages/dRepDirectoryPage";
import { Page } from "@playwright/test";
import { bech32 } from "bech32";

export async function fetchFirstActiveDRepDetails(page: Page) {
let dRepGivenName: string;
Expand Down Expand Up @@ -60,3 +61,44 @@ export async function calculateImageSHA256(imageUrl: string) {
return null;
}
}

export function fromHex(prefix: string, hex: string) {
return bech32.encode(prefix, bech32.toWords(Buffer.from(hex, "hex")));
}

export function tohex(drepId: string) {
return Buffer.from(
bech32.fromWords(bech32.decode(drepId, 100).words)
).toString("hex");
}

export function convertDRepToCIP129(drepId: string, script = false): string {
const hexPattern = /^[0-9a-fA-F]+$/;
let cip129DRep: string;
let cip129DrepHex: string;
const prefix = script ? "23" : "22";
const addPrefix = (hex: string) => {
if (hex.length === 56) {
return prefix + hex;
} else if (hex.length === 58) {
return hex;
} else {
throw new Error("Invalid DRep hex length");
}
};
const drepIdFromHex = (hex: string) => {
return fromHex("drep", hex);
};
if (hexPattern.test(drepId)) {
cip129DrepHex = addPrefix(drepId);
} else {
try {
const decodedHex = tohex(drepId);
cip129DrepHex = addPrefix(decodedHex);
} catch (error: any) {
throw new Error("Invalid DRep Bech32 format");
}
}
cip129DRep = drepIdFromHex(cip129DrepHex);
return cip129DRep;
}
36 changes: 28 additions & 8 deletions tests/govtool-frontend/playwright/lib/pages/dRepDirectoryPage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { convertDRepToCIP129 } from "@helpers/dRep";
import { Locator, Page, expect } from "@playwright/test";
import { IDRep } from "@types";
import environments from "lib/constants/environments";
Expand Down Expand Up @@ -127,23 +128,42 @@ export default class DRepDirectoryPage {
}

// Frontend validation
const dRepListFE = await this.getAllListedDRepIds();
const cip105DRepListFE = await this.getAllListedCIP105DRepIds();
const cip129DRepListFE = await this.getAllListedCIP129DRepIds();

for (let i = 0; i <= dRepListFE.length - 1; i++) {
await expect(dRepListFE[i]).toHaveText(dRepList[i].view);
const cip129DRepListApi = dRepList.map((dRep) =>
convertDRepToCIP129(dRep.drepId, dRep.isScriptBased)
);

for (let i = 0; i <= cip105DRepListFE.length - 1; i++) {
await expect(cip105DRepListFE[i]).toHaveText(dRepList[i].view);
await expect(cip129DRepListFE[i]).toHaveText(
`(CIP-129) ${cip129DRepListApi[i]}`
);
}
}
getDRepCard(dRepId: string) {
return this.page.getByTestId(`${dRepId}-drep-card`);
}

async getAllListedDRepIds() {
async getAllListedCIP105DRepIds() {
await this.page.waitForTimeout(2_000);

return await this.page
.getByRole("list")
.locator('[data-testid$="-copy-id-button"]')
.all();
const dRepCards = await this.getAllListedDReps();

return dRepCards.map((dRep) =>
dRep.locator('[data-testid$="-copy-id-button"]').first()
);
}

async getAllListedCIP129DRepIds() {
await this.page.waitForTimeout(2_000);

const dRepCards = await this.getAllListedDReps();

return dRepCards.map((dRep) =>
dRep.locator('[data-testid$="-copy-id-button"]').last()
);
}

async getAllListedDReps() {
Expand Down
1 change: 1 addition & 0 deletions tests/govtool-frontend/playwright/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export enum FullGovernanceDRepVoteActionsType {
export type DRepStatus = "Active" | "Inactive" | "Retired";

export type IDRep = {
isScriptBased: boolean;
drepId: string;
view: string;
url: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ test("2O. Should load more DReps on show more", async ({ page }) => {
const dRepDirectory = new DRepDirectoryPage(page);
await dRepDirectory.goto();

const dRepIdsBefore = await dRepDirectory.getAllListedDRepIds();
const dRepIdsBefore = await dRepDirectory.getAllListedCIP105DRepIds();
await dRepDirectory.showMoreBtn.click();

const dRepIdsAfter = await dRepDirectory.getAllListedDRepIds();
const dRepIdsAfter = await dRepDirectory.getAllListedCIP105DRepIds();
expect(dRepIdsAfter.length).toBeGreaterThanOrEqual(dRepIdsBefore.length);

if (dRepIdsAfter.length > dRepIdsBefore.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ test("4C_3. Should filter and sort Governance Action Type on governance actions
});

test("4L. Should search governance actions", async ({ page }) => {
let governanceActionTitle = "TreasuryTitle";
let governanceActionId: string;
await page.route("**/proposal/list?**", async (route) => {
const response = await route.fetch();
const data = await response.json();
const elementsWithTitle = data["elements"].filter(
(element) => element["title"]
const elementsWithIds = data["elements"].map(
(element) => element["txHash"] + "#" + element["index"]
);
if (elementsWithTitle.length !== 0) {
governanceActionTitle = elementsWithTitle[0]["title"];
if (elementsWithIds.length !== 0) {
governanceActionId = elementsWithIds[0];
}
await route.fulfill({
status: 200,
Expand All @@ -154,14 +154,14 @@ test("4L. Should search governance actions", async ({ page }) => {

await responsePromise;

await governanceActionPage.searchInput.fill(governanceActionTitle);
await governanceActionPage.searchInput.fill(governanceActionId);

const proposalCards = await governanceActionPage.getAllProposals();

for (const proposalCard of proposalCards) {
expect(
(await proposalCard.textContent()).includes(`${governanceActionTitle}`)
).toBeTruthy();
await expect(
proposalCard.getByTestId(`${governanceActionId}-id`)
).toBeVisible();
}
});

Expand Down

0 comments on commit 355de00

Please sign in to comment.