Skip to content

Commit

Permalink
Merge branch 'main' into feature/checkConfig_task
Browse files Browse the repository at this point in the history
  • Loading branch information
sirarthurmoney committed Jan 8, 2024
2 parents 6287cfd + a97bb50 commit 9b89c6e
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 62 deletions.
5 changes: 5 additions & 0 deletions .changeset/silver-drinks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@layerzerolabs/devtools": patch
---

Add isDeepEqual utility
10 changes: 8 additions & 2 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pnpm build
# Lints the entire project
pnpm lint

# Tests the entire project
pnpm test
# Tests the entire project in a containerized environment
pnpm test:ci

# Runs the project in development mode
pnpm dev
Expand Down Expand Up @@ -114,6 +114,12 @@ You also combine the environment variables:
DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-devtools-evm-hardhat-test DOCKER_COMPOSE_ARGS=--build pnpm test:local
```

You can also pass additional arguments to the individual `test` scripts this way. For example, if you're only interested in tests that match the pattern `wire.test` in `ua-devtools-evm-hardhat-test` package, you can run:

```bash
DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS="--filter=ua-devtools-evm-hardhat-test -- wire.test" pnpm test:ci
```

#### Container logs

To monitor the container logs you'll need to run:
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ services:
environment:
- NETWORK_URL_BRITNEY=http://network-britney:8545
- NETWORK_URL_VENGABOYS=http://network-vengaboys:8545
- DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=$DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS
volumes:
- ./node_modules/.cache/turbo:/app/node_modules/.cache/turbo
# Hardhat has an issue with caching compilers inside a docker container,
Expand Down
11 changes: 11 additions & 0 deletions packages/devtools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ const to: OmniPoint = {

const omniVector: OmniVector = { from, to };
```

### Common utilities

### isDeepEqual(a, b)

Compares two objects by value, returning `true` if they match, `false` otherwise.

```typescript
isDeepEqual({ a: 1 }, { a: 1 }); // true
isDeepEqual({ a: 1 }, { a: "1" }); // false
```
1 change: 1 addition & 0 deletions packages/devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --forceExit"
},
Expand Down
21 changes: 21 additions & 0 deletions packages/devtools/src/common/assertion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { deepStrictEqual } from 'assert'

/**
* Compares two object by value, returning `true` if they match
*
* ```
* const theyMatch = isDeepEqual({ a: 1 }, { a: 1 }) // true
* const theyDontMatch = isDeepEqual({ a: 1 }, { a: '1' }) // false
* ```
*
* @param {T} a
* @param {unknown} b
* @returns {boolean}
*/
export const isDeepEqual = (a: unknown, b: unknown): boolean => {
try {
return deepStrictEqual(a, b), true
} catch {
return false
}
}
1 change: 1 addition & 0 deletions packages/devtools/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './assertion'
export * from './promise'
102 changes: 102 additions & 0 deletions packages/devtools/test/common/assertion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { isDeepEqual } from '@/common/assertion'
import fc from 'fast-check'

