Files
simstudioai--sim/apps/sim/lib/workflows/operations/import-export.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

164 lines
4.4 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
vi.unmock('@/blocks/registry')
import {
extractWorkflowName,
parseWorkflowJson,
sanitizePathSegment,
} from '@/lib/workflows/operations/import-export'
function createLegacyState() {
return {
blocks: {
'start-1': {
id: 'start-1',
type: 'start_trigger',
name: 'Start',
position: { x: 0, y: 0 },
enabled: true,
subBlocks: {
inputFormat: {
id: 'inputFormat',
type: 'input-format',
value: [],
},
undefined: {
type: 'unknown',
value: 'stale duplicate',
},
},
outputs: {},
data: {},
},
},
edges: [],
loops: {},
parallels: {},
variables: {},
metadata: {
name: 'Wrapped Workflow',
},
}
}
describe('workflow import/export parsing', () => {
it('parses workflow exports wrapped in an API data envelope', () => {
const content = JSON.stringify({
data: {
version: '1.0',
exportedAt: '2026-05-07T06:45:06.892Z',
workflow: {
name: 'Wrapped Workflow',
},
state: createLegacyState(),
},
})
const result = parseWorkflowJson(content, false)
expect(result.errors).toEqual([])
expect(result.data?.blocks['start-1']).toBeDefined()
expect(result.data?.blocks['start-1'].subBlocks.inputFormat).toEqual({
id: 'inputFormat',
type: 'input-format',
value: [],
})
expect(result.data?.blocks['start-1'].subBlocks.undefined).toBeUndefined()
})
it('extracts workflow names from wrapped exports', () => {
const content = JSON.stringify({
data: {
workflow: {
name: 'Wrapped Workflow',
},
state: createLegacyState(),
},
})
expect(extractWorkflowName(content, 'wf.json')).toBe('Wrapped Workflow')
})
it('parses API envelopes that contain state without an export version', () => {
const content = JSON.stringify({
data: {
workflow: {
name: 'API Workflow',
},
state: createLegacyState(),
},
})
const result = parseWorkflowJson(content, false)
expect(result.errors).toEqual([])
expect(result.data?.blocks['start-1']).toBeDefined()
expect(result.data?.blocks['start-1'].subBlocks.undefined).toBeUndefined()
})
it('preserves malformed legacy renamed subBlocks during import parsing', () => {
const state = {
...createLegacyState(),
blocks: {
knowledge: {
id: 'knowledge',
type: 'knowledge',
name: 'Knowledge',
position: { x: 0, y: 0 },
enabled: true,
subBlocks: {
operation: { id: 'operation', type: 'dropdown', value: 'search' },
knowledgeBaseId: {
id: 'knowledgeBaseId',
type: 'unknown',
value: 'kb-uuid-123',
},
},
outputs: {},
data: {},
},
},
}
const content = JSON.stringify({ data: { workflow: { name: 'Knowledge Workflow' }, state } })
const result = parseWorkflowJson(content, false)
expect(result.errors).toEqual([])
expect(result.data?.blocks.knowledge.subBlocks.knowledgeBaseId).toBeUndefined()
expect(result.data?.blocks.knowledge.subBlocks.knowledgeBaseSelector).toEqual({
id: 'knowledgeBaseSelector',
type: 'knowledge-base-selector',
value: 'kb-uuid-123',
})
})
})
describe('sanitizePathSegment', () => {
it('should preserve ASCII alphanumeric characters', () => {
expect(sanitizePathSegment('workflow-123_abc')).toBe('workflow-123_abc')
})
it('should replace spaces with dashes', () => {
expect(sanitizePathSegment('my workflow')).toBe('my-workflow')
})
it('should replace special characters with dashes', () => {
expect(sanitizePathSegment('workflow!@#')).toBe('workflow-')
})
it('should preserve Korean characters (BUG REPRODUCTION)', () => {
expect(sanitizePathSegment('한글')).toBe('한글')
})
it('should preserve other Unicode characters', () => {
expect(sanitizePathSegment('日本語')).toBe('日本語')
})
it('should remove filesystem unsafe characters', () => {
expect(sanitizePathSegment('work/flow?name*')).not.toContain('/')
expect(sanitizePathSegment('work/flow?name*')).not.toContain('?')
expect(sanitizePathSegment('work/flow?name*')).not.toContain('*')
})
})