Skip to content

Commit

Permalink
feat(sdk): zod validation and vitest testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybuidl committed Jan 12, 2024
1 parent ac22739 commit 0ef5d26
Show file tree
Hide file tree
Showing 8 changed files with 992 additions and 59 deletions.
52 changes: 8 additions & 44 deletions kleros-sdk/dataMappings/utils/disputeDetailsTypes.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,8 @@
export type DisputeDetails = {
title: string;
description: string;
question: string;
type: QuestionType;
answers: Answer[];
policyURI: string;
attachment: Attachment;
frontendUrl: string;
arbitrableChainID: string;
arbitrableAddress: `0x${string}`;
arbitratorChainID: string;
arbitratorAddress: `0x${string}`;
category: string;
lang: string;
specification: string;
aliases?: Alias[];
version: string;
};

export enum QuestionType {
Bool = "bool",
Datetime = "datetime",
MultipleSelect = "multiple-select",
SingleSelect = "single-select",
Uint = "uint",
}

export type Answer = {
title: string;
description: string;
id: `0x${string}`;
reserved: boolean;
};

export type Alias = {
name: string;
address: `0x${string}` | `${string}.eth`;
};

export type Attachment = {
label: string;
uri: string;
};
import { z } from "zod";
import DisputeDetailsSchema, { AliasSchema, AnswerSchema, AttachmentSchema } from "./disputeDetailsSchema";

export { QuestionType } from "./disputeDetailsSchema";
export type DisputeDetails = z.infer<typeof DisputeDetailsSchema>;
export type Answer = z.infer<typeof AnswerSchema>;
export type Alias = z.infer<typeof AliasSchema>;
export type Attachment = z.infer<typeof AttachmentSchema>;
11 changes: 8 additions & 3 deletions kleros-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@
},
"scripts": {
"build": "your-build-script",
"test-data-mappings": "mocha -r ts-node/register/transpile-only dataMappings/test.ts"
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run"
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"@vitest/ui": "^1.1.3",
"chai": "^4.3.8",
"dotenv": "^16.3.1",
"mocha": "^10.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^1.1.3"
},
"dependencies": {
"@reality.eth/reality-eth-lib": "^3.2.30"
"@reality.eth/reality-eth-lib": "^3.2.30",
"zod": "^3.22.4"
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect } from "chai";
import { populateTemplate } from "./utils/populateTemplate";
import { jsonAction } from "./actions/jsonAction";
import { subgraphAction } from "./actions/subgraphAction";
import { callAction } from "./actions/callAction";
import { eventAction } from "./actions/eventAction";
import { fetchIpfsJsonAction } from "./actions/fetchIpfsJsonAction";
import { describe, expect, it } from "vitest";
import { populateTemplate } from "dataMappings/utils/populateTemplate";
import { jsonAction } from "dataMappings/actions/jsonAction";
import { subgraphAction } from "dataMappings/actions/subgraphAction";
import { callAction } from "dataMappings/actions/callAction";
import { eventAction } from "dataMappings/actions/eventAction";
import { fetchIpfsJsonAction } from "dataMappings/actions/fetchIpfsJsonAction";

