mirror of https://github.com/wg-easy/wg-easy
9 changed files with 144 additions and 42 deletions
@ -0,0 +1,26 @@ |
|||||
|
import { describe, expect, test, vi } from 'vitest'; |
||||
|
import { cacheFunction } from '../../server/utils/cache'; |
||||
|
|
||||
|
describe('cacheFunction', () => { |
||||
|
test('retries after a rejected async result instead of caching the failure', async () => { |
||||
|
const fn = vi |
||||
|
.fn<() => Promise<string>>() |
||||
|
.mockRejectedValueOnce(new Error('boom')) |
||||
|
.mockResolvedValueOnce('ok'); |
||||
|
|
||||
|
const cached = cacheFunction(fn, { expiry: 60_000 }); |
||||
|
|
||||
|
await expect(cached()).rejects.toThrow('boom'); |
||||
|
await expect(cached()).resolves.toBe('ok'); |
||||
|
expect(fn).toHaveBeenCalledTimes(2); |
||||
|
}); |
||||
|
|
||||
|
test('reuses successful async results until expiry', async () => { |
||||
|
const fn = vi.fn<() => Promise<string>>().mockResolvedValue('ok'); |
||||
|
const cached = cacheFunction(fn, { expiry: 60_000 }); |
||||
|
|
||||
|
await expect(cached()).resolves.toBe('ok'); |
||||
|
await expect(cached()).resolves.toBe('ok'); |
||||
|
expect(fn).toHaveBeenCalledTimes(1); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,43 @@ |
|||||
|
import { beforeAll, describe, expect, test } from 'vitest'; |
||||
|
|
||||
|
declare global { |
||||
|
var t: (value: string) => string; |
||||
|
} |
||||
|
|
||||
|
let GeneralUpdateSchema: typeof import('../../server/database/repositories/general/types').GeneralUpdateSchema; |
||||
|
|
||||
|
beforeAll(async () => { |
||||
|
globalThis.t = (value: string) => value; |
||||
|
({ GeneralUpdateSchema } = await import( |
||||
|
'../../server/database/repositories/general/types' |
||||
|
)); |
||||
|
}); |
||||
|
|
||||
|
describe('GeneralUpdateSchema', () => { |
||||
|
test('requires a metrics password when metrics are enabled', async () => { |
||||
|
await expect( |
||||
|
GeneralUpdateSchema.parseAsync({ |
||||
|
sessionTimeout: 60, |
||||
|
metricsPrometheus: true, |
||||
|
metricsJson: false, |
||||
|
metricsPassword: null, |
||||
|
}) |
||||
|
).rejects.toThrow(); |
||||
|
}); |
||||
|
|
||||
|
test('allows disabling metrics without a password', async () => { |
||||
|
await expect( |
||||
|
GeneralUpdateSchema.parseAsync({ |
||||
|
sessionTimeout: 60, |
||||
|
metricsPrometheus: false, |
||||
|
metricsJson: false, |
||||
|
metricsPassword: null, |
||||
|
}) |
||||
|
).resolves.toEqual({ |
||||
|
sessionTimeout: 60, |
||||
|
metricsPrometheus: false, |
||||
|
metricsJson: false, |
||||
|
metricsPassword: null, |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
Loading…
Reference in new issue