Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for conditions #396

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/taco/test/conditions/base/condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@ describe('validation', () => {
expect(result.error).toBeUndefined();
expect(result.data.contractAddress).toEqual(TEST_CONTRACT_ADDR);
});

it('rejects an incorrect schema', async () => {
const result = Condition.validate(condition.schema, {
...condition.value,
contractAddress: '0x123',
});
expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
contractAddress: {
_errors: ['Invalid', 'String must contain exactly 42 character(s)'],
},
});
});

it('rejects non-integer chain', () => {
const badObj = {
...condition.value,
chain: 'not-an-integer',
};
const result = Condition.validate(condition.schema, badObj);

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
chain: {
_errors: [
'Invalid literal value, expected 137',
'Invalid literal value, expected 80001',
'Invalid literal value, expected 5',
'Invalid literal value, expected 1',
],
},
});
});
});

describe('serialization', () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/taco/test/conditions/base/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,25 @@ describe('supports custom function abi', () => {
});
},
);

it.each([
{
contractAddress: '0x123',
error: ['Invalid', 'String must contain exactly 42 character(s)'],
},
{ contractAddress: undefined, error: ['Required'] },
])('rejects invalid contract address', async ({ contractAddress, error }) => {
const result = ContractCondition.validate(contractConditionSchema, {
...testContractConditionObj,
contractAddress,
});

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
contractAddress: {
_errors: error,
},
});
});
});
36 changes: 36 additions & 0 deletions packages/taco/test/conditions/base/rpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,41 @@ describe('validation', () => {
},
});
});

it('rejects empty parameters', () => {
derekpierre marked this conversation as resolved.
Show resolved Hide resolved
const rpcObj = {
...testRpcConditionObj,
parameters: [], // Update this after updating available RPC methods
};

const result = RpcCondition.validate(rpcConditionSchema, rpcObj);
expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
parameters: {
_errors: ['Array must contain at least 1 element(s)'],
},
});
});

it('rejects non-array parameters', () => {
const badRpcObj = {
...testRpcConditionObj,
parameters: 'not an array',
};

const result = RpcCondition.validate(rpcConditionSchema, badRpcObj);
expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
parameters: {
_errors: [
// Expecting two errors here because of the nested array-tuple in the parameters schema
'Expected array, received string',
'Expected array, received string',
],
},
});
});
});
});
17 changes: 17 additions & 0 deletions packages/taco/test/conditions/base/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,21 @@ describe('validation', () => {
});
expect(condition.value.conditionType).toEqual(TimeConditionType);
});

it('rejects non-existing method', () => {
const badObj = {
...returnValueTest,
method: 'non-existing-method',
chain: 5,
} as unknown as TimeConditionProps;
const result = TimeCondition.validate(timeConditionSchema, badObj);

expect(result.error).toBeDefined();
expect(result.data).toBeUndefined();
expect(result.error?.format()).toMatchObject({
method: {
_errors: ['Invalid literal value, expected "blocktime"'],
},
});
});
});
63 changes: 63 additions & 0 deletions packages/taco/test/conditions/compound-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,67 @@ describe('validation', () => {
});
expect(condition.value.conditionType).toEqual(CompoundConditionType);
});

it('rejects invalid operator', () => {
const badObj = {
operator: 'invalid-operator',
operands: [testContractConditionObj, testTimeConditionObj],
};
expect(() => new CompoundCondition(badObj)).toThrow();
});

it.each(['or', 'and', 'not'])(
'rejects empty operands for "%s" operator',
(operator) => {
const badObj = {
operator,
operands: [],
};
expect(() => new CompoundCondition(badObj)).toThrow();
},
);

it.each(['or', 'and', 'not'])(
'rejects non-array operands for "%s" operator',
(operator) => {
const badObj = {
operator,
operands: testContractConditionObj,
};
expect(() => new CompoundCondition(badObj)).toThrow();
},
);

it('rejects array operands with non-one length for "not" operator', () => {
const badObj = {
operator: 'not',
operands: [testContractConditionObj, testTimeConditionObj],
};
expect(() => new CompoundCondition(badObj)).toThrow();
});

it.each(['or', 'and'])(
'accepts array operands for "%s" operator',
(operator) => {
const obj = {
operator,
operands: [testContractConditionObj, testTimeConditionObj],
};
expect(new CompoundCondition(obj).toObj()).toEqual({
conditionType: CompoundConditionType,
...obj,
});
},
);

it.each(['or', 'and'])(
'rejects array operands with non-greater-than-one length for "%s" operator',
(operator) => {
const badObj = {
operator,
operands: [testContractConditionObj],
};
expect(() => new CompoundCondition(badObj)).toThrow();
},
);
});
20 changes: 19 additions & 1 deletion packages/taco/test/conditions/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ describe('param or context param schema', () => {
':slim_shady',
false,
],
]).success,
])
.success,
).toBe(true);
});

Expand All @@ -360,4 +361,21 @@ describe('param or context param schema', () => {
paramOrContextParamSchema.safeParse(new Uint8Array([1, 2, 3])).success,
).toBe(false);
});

it('rejects a null value', () => {
expect(paramOrContextParamSchema.safeParse(null).success).toBe(false);
});

it('rejects an undefined value', () => {
expect(paramOrContextParamSchema.safeParse(undefined).success).toBe(false);
});

it('rejects a date object', () => {
expect(paramOrContextParamSchema.safeParse(new Date()).success).toBe(false);
});

it('rejects a function', () => {
expect(paramOrContextParamSchema.safeParse(() => {}).success,
).toBe(false);
});
});