Files
simstudioai--sim/apps/sim/lib/core/config/appconfig.test.ts
T
wehub-resource-sync d25d482dc2
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
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

137 lines
4.6 KiB
TypeScript

/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockSend } = vi.hoisted(() => ({
mockSend: vi.fn(),
}))
vi.mock('@aws-sdk/client-appconfigdata', () => ({
AppConfigDataClient: class {
send = mockSend
},
StartConfigurationSessionCommand: class {
__type = 'start'
constructor(public input: unknown) {}
},
GetLatestConfigurationCommand: class {
__type = 'get'
constructor(public input: unknown) {}
},
}))
import { fetchAppConfigProfile } from '@/lib/core/config/appconfig'
const encode = (value: unknown) => new TextEncoder().encode(JSON.stringify(value))
let counter = 0
/** Unique identifiers per test so the module-level cache never bleeds across tests. */
function uniqueIds() {
counter += 1
return { application: `app-${counter}`, environment: `env-${counter}`, profile: 'access-control' }
}
describe('fetchAppConfigProfile', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('starts a session then returns the parsed configuration', async () => {
mockSend.mockImplementation((command: { __type: string }) => {
if (command.__type === 'start') return Promise.resolve({ InitialConfigurationToken: 'tok-1' })
return Promise.resolve({
Configuration: encode({ blockedSignupDomains: ['spam.example'] }),
NextPollConfigurationToken: 'tok-2',
})
})
const result = await fetchAppConfigProfile(
uniqueIds(),
(json) => json as Record<string, unknown>
)
expect(result).toEqual({ blockedSignupDomains: ['spam.example'] })
const sentTypes = mockSend.mock.calls.map(([c]) => c.__type)
expect(sentTypes).toEqual(['start', 'get'])
})
it('returns null when the cold fetch fails (never throws)', async () => {
mockSend.mockRejectedValue(new Error('appconfig down'))
const result = await fetchAppConfigProfile(uniqueIds(), (json) => json)
expect(result).toBeNull()
})
it('applies the parse function to the decoded JSON', async () => {
mockSend.mockImplementation((command: { __type: string }) => {
if (command.__type === 'start') return Promise.resolve({ InitialConfigurationToken: 'tok-1' })
return Promise.resolve({
Configuration: encode({ count: 2 }),
NextPollConfigurationToken: 'tok-2',
})
})
const result = await fetchAppConfigProfile(
uniqueIds(),
(json) => (json as { count: number }).count * 10
)
expect(result).toBe(20)
})
it('warms the cache on an empty payload and does not re-poll (unseeded profile)', async () => {
mockSend.mockImplementation((command: { __type: string }) => {
if (command.__type === 'start') return Promise.resolve({ InitialConfigurationToken: 'tok-1' })
return Promise.resolve({
Configuration: new Uint8Array(),
NextPollConfigurationToken: 'tok-2',
NextPollIntervalInSeconds: 60,
})
})
const ids = uniqueIds()
expect(await fetchAppConfigProfile(ids, (json) => json)).toBeNull()
const callsAfterFirst = mockSend.mock.calls.length
expect(await fetchAppConfigProfile(ids, (json) => json)).toBeNull()
expect(mockSend.mock.calls.length).toBe(callsAfterFirst)
})
it('keeps the session on a parse error (no re-StartConfigurationSession, no throw)', async () => {
mockSend.mockImplementation((command: { __type: string }) => {
if (command.__type === 'start') return Promise.resolve({ InitialConfigurationToken: 'tok-1' })
return Promise.resolve({
Configuration: new TextEncoder().encode('not json{'),
NextPollConfigurationToken: 'tok-2',
NextPollIntervalInSeconds: 60,
})
})
const ids = uniqueIds()
expect(await fetchAppConfigProfile(ids, (json) => json)).toBeNull()
// Network round trip succeeded, so exactly one session was started despite the
// parse failure — the rotated token was preserved, not discarded.
expect(mockSend.mock.calls.filter(([c]) => c.__type === 'start')).toHaveLength(1)
})
it('dedupes concurrent cold fetches into a single poll', async () => {
mockSend.mockImplementation((command: { __type: string }) => {
if (command.__type === 'start') return Promise.resolve({ InitialConfigurationToken: 'tok-1' })
return Promise.resolve({
Configuration: encode({ x: 1 }),
NextPollConfigurationToken: 'tok-2',
})
})
const ids = uniqueIds()
const [a, b] = await Promise.all([
fetchAppConfigProfile(ids, (json) => json),
fetchAppConfigProfile(ids, (json) => json),
])
expect(a).toEqual({ x: 1 })
expect(b).toEqual({ x: 1 })
expect(mockSend.mock.calls.map(([c]) => c.__type)).toEqual(['start', 'get'])
})
})