key routing persists across restarts

This commit is contained in:
2026-07-21 16:13:35 -05:00
parent d59673e0c5
commit 0755fb9d78
6 changed files with 49 additions and 7 deletions
+18 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { createKeyRotator, loadConfiguredKeys, parseKeys, parseKeysJson } from '../src/key-store.js';
import { createKeyRotator, createRedisKeyRotator, loadConfiguredKeys, parseKeys, parseKeysJson } from '../src/key-store.js';
describe('key store', () => {
it('parses JSON keys and prefers the environment variable', async () => {
@@ -16,4 +16,21 @@ describe('key store', () => {
const rotator = createKeyRotator(['a', 'b']);
expect([rotator.next(), rotator.next(), rotator.next()]).toEqual(['a', 'b', 'a']);
});
it('shares a Redis-backed rotation sequence across rotators', async () => {
const counters = new Map();
const client = {
incr: async (key) => {
const next = (counters.get(key) || 0) + 1;
counters.set(key, next);
return String(next);
},
};
const firstInstance = createRedisKeyRotator({ client, keys: ['a', 'b'], key: 'test:key-rotation' });
const restartedInstance = createRedisKeyRotator({ client, keys: ['a', 'b'], key: 'test:key-rotation' });
await expect(firstInstance.next()).resolves.toBe('a');
await expect(firstInstance.next()).resolves.toBe('b');
await expect(restartedInstance.next()).resolves.toBe('a');
});
});