67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { interpolateString, resolveBindings, isSectionVisible } from '../src/lib/config/bindings';
|
|
|
|
describe('Binding & Visibility Engine (PRD §5.2, CFG-08, CFG-09)', () => {
|
|
const context = {
|
|
tool: {
|
|
name: { en: 'Blemish Remover', hi: 'दाग हटाने वाला' },
|
|
slug: 'blemish-remover',
|
|
homeApp: {
|
|
name: { en: 'Portrait Studio AI' },
|
|
},
|
|
},
|
|
site: {
|
|
brand: { name: 'CRE Innovations' },
|
|
},
|
|
};
|
|
|
|
it('interpolates nested paths with locale resolution', () => {
|
|
const template = 'Open {{tool.name}} in {{tool.homeApp.name}}';
|
|
const result = interpolateString(template, context, 'en');
|
|
expect(result).toBe('Open Blemish Remover in Portrait Studio AI');
|
|
});
|
|
|
|
it('supports defaults and does not output raw placeholders', () => {
|
|
const template = 'Hello {{missing.key|Default Value}} and {{another.missing}}';
|
|
const result = interpolateString(template, context, 'en');
|
|
expect(result).toBe('Hello Default Value and ');
|
|
});
|
|
|
|
it('recursively resolves bindings on complex objects', () => {
|
|
const sectionProps = {
|
|
title: 'Try {{tool.name}} now',
|
|
action: {
|
|
href: '/go/{{tool.slug}}',
|
|
label: 'Start {{tool.name}}',
|
|
},
|
|
};
|
|
|
|
const resolved = resolveBindings(sectionProps, context, 'en');
|
|
expect(resolved.title).toBe('Try Blemish Remover now');
|
|
expect(resolved.action.href).toBe('/go/blemish-remover');
|
|
expect(resolved.action.label).toBe('Start Blemish Remover');
|
|
});
|
|
|
|
it('evaluates visibility rules for device and flags', () => {
|
|
const mobileOnlySection: any = {
|
|
id: 'quiz',
|
|
type: 'appPicker',
|
|
props: {},
|
|
visibleWhen: { device: 'mobile' },
|
|
};
|
|
|
|
expect(isSectionVisible(mobileOnlySection, { device: 'mobile' })).toBe(true);
|
|
expect(isSectionVisible(mobileOnlySection, { device: 'desktop' })).toBe(false);
|
|
|
|
const flaggedSection: any = {
|
|
id: 'feature',
|
|
type: 'featureList',
|
|
props: {},
|
|
visibleWhen: { flag: 'newFeature' },
|
|
};
|
|
|
|
expect(isSectionVisible(flaggedSection, { flags: { newFeature: false } })).toBe(false);
|
|
expect(isSectionVisible(flaggedSection, { flags: { newFeature: true } })).toBe(true);
|
|
});
|
|
});
|