feat: complete dynamic JSON-driven photo tools frontend connected to FastAPI backend

This commit is contained in:
2026-11-03 12:24:43 +05:30
commit fa2eca3643
91 changed files with 11372 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
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: 'Ekagni PhotoAI' },
},
};
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);
});
});
+127
View File
@@ -0,0 +1,127 @@
import { describe, it, expect } from 'vitest';
import { extractGroups, normaliseCatalog } from '../src/lib/catalog/loader';
describe('Catalogue Tolerant Parser & Normaliser (PRD §7.4, CAT-01..09)', () => {
it('accepts an array of groups', () => {
const raw = [
{
title: 'Group 1',
endpoints: [
{
id: 'ep-1',
full_url: 'http://127.0.0.1:6666/ep1/image/',
path: '/ep1/image/',
params: [{ n: 'image', ty: 'file (binary)', rq: true }],
},
],
},
];
const groups = extractGroups(raw);
expect(groups).toHaveLength(1);
expect(groups[0].title).toBe('Group 1');
});
it('accepts an object with list under apis or groups key', () => {
const raw = {
apis: [
{
title: 'Group API',
endpoints: [
{
id: 'ep-2',
full_url: 'http://127.0.0.1:6666/ep2/image/',
},
],
},
],
};
const groups = extractGroups(raw);
expect(groups).toHaveLength(1);
expect(groups[0].title).toBe('Group API');
});
it('accepts a bare list of endpoints', () => {
const raw = [
{
id: 'ep-bare',
full_url: 'http://127.0.0.1:6666/ep_bare/',
},
];
const groups = extractGroups(raw);
expect(groups).toHaveLength(1);
expect(groups[0].endpoints).toHaveLength(1);
});
it('normalises endpoint derived values correctly', () => {
const raw = [
{
title: 'Blemish_Remover',
endpoints: [
{
id: 'api-blemish-test',
full_url: 'http://127.0.0.1:6666/blemish_remover/image/',
path: '/blemish_remover/image/',
method: 'MULTI',
resp: 'image/jpeg',
params: [
{ n: 'image', ty: 'file (binary)', rq: true, ds: 'User photo' },
{ n: 'secretKey', ty: 'string', rq: true, ds: 'Secret key 12345' },
],
subs: [
{ d: 'Primary acne fix', m: 'POST', p: '/blemish_remover/image/' },
{ d: 'Eye fix', m: 'POST', p: '/blemish_remover/eye_fix/' },
],
},
],
},
];
const overlay = {
defaults: { timeoutMs: 60000 },
endpoints: {
'api-blemish-test': {
enabled: true,
publicTry: true,
paramSources: { image: 'user.file', secretKey: 'secret' },
},
},
};
const { endpoints } = normaliseCatalog(raw, overlay);
expect(endpoints).toHaveLength(1);
const ep = endpoints[0];
expect(ep.id).toBe('api-blemish-test');
expect(ep.baseUrl).toBe('http://127.0.0.1:6666');
expect(ep.variants).toHaveLength(2);
expect(ep.variants[0].id).toBe('image');
expect(ep.variants[1].id).toBe('eye_fix');
expect(ep.variants[1].url).toBe('http://127.0.0.1:6666/blemish_remover/eye_fix/');
// Param tests
const fileParam = ep.params.find((p) => p.name === 'image');
expect(fileParam?.kind).toBe('file');
expect(fileParam?.source).toBe('user.file');
const secretParam = ep.params.find((p) => p.name === 'secretKey');
expect(secretParam?.isSecret).toBe(true);
expect(secretParam?.source).toBe('secret');
expect(secretParam?.description).toBeUndefined(); // Redacted per PRD §7.11
});
it('skips corrupt entries without crashing', () => {
const raw = [
{
title: 'Corrupt Group',
endpoints: [
{ title: 'No id or url' },
{ id: 'good-ep', full_url: 'http://127.0.0.1:6666/test/' },
],
},
];
const { endpoints } = normaliseCatalog(raw, {});
expect(endpoints).toHaveLength(1);
expect(endpoints[0].id).toBe('good-ep');
});
});
+23
View File
@@ -0,0 +1,23 @@
import { describe, it, expect } from 'vitest';
import { checkIntegrity, getAllTools, getAllApps } from '../src/lib/config/loader';
import { getEndpoints } from '../src/lib/catalog/loader';
describe('Config & Catalogue Reference Integrity (PRD §5.3 CFG-07, APP-01)', () => {
it('has published tools and apps', () => {
const tools = getAllTools();
const apps = getAllApps();
expect(tools.length).toBeGreaterThan(0);
expect(apps.length).toBeGreaterThan(0);
});
it('validates that every tool has a valid homeApp and endpointRef', () => {
const { valid, errors } = checkIntegrity();
expect(errors).toEqual([]);
expect(valid).toBe(true);
});
it('has active endpoints normalised from catalogue', () => {
const endpoints = getEndpoints();
expect(endpoints.length).toBeGreaterThan(0);
});
});
+37
View File
@@ -0,0 +1,37 @@
import { describe, it, expect } from 'vitest';
import { checkRateLimits, recordUsage } from '../src/lib/api/rateLimiter';
describe('Rate Limiter & Budget Circuit Breaker (PRD §7.11 API-08)', () => {
it('enforces visitor daily limit', () => {
const visitorId = 'test_visitor_' + Math.random();
const ip = '127.0.0.1';
const limits = { perVisitorPerDay: 2, perIpPerHour: 10, dailyBudgetCalls: 100 };
// Try 1
const check1 = checkRateLimits(visitorId, ip, limits);
expect(check1.allowed).toBe(true);
recordUsage(visitorId, ip);
// Try 2
const check2 = checkRateLimits(visitorId, ip, limits);
expect(check2.allowed).toBe(true);
recordUsage(visitorId, ip);
// Try 3 -> Should be blocked
const check3 = checkRateLimits(visitorId, ip, limits);
expect(check3.allowed).toBe(false);
expect(check3.reason).toBe('visitor_daily');
});
it('trips daily budget breaker when budget exhausted', () => {
const visitorId = 'budget_test_visitor';
const ip = '127.0.0.2';
const limits = { perVisitorPerDay: 100, perIpPerHour: 100, dailyBudgetCalls: 1 };
recordUsage(visitorId, ip);
const check = checkRateLimits('new_visitor', ip, limits);
expect(check.allowed).toBe(false);
expect(check.reason).toBe('budget_exhausted');
});
});