Commit 5a6bf9c1 authored by Jude Gao's avatar Jude Gao

Add e2e tests for agentic editing mode

Tests the full apply→compile→feedback loop with agenticEditing enabled:
valid patches, compile error reporting, error recovery, file creation,
and verifying the watcher is disabled (direct writes don't trigger
recompilation).
parent fcdb017a
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import fs from 'fs'
import path from 'path'
describe('agentic-editing', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
})
if (!isNextDev) {
it('skip in production mode', () => {})
return
}
async function applyPatch(
patch: string,
urls: string[] = ['/']
): Promise<{
success: boolean
affectedFiles: string[]
diffs: Array<{ file: string; type: string; summary: string }>
pages: Array<{
url: string
compileErrors: string[]
runtimeErrors: string[]
screenshot: string
}>
durationMs: number
}> {
const res = await next.fetch('/_next/dev/apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ patch, urls }),
})
return res.json()
}
it('should apply a valid patch and serve updated content', async () => {
const result = await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>hello world</h1>',
'--- replace',
' return <h1>patched via endpoint</h1>',
'---',
].join('\n')
)
expect(result.success).toBe(true)
expect(result.affectedFiles).toEqual(['app/page.tsx'])
expect(result.diffs).toHaveLength(1)
expect(result.diffs[0].type).toBe('edit')
expect(result.pages[0].compileErrors).toHaveLength(0)
expect(result.pages[0].runtimeErrors).toHaveLength(0)
// Verify the page serves updated content
await retry(async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('patched via endpoint')
})
})
it('should report compile errors for broken patches', async () => {
const result = await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>patched via endpoint</h1>',
'--- replace',
' return <h1>broken{</h1>',
'---',
].join('\n')
)
expect(result.success).toBe(false)
expect(result.pages[0].compileErrors.length).toBeGreaterThan(0)
expect(result.pages[0].compileErrors[0]).toContain('Expression expected')
})
it('should recover after fixing a broken patch', async () => {
const result = await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>broken{</h1>',
'--- replace',
' return <h1>recovered</h1>',
'---',
].join('\n')
)
expect(result.success).toBe(true)
expect(result.pages[0].compileErrors).toHaveLength(0)
await retry(async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('recovered')
})
})
it('should fail when search text is not found', async () => {
const result = await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>nonexistent text</h1>',
'--- replace',
' return <h1>replaced</h1>',
'---',
].join('\n')
)
expect(result.success).toBe(false)
expect(result.pages[0].compileErrors[0]).toContain('not found')
})
it('should create new files with write operations', async () => {
const result = await applyPatch(
[
'--- write: app/about/page.tsx',
'export default function About() {',
' return <h1>about page</h1>',
'}',
'---',
].join('\n'),
['/about']
)
expect(result.success).toBe(true)
expect(result.affectedFiles).toEqual(['app/about/page.tsx'])
await retry(async () => {
const $ = await next.render$('/about')
expect($('h1').text()).toBe('about page')
})
})
it('should not recompile on direct file writes (watcher is off)', async () => {
// Get current content via the endpoint
const $ = await next.render$('/')
const currentText = $('h1').text()
// Write directly to disk (bypassing the apply endpoint)
const pagePath = path.join(next.testDir, 'app/page.tsx')
fs.writeFileSync(
pagePath,
`export default function Page() {
return <h1>direct write should not appear</h1>
}
`
)
// Wait a bit for any potential watcher to pick it up
await new Promise((r) => setTimeout(r, 3000))
// Page should still show the old content (watcher is disabled)
const $after = await next.render$('/')
expect($after('h1').text()).toBe(currentText)
// Restore the file via the apply endpoint so subsequent tests work
await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>direct write should not appear</h1>',
'--- replace',
` return <h1>${currentText}</h1>`,
'---',
].join('\n')
)
})
})
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <h1>hello world</h1>
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
agenticEditing: true,
},
}
module.exports = nextConfig
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