|
1 | | -import { JSONLineCache, CacheEntry } from "./cache" |
2 | 1 | import { describe, test, beforeEach } from "node:test" |
3 | 2 | import assert from "node:assert/strict" |
4 | 3 | import * as fs from "node:fs/promises" |
5 | 4 | import * as path from "node:path" |
6 | 5 | import { TestHost } from "./testhost" |
| 6 | +import { JSONLineCache } from "./jsonlinecache" |
| 7 | +import { createCache } from "./cache" |
7 | 8 |
|
8 | 9 | const tempDir = path.join(".genaiscript", "temp") |
9 | 10 |
|
10 | | -describe("Cache", () => { |
11 | | - beforeEach(async () => { |
12 | | - TestHost.install() |
13 | | - await fs.mkdir(tempDir, { recursive: true }) |
14 | | - }) |
15 | | - test("JSONLineCache instance creation with byName", async () => { |
16 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
17 | | - assert.ok(cache instanceof JSONLineCache) |
18 | | - }) |
19 | | - test("JSONLineCache set key-value pair", async () => { |
20 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
21 | | - await cache.set("anotherKey", 99) |
22 | | - const value = await cache.get("anotherKey") |
23 | | - assert.strictEqual(value, 99) |
24 | | - }) |
| 11 | +for (const type of ["memory", "jsonl", "fs"]) { |
| 12 | + describe(`cache.${type}`, () => { |
| 13 | + beforeEach(async () => { |
| 14 | + TestHost.install() |
| 15 | + await fs.mkdir(tempDir, { recursive: true }) |
| 16 | + }) |
| 17 | + test("instance creation with byName", async () => { |
| 18 | + const cache = createCache<string, number>("testCache", { |
| 19 | + type: type as any, |
| 20 | + }) |
| 21 | + assert.ok(cache instanceof JSONLineCache) |
| 22 | + }) |
| 23 | + test("set key-value pair", async () => { |
| 24 | + const cache = createCache<string, number>("testCache", { |
| 25 | + type: type as any, |
| 26 | + }) |
| 27 | + await cache.set("anotherKey", 99) |
| 28 | + const value = await cache.get("anotherKey") |
| 29 | + assert.strictEqual(value, 99) |
| 30 | + }) |
25 | 31 |
|
26 | | - test("JSONLineCache getKeySHA computation", async () => { |
27 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
28 | | - const sha = await cache.getKeySHA("testKey") |
29 | | - assert.ok(sha) |
30 | | - assert.strictEqual(typeof sha, "string") |
31 | | - }) |
| 32 | + test("getSha computation", async () => { |
| 33 | + const cache = createCache<string, number>("testCache", { |
| 34 | + type: type as any, |
| 35 | + }) |
| 36 | + const sha = await cache.getSha("testKey") |
| 37 | + assert.ok(sha) |
| 38 | + assert.strictEqual(typeof sha, "string") |
| 39 | + }) |
32 | 40 |
|
33 | | - test("keySHA generates SHA256 hash from a key", async () => { |
34 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
35 | | - const sha = await cache.getKeySHA("testKey") |
36 | | - assert.ok(sha) |
37 | | - assert.strictEqual(typeof sha, "string") |
38 | | - }) |
39 | | - test("JSONLineCache getOrUpdate retrieves existing value", async () => { |
40 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
41 | | - await cache.set("existingKey", 42) |
42 | | - const value = await cache.getOrUpdate( |
43 | | - "existingKey", |
44 | | - async () => 99, |
45 | | - () => true |
46 | | - ) |
47 | | - assert.strictEqual(value.value, 42) |
48 | | - }) |
| 41 | + test("keySHA generates SHA256 hash from a key", async () => { |
| 42 | + const cache = createCache<string, number>("testCache", { |
| 43 | + type: type as any, |
| 44 | + }) |
| 45 | + const sha = await cache.getSha("testKey") |
| 46 | + assert.ok(sha) |
| 47 | + assert.strictEqual(typeof sha, "string") |
| 48 | + }) |
| 49 | + test("getOrUpdate retrieves existing value", async () => { |
| 50 | + const cache = createCache<string, number>("testCache", { |
| 51 | + type: type as any, |
| 52 | + }) |
| 53 | + await cache.set("existingKey", 42) |
| 54 | + const value = await cache.getOrUpdate( |
| 55 | + "existingKey", |
| 56 | + async () => 99, |
| 57 | + () => true |
| 58 | + ) |
| 59 | + assert.strictEqual(value.value, 42) |
| 60 | + }) |
49 | 61 |
|
50 | | - test("JSONLineCache getOrUpdate updates with new value if key does not exist", async () => { |
51 | | - const cache = JSONLineCache.byName<string, number>("testCache") |
52 | | - const value = await cache.getOrUpdate( |
53 | | - "newKey", |
54 | | - async () => 99, |
55 | | - () => true |
56 | | - ) |
57 | | - assert.strictEqual(value.value, 99) |
58 | | - const cachedValue = await cache.get("newKey") |
59 | | - assert.strictEqual(cachedValue, 99) |
| 62 | + test("getOrUpdate updates with new value if key does not exist", async () => { |
| 63 | + const cache = createCache<string, number>("testCache", { |
| 64 | + type: type as any, |
| 65 | + }) |
| 66 | + const value = await cache.getOrUpdate( |
| 67 | + "newKey", |
| 68 | + async () => 99, |
| 69 | + () => true |
| 70 | + ) |
| 71 | + assert.strictEqual(value.value, 99) |
| 72 | + const cachedValue = await cache.get("newKey") |
| 73 | + assert.strictEqual(cachedValue, 99) |
| 74 | + }) |
60 | 75 | }) |
61 | | -}) |
| 76 | +} |
0 commit comments