Skip to content

Commit

Permalink
test(credentials): add more tests for different logical expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
vplasencia committed Jun 27, 2024
1 parent 214e9a0 commit 74e7874
Showing 1 changed file with 143 additions and 3 deletions.
146 changes: 143 additions & 3 deletions libs/credentials/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber } from "ethers"
import blockchainBalance from "./validators/blockchainBalance"
import blockchainTransactions from "./validators/blockchainTransactions"
import githubPersonalStars from "./validators/githubPersonalStars"
import addProvider from "./addProvider"
import addProviders from "./addProviders"
import addValidator from "./addValidator"
Expand All @@ -13,6 +14,8 @@ import {
validateCredentials,
validateManyCredentials
} from "./validateCredentials"
import { testUtils } from "."
import { evaluate, tokenize } from "./evaluateExpression"

describe("Credentials library", () => {
describe("# addProvider", () => {
Expand Down Expand Up @@ -81,8 +84,9 @@ describe("Credentials library", () => {
getTransactionCount: jest.fn()
}
it("Should return true if an account has a balance greater than or equal to 10", async () => {
jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12))
jsonRpcProviderMocked.getTransactionCount.mockReturnValue(12)
jsonRpcProviderMocked.getBalance.mockReturnValue(
BigNumber.from("12000000000000000000")
)

const result = await validateCredentials(
{
Expand All @@ -101,7 +105,9 @@ describe("Credentials library", () => {
expect(result).toBeTruthy()
})
it("Should return true if an account has a balance greater than or equal to 10 and greater than or equal to 10 transactions", async () => {
jsonRpcProviderMocked.getBalance.mockReturnValue(BigNumber.from(12))
jsonRpcProviderMocked.getBalance.mockReturnValue(
BigNumber.from("12000000000000000000")
)
jsonRpcProviderMocked.getTransactionCount.mockReturnValue(12)

const credentials = [
Expand Down Expand Up @@ -140,5 +146,139 @@ describe("Credentials library", () => {

expect(result).toBeTruthy()
})
it("Should validate many credentials with parentheses in the expression", async () => {
// This will meet the criteria
jsonRpcProviderMocked.getBalance.mockReturnValue(
BigNumber.from("12000000000000000000")
)
// This will not meet the criteria
jsonRpcProviderMocked.getTransactionCount.mockReturnValue(8)
// This will meet the criteria
testUtils.mockAPIOnce(
Array.from(Array(20).keys()).map(() => ({
stargazers_count: 10
}))
)

const credentials = [
{
id: blockchainBalance.id,
criteria: {
minBalance: "10",
network: "sepolia"
}
},
{
id: blockchainTransactions.id,
criteria: {
minTransactions: 10,
network: "sepolia"
}
},
{
id: githubPersonalStars.id,
criteria: {
minStars: 100
}
}
]

const contexts = [
{
address: "0x",
jsonRpcProvider: jsonRpcProviderMocked
},
{
address: "0x",
jsonRpcProvider: jsonRpcProviderMocked
},
{
profile: {},
accessTokens: { github: "token" }
}
]
const expression = ["", "and", "(", "", "or", "", ")"]
const result = await validateManyCredentials(
credentials,
contexts,
expression
)

expect(result).toBeTruthy()
})
})
describe("# evaluateExpression", () => {
describe("# tokenize", () => {
it("Should sucessfully tokenize a logical expression", () => {
const expression = "true and false or ( true and true )"

const tokens = tokenize(expression)

const result = [
"true",
"and",
"false",
"or",
"(",
"true",
"and",
"true",
")"
]

expect(tokens).toStrictEqual(result)
})
})
describe("# evaluate", () => {
it("Should sucessfully evaluate a logical expression with the and operator", () => {
const expression = ["true", "and", "false"]
const result = evaluate(expression)
expect(result).toBeFalsy()
})
it("Should sucessfully evaluate a logical expression with the or operator", () => {
const expression = ["true", "or", "false"]
const result = evaluate(expression)
expect(result).toBeTruthy()
})
it("Should sucessfully evaluate a logical expression with the not operator", () => {
const expression = ["not", "false"]
const result = evaluate(expression)
expect(result).toBeTruthy()
})
it("Should sucessfully evaluate a logical expression with the xor operator", () => {
const expression = ["false", "xor", "true"]
const result = evaluate(expression)
expect(result).toBeTruthy()
})
it("Should sucessfully evaluate a logical expression with the and or not and xor operators", () => {
const expression = [
"true",
"and",
"false",
"or",
"not",
"false",
"xor",
"true"
]
const result = evaluate(expression)
expect(result).toBeFalsy()
})
it("Should sucessfully evaluate a logical expression with parentheses", () => {
const expression = [
"true",
"and",
"false",
"or",
"(",
"true",
"and",
"true",
")"
]
const result = evaluate(expression)
expect(result).toBeTruthy()
})
})
})
})

0 comments on commit 74e7874

Please sign in to comment.