File size: 1,202 Bytes
bf48b89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { describe, expect, it, vi } from 'vitest';

const setSpy = vi.fn(() => null);
const getSpy = vi.fn(() => null);

vi.mock('xxhash-wasm', () => ({
    default: () =>
        Promise.resolve({
            h64ToString: () => 'hash',
        }),
}));

vi.mock('@/utils/cache/index', () => ({
    default: {
        status: { available: true },
        globalCache: {
            get: getSpy,
            set: setSpy,
        },
    },
}));

describe('cache middleware', () => {
    it('clears control key when downstream throws', async () => {
        const { default: cacheMiddleware } = await import('@/middleware/cache');

        const ctx = {
            req: {
                path: '/test',
                query: () => null,
            },
            res: {
                headers: new Headers(),
            },
            status: vi.fn(),
            header: vi.fn(),
            set: vi.fn(),
            get: vi.fn(),
        };

        await expect(
            cacheMiddleware(ctx as any, () => {
                throw new Error('boom');
            })
        ).rejects.toThrow('boom');

        expect(setSpy.mock.calls.some(([, value]) => value === '0')).toBe(true);
    });
});