Files
simstudioai--sim/apps/sim/lib/api/contracts/data-retention.test.ts
T
wehub-resource-sync d25d482dc2
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) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

158 lines
4.8 KiB
TypeScript

/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import { updateOrganizationDataRetentionBodySchema } from '@/lib/api/contracts/organization'
import {
piiRedactionRuleSchema,
piiRedactionSettingsSchema,
retentionOverridesSchema,
} from '@/lib/api/contracts/primitives'
describe('retentionOverridesSchema', () => {
it('accepts an override that overrides one field and inherits the rest', () => {
const result = retentionOverridesSchema.safeParse([
{ workspaceId: 'ws-1', logRetentionHours: 168 },
])
expect(result.success).toBe(true)
})
it('accepts null (forever) for a field', () => {
const result = retentionOverridesSchema.safeParse([
{ workspaceId: 'ws-1', logRetentionHours: null },
])
expect(result.success).toBe(true)
})
it('rejects two overrides for the same workspace', () => {
const result = retentionOverridesSchema.safeParse([
{ workspaceId: 'ws-1', logRetentionHours: 168 },
{ workspaceId: 'ws-1', softDeleteRetentionHours: 720 },
])
expect(result.success).toBe(false)
})
it('allows distinct workspaces', () => {
const result = retentionOverridesSchema.safeParse([
{ workspaceId: 'ws-1', logRetentionHours: 168 },
{ workspaceId: 'ws-2', logRetentionHours: 720 },
])
expect(result.success).toBe(true)
})
it('rejects hours below the 24h minimum and above the ~5y maximum', () => {
expect(
retentionOverridesSchema.safeParse([{ workspaceId: 'ws-1', logRetentionHours: 1 }]).success
).toBe(false)
expect(
retentionOverridesSchema.safeParse([{ workspaceId: 'ws-1', logRetentionHours: 100000 }])
.success
).toBe(false)
})
it('rejects an empty workspaceId', () => {
expect(
retentionOverridesSchema.safeParse([{ workspaceId: '', logRetentionHours: 168 }]).success
).toBe(false)
})
})
describe('updateOrganizationDataRetentionBodySchema', () => {
it('accepts retentionOverrides alongside the org hours', () => {
const result = updateOrganizationDataRetentionBodySchema.safeParse({
logRetentionHours: 720,
retentionOverrides: [{ workspaceId: 'ws-1', logRetentionHours: 168 }],
})
expect(result.success).toBe(true)
})
it('accepts a body with no retentionOverrides (field is optional)', () => {
const result = updateOrganizationDataRetentionBodySchema.safeParse({ logRetentionHours: 720 })
expect(result.success).toBe(true)
})
})
describe('piiRedactionRuleSchema', () => {
const stage = (enabled: boolean, entityTypes: string[], language?: string) => ({
enabled,
entityTypes,
...(language ? { language } : {}),
})
it('accepts a legacy flat rule (entityTypes only)', () => {
const result = piiRedactionRuleSchema.safeParse({
id: 'r-1',
workspaceId: null,
entityTypes: ['EMAIL_ADDRESS'],
})
expect(result.success).toBe(true)
})
it('accepts a per-stage rule', () => {
const result = piiRedactionRuleSchema.safeParse({
id: 'r-1',
workspaceId: 'ws-1',
stages: {
input: stage(true, ['PERSON'], 'es'),
blockOutputs: stage(false, []),
logs: stage(true, ['US_SSN']),
},
})
expect(result.success).toBe(true)
})
it('rejects a rule with neither stages nor entityTypes', () => {
const result = piiRedactionRuleSchema.safeParse({ id: 'r-1', workspaceId: null })
expect(result.success).toBe(false)
})
it('rejects an enabled stage with no entity types (redact-all is not expressible)', () => {
const result = piiRedactionRuleSchema.safeParse({
id: 'r-1',
workspaceId: null,
stages: {
input: stage(true, []),
blockOutputs: stage(false, []),
logs: stage(false, []),
},
})
expect(result.success).toBe(false)
})
it('accepts a disabled stage with no entity types (off)', () => {
const result = piiRedactionRuleSchema.safeParse({
id: 'r-1',
workspaceId: null,
stages: {
input: stage(false, []),
blockOutputs: stage(false, []),
logs: stage(true, ['PERSON']),
},
})
expect(result.success).toBe(true)
})
it('rejects an unsupported stage language', () => {
const result = piiRedactionRuleSchema.safeParse({
id: 'r-1',
workspaceId: null,
stages: {
input: stage(true, ['PERSON'], 'de'),
blockOutputs: stage(false, []),
logs: stage(false, []),
},
})
expect(result.success).toBe(false)
})
it('enforces one rule per scope (uniqueness refine still applies)', () => {
const result = piiRedactionSettingsSchema.safeParse({
rules: [
{ id: 'r-1', workspaceId: 'ws-1', entityTypes: ['PERSON'] },
{ id: 'r-2', workspaceId: 'ws-1', entityTypes: ['US_SSN'] },
],
})
expect(result.success).toBe(false)
})
})