From 8082fd007f495fcfc0097ac8469a3475ec19ce9d Mon Sep 17 00:00:00 2001 From: mhchia Date: Fri, 30 Jun 2023 17:25:50 +0800 Subject: [PATCH] tests: add tests for checkProof --- src/cache.ts | 2 +- tests/cache.test.ts | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/cache.ts b/src/cache.ts index 02424b24..1fe0b2be 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -57,7 +57,7 @@ export interface ICache { checkProof(proof: CachedProof): EvaluatedProof } -const DEFAULT_CACHE_SIZE = 100 +export const DEFAULT_CACHE_SIZE = 100 /** * Cache for storing proofs and automatically evaluating them for rate limit breaches * in the memory. diff --git a/tests/cache.test.ts b/tests/cache.test.ts index eeb39d31..68e2b723 100644 --- a/tests/cache.test.ts +++ b/tests/cache.test.ts @@ -1,5 +1,5 @@ import { MemoryCache } from "../src" -import { CachedProof, Status } from '../src/cache' +import { CachedProof, DEFAULT_CACHE_SIZE, Status } from '../src/cache' import { fieldFactory } from "./utils" describe("MemoryCache", () => { @@ -32,32 +32,41 @@ describe("MemoryCache", () => { }) test("should have a cache length of 100", () => { - expect(cache.cacheLength).toBe(100) + expect(cache.cacheLength).toBe(DEFAULT_CACHE_SIZE) }) test("should successfully add proof", () => { + const resultCheckProof = cache.checkProof(proof1) + expect(resultCheckProof.status).toBe(Status.VALID) const result1 = cache.addProof(proof1) - expect(result1.status).toBe(Status.VALID) + expect(result1.status).toBe(resultCheckProof.status) expect(Object.keys(cache.cache).length ).toBe(1) }) test("should detect breach and return secret", () => { + const resultCheckProof = cache.checkProof(proof2) + expect(resultCheckProof.status).toBe(Status.BREACH) + expect(resultCheckProof.secret).toBeGreaterThan(0) const result2 = cache.addProof(proof2) - expect(result2.status).toBe(Status.BREACH) + expect(result2.status).toBe(resultCheckProof.status) expect(result2.secret).toBeGreaterThan(0) expect(Object.keys(cache.cache).length ).toBe(1) }) test("should check proof 3", () => { + const resultCheckProof = cache.checkProof(proof3) + expect(resultCheckProof.status).toBe(Status.VALID) const result3 = cache.addProof(proof3) - expect(result3.status).toBe(Status.VALID) + expect(result3.status).toBe(resultCheckProof.status) }) test("should check proof 4", () => { + const resultCheckProof = cache.checkProof(proof4) + expect(resultCheckProof.status).toBe(Status.VALID) const result4 = cache.addProof(proof4) - expect(result4.status).toBe(Status.VALID) + expect(result4.status).toBe(resultCheckProof.status) // two epochs are added to the cache now expect(Object.keys(cache.cache).length ).toBe(2) @@ -65,6 +74,8 @@ describe("MemoryCache", () => { test("should fail for proof 1 (duplicate proof)", () => { // Proof 1 is already in the cache + const resultCheckProof = cache.checkProof(proof1) + expect(resultCheckProof.status).toBe(Status.DUPLICATE) const result1 = cache.addProof(proof1) expect(result1.status).toBe(Status.DUPLICATE) });