@trieb.work/nextjs-turbo-redis-cache 1.14.0 → 1.14.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.test.ts CHANGED
@@ -1,6 +1,91 @@
1
- import { describe, it, expect } from 'vitest';
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
2
 
3
3
  import type { CreateRedisStringsHandlerOptions } from './index';
4
+ import RedisStringsHandler from './RedisStringsHandler';
5
+
6
+ vi.mock('redis', () => {
7
+ const createClient = () => {
8
+ return {
9
+ isReady: true,
10
+ on: vi.fn(),
11
+ connect: vi.fn(async () => undefined),
12
+ disconnect: vi.fn(),
13
+ quit: vi.fn(),
14
+ duplicate: vi.fn(() => ({
15
+ connect: vi.fn(async () => undefined),
16
+ subscribe: vi.fn(async () => undefined),
17
+ on: vi.fn(),
18
+ quit: vi.fn(async () => undefined),
19
+ configGet: vi.fn(async () => ({ 'notify-keyspace-events': 'Exe' })),
20
+ })),
21
+ get: vi.fn(async () => {
22
+ const err = new Error('timeout') as Error & { name: string };
23
+ err.name = 'AbortError';
24
+ throw err;
25
+ }),
26
+ hScan: vi.fn(async () => ({ cursor: 0, tuples: [] })),
27
+ scan: vi.fn(async () => ({ cursor: 0, keys: [] })),
28
+ hSet: vi.fn(async () => 1),
29
+ hDel: vi.fn(async () => 1),
30
+ publish: vi.fn(async () => 1),
31
+ unlink: vi.fn(async () => 1),
32
+ set: vi.fn(async () => 'OK'),
33
+ };
34
+ };
35
+ return {
36
+ createClient,
37
+ commandOptions: vi.fn((opts) => opts),
38
+ };
39
+ });
40
+
41
+ vi.mock('./SyncedMap', () => {
42
+ class SyncedMap {
43
+ waitUntilReady = vi.fn(async () => undefined);
44
+ get = vi.fn(() => undefined);
45
+ set = vi.fn(async () => undefined);
46
+ delete = vi.fn(async () => undefined);
47
+ entries = vi.fn(function* () {
48
+ return;
49
+ });
50
+
51
+ constructor() {}
52
+ }
53
+
54
+ return { SyncedMap };
55
+ });
56
+
57
+ describe('RedisStringsHandler', () => {
58
+ beforeEach(() => {
59
+ vi.restoreAllMocks();
60
+ });
61
+
62
+ afterEach(() => {
63
+ vi.unstubAllEnvs();
64
+ });
65
+
66
+ it('treats aborted GET (timeout) as cache miss without console.error', async () => {
67
+ const consoleErrorSpy = vi
68
+ .spyOn(console, 'error')
69
+ .mockImplementation(() => undefined);
70
+
71
+ const handler = new RedisStringsHandler({
72
+ redisUrl: 'redis://localhost:6379',
73
+ keyPrefix: 'test:',
74
+ database: 0,
75
+ getTimeoutMs: 1,
76
+ redisGetDeduplication: false,
77
+ });
78
+
79
+ const res = await handler.get('missing-key', {
80
+ kind: 'APP_PAGE',
81
+ isRoutePPREnabled: false,
82
+ isFallback: false,
83
+ });
84
+
85
+ expect(res).toBeNull();
86
+ expect(consoleErrorSpy).not.toHaveBeenCalled();
87
+ });
88
+ });
4
89
 
5
90
  describe('Public exports', () => {
6
91
  it('exports CreateRedisStringsHandlerOptions type', () => {
@@ -618,6 +618,7 @@ describe('Next.js Turbo Redis Cache Integration', () => {
618
618
  });
619
619
 
620
620
  it('Redis should have a key for the page which should have a TTL set to 28 days (2 * 14 days default revalidate time)', async () => {
621
+ await delay(REDIS_BACKGROUND_SYNC_DELAY);
621
622
  // check Redis keys
622
623
  const ttl = await redisClient.ttl(
623
624
  process.env.VERCEL_URL + '/pages/no-fetch/default-page',