chore: import upstream snapshot with attribution
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { getCachedProviderClient } from '@/providers/client-cache'
|
||||
|
||||
/**
|
||||
* Builds a fresh fake "client" object on every call so identity comparisons
|
||||
* (`toBe`) tell us whether the cache returned the memoized instance or a new one
|
||||
* from the factory. We never construct a real SDK client — these tests exercise
|
||||
* the cache, not any provider SDK.
|
||||
*/
|
||||
function makeFactory() {
|
||||
return vi.fn(() => ({}) as object)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a unique suffix per test so distinct tests never collide on cache
|
||||
* keys. The cache util exposes no reset hook, so isolation is achieved by
|
||||
* namespacing keys rather than clearing shared state.
|
||||
*/
|
||||
let keyCounter = 0
|
||||
function uniqueNs(): string {
|
||||
keyCounter += 1
|
||||
return `ns-${keyCounter}-${Date.now()}`
|
||||
}
|
||||
|
||||
describe('getCachedProviderClient', () => {
|
||||
it('returns the SAME instance for an identical key and runs the factory once (memoized)', () => {
|
||||
const key = `anthropic::${uniqueNs()}::default`
|
||||
const factory = makeFactory()
|
||||
|
||||
const first = getCachedProviderClient(key, factory)
|
||||
const second = getCachedProviderClient(key, factory)
|
||||
|
||||
expect(second).toBe(first)
|
||||
expect(factory).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('returns a DIFFERENT instance for a different apiKey (tenant isolation)', () => {
|
||||
const ns = uniqueNs()
|
||||
const factoryA = makeFactory()
|
||||
const factoryB = makeFactory()
|
||||
|
||||
const tenantA = getCachedProviderClient(`anthropic::${ns}-tenant-a::default`, factoryA)
|
||||
const tenantB = getCachedProviderClient(`anthropic::${ns}-tenant-b::default`, factoryB)
|
||||
|
||||
expect(tenantB).not.toBe(tenantA)
|
||||
expect(factoryA).toHaveBeenCalledTimes(1)
|
||||
expect(factoryB).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('namespaces by provider: the same apiKey under different provider prefixes does not collide', () => {
|
||||
const ns = uniqueNs()
|
||||
const apiKey = `${ns}-shared-key`
|
||||
const anthropicFactory = makeFactory()
|
||||
const bedrockFactory = makeFactory()
|
||||
|
||||
const anthropicClient = getCachedProviderClient(`anthropic::${apiKey}`, anthropicFactory)
|
||||
const bedrockClient = getCachedProviderClient(`bedrock::${apiKey}`, bedrockFactory)
|
||||
|
||||
expect(bedrockClient).not.toBe(anthropicClient)
|
||||
})
|
||||
|
||||
it('treats every distinct key dimension as a distinct client', () => {
|
||||
const ns = uniqueNs()
|
||||
const base = `azure-anthropic::${ns}-key::https://a.example.com::2023-06-01::10.0.0.1::default`
|
||||
const baseFactory = makeFactory()
|
||||
const baseClient = getCachedProviderClient(base, baseFactory)
|
||||
|
||||
const variants = [
|
||||
`azure-anthropic::${ns}-key::https://b.example.com::2023-06-01::10.0.0.1::default`,
|
||||
`azure-anthropic::${ns}-key::https://a.example.com::2024-10-22::10.0.0.1::default`,
|
||||
`azure-anthropic::${ns}-key::https://a.example.com::2023-06-01::10.0.0.2::default`,
|
||||
`azure-anthropic::${ns}-key::https://a.example.com::2023-06-01::no-pin::default`,
|
||||
`azure-anthropic::${ns}-key::https://a.example.com::2023-06-01::10.0.0.1::beta`,
|
||||
]
|
||||
|
||||
for (const key of variants) {
|
||||
const factory = makeFactory()
|
||||
const client = getCachedProviderClient(key, factory)
|
||||
expect(client).not.toBe(baseClient)
|
||||
expect(factory).toHaveBeenCalledTimes(1)
|
||||
}
|
||||
})
|
||||
|
||||
it('evicts the least-recently-used entry once the cache cap is exceeded', () => {
|
||||
const ns = uniqueNs()
|
||||
const CAP = 1_000
|
||||
|
||||
const oldestKey = `evict::${ns}::0`
|
||||
const oldestFactory = makeFactory()
|
||||
getCachedProviderClient(oldestKey, oldestFactory)
|
||||
expect(oldestFactory).toHaveBeenCalledTimes(1)
|
||||
|
||||
// Fill the remaining capacity, then push one past the cap. The oldest key has
|
||||
// not been touched since insertion, so it is the LRU eviction victim.
|
||||
for (let i = 1; i <= CAP; i += 1) {
|
||||
getCachedProviderClient(`evict::${ns}::${i}`, makeFactory())
|
||||
}
|
||||
|
||||
const reFactory = makeFactory()
|
||||
getCachedProviderClient(oldestKey, reFactory)
|
||||
expect(reFactory).toHaveBeenCalledTimes(1)
|
||||
expect(oldestFactory).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user