Files
Tony 6697901e29 style: use cloudflare/oxlint-config (#23968)
* style: use cloudflare/oxlint-config

* style: fix no-known-value-widening

* style: fix typescript/only-throw-error

* style: fix typescript/no-floating-promises

* style: fix typescript/use-unknown-in-catch-callback-variable

* style: fix typescript/no-misused-promises

* style: fix typescript/no-unsafe-unary-minus

* style: fix typescript/no-for-in-array

* style: fix react/purity

* style: fix typescript/switch-exhaustiveness-check

* style: fix typescript/unbound-method

* style: fix react/rules-of-hooks

* style: fix typescript/return-await

* style: fix react/button-has-type

* style: fix react/no-unescaped-entities

* style: fix import/no-namespace

* style: fix typescript/no-import-type-side-effects

* style: fix no-promise-executor-return

* style: fix no-useless-assignment

* style: fix typescript/no-misused-spread

* style: fix react/jsx-no-target-blank

* style: fix typescript/await-thenable

* style: fix typescript/no-unnecessary-type-assertion

* style: fix import/consistent-type-specifier-style

* style: fix react/no-unknown-property

* style: remove unused disable directive

* style: merge region

* style: enable prefer-smaller-scope

* style: add react/no-danger

* style: add unicorn-js/prefer-uint8array-base64/hex

* style: move non-recommended unicorn rules to custom region
2026-11-21 03:38:53 +08:00

47 lines
2.3 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { runInCacheNamespace, scopeCacheKey } from './scope';
describe('cache key scope', () => {
afterEach(() => {
delete process.env.CACHE_TYPE;
vi.resetModules();
});
it('prefixes keys with the namespace of the running route', () => {
expect(scopeCacheKey('https://example.com/a')).toBe('https://example.com/a');
expect(runInCacheNamespace('bast', () => scopeCacheKey('lorientlejour:token'))).toBe('bast:lorientlejour:token');
expect(runInCacheNamespace('bast', () => scopeCacheKey('bast:detail'))).toBe('bast:detail');
});
it('keeps the namespace across awaits', async () => {
const key = await runInCacheNamespace('bast', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
return scopeCacheKey('https://example.com/a');
});
expect(key).toBe('bast:https://example.com/a');
});
it('refuses internal keys', () => {
expect(() => scopeCacheKey('rsshub:koa-redis-cache:abc')).toThrow('reserved for the internal usage');
expect(() => runInCacheNamespace('rsshub', () => scopeCacheKey('path-requested:abc'))).toThrow('reserved for the internal usage');
expect(() => scopeCacheKey(1 as unknown as string)).toThrow('Cache key must be a string');
});
it('separates content cache entries of different namespaces', async () => {
process.env.CACHE_TYPE = 'memory';
const cache = (await import('@/utils/cache')).default;
// the cache module was re-imported, so use its copy of the scope
const { runInCacheNamespace } = await import('./scope');
await runInCacheNamespace('owner', () => cache.set('owner:token', 'secret'));
expect(await runInCacheNamespace('owner', () => cache.get('owner:token'))).toBe('secret');
expect(await runInCacheNamespace('other', () => cache.get('owner:token'))).toBeNull();
const value = await runInCacheNamespace('other', () => cache.tryGet('owner:token', () => Promise.resolve({ title: 'fresh' })));
expect(value).toEqual({ title: 'fresh' });
expect(await runInCacheNamespace('owner', () => cache.get('owner:token'))).toBe('secret');
});
});