Unverified Commit fb3ebba6 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

Add view transitions eval (agent-043) (#92260)

## Summary

- Adds `agent-043-view-transitions`, a new eval that tests whether
agents can add React View Transitions to a Next.js product gallery app
- Starter app is a simple product list/detail gallery with Suspense
boundaries, no view transitions
- EVAL.ts checks 8 patterns: `viewTransition` config flag,
`ViewTransition` imported from `react`, shared element `name` props,
`transitionTypes` on Link, Suspense enter/exit animations,
`default="none"` isolation, `prefers-reduced-motion` CSS, and
`::view-transition-*` CSS pseudo-elements

## Test plan

- [x] Starter app builds with `next build`
- [x] All 8 EVAL.ts tests fail on unmodified starter (no false
positives)
- [x] All 8 EVAL.ts tests pass on a correct golden solution
- [x] Golden solution builds with `next build` (viewTransition
experiment detected)
- [x] Commit passes lint-staged (prettier + eslint)
parent 41839289
/**
* View Transitions
*
* Tests whether the agent correctly adds React View Transitions to a Next.js
* product gallery app covering shared element morphs, directional navigation,
* Suspense reveal animations, and accessibility.
*
* Tricky because agents may:
* - Not know about the experimental.viewTransition flag in next.config
* - Try to call document.startViewTransition manually instead of using
* React's <ViewTransition> component
* - Import ViewTransition from a third-party library instead of 'react'
* - Not know about the transitionTypes prop on next/link
* - Forget default="none" causing every transition to cross-fade everything
* - Skip prefers-reduced-motion (React does NOT disable animations automatically)
*/
import { expect, test } from 'vitest'
import { readFileSync, existsSync, readdirSync } from 'fs'
import { join } from 'path'
const IGNORE_DIRS = new Set([
'node_modules',
'.next',
'.git',
'dist',
'build',
'coverage',
])
function findFiles(dir: string, ext: string): string[] {
const results: string[] = []
const base = join(process.cwd(), dir)
if (!existsSync(base)) return results
function walk(d: string) {
for (const entry of readdirSync(d, { withFileTypes: true })) {
if (IGNORE_DIRS.has(entry.name)) continue
const full = join(d, entry.name)
if (entry.isDirectory()) walk(full)
else if (entry.name.endsWith(ext)) results.push(full)
}
}
walk(base)
return results
}
function stripComments(source: string): string {
return source.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/[^\r\n]*/g, '')
}
const SOURCE_EXTS = ['.tsx', '.ts', '.jsx', '.js']
const SOURCE_DIRS = ['app', 'lib', 'components', 'src']
function readAllSourceFiles(): string {
const files = SOURCE_DIRS.flatMap((dir) =>
SOURCE_EXTS.flatMap((ext) => findFiles(dir, ext))
)
return files
.map((f) => stripComments(readFileSync(f, 'utf-8')))
.join('\n---FILE---\n')
}
function readAllCssFiles(): string {
const files = [
...findFiles('app', '.css'),
...findFiles('src', '.css'),
...findFiles('.', '.css').filter(
(f) => !f.includes('node_modules') && !f.includes('.next')
),
]
const unique = [...new Set(files)]
return unique.map((f) => readFileSync(f, 'utf-8')).join('\n')
}
test('next.config enables viewTransition', () => {
const configPath = existsSync(join(process.cwd(), 'next.config.ts'))
? 'next.config.ts'
: 'next.config.js'
const content = stripComments(
readFileSync(join(process.cwd(), configPath), 'utf-8')
)
expect(content).toMatch(/viewTransition\s*:\s*true/)
})
test('ViewTransition is imported from react', () => {
const allSource = readAllSourceFiles()
expect(allSource).toMatch(
/import\s+\{[^}]*ViewTransition[^}]*\}\s+from\s+['"]react['"]/
)
})
test('Shared element transitions use named ViewTransition', () => {
const allSource = readAllSourceFiles()
const nameMatches = allSource.match(/<ViewTransition[^>]*\bname\s*=/g)
expect(nameMatches).not.toBeNull()
expect(nameMatches!.length).toBeGreaterThanOrEqual(2)
})
test('Link uses transitionTypes for directional navigation', () => {
const allSource = readAllSourceFiles()
expect(allSource).toMatch(/transitionTypes/)
})
test('Suspense content uses ViewTransition with enter or exit', () => {
const allSource = readAllSourceFiles()
expect(allSource).toMatch(/<ViewTransition[^>]*\b(enter|exit)\s*=/)
})
test('default="none" prevents unintended animations', () => {
const allSource = readAllSourceFiles()
expect(allSource).toMatch(/default\s*=\s*["']none["']/)
})
test('CSS handles prefers-reduced-motion', () => {
const allCss = readAllCssFiles()
expect(allCss).toMatch(/prefers-reduced-motion/)
})
test('CSS defines view transition animations', () => {
const allCss = readAllCssFiles()
expect(allCss).toMatch(/::view-transition-(old|new|group)/)
})
Add view transitions to this product gallery app. When navigating from the grid to a product detail page, the product image should smoothly morph between the thumbnail and the large detail image using a shared element transition. Page navigations should slide directionally. Suspense loading states should transition smoothly from skeleton to content. All animations must respect the user's prefers-reduced-motion preference.
import Link from 'next/link'
import { products } from '@/lib/products'
export function ProductGrid() {
return (
<div className="product-grid">
{products.map((product) => (
<Link
key={product.slug}
href={`/product/${product.slug}`}
className="product-card"
>
<div
className="product-image"
style={{ backgroundColor: product.color }}
/>
<h2>{product.name}</h2>
<p>${product.price}</p>
</Link>
))}
</div>
)
}
export function ProductGridSkeleton() {
return (
<div className="product-grid">
{Array.from({ length: 4 }).map((_, i) => (
<div key={i} className="product-card">
<div className="product-image skeleton-image" />
<div className="skeleton-text" />
<div className="skeleton-text short" />
</div>
))}
</div>
)
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family:
system-ui,
-apple-system,
sans-serif;
color: #111;
background: #fafafa;
}
main {
max-width: 1200px;
margin: 0 auto;
padding: 2rem;
}
h1 {
margin-bottom: 1.5rem;
}
.product-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1.5rem;
}
.product-card {
border-radius: 12px;
overflow: hidden;
background: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
text-decoration: none;
color: inherit;
}
.product-card h2 {
padding: 0.75rem 1rem 0;
font-size: 1rem;
}
.product-card p {
padding: 0.25rem 1rem 1rem;
color: #666;
}
.product-image {
width: 100%;
aspect-ratio: 4 / 3;
}
.product-detail {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 2rem;
align-items: start;
}
.product-hero {
width: 100%;
aspect-ratio: 1;
border-radius: 12px;
}
.product-info h1 {
font-size: 2rem;
}
.product-price {
font-size: 1.5rem;
font-weight: 600;
margin: 0.5rem 0;
}
.product-description {
color: #666;
line-height: 1.6;
}
.back-link {
display: inline-block;
margin-bottom: 1.5rem;
color: #666;
text-decoration: none;
}
.skeleton-image {
background: #e5e5e5;
animation: pulse 1.5s ease-in-out infinite;
}
.skeleton-text {
height: 1rem;
background: #e5e5e5;
border-radius: 4px;
margin: 0.5rem 1rem;
animation: pulse 1.5s ease-in-out infinite;
}
.skeleton-text.short {
width: 60%;
}
.skeleton-text.large {
height: 2rem;
width: 70%;
}
@keyframes pulse {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
import type { Metadata } from 'next'
import './globals.css'
export const metadata: Metadata = {
title: 'Product Gallery',
description: 'A simple product gallery',
}
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { ProductGrid } from './ProductGrid'
import { ProductGridSkeleton } from './ProductSkeleton'
export default function Home() {
return (
<main>
<h1>Products</h1>
<Suspense fallback={<ProductGridSkeleton />}>
<ProductGrid />
</Suspense>
</main>
)
}
import { Suspense } from 'react'
import Link from 'next/link'
import { getProduct, products } from '@/lib/products'
import { notFound } from 'next/navigation'
async function ProductDetails({ slug }: { slug: string }) {
await new Promise((resolve) => setTimeout(resolve, 100))
const product = getProduct(slug)
if (!product) notFound()
return (
<div className="product-info">
<h1>{product.name}</h1>
<p className="product-price">${product.price}</p>
<p className="product-description">{product.description}</p>
</div>
)
}
function DetailsSkeleton() {
return (
<div className="product-info">
<div className="skeleton-text large" />
<div className="skeleton-text short" />
<div className="skeleton-text" />
</div>
)
}
export default async function ProductPage({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
const product = products.find((p) => p.slug === slug)
if (!product) notFound()
return (
<div>
<Link href="/" className="back-link">
← Back to products
</Link>
<div className="product-detail">
<div
className="product-hero"
style={{ backgroundColor: product.color }}
/>
<Suspense fallback={<DetailsSkeleton />}>
<ProductDetails slug={slug} />
</Suspense>
</div>
</div>
)
}
export type Product = {
slug: string
name: string
description: string
price: number
color: string
}
export const products: Product[] = [
{
slug: 'classic-sneakers',
name: 'Classic Sneakers',
description: 'Timeless design meets everyday comfort.',
price: 89,
color: '#4F46E5',
},
{
slug: 'leather-backpack',
name: 'Leather Backpack',
description: 'Handcrafted from premium full-grain leather.',
price: 129,
color: '#059669',
},
{
slug: 'wireless-headphones',
name: 'Wireless Headphones',
description: 'Crystal-clear audio with active noise cancellation.',
price: 199,
color: '#DC2626',
},
{
slug: 'cotton-hoodie',
name: 'Cotton Hoodie',
description: 'Ultra-soft organic cotton blend for all-day wear.',
price: 65,
color: '#D97706',
},
]
export function getProduct(slug: string): Product | undefined {
return products.find((p) => p.slug === slug)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
{
"private": true,
"type": "module",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^16",
"react": "19.1.0",
"react-dom": "19.1.0"
},
"devDependencies": {
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"typescript": "^5",
"vitest": "^3.1.3",
"@vitejs/plugin-react": "^4.4.1",
"vite-tsconfig-paths": "^5.1.4"
}
}
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment