|
1 | 1 | import { describe, expect, it } from "bun:test";
|
2 |
| -import { DEFAULT_MAX_LENGTH, fraci } from "./factory.js"; |
| 2 | +import { DEFAULT_MAX_LENGTH, createFraciCache, fraci } from "./factory.js"; |
3 | 3 |
|
4 | 4 | describe("fraci", () => {
|
5 | 5 | const digitBase = "0123456789";
|
@@ -87,3 +87,51 @@ describe("fraci", () => {
|
87 | 87 | expect(generator.next().done).toBe(true);
|
88 | 88 | });
|
89 | 89 | });
|
| 90 | + |
| 91 | +describe("createFraciCache", () => { |
| 92 | + const digitBase = "0123456789"; |
| 93 | + const lengthBase = "0123456789"; |
| 94 | + |
| 95 | + it("should create an empty Map instance", () => { |
| 96 | + const cache = createFraciCache(); |
| 97 | + expect(cache).toBeInstanceOf(Map); |
| 98 | + expect(cache.size).toBe(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should be usable with fraci function", () => { |
| 102 | + const cache = createFraciCache(); |
| 103 | + const indexing = fraci({ digitBase, lengthBase }, cache); |
| 104 | + |
| 105 | + // Generate a key to trigger cache population |
| 106 | + const generator = indexing.generateKeyBetween(null, null); |
| 107 | + const [key] = generator; |
| 108 | + |
| 109 | + // Cache should now contain entries |
| 110 | + expect(cache.size).toBeGreaterThan(0); |
| 111 | + expect(typeof key).toBe("string"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should share cached computations between multiple fraci instances", () => { |
| 115 | + const cache = createFraciCache(); |
| 116 | + |
| 117 | + // Create two fraci instances with the same configuration and cache |
| 118 | + const indexing1 = fraci({ digitBase, lengthBase }, cache); |
| 119 | + const indexing2 = fraci({ digitBase, lengthBase }, cache); |
| 120 | + |
| 121 | + // Generate a key with the first instance to populate the cache |
| 122 | + const [key1] = indexing1.generateKeyBetween(null, null); |
| 123 | + |
| 124 | + // Record cache size after first use |
| 125 | + const sizeBefore = cache.size; |
| 126 | + expect(sizeBefore).toBeGreaterThan(0); |
| 127 | + |
| 128 | + // Generate a key with the second instance |
| 129 | + const [key2] = indexing2.generateKeyBetween(null, null); |
| 130 | + |
| 131 | + // Cache size should remain the same since computations are shared |
| 132 | + expect(cache.size).toBe(sizeBefore); |
| 133 | + |
| 134 | + // Keys should be the same since they share the same cache |
| 135 | + expect(key1).toBe(key2); |
| 136 | + }); |
| 137 | +}); |
0 commit comments