Now that you've built and understood the PriceFeed Oracle Program, it's time to run the tests to verify that everything is working correctly. Testing ensures that both the execution and tally phases of your Oracle Program are functioning as expected.
The test cases are located in the tests/ folder. Here's an example of a test for the PriceFeed Oracle Program:
import { afterEach, describe, it, expect, mock } from"bun:test";import { file } from"bun";import { testOracleProgramExecution, testOracleProgramTally } from"@seda-protocol/dev-tools"import { BigNumber } from'bignumber.js'constWASM_PATH="build/debug.wasm";constfetchMock=mock();afterEach(() => {fetchMock.mockRestore();});describe("data request execution", () => {it("should aggregate the results from the different APIs",async () => {fetchMock.mockImplementation((url) => {if (url.host ==="api.binance.com") {returnnewResponse(JSON.stringify({ price:"2452.30000" })); }returnnewResponse('Unknown request'); });constwasmBinary=awaitfile(WASM_PATH).arrayBuffer();constvmResult=awaittestOracleProgramExecution(Buffer.from(wasmBinary),Buffer.from("eth-usdc"), fetchMock );expect(vmResult.exitCode).toBe(0);// BigNumber.js is big endianconsthex=Buffer.from(vmResult.result.toReversed()).toString('hex');constresult=BigNumber(`0x${hex}`);expect(result).toEqual(BigNumber('2452300032')); });it('should tally all results in a single data point',async () => {constwasmBinary=awaitfile(WASM_PATH).arrayBuffer();// Result from the execution testlet buffer =Buffer.from([0,33,43,146,0,0,0,0,0,0,0,0,0,0,0,0]);constvmResult=awaittestOracleProgramTally(Buffer.from(wasmBinary),Buffer.from('tally-inputs'), [{ exitCode:0, gasUsed:0, inConsensus:true, result: buffer, }]);expect(vmResult.exitCode).toBe(0);consthex=Buffer.from(vmResult.result).toString('hex');constresult=BigNumber(`0x${hex}`);expect(result).toEqual(BigNumber('2452300032')); });});