|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { cachedCall, withCache } from "../helpers"; |
| 2 | +import { cachedCall, withCache, defaultKeyGenerator } from "../helpers"; |
3 | 3 |
|
4 | 4 | // A mock function that simulates an expensive async operation |
5 | 5 | async function expensiveFunction(param: string): Promise<string> { |
@@ -145,3 +145,74 @@ describe("cachedFunction Wrapper with complex parameters", () => { |
145 | 145 | ).toBeGreaterThanOrEqual(500); |
146 | 146 | }); |
147 | 147 | }); |
| 148 | + |
| 149 | +async function noArgFunction(): Promise<string> { |
| 150 | + return new Promise((resolve) => { |
| 151 | + setTimeout(() => { |
| 152 | + resolve("no_arg_result"); |
| 153 | + }, 500); |
| 154 | + }); |
| 155 | +} |
| 156 | + |
| 157 | +describe("cachedCall with no arguments", () => { |
| 158 | + it("should run the function and cache the result on initial call", async () => { |
| 159 | + const result = await cachedCall(noArgFunction, { |
| 160 | + ttl: 2, |
| 161 | + tags: ["testTag"], |
| 162 | + }); |
| 163 | + expect(result).toBe("no_arg_result"); |
| 164 | + }); |
| 165 | + |
| 166 | + it("should return cached result on second call", async () => { |
| 167 | + await cachedCall(noArgFunction, { ttl: 2, tags: ["testTag"] }); |
| 168 | + const startCachedCall = Date.now(); |
| 169 | + const result = await cachedCall(noArgFunction, { |
| 170 | + ttl: 2, |
| 171 | + tags: ["testTag"], |
| 172 | + }); |
| 173 | + const endCachedCall = Date.now(); |
| 174 | + expect(result).toBe("no_arg_result"); |
| 175 | + expect(endCachedCall - startCachedCall).toBeLessThan(100); |
| 176 | + }); |
| 177 | +}); |
| 178 | + |
| 179 | +const cachedNoArgFunction = withCache(noArgFunction, { |
| 180 | + ttl: 3600, |
| 181 | + tags: ["tag1"], |
| 182 | +}); |
| 183 | + |
| 184 | +describe("withCache with no arguments", () => { |
| 185 | + it("should run the function and cache the result on initial call", async () => { |
| 186 | + const startFirstCall = Date.now(); |
| 187 | + const cachedResult = await cachedNoArgFunction(); |
| 188 | + const endFirstCall = Date.now(); |
| 189 | + expect(cachedResult).toBe("no_arg_result"); |
| 190 | + expect(endFirstCall - startFirstCall).toBeGreaterThanOrEqual(500); |
| 191 | + }); |
| 192 | + |
| 193 | + it("should return cached result on second call", async () => { |
| 194 | + await cachedNoArgFunction(); |
| 195 | + const startCachedCall2 = Date.now(); |
| 196 | + const cachedResult = await cachedNoArgFunction(); |
| 197 | + const endCachedCall2 = Date.now(); |
| 198 | + expect(cachedResult).toBe("no_arg_result"); |
| 199 | + expect(endCachedCall2 - startCachedCall2).toBeLessThan(100); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
| 203 | +describe("defaultKeyGenerator", () => { |
| 204 | + it("should generate a key with no arguments", () => { |
| 205 | + const key = defaultKeyGenerator("testFunction", []); |
| 206 | + expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/); |
| 207 | + }); |
| 208 | + |
| 209 | + it("should generate a key with null arguments", () => { |
| 210 | + const key = defaultKeyGenerator("testFunction", null as any); |
| 211 | + expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/); |
| 212 | + }); |
| 213 | + |
| 214 | + it("should generate a key with undefined arguments", () => { |
| 215 | + const key = defaultKeyGenerator("testFunction", undefined); |
| 216 | + expect(key).toMatch(/^testFunction:[a-f0-9]{40}$/); |
| 217 | + }); |
| 218 | +}); |
0 commit comments