Files
simstudioai--sim/apps/sim/lib/copilot/chat/sim-key-redaction.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

155 lines
6.2 KiB
TypeScript

/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import type { ChatMessage } from '@/app/workspace/[workspaceId]/home/types'
import {
captureRevealedSimKeys,
extractRevealedSimKeys,
restoreRevealedSimKeysForMessage,
} from './sim-key-redaction'
const credential = (value: string) =>
`<credential>${JSON.stringify({ value, type: 'sim_key' })}</credential>`
const redacted = `<credential>${JSON.stringify({ type: 'sim_key', redacted: true })}</credential>`
describe('sim-key-redaction', () => {
describe('extractRevealedSimKeys', () => {
it('returns sim_key values in document order', () => {
const text = `first ${credential('sk-sim-A')} mid ${credential('sk-sim-B')}`
expect(extractRevealedSimKeys(text)).toEqual(['sk-sim-A', 'sk-sim-B'])
})
it('skips redacted entries and non-sim_key tags', () => {
const link = `<credential>${JSON.stringify({ value: 'https://x', type: 'link', provider: 'slack' })}</credential>`
const text = `${link} ${credential('sk-sim-A')} ${redacted}`
expect(extractRevealedSimKeys(text)).toEqual(['sk-sim-A'])
})
})
describe('captureRevealedSimKeys', () => {
it('records new keys under each provided key', () => {
const cache = new Map<string, string[]>()
captureRevealedSimKeys(cache, ['msg-1', 'req-1'], credential('sk-sim-A'))
expect(cache.get('msg-1')).toEqual(['sk-sim-A'])
expect(cache.get('req-1')).toEqual(['sk-sim-A'])
})
it('extends but never shrinks the captured list across calls', () => {
const cache = new Map<string, string[]>()
captureRevealedSimKeys(
cache,
['msg-1'],
`${credential('sk-sim-A')} ${credential('sk-sim-B')}`
)
captureRevealedSimKeys(cache, ['msg-1'], credential('sk-sim-A'))
expect(cache.get('msg-1')).toEqual(['sk-sim-A', 'sk-sim-B'])
})
it('skips undefined keys without throwing', () => {
const cache = new Map<string, string[]>()
captureRevealedSimKeys(cache, ['msg-1', undefined], credential('sk-sim-A'))
expect(cache.get('msg-1')).toEqual(['sk-sim-A'])
expect(cache.size).toBe(1)
})
it('ignores content with no credential tag', () => {
const cache = new Map<string, string[]>()
captureRevealedSimKeys(cache, ['msg-1'], 'plain assistant text')
expect(cache.has('msg-1')).toBe(false)
})
})
describe('restoreRevealedSimKeysForMessage', () => {
it('substitutes the live key back into a redacted message', () => {
const cache = new Map<string, string[]>([['msg-1', ['sk-sim-A']]])
const msg: ChatMessage = {
id: 'msg-1',
role: 'assistant',
content: `Here is your key: ${redacted} save it.`,
contentBlocks: [{ type: 'text', content: `Here is your key: ${redacted} save it.` }],
}
const restored = restoreRevealedSimKeysForMessage(msg, cache)
expect(restored.content).toContain('"sk-sim-A"')
expect(restored.content).not.toContain('"redacted":true')
expect(restored.contentBlocks?.[0].content).toContain('"sk-sim-A"')
})
it('substitutes multiple keys in stream order', () => {
const cache = new Map<string, string[]>([['msg-1', ['sk-sim-A', 'sk-sim-B']]])
const msg: ChatMessage = {
id: 'msg-1',
role: 'assistant',
content: `first ${redacted} second ${redacted}`,
}
const restored = restoreRevealedSimKeysForMessage(msg, cache)
expect(restored.content).toBe(
`first ${credential('sk-sim-A')} second ${credential('sk-sim-B')}`
)
})
it('leaves a redacted tag in place if no live value is captured for that slot', () => {
const cache = new Map<string, string[]>([['msg-1', ['sk-sim-A']]])
const msg: ChatMessage = {
id: 'msg-1',
role: 'assistant',
content: `first ${redacted} second ${redacted}`,
}
const restored = restoreRevealedSimKeysForMessage(msg, cache)
expect(restored.content).toBe(`first ${credential('sk-sim-A')} second ${redacted}`)
})
it('returns the same message reference when nothing to restore', () => {
const cache = new Map<string, string[]>()
const msg: ChatMessage = {
id: 'msg-1',
role: 'assistant',
content: 'no credentials here',
}
expect(restoreRevealedSimKeysForMessage(msg, cache)).toBe(msg)
})
it('does nothing for user messages', () => {
const cache = new Map<string, string[]>([['msg-1', ['sk-sim-A']]])
const msg: ChatMessage = {
id: 'msg-1',
role: 'user',
content: redacted,
}
expect(restoreRevealedSimKeysForMessage(msg, cache)).toBe(msg)
})
it('threads the cursor across separate content blocks so each block gets its matching key', () => {
const cache = new Map<string, string[]>([['msg-1', ['sk-sim-A', 'sk-sim-B']]])
const msg: ChatMessage = {
id: 'msg-1',
role: 'assistant',
content: `first ${redacted} (tool ran) second ${redacted}`,
contentBlocks: [
{ type: 'text', content: `first ${redacted}` },
{ type: 'tool_call', content: '' },
{ type: 'text', content: `second ${redacted}` },
],
}
const restored = restoreRevealedSimKeysForMessage(msg, cache)
expect(restored.contentBlocks?.[0].content).toContain('"sk-sim-A"')
expect(restored.contentBlocks?.[0].content).not.toContain('"sk-sim-B"')
expect(restored.contentBlocks?.[2].content).toContain('"sk-sim-B"')
expect(restored.contentBlocks?.[2].content).not.toContain('"sk-sim-A"')
})
it('isolates revealed values by message id (multiple keys across messages)', () => {
const cache = new Map<string, string[]>([
['msg-1', ['sk-sim-A']],
['msg-2', ['sk-sim-B']],
])
const msg1: ChatMessage = { id: 'msg-1', role: 'assistant', content: redacted }
const msg2: ChatMessage = { id: 'msg-2', role: 'assistant', content: redacted }
expect(restoreRevealedSimKeysForMessage(msg1, cache).content).toContain('sk-sim-A')
expect(restoreRevealedSimKeysForMessage(msg2, cache).content).toContain('sk-sim-B')
expect(restoreRevealedSimKeysForMessage(msg1, cache).content).not.toContain('sk-sim-B')
})
})
})