Files
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

150 lines
5.1 KiB
TypeScript

/**
* @vitest-environment node
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
vi.unmock('@/blocks/registry')
const { synthRegistry } = vi.hoisted(() => {
const block = (type: string, extra: Record<string, unknown> = {}) => ({
type,
name: type.toUpperCase(),
description: '',
category: 'tools',
bgColor: '#000',
icon: () => null,
subBlocks: [],
tools: { access: [] },
inputs: {},
outputs: {},
...extra,
})
return {
synthRegistry: {
slack: block('slack'),
gmail_v2: block('gmail_v2', { preview: true }),
old_v1: block('old_v1', { hideFromToolbar: true }),
},
}
})
vi.mock('@/blocks/registry-maps', () => ({
BLOCK_REGISTRY: synthRegistry,
BLOCK_META_REGISTRY: {},
}))
import type { BlockVisibilityState } from '@/lib/core/config/block-visibility'
import { registerBlockOverlayResolver } from '@/blocks/custom/overlay'
import { getAllBlocks, getBlock, getCanonicalBlocksByCategory } from '@/blocks/registry'
import type { BlockConfig } from '@/blocks/types'
import { isHiddenUnder, registerBlockVisibilityResolver } from '@/blocks/visibility/context'
function vis(partial: Partial<BlockVisibilityState> = {}): BlockVisibilityState {
return {
revealed: new Set(),
disabled: new Set(),
previewTagged: new Set(),
...partial,
}
}
function withVisibility(state: BlockVisibilityState | null) {
registerBlockVisibilityResolver(state ? { current: () => state } : null)
}
const byType = (blocks: BlockConfig[], type: string) => blocks.find((b) => b.type === type)
afterEach(() => {
registerBlockVisibilityResolver(null)
registerBlockOverlayResolver(null)
})
describe('isHiddenUnder', () => {
it('hides unrevealed preview blocks even with a null state (fail-closed)', () => {
expect(isHiddenUnder(null, { type: 'gmail_v2', preview: true })).toBe(true)
expect(isHiddenUnder(null, { type: 'slack' })).toBe(false)
})
it('reveals preview blocks named in revealed', () => {
const state = vis({ revealed: new Set(['gmail_v2']) })
expect(isHiddenUnder(state, { type: 'gmail_v2', preview: true })).toBe(false)
})
it('hides kill-switched types only with an active state', () => {
const state = vis({ disabled: new Set(['slack']) })
expect(isHiddenUnder(state, { type: 'slack' })).toBe(true)
expect(isHiddenUnder(null, { type: 'slack' })).toBe(false)
})
})
describe('registry projection', () => {
it('hides preview blocks without any context: clone-not-remove', () => {
withVisibility(null)
const all = getAllBlocks()
const gmail = byType(all, 'gmail_v2')
expect(gmail).toBeDefined()
expect(gmail?.hideFromToolbar).toBe(true)
// the underlying registry entry is untouched
expect(getBlock('gmail_v2')?.hideFromToolbar).toBeUndefined()
// canonical (filtered) set excludes it
expect(byType(getCanonicalBlocksByCategory('tools'), 'gmail_v2')).toBeUndefined()
})
it('reveals a preview block with a " (Preview)" display suffix when tagged', () => {
withVisibility(vis({ revealed: new Set(['gmail_v2']), previewTagged: new Set(['gmail_v2']) }))
expect(byType(getAllBlocks(), 'gmail_v2')?.name).toBe('GMAIL_V2 (Preview)')
const canonical = byType(getCanonicalBlocksByCategory('tools'), 'gmail_v2')
expect(canonical?.name).toBe('GMAIL_V2 (Preview)')
expect(canonical?.hideFromToolbar).toBeUndefined()
})
it('reveals a config-GA preview block without a suffix', () => {
withVisibility(vis({ revealed: new Set(['gmail_v2']) }))
expect(byType(getAllBlocks(), 'gmail_v2')?.name).toBe('GMAIL_V2')
})
it('kill-switches a shipped block only when a context is active', () => {
withVisibility(vis({ disabled: new Set(['slack']) }))
expect(byType(getAllBlocks(), 'slack')?.hideFromToolbar).toBe(true)
expect(byType(getCanonicalBlocksByCategory('tools'), 'slack')).toBeUndefined()
withVisibility(null)
expect(byType(getAllBlocks(), 'slack')?.hideFromToolbar).toBeUndefined()
})
it('keeps getBlock pure regardless of visibility', () => {
withVisibility(
vis({
revealed: new Set(['gmail_v2']),
previewTagged: new Set(['gmail_v2']),
disabled: new Set(['slack']),
})
)
expect(getBlock('gmail_v2')?.name).toBe('GMAIL_V2')
expect(getBlock('slack')?.hideFromToolbar).toBeUndefined()
})
it('returns untouched references for unaffected blocks', () => {
withVisibility(vis({ revealed: new Set(['gmail_v2']) }))
expect(byType(getAllBlocks(), 'slack')).toBe(synthRegistry.slack)
expect(byType(getAllBlocks(), 'old_v1')).toBe(synthRegistry.old_v1)
})
it('never re-clones or suffixes already-hidden custom blocks', () => {
const disabledCustom = {
...synthRegistry.slack,
type: 'custom_block_abc',
name: 'My Custom',
hideFromToolbar: true,
} as BlockConfig
registerBlockOverlayResolver({
get: (t) => (t === disabledCustom.type ? disabledCustom : undefined),
all: () => [disabledCustom],
})
withVisibility(vis({ revealed: new Set(['gmail_v2']) }))
const projected = byType(getAllBlocks(), 'custom_block_abc')
expect(projected).toBe(disabledCustom)
expect(projected?.name).toBe('My Custom')
})
})