Commit a1b1a3bf authored by Jude Gao's avatar Jude Gao

Expand test coverage for agentic editing

Unit tests: add atomicity guarantee (second op fails → first file
untouched), trailing whitespace tolerance, multi-line search/replace,
multiple edits to same file, write overwrite, diffs summary.

E2E tests: add multi-file patch, multiple URLs, empty/malformed/
unparseable body → 400, non-POST passthrough, runtime error detection,
durationMs assertion.
parent 5a6bf9c1
......@@ -54,8 +54,8 @@ describe('agentic-editing', () => {
expect(result.diffs[0].type).toBe('edit')
expect(result.pages[0].compileErrors).toHaveLength(0)
expect(result.pages[0].runtimeErrors).toHaveLength(0)
expect(result.durationMs).toBeGreaterThan(0)
// Verify the page serves updated content
await retry(async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('patched via endpoint')
......@@ -137,6 +137,108 @@ describe('agentic-editing', () => {
})
})
it('should apply multi-file patches in a single call', async () => {
const result = await applyPatch(
[
'--- edit: app/page.tsx',
'--- search',
' return <h1>recovered</h1>',
'--- replace',
' return <h1>multi-file main</h1>',
'---',
'--- write: app/contact/page.tsx',
'export default function Contact() {',
' return <h1>contact page</h1>',
'}',
'---',
].join('\n'),
['/', '/contact']
)
expect(result.success).toBe(true)
expect(result.affectedFiles).toEqual([
'app/page.tsx',
'app/contact/page.tsx',
])
expect(result.diffs).toHaveLength(2)
expect(result.pages).toHaveLength(2)
expect(result.pages[0].url).toBe('/')
expect(result.pages[1].url).toBe('/contact')
await retry(async () => {
const $ = await next.render$('/')
expect($('h1').text()).toBe('multi-file main')
})
await retry(async () => {
const $ = await next.render$('/contact')
expect($('h1').text()).toBe('contact page')
})
})
it('should return 400 for empty patch body', async () => {
const res = await next.fetch('/_next/dev/apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ patch: '', urls: ['/'] }),
})
expect(res.status).toBe(400)
const body = await res.json()
expect(body.success).toBe(false)
})
it('should return 400 for unparseable patch format', async () => {
const res = await next.fetch('/_next/dev/apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
patch: 'just some random text with no directives',
urls: ['/'],
}),
})
expect(res.status).toBe(400)
const body = await res.json()
expect(body.success).toBe(false)
})
it('should return 400 for malformed JSON body', async () => {
const res = await next.fetch('/_next/dev/apply', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{not valid json',
})
expect(res.status).toBe(400)
})
it('should pass through non-POST requests', async () => {
const res = await next.fetch('/_next/dev/apply', { method: 'GET' })
// Should not be handled by the middleware (passes to next handler)
expect(res.status).not.toBe(200)
})
it('should detect runtime errors from a throwing component', async () => {
// Write a component that throws at render time
const result = await applyPatch(
[
'--- write: app/throwing/page.tsx',
'"use client"',
'export default function Throwing() {',
' throw new Error("intentional runtime error")',
' return <h1>never rendered</h1>',
'}',
'---',
].join('\n'),
['/throwing']
)
// The page should compile successfully but have runtime errors
expect(result.pages[0].url).toBe('/throwing')
// Runtime errors may or may not be captured depending on next-browser
// availability, but the response should still be returned
expect(result.durationMs).toBeGreaterThan(0)
expect(result.affectedFiles).toEqual(['app/throwing/page.tsx'])
})
it('should not recompile on direct file writes (watcher is off)', async () => {
// Get current content via the endpoint
const $ = await next.render$('/')
......
......@@ -54,6 +54,50 @@ export default function Layout({ children }) {
expect(parsePatch('')).toHaveLength(0)
expect(parsePatch(' \n\n ')).toHaveLength(0)
})
it('returns empty array for garbage input with no directives', () => {
expect(
parsePatch('just some random text\nno directives here')
).toHaveLength(0)
})
it('parses multi-line search/replace blocks', () => {
const patch = `--- edit: app/page.tsx
--- search
function foo() {
return 1
}
--- replace
function foo() {
return 2
}
---`
const ops = parsePatch(patch)
expect(ops).toHaveLength(1)
expect(ops[0].search).toContain('function foo() {\n return 1\n}')
expect(ops[0].replace).toContain('function foo() {\n return 2\n}')
})
it('parses multiple edits to the same file', () => {
const patch = `--- edit: app/page.tsx
--- search
foo
--- replace
bar
---
--- edit: app/page.tsx
--- search
baz
--- replace
qux
---`
const ops = parsePatch(patch)
expect(ops).toHaveLength(2)
expect(ops[0].filePath).toBe('app/page.tsx')
expect(ops[1].filePath).toBe('app/page.tsx')
expect(ops[0].search).toContain('foo')
expect(ops[1].search).toContain('baz')
})
})
describe('applyPatches', () => {
......@@ -90,6 +134,18 @@ nested
)
})
it('write overwrites an existing file', () => {
const filePath = path.join(tmpDir, 'existing.txt')
fs.writeFileSync(filePath, 'old content')
const ops = parsePatch(`--- write: existing.txt
new content
---`)
const result = applyPatches(ops, tmpDir)
expect(result.success).toBe(true)
expect(fs.readFileSync(filePath, 'utf-8')).toBe('new content')
})
it('applies a search/replace edit', () => {
const filePath = path.join(tmpDir, 'page.tsx')
fs.writeFileSync(filePath, '<h1>Hello World</h1>\n<p>Content</p>\n')
......@@ -107,6 +163,47 @@ nested
expect(content).toContain('<p>Content</p>')
})
it('tolerates trailing whitespace differences in search', () => {
const filePath = path.join(tmpDir, 'page.tsx')
// File has trailing spaces on the line
fs.writeFileSync(filePath, '<h1>Hello</h1> \n<p>World</p>\n')
// Search text has no trailing spaces
const ops = parsePatch(`--- edit: page.tsx
--- search
<h1>Hello</h1>
--- replace
<h1>Changed</h1>
---`)
const result = applyPatches(ops, tmpDir)
expect(result.success).toBe(true)
expect(fs.readFileSync(filePath, 'utf-8')).toContain('<h1>Changed</h1>')
})
it('applies multi-line search/replace', () => {
const filePath = path.join(tmpDir, 'page.tsx')
fs.writeFileSync(
filePath,
'function foo() {\n return 1\n}\n\nfunction bar() {}\n'
)
const ops = parsePatch(`--- edit: page.tsx
--- search
function foo() {
return 1
}
--- replace
function foo() {
return 42
}
---`)
const result = applyPatches(ops, tmpDir)
expect(result.success).toBe(true)
const content = fs.readFileSync(filePath, 'utf-8')
expect(content).toContain('return 42')
expect(content).toContain('function bar() {}')
})
it('fails when search text is not found', () => {
const filePath = path.join(tmpDir, 'page.tsx')
fs.writeFileSync(filePath, '<h1>Hello World</h1>\n')
......@@ -157,4 +254,49 @@ new-b
expect(fs.readFileSync(path.join(tmpDir, 'a.txt'), 'utf-8')).toBe('new-a')
expect(fs.readFileSync(path.join(tmpDir, 'b.txt'), 'utf-8')).toBe('new-b')
})
it('does not write first file when second operation fails (atomicity)', () => {
fs.writeFileSync(path.join(tmpDir, 'a.txt'), 'original-a')
fs.writeFileSync(path.join(tmpDir, 'b.txt'), 'original-b')
const ops = parsePatch(`--- edit: a.txt
--- search
original-a
--- replace
modified-a
---
--- edit: b.txt
--- search
text-that-does-not-exist
--- replace
modified-b
---`)
const result = applyPatches(ops, tmpDir)
expect(result.success).toBe(false)
// a.txt should NOT have been modified because b.txt failed
expect(fs.readFileSync(path.join(tmpDir, 'a.txt'), 'utf-8')).toBe(
'original-a'
)
expect(fs.readFileSync(path.join(tmpDir, 'b.txt'), 'utf-8')).toBe(
'original-b'
)
})
it('returns correct diffs summary', () => {
fs.writeFileSync(path.join(tmpDir, 'page.tsx'), 'line1\nline2\nline3\n')
const ops = parsePatch(`--- edit: page.tsx
--- search
line2
--- replace
replaced-line2a
replaced-line2b
---`)
const result = applyPatches(ops, tmpDir)
expect(result.success).toBe(true)
expect(result.diffs).toHaveLength(1)
expect(result.diffs[0].file).toBe('page.tsx')
expect(result.diffs[0].type).toBe('edit')
expect(result.diffs[0].summary).toBe('-1 lines, +2 lines')
})
})
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