const exampleObject = {
evidence: {
Expand Down
97 changes: 97 additions & 0 deletions kleros-sdk/test/disputeDetailsSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, it } from "vitest";
import { ethAddressSchema, ensNameSchema, ethAddressOrEnsNameSchema } from "dataMappings/utils/disputeDetailsSchema";

describe("Dispute Details Schema", () => {
it("snapshot", () => {
expect({ foo: "bar" }).toMatchSnapshot();
});

const validAddresses = [
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // checksummed
"0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045", // uppercase
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045", // lowercase
];

const invalidAddressesNoEns = [
"",
"yo",
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA960",
"d8dA6BF26964aF9D7eEd9e03E53415D37aA960",
"xd8dA6BF26964aF9D7eEd9e03E53415D37aA960",
"0xd8Wa6bf26964af9d7eed9e03e53415d37aa96045",
];

const validEnsNames = ["vitalik.eth", "vitalik.eth.eth"];

const invalidEnsNamesNoAddress = ["", "vitalik", "vitalik.ether", "vitalik.sol", "eth.vitalik"];

describe("ethAddressSchema", () => {
it("Should accept a valid address", async () => {
validAddresses.forEach((address) => {
expect(() => ethAddressSchema.parse(address)).not.toThrow();
});
});

it("Should refuse an invalid address", async () => {
expect(() => ethAddressSchema.parse(undefined)).toThrowError("invalid_type");

const invalidAddress = "Provided address is invalid.";
[...invalidAddressesNoEns, ...validEnsNames].forEach((address) => {
expect(() => ethAddressSchema.parse(address)).toThrowError(invalidAddress);
});
});
});

describe("ensNameSchema", () => {
it("Should accept a valid ENS name", async () => {
validEnsNames.forEach((ensName) => {
expect(() => ensNameSchema.parse(ensName)).not.toThrow();
});
});

it("Should refuse an invalid ENS name", async () => {
expect(() => ensNameSchema.parse(undefined)).toThrowError("invalid_type");

const invalidEns = "Provided ENS name is invalid.";
[...invalidEnsNamesNoAddress, ...validAddresses].forEach((ensName) => {
expect(() => ensNameSchema.parse(ensName)).toThrowError(invalidEns);
});
});
});

describe("ethAddressOrEnsNameSchema", () => {
it("Should accept a valid address", async () => {
validAddresses.forEach((address) => {
expect(() => ethAddressOrEnsNameSchema.parse(address)).not.toThrow();
});
});

it("Should accept a valid ENS name", async () => {
validEnsNames.forEach((ensName) => {
expect(() => ethAddressOrEnsNameSchema.parse(ensName)).not.toThrow();
});
});

it("Should refuse an invalid address", async () => {
expect(() => ethAddressOrEnsNameSchema.parse(undefined)).toThrowError("invalid_type");

invalidAddressesNoEns.forEach((address) => {
// FIXME: the errorMap is not working, expecting "Provided address or ENS name is invalid.");
expect(() => ethAddressOrEnsNameSchema.parse(address)).toThrowError("custom");
});
});

it("Should refuse an invalid ENS name", async () => {
expect(() => ethAddressOrEnsNameSchema.parse(undefined)).toThrowError("invalid_type");

invalidEnsNamesNoAddress.forEach((ensName) => {
// FIXME: the errorMap is not working, expecting "Provided address or ENS name is invalid.");
expect(() => ethAddressOrEnsNameSchema.parse(ensName)).toThrowError("custom");
});
});
});

describe("disputeDetailsSchema", () => {
// TODO: add tests
});
});
5 changes: 4 additions & 1 deletion kleros-sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"baseUrl": ".",
"target": "ES6",
"module": "CommonJS",
"rootDir": "./",
"outDir": "build/dist",
"allowJs": true,
"forceConsistentCasingInFileNames": true,
Expand All @@ -15,6 +14,10 @@
"removeComments": true,
"isolatedModules": true
},
"include": [
".",
"test"
],
"exclude": [
"node_modules",
"build",
Expand Down
12 changes: 12 additions & 0 deletions kleros-sdk/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference types="vitest" />

// Configure Vitest (https://vitest.dev/config/)

import { defineConfig } from "vite";

export default defineConfig({
test: {
/* for example, use global to avoid globals imports (describe, test, expect): */
// globals: true,
},
});
2 changes: 2 additions & 0 deletions web/src/context/NewDisputeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface IDisputeTemplate {
question: string;
specification?: string;
title: string;
// attachment: Attachment;
// type: string;
}

interface IDisputeData extends IDisputeTemplate {
Expand Down
Loading

0 comments on commit 0ef5d26

Please sign in to comment.