diff --git a/package-lock.json b/package-lock.json index 26062fb..9ca6da6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "valibot": "^0.36.0" }, "devDependencies": { - "@faker-js/faker": "^8.4.1", "@tsconfig/strictest": "^2.0.5", "prettier": "^3.3.3", "tsup": "^8.2.3", @@ -407,22 +406,6 @@ "node": ">=18" } }, - "node_modules/@faker-js/faker": { - "version": "8.4.1", - "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.4.1.tgz", - "integrity": "sha512-XQ3cU+Q8Uqmrbf2e0cIC/QN43sTBSC8KF12u29Mb47tWrt2hAgBXSgpZMj4Ao8Uk0iJcU99QsOCaIL8934obCg==", - "dev": true, - "funding": [ - { - "type": "opencollective", - "url": "https://opencollective.com/fakerjs" - } - ], - "engines": { - "node": "^14.17.0 || ^16.13.0 || >=18.0.0", - "npm": ">=6.14.13" - } - }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", diff --git a/package.json b/package.json index 8788c92..caa63e0 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,6 @@ "node": ">=20" }, "devDependencies": { - "@faker-js/faker": "^8.4.1", "@tsconfig/strictest": "^2.0.5", "prettier": "^3.3.3", "tsup": "^8.2.3", @@ -67,4 +66,4 @@ "dependencies": { "valibot": "^0.36.0" } -} \ No newline at end of file +} diff --git a/src/marketplace/marketplace.test.ts b/src/marketplace/marketplace.test.ts index e2fc877..77b9949 100644 --- a/src/marketplace/marketplace.test.ts +++ b/src/marketplace/marketplace.test.ts @@ -1,49 +1,22 @@ -import { faker } from "@faker-js/faker"; import { afterEach, assert, describe, it, vi } from "vitest"; import { Fetch } from "../fetch-safe/fetch-safe"; import { CodexMarketplace } from "./marketplace"; - -// function createSlot() { -// return { -// "id": faker.string.alphanumeric(64), -// "request": { - -// "id": faker.string.alphanumeric(64), -// "client": faker.finance.ethereumAddress(), -// "ask": -// { -// "slots": faker.number.int({ min: 0, max: 9 }), -// "slotSize": faker.number.float({ max: 10000 }).toString(), -// "duration": faker.number.int({ max: 300000 }).toString(), -// "proofProbability": faker.number.int({ max: 9 }), -// "reward": faker.number.float({ max: 1000 }).toString(), -// "maxSlotLoss": faker.number.int({ max: 9 }) -// }, -// "content": { -// "cid": faker.string.alphanumeric(64), -// "por": { -// "u": faker.string.alphanumeric(16), -// "publicKey": faker.string.alphanumeric(64), -// "name": faker.string.alphanumeric(16) -// } -// }, -// "expiry": faker.number.int({ min: 2, max: 59 }) + " minutes", -// "nonce": faker.string.alphanumeric(64) -// }, -// "slotIndex": faker.number.int({ min: 0, max: 9 }) -// } -// } +import { + randomEthereumAddress, + randomInt, + randomString, +} from "../tests/tests.util"; function createStorageRequest() { return { - cid: faker.string.alphanumeric(64), - duration: faker.number.int({ min: 1 }), - reward: faker.number.int(), - proofProbability: faker.number.int(), - nodes: faker.number.int(), - tolerance: faker.number.int(), - expiry: faker.number.int({ min: 1 }), - collateral: faker.number.int(), + cid: randomString(64), + duration: randomInt(1, 64000), + reward: randomInt(1, 100), + proofProbability: randomInt(1, 100), + nodes: randomInt(1, 5), + tolerance: randomInt(1, 100), + expiry: randomInt(1, 100), + collateral: randomInt(1, 100), }; } @@ -134,15 +107,15 @@ function minNumberValidationError(field: string, min: number) { function createAvailability() { return { - id: faker.finance.ethereumAddress(), - availabilityId: faker.finance.ethereumAddress(), - size: faker.number.int({ min: 3000, max: 300000 }), - requestId: faker.finance.ethereumAddress(), - slotIndex: faker.number.int({ min: 0, max: 9 }), - totalSize: faker.number.int({ min: 0, max: 9 }).toString(), - duration: faker.number.int({ min: 0, max: 9 }).toString(), - minPrice: faker.number.int({ min: 0, max: 9 }).toString(), - maxCollateral: faker.number.int({ min: 0, max: 9 }).toString(), + id: randomEthereumAddress(), + availabilityId: randomEthereumAddress(), + size: randomInt(3000, 300000), + requestId: randomEthereumAddress(), + slotIndex: randomInt(0, 9), + totalSize: randomInt(0, 9).toString(), + duration: randomInt(0, 9).toString(), + minPrice: randomInt(0, 9).toString(), + maxCollateral: randomInt(0, 9).toString(), }; } @@ -284,7 +257,7 @@ describe("marketplace", () => { it("returns an error when trying to update an availability with zero total size", async () => { const response = await marketplace.updateAvailability({ - id: faker.string.alphanumeric(64), + id: randomString(64), totalSize: 0, }); @@ -293,7 +266,7 @@ describe("marketplace", () => { it("returns an error when trying to update an availability with zero duration", async () => { const response = await marketplace.updateAvailability({ - id: faker.string.alphanumeric(64), + id: randomString(64), duration: 0, }); @@ -307,7 +280,7 @@ describe("marketplace", () => { spy.mockImplementationOnce(() => Promise.resolve({ error: false, data })); const response = await marketplace.updateAvailability({ - id: faker.string.alphanumeric(64), + id: randomString(64), totalSize: 3000, }); diff --git a/src/tests/tests.util.ts b/src/tests/tests.util.ts new file mode 100644 index 0000000..c456104 --- /dev/null +++ b/src/tests/tests.util.ts @@ -0,0 +1,24 @@ +export const randomEthereumAddress = () => { + const randomBytes = crypto.getRandomValues(new Uint8Array(20)); + + // Convert bytes to a hexadecimal string + const address = Array.from(randomBytes) + .map((byte) => ("0" + byte.toString(16)).slice(-2)) // Convert to hex and pad with zero + .join(""); + + return "0x" + address; // Prefix with '0x' +}; + +export const randomString = (length: number) => { + const characters = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let result = ""; + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * characters.length); + result += characters[randomIndex]; + } + return result; +}; + +export const randomInt = (min: number, max: number) => + Math.floor(Math.random() * (max - min)) + min;