|
1 | 1 | import { db } from '@sim/db' |
2 | | -import { dbChainMockFns, resetDbChainMock } from '@sim/testing' |
3 | | -import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { databaseMockFns, dbChainMockFns, resetDbChainMock } from '@sim/testing' |
| 3 | +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
4 | 4 | import { |
5 | 5 | getMemberOrganizationId, |
6 | 6 | getSecurityPolicyVersion, |
@@ -146,3 +146,37 @@ describe('getSessionCookieCacheVersion', () => { |
146 | 146 | expect(limit).toHaveBeenCalledTimes(2) |
147 | 147 | }) |
148 | 148 | }) |
| 149 | + |
| 150 | +describe('shared caches behind a sub-pool client', () => { |
| 151 | + beforeEach(() => { |
| 152 | + resetDbChainMock() |
| 153 | + invalidateMembershipCache('pool-user') |
| 154 | + invalidateSecurityPolicyVersionCache('pool-org') |
| 155 | + }) |
| 156 | + |
| 157 | + afterEach(() => { |
| 158 | + databaseMockFns.mockIsPrimaryPoolClient.mockReset() |
| 159 | + }) |
| 160 | + |
| 161 | + /** |
| 162 | + * Better Auth reads the cookie-cache version on every cached-session request through the auth |
| 163 | + * pool, not `db`; caching only reads made through `db` would cost two queries per request. |
| 164 | + */ |
| 165 | + it('serves a repeat cookie-cache version from cache when read through the auth pool', async () => { |
| 166 | + const authPool: typeof db = Object.create(db) |
| 167 | + databaseMockFns.mockIsPrimaryPoolClient.mockImplementation( |
| 168 | + (executor) => executor === db || executor === authPool |
| 169 | + ) |
| 170 | + dbChainMockFns.limit |
| 171 | + .mockResolvedValueOnce([{ organizationId: 'pool-org' }]) |
| 172 | + .mockResolvedValueOnce([{ version: 3 }]) |
| 173 | + |
| 174 | + await expect(getSessionCookieCacheVersion({ userId: 'pool-user' }, authPool)).resolves.toBe( |
| 175 | + 'pool-org:3' |
| 176 | + ) |
| 177 | + await expect(getSessionCookieCacheVersion({ userId: 'pool-user' }, authPool)).resolves.toBe( |
| 178 | + 'pool-org:3' |
| 179 | + ) |
| 180 | + expect(dbChainMockFns.limit).toHaveBeenCalledTimes(2) |
| 181 | + }) |
| 182 | +}) |
0 commit comments