Skip to content

Commit 0814aa5

Browse files
committed
test: add coverage for Data.parse v2 migration and Hit callback
1 parent dd5d3e1 commit 0814aa5

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

test/data.test.cjs

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
const { describe, it } = require('node:test');
2+
const assert = require('assert');
3+
4+
const { Cacheism } = require('../dist/index.cjs');
5+
6+
describe('Data.parse()', function() {
7+
8+
describe('with version 2 cache format', function() {
9+
10+
it('should upgrade version 2 to version 3', function() {
11+
const v2Json = JSON.stringify({
12+
version: 2,
13+
cacheName: 'test/cache',
14+
created: '2024-01-15T12:00:00.000Z',
15+
data: { foo: 'bar' },
16+
etag: '"abc123"'
17+
});
18+
19+
const data = Cacheism.Data.parse(v2Json);
20+
21+
assert.strictEqual(data.version, 3);
22+
assert.strictEqual(data.type, Cacheism.Type.hit);
23+
assert.strictEqual(data.cacheName, 'test/cache');
24+
assert.deepStrictEqual(data.data, { foo: 'bar' });
25+
assert.strictEqual(data.etag, '"abc123"');
26+
assert.ok(data.created instanceof Date);
27+
assert.strictEqual(data.created.toISOString(), '2024-01-15T12:00:00.000Z');
28+
});
29+
30+
it('should set default values for v3-only fields', function() {
31+
const v2Json = JSON.stringify({
32+
version: 2,
33+
cacheName: 'test/cache',
34+
created: '2024-01-15T12:00:00.000Z',
35+
data: 'test data',
36+
etag: '"def456"'
37+
});
38+
39+
const data = Cacheism.Data.parse(v2Json);
40+
41+
assert.strictEqual(data.error, null);
42+
assert.strictEqual(data.errorTime, null);
43+
assert.strictEqual(data.consecutiveErrors, 0);
44+
});
45+
46+
it('should produce a valid Hit when response() is called', function() {
47+
const v2Json = JSON.stringify({
48+
version: 2,
49+
cacheName: 'test/cache',
50+
created: '2024-01-15T12:00:00.000Z',
51+
data: 'cached value',
52+
etag: '"etag789"'
53+
});
54+
55+
const data = Cacheism.Data.parse(v2Json);
56+
const response = data.response();
57+
58+
assert.ok(response instanceof Cacheism.Hit);
59+
assert.strictEqual(response.isHit, true);
60+
assert.strictEqual(response.isMiss, false);
61+
assert.strictEqual(response.data, 'cached value');
62+
assert.strictEqual(response.etag, '"etag789"');
63+
assert.strictEqual(response.cached, true);
64+
assert.strictEqual(response.consecutiveErrors, 0);
65+
});
66+
67+
});
68+
69+
describe('with version 3 cache format', function() {
70+
71+
it('should parse version 3 Hit correctly', function() {
72+
const v3Json = JSON.stringify({
73+
version: 3,
74+
type: 'Hit',
75+
cacheName: 'test/cache',
76+
created: '2024-01-15T12:00:00.000Z',
77+
data: { value: 123 },
78+
error: null,
79+
errorTime: null,
80+
consecutiveErrors: 0,
81+
etag: '"v3etag"'
82+
});
83+
84+
const data = Cacheism.Data.parse(v3Json);
85+
86+
assert.strictEqual(data.version, 3);
87+
assert.strictEqual(data.type, Cacheism.Type.hit);
88+
assert.strictEqual(data.cacheName, 'test/cache');
89+
assert.deepStrictEqual(data.data, { value: 123 });
90+
assert.strictEqual(data.etag, '"v3etag"');
91+
assert.strictEqual(data.error, null);
92+
assert.strictEqual(data.errorTime, null);
93+
assert.strictEqual(data.consecutiveErrors, 0);
94+
});
95+
96+
it('should parse version 3 Miss correctly', function() {
97+
const v3Json = JSON.stringify({
98+
version: 3,
99+
type: 'Miss',
100+
cacheName: 'test/cache',
101+
created: '2024-01-15T12:00:00.000Z',
102+
data: null,
103+
error: 'Error: Network failure',
104+
errorTime: '2024-01-15T12:00:00.000Z',
105+
consecutiveErrors: 3,
106+
etag: null
107+
});
108+
109+
const data = Cacheism.Data.parse(v3Json);
110+
111+
assert.strictEqual(data.version, 3);
112+
assert.strictEqual(data.type, Cacheism.Type.miss);
113+
assert.strictEqual(data.data, null);
114+
assert.strictEqual(data.error, 'Error: Network failure');
115+
assert.ok(data.errorTime instanceof Date);
116+
assert.strictEqual(data.consecutiveErrors, 3);
117+
assert.strictEqual(data.etag, null);
118+
});
119+
120+
});
121+
122+
describe('Data.response()', function() {
123+
124+
it('should return a Miss when type is Miss', function() {
125+
const v3Json = JSON.stringify({
126+
version: 3,
127+
type: 'Miss',
128+
cacheName: 'test/cache',
129+
created: '2024-01-15T12:00:00.000Z',
130+
data: null,
131+
error: 'Error: Something failed',
132+
errorTime: '2024-01-15T12:05:00.000Z',
133+
consecutiveErrors: 2,
134+
etag: null
135+
});
136+
137+
const data = Cacheism.Data.parse(v3Json);
138+
const response = data.response();
139+
140+
assert.ok(response instanceof Cacheism.Miss);
141+
assert.strictEqual(response.isHit, false);
142+
assert.strictEqual(response.isMiss, true);
143+
assert.strictEqual(response.data, null);
144+
assert.strictEqual(response.error, 'Error: Something failed');
145+
assert.strictEqual(response.consecutiveErrors, 2);
146+
assert.ok(response.errorTime instanceof Date);
147+
});
148+
149+
it('should handle Miss with null errorTime', function() {
150+
const v3Json = JSON.stringify({
151+
version: 3,
152+
type: 'Miss',
153+
cacheName: 'test/cache',
154+
created: '2024-01-15T12:00:00.000Z',
155+
data: null,
156+
error: 'Error: Something failed',
157+
errorTime: null,
158+
consecutiveErrors: 1,
159+
etag: null
160+
});
161+
162+
const data = Cacheism.Data.parse(v3Json);
163+
const response = data.response();
164+
165+
assert.ok(response instanceof Cacheism.Miss);
166+
assert.ok(response.errorTime instanceof Date);
167+
});
168+
169+
});
170+
171+
describe('with unknown version', function() {
172+
173+
it('should throw an error for version 1', function() {
174+
const v1Json = JSON.stringify({
175+
version: 1,
176+
cacheName: 'test/cache',
177+
created: '2024-01-15T12:00:00.000Z',
178+
data: 'old data'
179+
});
180+
181+
assert.throws(() => {
182+
Cacheism.Data.parse(v1Json);
183+
}, /Unknown cache version number: 1/);
184+
});
185+
186+
it('should throw an error for version 4', function() {
187+
const v4Json = JSON.stringify({
188+
version: 4,
189+
cacheName: 'test/cache',
190+
created: '2024-01-15T12:00:00.000Z',
191+
data: 'future data'
192+
});
193+
194+
assert.throws(() => {
195+
Cacheism.Data.parse(v4Json);
196+
}, /Unknown cache version number: 4/);
197+
});
198+
199+
});
200+
201+
});

test/memory.test.cjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ describe('memory', function() {
1818
assert.strictEqual(typeof cache.go, 'function');
1919
});
2020

21+
describe('when callback returns a Hit instance', function() {
22+
23+
it('should use the returned Hit directly', async function() {
24+
const customHit = new Cacheism.Hit('-internal/cache', 'custom data', '"custom-etag"');
25+
26+
const c = await cache.go('-internal', 'cache', Cacheism.Status.onlyFresh, async () => {
27+
return customHit;
28+
});
29+
30+
assert.ok(c instanceof Cacheism.Hit);
31+
assert.strictEqual(c.data, 'custom data');
32+
assert.strictEqual(c.etag, '"custom-etag"');
33+
assert.strictEqual(c.cached, false);
34+
});
35+
36+
});
37+
2138
describe('when status=onlyFresh', async function () {
2239

2340
describe('and no existing cache', async function () {

0 commit comments

Comments
 (0)