describe('common/assertion', () => {
describe('isDeepEqual', () => {
const arrayArbitrary = fc.array(fc.anything())
const entriesArbitrary = fc.array(fc.tuple(fc.anything(), fc.anything()))

it('should return true for identical values', () => {
fc.assert(
fc.property(fc.anything(), (value) => {
expect(isDeepEqual(value, value)).toBeTruthy()
})
)
})

it('should return true for arrays containing the same values', () => {
fc.assert(
fc.property(arrayArbitrary, (array) => {
expect(isDeepEqual(array, [...array])).toBeTruthy()
expect(isDeepEqual([...array], array)).toBeTruthy()
})
)
})

it('should return false for arrays containing different values', () => {
fc.assert(
fc.property(arrayArbitrary, arrayArbitrary, (arrayA, arrayB) => {
// We'll do a very simplified precondition - we'll only run tests when the first elements are different
fc.pre(!isDeepEqual(arrayA[0], arrayB[0]))

expect(isDeepEqual(arrayA, arrayB)).toBeFalsy()
expect(isDeepEqual(arrayB, arrayA)).toBeFalsy()
})
)
})

it('should return false for arrays containing more values', () => {
fc.assert(
fc.property(arrayArbitrary, fc.anything(), (array, extraValue) => {
expect(isDeepEqual(array, [...array, extraValue])).toBeFalsy()
expect(isDeepEqual([...array, extraValue], array)).toBeFalsy()
})
)
})

it('should return true for sets containing the same values', () => {
fc.assert(
fc.property(arrayArbitrary, (array) => {
const setA = new Set(array)
const setB = new Set(array)

expect(isDeepEqual(setA, setB)).toBeTruthy()
expect(isDeepEqual(setB, setA)).toBeTruthy()
})
)
})

it('should return true for maps containing the same values', () => {
fc.assert(
fc.property(entriesArbitrary, (entries) => {
const mapA = new Map(entries)
const mapB = new Map(entries)

expect(isDeepEqual(mapA, mapB)).toBeTruthy()
expect(isDeepEqual(mapB, mapA)).toBeTruthy()
})
)
})

it('should return true for objects containing the same values', () => {
fc.assert(
fc.property(
fc.record({
value: fc.anything(),
}),
(object) => {
expect(isDeepEqual(object, { ...object })).toBeTruthy()
expect(isDeepEqual({ ...object }, object)).toBeTruthy()
}
)
)
})

it('should return false for objects containing different values', () => {
fc.assert(
fc.property(
fc.record({
value: fc.anything(),
}),
fc.anything(),
(object, value) => {
fc.pre(!isDeepEqual(object.value, value))

expect(isDeepEqual(object, { value })).toBeFalsy()
expect(isDeepEqual({ value }, object)).toBeFalsy()
}
)
)
})
})
})
6 changes: 6 additions & 0 deletions packages/io-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --passWithNoTests"
},
Expand All @@ -47,6 +48,7 @@
"fast-check": "^3.15.0",
"ink": "^3.2.0",
"ink-gradient": "^2.0.0",
"ink-table": "^3.1.0",
"jest": "^29.7.0",
"react": "^17.0.2",
"ts-jest": "^29.1.1",
Expand All @@ -59,6 +61,7 @@
"peerDependencies": {
"ink": "^3.2.0",
"ink-gradient": "^2.0.0",
"ink-table": "^3.1.0",
"react": "^17.0.2",
"yoga-layout-prebuilt": "^1.9.6",
"zod": "^3.22.4"
Expand All @@ -70,6 +73,9 @@
"ink-gradient": {
"optional": true
},
"ink-table": {
"optional": true
},
"react": {
"optional": true
},
Expand Down
2 changes: 1 addition & 1 deletion packages/io-devtools/src/stdio/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const printRecord = <TRecord extends object>(obj: TRecord, title?: string
* @param {boolean | null | undefined} value
* @returns {string}
*/
export const printBoolean = (value: boolean | null | undefined): string => (value == null ? '∅' : value ? '' : '')
export const printBoolean = (value: boolean | null | undefined): string => (value == null ? '∅' : value ? '' : '')

export const printZodErrors = (error: ZodError<unknown>): string => {
// Here we will go through all the errors and prefix them with the name
Expand Down
10 changes: 1 addition & 9 deletions packages/io-devtools/src/swag/components/record.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Box, Text } from "ink";
import React from "react";

export type PrimitiveValue =
| string
| number
| boolean
| bigint
| symbol
| null
| undefined;
import type { PrimitiveValue } from "./types";

export type RecordData = Record<string, PrimitiveValue>;

Expand Down
17 changes: 17 additions & 0 deletions packages/io-devtools/src/swag/components/table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import InkTable from "ink-table";
import type { PrimitiveValue } from "./types";
import { Text } from "ink";

export type TableRow = Record<string | number, PrimitiveValue>;

export const Table: React.FC<{ data: TableRow[] }> = ({ data }) => (
<InkTable
data={data as any}
header={({ children }) => (
<Text bold color="magenta">
{children}
</Text>
)}
/>
);
1 change: 1 addition & 0 deletions packages/io-devtools/src/swag/components/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PrimitiveValue = string | number | boolean | bigint | symbol | null | undefined
5 changes: 4 additions & 1 deletion packages/io-devtools/src/swag/printer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { render } from "ink";
import { Logo } from "./components/logo";
import { RecordList, RecordData, Record } from "./components/record";
import { Record, RecordList, type RecordData } from "./components/record";
import { Table, type TableRow } from "./components/table";

export const printLogo = () => render(<Logo />).unmount();

Expand All @@ -10,3 +11,5 @@ export const printRecord = (data: RecordData) =>

export const printRecords = (data: RecordData[]) =>
render(<RecordList data={data} />).unmount();

export const printTable = (data: TableRow[]) => render(<Table data={data} />);
1 change: 1 addition & 0 deletions packages/omnicounter-devtools-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --passWithNoTests"
},
Expand Down
1 change: 1 addition & 0 deletions packages/omnicounter-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --passWithNoTests"
},
Expand Down
1 change: 1 addition & 0 deletions packages/protocol-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --passWithNoTests"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/toolbox-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"scripts": {
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest --passWithNoTests"
},
Expand All @@ -43,6 +44,7 @@
"@layerzerolabs/ua-devtools-evm-hardhat": "~0.0.1",
"ink": "^3.2.0",
"ink-gradient": "^2.0.0",
"ink-table": "^3.1.0",
"react": "^17.0.2",
"yoga-layout-prebuilt": "^1.10.0",
"zod": "^3.22.4"
Expand Down
1 change: 1 addition & 0 deletions packages/ua-devtools-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prebuild": "tsc -noEmit",
"build": "$npm_execpath tsup",
"clean": "rm -rf dist",
"dev": "$npm_execpath tsup --watch",
"lint": "$npm_execpath eslint '**/*.{js,ts,json}'",
"test": "jest"
},
Expand Down
Loading

0 comments on commit 9b89c6e

Please sign in to comment.