@open-mercato/cache 0.11.1-develop.8593.1.50784dae35 → 0.11.1-develop.8665.1.2455b2503c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/AGENTS.md +27 -21
- package/README.md +30 -0
- package/dist/codec.js +1 -74
- package/dist/codec.js.map +2 -2
- package/dist/defaults.js +1 -6
- package/dist/defaults.js.map +2 -2
- package/dist/errors.js +1 -12
- package/dist/errors.js.map +2 -2
- package/dist/index.js.map +1 -1
- package/dist/patterns.js +1 -35
- package/dist/patterns.js.map +2 -2
- package/dist/provider.js +440 -0
- package/dist/provider.js.map +7 -0
- package/dist/redisConnection.js +2 -0
- package/dist/redisConnection.js.map +7 -0
- package/dist/service.js +320 -80
- package/dist/service.js.map +3 -3
- package/dist/strategies/jsonfile.js +1 -186
- package/dist/strategies/jsonfile.js.map +3 -3
- package/dist/strategies/memory.js +1 -196
- package/dist/strategies/memory.js.map +3 -3
- package/dist/strategies/redis.js +1 -443
- package/dist/strategies/redis.js.map +3 -3
- package/dist/strategies/sqlite.js +1 -196
- package/dist/strategies/sqlite.js.map +3 -3
- package/dist/testing/conformance.js +448 -0
- package/dist/testing/conformance.js.map +7 -0
- package/dist/testing/index.js +12 -0
- package/dist/testing/index.js.map +7 -0
- package/dist/testing/remote.js +182 -0
- package/dist/testing/remote.js.map +7 -0
- package/fixtures/build-conformance-host.mjs +58 -0
- package/fixtures/cache-provider-fixture/index.d.ts +18 -0
- package/fixtures/cache-provider-fixture/index.js +307 -0
- package/fixtures/cache-provider-fixture/package.json +25 -0
- package/fixtures/conformance-child.mjs +41 -0
- package/jest.config.cjs +3 -1
- package/package.json +10 -2
- package/src/__tests__/cache-di-contract-docs.test.ts +2 -2
- package/src/__tests__/conformance.memory-provider.test.ts +77 -0
- package/src/__tests__/provider-fixture.packed.test.ts +178 -0
- package/src/__tests__/provider.test.ts +469 -0
- package/src/__tests__/service.provider.test.ts +513 -0
- package/src/__tests__/service.test.ts +94 -3
- package/src/codec.ts +1 -94
- package/src/defaults.ts +1 -2
- package/src/errors.ts +1 -14
- package/src/index.ts +4 -0
- package/src/patterns.ts +1 -40
- package/src/provider.ts +641 -0
- package/src/redisConnection.ts +1 -0
- package/src/service.ts +396 -89
- package/src/strategies/jsonfile.ts +1 -242
- package/src/strategies/memory.ts +1 -275
- package/src/strategies/redis.ts +1 -615
- package/src/strategies/sqlite.ts +1 -292
- package/src/testing/conformance.ts +569 -0
- package/src/testing/index.ts +11 -0
- package/src/testing/remote.ts +240 -0
- package/src/types.ts +8 -106
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import { inspect } from 'node:util'
|
|
2
|
+
import {
|
|
3
|
+
CACHE_PROVIDER_API_VERSION,
|
|
4
|
+
CacheProviderError,
|
|
5
|
+
createCacheProviderHostContext,
|
|
6
|
+
defineCacheProvider,
|
|
7
|
+
defineCacheProviderRegistration,
|
|
8
|
+
getPreparedCacheProvider,
|
|
9
|
+
prepareCacheProviderSelection,
|
|
10
|
+
resetCacheProviderSelectionForTests,
|
|
11
|
+
resolveCacheProvider,
|
|
12
|
+
setCacheProviderRegistrations,
|
|
13
|
+
type CacheProviderDefinition,
|
|
14
|
+
type CacheProviderErrorCode,
|
|
15
|
+
type CacheProviderRegistration,
|
|
16
|
+
type CacheProviderSettingsSchema,
|
|
17
|
+
} from '../provider'
|
|
18
|
+
import { createMemoryStrategy } from '../strategies/memory'
|
|
19
|
+
|
|
20
|
+
const SECRET = 'sup3r-s3cret-password-value'
|
|
21
|
+
|
|
22
|
+
type FixtureConfig = { url: string; password: string; placement: 'local' | 'shared' }
|
|
23
|
+
|
|
24
|
+
const fixtureSchema: CacheProviderSettingsSchema<FixtureConfig> = {
|
|
25
|
+
safeParse(input) {
|
|
26
|
+
const record = (typeof input === 'object' && input !== null ? input : {}) as Record<string, unknown>
|
|
27
|
+
const issues: Array<{ path: PropertyKey[]; message: string }> = []
|
|
28
|
+
if (typeof record.url !== 'string' || !record.url.startsWith('fixture://')) {
|
|
29
|
+
issues.push({ path: ['url'], message: `expected fixture url, received ${String(record.url)}` })
|
|
30
|
+
}
|
|
31
|
+
if (typeof record.password !== 'string' || record.password.length < 4) {
|
|
32
|
+
issues.push({ path: ['auth', 'password'], message: `bad password ${String(record.password)}` })
|
|
33
|
+
}
|
|
34
|
+
if (issues.length > 0) return { success: false, error: { issues } }
|
|
35
|
+
return {
|
|
36
|
+
success: true,
|
|
37
|
+
data: {
|
|
38
|
+
url: record.url as string,
|
|
39
|
+
password: record.password as string,
|
|
40
|
+
placement: record.placement === 'local' ? 'local' : 'shared',
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function fixtureDefinition(id = 'acme/fixture'): CacheProviderDefinition<FixtureConfig> {
|
|
47
|
+
return defineCacheProvider<FixtureConfig>({
|
|
48
|
+
id,
|
|
49
|
+
settings: fixtureSchema,
|
|
50
|
+
capability: (config) => ({ placement: config.placement }),
|
|
51
|
+
create: () => ({ ...createMemoryStrategy(), healthcheck: async () => {}, close: async () => {} }),
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function registration(overrides: Partial<CacheProviderRegistration> & { id?: string } = {}): CacheProviderRegistration {
|
|
56
|
+
const id = overrides.id ?? 'acme/fixture'
|
|
57
|
+
return defineCacheProviderRegistration({
|
|
58
|
+
id,
|
|
59
|
+
load: async () => fixtureDefinition(id),
|
|
60
|
+
settings: () => ({ url: 'fixture://local', password: SECRET }),
|
|
61
|
+
...overrides,
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const baseEnv = { CACHE_PROVIDER: 'acme/fixture', CACHE_PROVIDER_NAMESPACE: 'deploy-1' }
|
|
66
|
+
|
|
67
|
+
async function expectProviderError(promise: Promise<unknown>, code: CacheProviderErrorCode): Promise<CacheProviderError> {
|
|
68
|
+
try {
|
|
69
|
+
await promise
|
|
70
|
+
} catch (error) {
|
|
71
|
+
expect(error).toBeInstanceOf(CacheProviderError)
|
|
72
|
+
const providerError = error as CacheProviderError
|
|
73
|
+
expect(providerError.code).toBe(code)
|
|
74
|
+
return providerError
|
|
75
|
+
}
|
|
76
|
+
throw new Error(`expected CacheProviderError ${code}`)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function expectRedacted(error: CacheProviderError) {
|
|
80
|
+
expect(error.message).not.toContain(SECRET)
|
|
81
|
+
expect(JSON.stringify(error)).not.toContain(SECRET)
|
|
82
|
+
expect(inspect(error, { depth: 10 })).not.toContain(SECRET)
|
|
83
|
+
expect(String(error.stack)).not.toContain(SECRET)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe('cache provider contract', () => {
|
|
87
|
+
afterEach(() => {
|
|
88
|
+
resetCacheProviderSelectionForTests()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('defineCacheProvider stamps the supported api version', () => {
|
|
92
|
+
const definition = fixtureDefinition()
|
|
93
|
+
expect(definition.apiVersion).toBe(CACHE_PROVIDER_API_VERSION)
|
|
94
|
+
expect(CACHE_PROVIDER_API_VERSION).toBe(1)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('defineCacheProviderRegistration has no side effects', () => {
|
|
98
|
+
const load = jest.fn(async () => fixtureDefinition())
|
|
99
|
+
const settings = jest.fn(() => ({}))
|
|
100
|
+
const result = defineCacheProviderRegistration({ id: 'acme/fixture', load, settings })
|
|
101
|
+
expect(result.id).toBe('acme/fixture')
|
|
102
|
+
expect(load).not.toHaveBeenCalled()
|
|
103
|
+
expect(settings).not.toHaveBeenCalled()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('returns null when CACHE_PROVIDER is unset or empty', async () => {
|
|
107
|
+
const reg = registration({ load: jest.fn(async () => fixtureDefinition()), settings: jest.fn(() => ({})) })
|
|
108
|
+
expect(await resolveCacheProvider([reg], { env: {} })).toBeNull()
|
|
109
|
+
expect(await resolveCacheProvider([reg], { env: { CACHE_PROVIDER: '' } })).toBeNull()
|
|
110
|
+
expect(await resolveCacheProvider([reg], { env: { CACHE_PROVIDER: ' ' } })).toBeNull()
|
|
111
|
+
expect(reg.load).not.toHaveBeenCalled()
|
|
112
|
+
expect(reg.settings).not.toHaveBeenCalled()
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('resolves the selected provider with config, placement and namespace', async () => {
|
|
116
|
+
const resolved = await resolveCacheProvider([registration()], { env: baseEnv })
|
|
117
|
+
expect(resolved).not.toBeNull()
|
|
118
|
+
expect(resolved?.id).toBe('acme/fixture')
|
|
119
|
+
expect(resolved?.placement).toBe('shared')
|
|
120
|
+
expect(resolved?.namespace).toBe('deploy-1')
|
|
121
|
+
expect(resolved?.config).toEqual({ url: 'fixture://local', password: SECRET, placement: 'shared' })
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
it('honours an explicit selectedId over the environment', async () => {
|
|
125
|
+
const resolved = await resolveCacheProvider(
|
|
126
|
+
[registration(), registration({ id: 'acme/other', load: async () => fixtureDefinition('acme/other') })],
|
|
127
|
+
{ env: baseEnv, selectedId: 'acme/other' },
|
|
128
|
+
)
|
|
129
|
+
expect(resolved?.id).toBe('acme/other')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('never loads or evaluates settings of inactive registrations', async () => {
|
|
133
|
+
const inactiveLoad = jest.fn(async () => fixtureDefinition('acme/inactive'))
|
|
134
|
+
const inactiveSettings = jest.fn(() => ({ url: 'fixture://x', password: SECRET }))
|
|
135
|
+
const activeSettings = jest.fn(() => ({ url: 'fixture://x', password: SECRET }))
|
|
136
|
+
await resolveCacheProvider(
|
|
137
|
+
[
|
|
138
|
+
registration({ id: 'acme/inactive', load: inactiveLoad, settings: inactiveSettings }),
|
|
139
|
+
registration({ settings: activeSettings }),
|
|
140
|
+
],
|
|
141
|
+
{ env: baseEnv },
|
|
142
|
+
)
|
|
143
|
+
expect(inactiveLoad).not.toHaveBeenCalled()
|
|
144
|
+
expect(inactiveSettings).not.toHaveBeenCalled()
|
|
145
|
+
expect(activeSettings).toHaveBeenCalledTimes(1)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
describe('registration validation', () => {
|
|
149
|
+
it.each(['memory', 'redis', 'sqlite', 'jsonfile', 'custom'])('rejects reserved id %s', async (id) => {
|
|
150
|
+
await expectProviderError(resolveCacheProvider([registration({ id })], { env: baseEnv }), 'reserved_id')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it.each(['Acme/fixture', 'acme/', '/fixture', 'acme/fix ture', 'acme/a/b', ''])('rejects invalid id %p', async (id) => {
|
|
154
|
+
await expectProviderError(resolveCacheProvider([registration({ id })], { env: baseEnv }), 'invalid_registration')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('rejects missing load or settings functions', async () => {
|
|
158
|
+
const noLoad = { id: 'acme/fixture', settings: () => ({}) } as unknown as CacheProviderRegistration
|
|
159
|
+
const noSettings = { id: 'acme/fixture', load: async () => fixtureDefinition() } as unknown as CacheProviderRegistration
|
|
160
|
+
await expectProviderError(resolveCacheProvider([noLoad], { env: baseEnv }), 'invalid_registration')
|
|
161
|
+
await expectProviderError(resolveCacheProvider([noSettings], { env: baseEnv }), 'invalid_registration')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('ignores registrations entirely when nothing is selected', async () => {
|
|
165
|
+
const invalid = [
|
|
166
|
+
registration({ id: 'memory' }),
|
|
167
|
+
registration({ id: 'Bad Id' }),
|
|
168
|
+
{ id: 'acme/fixture' } as unknown as CacheProviderRegistration,
|
|
169
|
+
registration(),
|
|
170
|
+
registration(),
|
|
171
|
+
]
|
|
172
|
+
await expect(resolveCacheProvider(invalid, { env: {} })).resolves.toBeNull()
|
|
173
|
+
await expect(resolveCacheProvider(invalid, { env: { CACHE_PROVIDER: ' ' } })).resolves.toBeNull()
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('rejects duplicate ids before loading anything', async () => {
|
|
177
|
+
const load = jest.fn(async () => fixtureDefinition())
|
|
178
|
+
const error = await expectProviderError(
|
|
179
|
+
resolveCacheProvider([registration({ load }), registration({ load })], { env: baseEnv }),
|
|
180
|
+
'duplicate_id',
|
|
181
|
+
)
|
|
182
|
+
expect(error.providerId).toBe('acme/fixture')
|
|
183
|
+
expect(load).not.toHaveBeenCalled()
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it('rejects a reserved selection', async () => {
|
|
187
|
+
await expectProviderError(
|
|
188
|
+
resolveCacheProvider([registration()], { env: { ...baseEnv, CACHE_PROVIDER: 'redis' } }),
|
|
189
|
+
'reserved_id',
|
|
190
|
+
)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('reports an unregistered selection', async () => {
|
|
194
|
+
const error = await expectProviderError(
|
|
195
|
+
resolveCacheProvider([registration()], { env: { ...baseEnv, CACHE_PROVIDER: 'acme/missing' } }),
|
|
196
|
+
'not_registered',
|
|
197
|
+
)
|
|
198
|
+
expect(error.providerId).toBe('acme/missing')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('definition validation', () => {
|
|
203
|
+
it('wraps load failures without leaking the cause', async () => {
|
|
204
|
+
const cause = new Error(`connect failed with password=${SECRET}`)
|
|
205
|
+
const error = await expectProviderError(
|
|
206
|
+
resolveCacheProvider([registration({ load: async () => { throw cause } })], { env: baseEnv }),
|
|
207
|
+
'load_failed',
|
|
208
|
+
)
|
|
209
|
+
expect(error.originalError).toBe(cause)
|
|
210
|
+
expect(Object.keys(error)).not.toContain('originalError')
|
|
211
|
+
expectRedacted(error)
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
it('rejects unsupported api versions', async () => {
|
|
215
|
+
const definition = { ...fixtureDefinition(), apiVersion: 2 } as unknown as CacheProviderDefinition<unknown>
|
|
216
|
+
await expectProviderError(
|
|
217
|
+
resolveCacheProvider([registration({ load: async () => definition })], { env: baseEnv }),
|
|
218
|
+
'unsupported_api_version',
|
|
219
|
+
)
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
it('rejects id mismatch between registration and definition', async () => {
|
|
223
|
+
await expectProviderError(
|
|
224
|
+
resolveCacheProvider([registration({ load: async () => fixtureDefinition('acme/else') })], { env: baseEnv }),
|
|
225
|
+
'id_mismatch',
|
|
226
|
+
)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('rejects malformed definitions', async () => {
|
|
230
|
+
const broken = { apiVersion: 1, id: 'acme/fixture', settings: {}, capability: () => ({ placement: 'shared' }), create: () => null } as unknown as CacheProviderDefinition<unknown>
|
|
231
|
+
await expectProviderError(
|
|
232
|
+
resolveCacheProvider([registration({ load: async () => broken })], { env: baseEnv }),
|
|
233
|
+
'invalid_registration',
|
|
234
|
+
)
|
|
235
|
+
await expectProviderError(
|
|
236
|
+
resolveCacheProvider([registration({ load: async () => null as unknown as CacheProviderDefinition<unknown> })], { env: baseEnv }),
|
|
237
|
+
'invalid_registration',
|
|
238
|
+
)
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('rejects an invalid placement from capability()', async () => {
|
|
242
|
+
const definition = { ...fixtureDefinition(), capability: () => ({ placement: 'global' }) } as unknown as CacheProviderDefinition<unknown>
|
|
243
|
+
await expectProviderError(
|
|
244
|
+
resolveCacheProvider([registration({ load: async () => definition })], { env: baseEnv }),
|
|
245
|
+
'invalid_registration',
|
|
246
|
+
)
|
|
247
|
+
})
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
describe('settings', () => {
|
|
251
|
+
it('reports only field paths for invalid settings', async () => {
|
|
252
|
+
const error = await expectProviderError(
|
|
253
|
+
resolveCacheProvider(
|
|
254
|
+
[registration({ settings: () => ({ url: `http://${SECRET}`, password: 'x' }) })],
|
|
255
|
+
{ env: baseEnv },
|
|
256
|
+
),
|
|
257
|
+
'invalid_settings',
|
|
258
|
+
)
|
|
259
|
+
expect(error.fieldPaths).toEqual(['url', 'auth.password'])
|
|
260
|
+
expect(error.message).toContain('url')
|
|
261
|
+
expect(error.message).toContain('auth.password')
|
|
262
|
+
expect(error.message).not.toContain('expected fixture url')
|
|
263
|
+
expectRedacted(error)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
it('wraps a throwing settings function without leaking the cause', async () => {
|
|
267
|
+
const error = await expectProviderError(
|
|
268
|
+
resolveCacheProvider(
|
|
269
|
+
[registration({ settings: () => { throw new Error(`env ${SECRET} missing`) } })],
|
|
270
|
+
{ env: baseEnv },
|
|
271
|
+
),
|
|
272
|
+
'invalid_settings',
|
|
273
|
+
)
|
|
274
|
+
expectRedacted(error)
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('requires a namespace', async () => {
|
|
278
|
+
await expectProviderError(
|
|
279
|
+
resolveCacheProvider([registration()], { env: { CACHE_PROVIDER: 'acme/fixture' } }),
|
|
280
|
+
'missing_namespace',
|
|
281
|
+
)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it.each(['has space', 'a'.repeat(65), 'tenant:1', 'ns/1'])('rejects invalid namespace %p', async (namespace) => {
|
|
285
|
+
const error = await expectProviderError(
|
|
286
|
+
resolveCacheProvider([registration()], { env: { ...baseEnv, CACHE_PROVIDER_NAMESPACE: namespace } }),
|
|
287
|
+
'invalid_namespace',
|
|
288
|
+
)
|
|
289
|
+
expect(error.message).not.toContain(namespace)
|
|
290
|
+
})
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
describe('error serialization', () => {
|
|
294
|
+
it('serializes code, provider id and field paths only', () => {
|
|
295
|
+
const error = new CacheProviderError('invalid_settings', 'bad', { providerId: 'acme/fixture', fieldPaths: ['url'], cause: SECRET })
|
|
296
|
+
expect(JSON.parse(JSON.stringify(error))).toEqual({
|
|
297
|
+
name: 'CacheProviderError',
|
|
298
|
+
code: 'invalid_settings',
|
|
299
|
+
message: 'bad',
|
|
300
|
+
providerId: 'acme/fixture',
|
|
301
|
+
fieldPaths: ['url'],
|
|
302
|
+
})
|
|
303
|
+
expectRedacted(error)
|
|
304
|
+
})
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
describe('process selection holder', () => {
|
|
308
|
+
const originalEnv = { ...process.env }
|
|
309
|
+
|
|
310
|
+
afterEach(() => {
|
|
311
|
+
process.env = { ...originalEnv }
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
it('returns null when no provider is selected', async () => {
|
|
315
|
+
delete process.env.CACHE_PROVIDER
|
|
316
|
+
setCacheProviderRegistrations([registration()])
|
|
317
|
+
await expect(prepareCacheProviderSelection()).resolves.toBeNull()
|
|
318
|
+
expect(getPreparedCacheProvider()).toBeNull()
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
it('evaluates settings exactly once per process', async () => {
|
|
322
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
323
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
324
|
+
const settings = jest.fn(() => ({ url: 'fixture://x', password: SECRET }))
|
|
325
|
+
const load = jest.fn(async () => fixtureDefinition())
|
|
326
|
+
setCacheProviderRegistrations([registration({ settings, load })])
|
|
327
|
+
const [first, second] = await Promise.all([prepareCacheProviderSelection(), prepareCacheProviderSelection()])
|
|
328
|
+
const third = await prepareCacheProviderSelection()
|
|
329
|
+
expect(first).toBe(second)
|
|
330
|
+
expect(third).toBe(first)
|
|
331
|
+
expect(settings).toHaveBeenCalledTimes(1)
|
|
332
|
+
expect(load).toHaveBeenCalledTimes(1)
|
|
333
|
+
expect(getPreparedCacheProvider()).toBe(first)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
it('throws not_resolved when selection is required but not prepared', () => {
|
|
337
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
338
|
+
setCacheProviderRegistrations([registration()])
|
|
339
|
+
try {
|
|
340
|
+
getPreparedCacheProvider()
|
|
341
|
+
throw new Error('expected not_resolved')
|
|
342
|
+
} catch (error) {
|
|
343
|
+
expect((error as CacheProviderError).code).toBe('not_resolved')
|
|
344
|
+
}
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
it('treats empty CACHE_PROVIDER as unset in the accessor', () => {
|
|
348
|
+
process.env.CACHE_PROVIDER = ''
|
|
349
|
+
expect(getPreparedCacheProvider()).toBeNull()
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
it('memoizes and rethrows a failed selection', async () => {
|
|
353
|
+
process.env.CACHE_PROVIDER = 'acme/missing'
|
|
354
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
355
|
+
setCacheProviderRegistrations([registration()])
|
|
356
|
+
await expectProviderError(prepareCacheProviderSelection(), 'not_registered')
|
|
357
|
+
await expectProviderError(prepareCacheProviderSelection(), 'not_registered')
|
|
358
|
+
expect(() => getPreparedCacheProvider()).toThrow(CacheProviderError)
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
it('lets stock selection succeed with invalid registrations when CACHE_PROVIDER is unset', async () => {
|
|
362
|
+
delete process.env.CACHE_PROVIDER
|
|
363
|
+
setCacheProviderRegistrations([registration({ id: 'redis' }), registration({ id: 'not valid' })])
|
|
364
|
+
await expect(prepareCacheProviderSelection()).resolves.toBeNull()
|
|
365
|
+
expect(getPreparedCacheProvider()).toBeNull()
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
it('discards the memoized selection when registrations are replaced', async () => {
|
|
369
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
370
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
371
|
+
const firstLoad = jest.fn(async () => fixtureDefinition())
|
|
372
|
+
setCacheProviderRegistrations([registration({ load: firstLoad })])
|
|
373
|
+
const first = await prepareCacheProviderSelection()
|
|
374
|
+
const secondLoad = jest.fn(async () => fixtureDefinition())
|
|
375
|
+
setCacheProviderRegistrations([registration({ load: secondLoad })])
|
|
376
|
+
expect(() => getPreparedCacheProvider()).toThrow(expect.objectContaining({ code: 'not_resolved' }))
|
|
377
|
+
const second = await prepareCacheProviderSelection()
|
|
378
|
+
expect(second).not.toBe(first)
|
|
379
|
+
expect(firstLoad).toHaveBeenCalledTimes(1)
|
|
380
|
+
expect(secondLoad).toHaveBeenCalledTimes(1)
|
|
381
|
+
expect(getPreparedCacheProvider()).toBe(second)
|
|
382
|
+
})
|
|
383
|
+
|
|
384
|
+
it('re-resolves when the selected id or namespace changes', async () => {
|
|
385
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
386
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
387
|
+
const load = jest.fn(async () => fixtureDefinition())
|
|
388
|
+
const otherLoad = jest.fn(async () => fixtureDefinition('acme/other'))
|
|
389
|
+
setCacheProviderRegistrations([registration({ load }), registration({ id: 'acme/other', load: otherLoad })])
|
|
390
|
+
const first = await prepareCacheProviderSelection()
|
|
391
|
+
expect(first?.id).toBe('acme/fixture')
|
|
392
|
+
|
|
393
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-2'
|
|
394
|
+
expect(() => getPreparedCacheProvider()).toThrow(expect.objectContaining({ code: 'not_resolved' }))
|
|
395
|
+
const renamed = await prepareCacheProviderSelection()
|
|
396
|
+
expect(renamed?.namespace).toBe('deploy-2')
|
|
397
|
+
expect(load).toHaveBeenCalledTimes(2)
|
|
398
|
+
|
|
399
|
+
process.env.CACHE_PROVIDER = 'acme/other'
|
|
400
|
+
const switched = await prepareCacheProviderSelection()
|
|
401
|
+
expect(switched?.id).toBe('acme/other')
|
|
402
|
+
expect(getPreparedCacheProvider()).toBe(switched)
|
|
403
|
+
expect(otherLoad).toHaveBeenCalledTimes(1)
|
|
404
|
+
})
|
|
405
|
+
|
|
406
|
+
it('does not memoize not_registered against an empty registration list', async () => {
|
|
407
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
408
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
409
|
+
await expectProviderError(prepareCacheProviderSelection(), 'not_registered')
|
|
410
|
+
expect(() => getPreparedCacheProvider()).toThrow(expect.objectContaining({ code: 'not_resolved' }))
|
|
411
|
+
setCacheProviderRegistrations([registration()])
|
|
412
|
+
const resolved = await prepareCacheProviderSelection()
|
|
413
|
+
expect(resolved?.id).toBe('acme/fixture')
|
|
414
|
+
})
|
|
415
|
+
|
|
416
|
+
it('is shared through a global symbol', async () => {
|
|
417
|
+
process.env.CACHE_PROVIDER = 'acme/fixture'
|
|
418
|
+
process.env.CACHE_PROVIDER_NAMESPACE = 'deploy-1'
|
|
419
|
+
setCacheProviderRegistrations([registration()])
|
|
420
|
+
await prepareCacheProviderSelection()
|
|
421
|
+
const holder = (globalThis as Record<symbol, unknown>)[Symbol.for('@open-mercato/cache/provider-selection')]
|
|
422
|
+
expect(holder).toBeDefined()
|
|
423
|
+
})
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
describe('host context', () => {
|
|
427
|
+
it('exposes namespace, codec helpers and a redacting logger', () => {
|
|
428
|
+
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {})
|
|
429
|
+
try {
|
|
430
|
+
const context = createCacheProviderHostContext({ id: 'acme/fixture', namespace: 'deploy-1' }, { defaultTtl: 500, codec: { compression: 'gzip', compressionMinBytes: 1 } })
|
|
431
|
+
expect(context.namespace).toBe('deploy-1')
|
|
432
|
+
expect(context.defaultTtl).toBe(500)
|
|
433
|
+
const json = JSON.stringify({ value: 'x'.repeat(200) })
|
|
434
|
+
const encoded = context.helpers.encode(json)
|
|
435
|
+
expect(Buffer.isBuffer(encoded)).toBe(true)
|
|
436
|
+
expect(context.helpers.decode(encoded)).toBe(json)
|
|
437
|
+
expect(context.helpers.matchPattern('a:b', 'a:*')).toBe(true)
|
|
438
|
+
context.logger.warn('reconnecting', { password: SECRET, attempt: 2, nested: { token: SECRET } })
|
|
439
|
+
const logged = inspect(warn.mock.calls)
|
|
440
|
+
expect(logged).not.toContain(SECRET)
|
|
441
|
+
expect(logged).toContain('attempt')
|
|
442
|
+
} finally {
|
|
443
|
+
warn.mockRestore()
|
|
444
|
+
}
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
it('scrubs URL user-info and secret pairs from string meta values', () => {
|
|
448
|
+
const error = jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
449
|
+
try {
|
|
450
|
+
const context = createCacheProviderHostContext({ id: 'acme/fixture', namespace: 'deploy-1' })
|
|
451
|
+
context.logger.error('connection lost', {
|
|
452
|
+
reason: `connect ECONNREFUSED rediss://default:${SECRET}@cache.internal:6380/0`,
|
|
453
|
+
detail: `host=db.internal port=5432 password=${SECRET} sslmode=require`,
|
|
454
|
+
quoted: `Server=x;User Id=app;Password="${SECRET}";`,
|
|
455
|
+
atSign: `redis://user:p@${SECRET}@host:6379`,
|
|
456
|
+
plain: 'retrying in 500ms',
|
|
457
|
+
})
|
|
458
|
+
const [, meta] = error.mock.calls[0] as [string, Record<string, string>]
|
|
459
|
+
expect(inspect(error.mock.calls)).not.toContain(SECRET)
|
|
460
|
+
expect(meta.reason).toBe('connect ECONNREFUSED rediss://***@cache.internal:6380/0')
|
|
461
|
+
expect(meta.detail).toBe('host=db.internal port=5432 password=*** sslmode=require')
|
|
462
|
+
expect(meta.atSign).toBe('redis://***@host:6379')
|
|
463
|
+
expect(meta.plain).toBe('retrying in 500ms')
|
|
464
|
+
} finally {
|
|
465
|
+
error.mockRestore()
|
|
466
|
+
}
|
|
467
|
+
})
|
|
468
|
+
})
|
|
469
|
+
})
|