An open source design system that's fully customizable and agent ready
TypeScript
fix(vibe-tests): extract TSX from JSON result files for previews (#1368)
Cindy Zhang79c017f825b0
diff --git a/internal/vibe-tests/package.json b/internal/vibe-tests/package.jsonindex b68fbde646..d943830a17 100644--- a/internal/vibe-tests/package.json+++ b/internal/vibe-tests/package.json@@ -41,4 +41,4 @@"tailwindcss": "^4.2.1","tsx": "^4.19.0"}-}\ No newline at end of file+}diff --git a/internal/vibe-tests/src/build-previews.ts b/internal/vibe-tests/src/build-previews.tsindex ea48c11d60..32b647a2b7 100644--- a/internal/vibe-tests/src/build-previews.ts+++ b/internal/vibe-tests/src/build-previews.ts@@ -14,7 +14,13 @@ import * as fs from 'node:fs';import * as path from 'node:path';import * as crypto from 'node:crypto';import {execSync} from 'node:child_process';-import {getResultsDir, ensureDir, readJson, writeJson} from './utils.js';+import {+ getResultsDir,+ ensureDir,+ readJson,+ writeJson,+ ensureTsxFiles,+} from './utils.js';const __dirname = path.dirname(new URL(import.meta.url).pathname);const VIBE_DIR = path.resolve(__dirname, '..');@@ -36,7 +42,6 @@ const LIGHTNINGCSS_TARGETS_JS = `{safari: (17 << 16) | (5 << 8),}`;-/*** Fix missing XDS component imports in AI-generated .tsx files.* Scans for XDS* identifiers used in JSX that aren't imported,@@ -56,7 +61,10 @@ function fixMissingXDSImports(filePath: string): string[] {/import\s*\{([^}]+)\}\s*from\s*['"]@xds\/core[^'"]*['"]/g;while ((match = importPattern.exec(code)) !== null) {for (const specifier of match[1].split(',')) {- const name = specifier.trim().split(/\s+as\s+/)[0].trim();+ const name = specifier+ .trim()+ .split(/\s+as\s+/)[0]+ .trim();if (name.startsWith('XDS')) importedComponents.add(name);}}@@ -367,10 +375,10 @@ import {viteSingleFile} from 'vite-plugin-singlefile';`;.resolve(REPO_ROOT, 'packages/core/src').replace(/\\/g, '/')}','@xds/theme/default': '${path- .resolve(REPO_ROOT, 'packages/themes/default/src')+ .resolve(REPO_ROOT, 'packages/themes/default/src/source.ts').replace(/\\/g, '/')}','@xds/theme/neutral': '${path- .resolve(REPO_ROOT, 'packages/themes/neutral/src')+ .resolve(REPO_ROOT, 'packages/themes/neutral/src/source.ts').replace(/\\/g, '/')}',},`;@@ -378,7 +386,7 @@ import {viteSingleFile} from 'vite-plugin-singlefile';`;target === 'xds' || target === 'xds-tailwind'? xdsAliases: target === 'baseline'- ? `+ ? `alias: {'@/components/ui': '${path.resolve(VIBE_DIR, '.baseline-shims/components/ui')@@ -387,7 +395,7 @@ import {viteSingleFile} from 'vite-plugin-singlefile';`;.resolve(VIBE_DIR, '.baseline-shims/lib/utils').replace(/\\/g, '/')}',},`- : '';+ : '';// XDS+Tailwind needs PostCSS with Tailwind for utility class resolutionconst cssConfig =@@ -675,6 +683,9 @@ async function main() {if (!fs.existsSync(codeDir)) continue;+ // Extract .tsx from JSON results if no .tsx files exist yet+ ensureTsxFiles(codeDir);+const files = fs.readdirSync(codeDir).filter(f => f.endsWith('.tsx'));for (const file of files) {diff --git a/internal/vibe-tests/src/build-report.ts b/internal/vibe-tests/src/build-report.tsindex c8cb26fc81..3839eaa815 100644--- a/internal/vibe-tests/src/build-report.ts+++ b/internal/vibe-tests/src/build-report.ts@@ -16,7 +16,7 @@import * as fs from 'node:fs';import * as path from 'node:path';import {execSync} from 'node:child_process';-import {getResultsDir, readJson, ensureDir} from './utils.js';+import {getResultsDir, readJson, ensureDir, ensureTsxFiles} from './utils.js';import type {UniversalAggregate, UniversalComparison} from './types.js';const APP_DIR = path.join(import.meta.dirname, '..', 'app');@@ -275,6 +275,7 @@ async function main() {const sourceCode: Record<string, string> = {};const codeDir = path.join(iterDir, 'results');if (fs.existsSync(codeDir)) {+ ensureTsxFiles(codeDir);for (const file of fs.readdirSync(codeDir).filter(f => f.endsWith('.tsx'))) {@@ -288,6 +289,7 @@ async function main() {baselineSourceCode = {};const baseCodeDir = path.join(resultsDir, baseline, 'results');if (fs.existsSync(baseCodeDir)) {+ ensureTsxFiles(baseCodeDir);for (const file of fs.readdirSync(baseCodeDir).filter(f => f.endsWith('.tsx'))) {@@ -305,6 +307,7 @@ async function main() {htmlSourceCode = {};const htmlCodeDir = path.join(resultsDir, html, 'results');if (fs.existsSync(htmlCodeDir)) {+ ensureTsxFiles(htmlCodeDir);for (const file of fs.readdirSync(htmlCodeDir).filter(f => f.endsWith('.tsx'))) {diff --git a/internal/vibe-tests/src/deploy-report.ts b/internal/vibe-tests/src/deploy-report.tsindex 3203ff3450..5f6a120b6d 100644--- a/internal/vibe-tests/src/deploy-report.ts+++ b/internal/vibe-tests/src/deploy-report.ts@@ -79,14 +79,16 @@ async function main() {if (dryRun) console.log(` ⚠️ DRY RUN — will build but not push`);console.log('');- // Step 1: Build previews if .tsx result files exist+ // Step 1: Build previews if result files exist (.tsx or .json)if (!skipPreviews) {const iterationsWithCode: string[] = [];for (const id of [iteration, baseline, html].filter(Boolean) as string[]) {const codeDir = path.join(VIBE_DIR, 'results', id, 'results');if (fs.existsSync(codeDir) &&- fs.readdirSync(codeDir).some(f => f.endsWith('.tsx'))+ fs+ .readdirSync(codeDir)+ .some(f => f.endsWith('.tsx') || f.endsWith('.json'))) {iterationsWithCode.push(id);}diff --git a/internal/vibe-tests/src/ensure-tsx.test.ts b/internal/vibe-tests/src/ensure-tsx.test.tsnew file mode 100644index 0000000000..0389b77ce2--- /dev/null+++ b/internal/vibe-tests/src/ensure-tsx.test.ts@@ -0,0 +1,102 @@+import {describe, it, expect, afterAll} from 'vitest';+import * as fs from 'node:fs';+import * as path from 'node:path';+import * as os from 'node:os';+import {ensureTsxFiles} from './utils.js';++const dirs: string[] = [];+function tmpDir(): string {+ const d = fs.mkdtempSync(path.join(os.tmpdir(), 'vibe-'));+ dirs.push(d);+ return d;+}+afterAll(() => dirs.forEach(d => fs.rmSync(d, {recursive: true})));++describe('ensureTsxFiles', () => {+ it('extracts depth-0 TSX from JSON result arrays', () => {+ const dir = tmpDir();+ fs.writeFileSync(+ path.join(dir, 'cwm-1.json'),+ JSON.stringify([+ {response: 'import {XDSButton} from "@xds/core";', trajectoryDepth: 0},+ {response: 'import {XDSCard} from "@xds/core";', trajectoryDepth: 1},+ ]),+ );+ fs.writeFileSync(+ path.join(dir, 'dd-2.json'),+ JSON.stringify([+ {response: 'export default function Dash() {}', trajectoryDepth: 0},+ ]),+ );++ const ids = ensureTsxFiles(dir);+ expect(ids).toHaveLength(2);+ expect(fs.readFileSync(path.join(dir, 'cwm-1.tsx'), 'utf-8')).toContain(+ 'XDSButton',+ );+ expect(fs.readFileSync(path.join(dir, 'cwm-1.tsx'), 'utf-8')).not.toContain(+ 'XDSCard',+ );+ expect(fs.existsSync(path.join(dir, 'dd-2.tsx'))).toBe(true);+ });++ it('preserves existing .tsx files without overwriting', () => {+ const dir = tmpDir();+ fs.writeFileSync(path.join(dir, 'p1.tsx'), 'original');+ fs.writeFileSync(+ path.join(dir, 'p1.json'),+ JSON.stringify([{response: 'OVERWRITE', trajectoryDepth: 0}]),+ );++ const ids = ensureTsxFiles(dir);+ expect(ids).toEqual(['p1']);+ expect(fs.readFileSync(path.join(dir, 'p1.tsx'), 'utf-8')).toBe('original');+ });++ it('skips malformed JSON gracefully', () => {+ const dir = tmpDir();+ fs.writeFileSync(path.join(dir, 'bad.json'), '{{{not json');+ fs.writeFileSync(+ path.join(dir, 'ok.json'),+ JSON.stringify([{response: 'valid', trajectoryDepth: 0}]),+ );++ const ids = ensureTsxFiles(dir);+ expect(ids).toHaveLength(1);+ expect(fs.existsSync(path.join(dir, 'bad.tsx'))).toBe(false);+ });++ it('skips JSON without a response field', () => {+ const dir = tmpDir();+ fs.writeFileSync(+ path.join(dir, 'meta.json'),+ JSON.stringify({some: 'data'}),+ );+ fs.writeFileSync(+ path.join(dir, 'real.json'),+ JSON.stringify([{response: 'code', trajectoryDepth: 0}]),+ );++ const ids = ensureTsxFiles(dir);+ expect(ids).toEqual(['real']);+ expect(fs.existsSync(path.join(dir, 'meta.tsx'))).toBe(false);+ });++ it('returns empty array for empty directory', () => {+ expect(ensureTsxFiles(tmpDir())).toEqual([]);+ });++ it('handles single-object JSON (non-array)', () => {+ const dir = tmpDir();+ fs.writeFileSync(+ path.join(dir, 'single.json'),+ JSON.stringify({response: 'single entry', trajectoryDepth: 0}),+ );++ const ids = ensureTsxFiles(dir);+ expect(ids).toHaveLength(1);+ expect(fs.readFileSync(path.join(dir, 'single.tsx'), 'utf-8')).toBe(+ 'single entry',+ );+ });+});diff --git a/internal/vibe-tests/src/universal-aggregate.ts b/internal/vibe-tests/src/universal-aggregate.tsindex 2d06b211c2..e26c72db74 100644--- a/internal/vibe-tests/src/universal-aggregate.ts+++ b/internal/vibe-tests/src/universal-aggregate.ts@@ -16,7 +16,7 @@ import type {UniversalScore,UniversalAggregate,} from './types.js';-import {getResultsDir, writeJson} from './utils.js';+import {getResultsDir, writeJson, ensureTsxFiles} from './utils.js';import {evaluate, getDimensionNames} from './universal-eval.js';const DIMENSION_LABELS: Partial<Record<UniversalDimension, string>> = {@@ -78,7 +78,8 @@ async function main() {]),);- // Load .tsx files+ // Load .tsx files (extract from JSON results if needed)+ ensureTsxFiles(codeDir);const files = fs.readdirSync(codeDir).filter(f => f.endsWith('.tsx'));if (files.length === 0) {diff --git a/internal/vibe-tests/src/utils.ts b/internal/vibe-tests/src/utils.tsindex 2e3ab4dfdc..83d4dd530b 100644--- a/internal/vibe-tests/src/utils.ts+++ b/internal/vibe-tests/src/utils.ts@@ -189,3 +189,65 @@ export function progressBar(export function timestamp(): string {return new Date().toISOString();}++/**+ * Result file entry from the JSON evaluation format.+ * Each JSON file is an array of these (one per trajectory depth).+ */+interface JsonResultEntry {+ response: string;+ trajectoryDepth: number;+ [key: string]: unknown;+}++/**+ * Ensure .tsx files exist in a results directory.+ *+ * Handles two result formats:+ * 1. Direct .tsx files (old format) — already present, nothing to do+ * 2. .json evaluation files (new format) — each is an array of entries+ * with a `response` field containing TSX code; extracts depth-0 entry+ *+ * Returns the list of prompt IDs that have .tsx files after extraction.+ */+export function ensureTsxFiles(codeDir: string): string[] {+ const tsxFiles = fs.readdirSync(codeDir).filter(f => f.endsWith('.tsx'));+ if (tsxFiles.length > 0) {+ return tsxFiles.map(f => path.basename(f, '.tsx'));+ }++ // No .tsx files — try extracting from JSON results+ const jsonFiles = fs.readdirSync(codeDir).filter(f => f.endsWith('.json'));+ const extracted: string[] = [];++ for (const jsonFile of jsonFiles) {+ const promptId = path.basename(jsonFile, '.json');+ const jsonPath = path.join(codeDir, jsonFile);++ try {+ const data = JSON.parse(+ fs.readFileSync(jsonPath, 'utf-8'),+ ) as JsonResultEntry | JsonResultEntry[];++ // Find the depth-0 entry (initial generation, before follow-ups)+ const entries = Array.isArray(data) ? data : [data];+ const entry = entries.find(e => e.trajectoryDepth === 0) ?? entries[0];++ if (entry?.response && typeof entry.response === 'string') {+ const tsxPath = path.join(codeDir, `${promptId}.tsx`);+ fs.writeFileSync(tsxPath, entry.response);+ extracted.push(promptId);+ }+ } catch {+ // Skip malformed JSON files+ }+ }++ if (extracted.length > 0) {+ console.log(+ ` ⚡ Extracted ${extracted.length} .tsx file(s) from JSON results`,+ );+ }++ return extracted;+}