Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions src/core/diff/strategies/__tests__/multi-search-replace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,121 @@ function five() {
expect(result.success).toBe(false)
expect(result.error).toContain("'=======' found in your diff content")
})

describe("command line processing", () => {
let strategy: MultiSearchReplaceDiffStrategy
beforeEach(() => {
strategy = new MultiSearchReplaceDiffStrategy()
})

it("should process diff from command line arguments", async () => {
// This test is designed to be run from the command line with file arguments
// Example: npx jest src/core/diff/strategies/__tests__/multi-search-replace.test.ts -t "should process diff" -- file.ts diff.diff

// Get command line arguments
const args = process.argv.slice(2)

// Skip test if not run with arguments
// Parse command line arguments for --source and --diff flags
let sourceFile: string | undefined
let diffFile: string | undefined

for (let i = 0; i < args.length; i++) {
if (args[i] === "--source" && i + 1 < args.length) {
sourceFile = args[i + 1]
i++ // Skip the next argument as it's the value
} else if (args[i] === "--diff" && i + 1 < args.length) {
diffFile = args[i + 1]
i++ // Skip the next argument as it's the value
}
}

if (!sourceFile || !diffFile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid silently returning when required file arguments are missing. Instead, consider using test.skip or explicitly failing so that missing arguments don't yield a false positive.

console.debug(
`Optional debug usage: npx jest multi-search-replace.test.ts -- --source <file.ts> --diff <diff.diff>\n`,
)
// console.debug('All args:', args);
return
}

try {
// Read files
const fs = require("fs")
const sourceContent = fs.readFileSync(sourceFile, "utf8")
let diffContent = fs.readFileSync(diffFile, "utf8")

// Show first 50 lines of source content
process.stdout.write(
`\n\n====================================================================\n`,
)
process.stdout.write(`== ${sourceFile} first 50 lines ==\n`)
process.stdout.write(
`====================================================================\n`,
)
sourceContent
.split("\n")
.slice(0, 50)
.forEach((line: string) => {
process.stdout.write(`${line}\n`)
})
process.stdout.write(
`=============================== END ================================\n`,
)

process.stdout.write(
`\n\n====================================================================\n`,
)
process.stdout.write(`== ${diffFile} first 50 lines ==\n`)
process.stdout.write(
`====================================================================\n`,
)

// Show first 50 lines of diff content
diffContent
.split("\n")
.slice(0, 50)
.forEach((line: string) => {
process.stdout.write(`${line}\n`)
})
process.stdout.write(
`=============================== END ================================\n`,
)

// Apply the diff
const result = await strategy.applyDiff(sourceContent, diffContent)

if (result.success) {
process.stdout.write(
`\n\n====================================================================\n`,
)
process.stdout.write(`== Diff applied successfully ==\n`)
process.stdout.write(
`====================================================================\n`,
)
process.stdout.write(result.content + "\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @KJ7LNW, for this line where result.content is printed: does it make sense to print the whole content here since it might be long? Or perhaps print only the first 50 lines, similar to how the input source (line 1411) and diff (line 1430) are truncated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really a debugging tool wrapped in jest, not a test itself: this is just for debugging for direct user interact , it is not intended for automated testing.

I wrote this because I was troubleshooting some apply_diff problems and I have been collecting you failed samples from a number of bug reports. In order to understand the issue when a diff fails to apply I almost always need to see the entire output.

Maybe the line limit should be trimmed, but I specifically remember not wanting it to trim: the model originally wrote this trimming the output, but I had to tell it not to because I needed to see it all at once to compare what was happening.

process.stdout.write(
`=============================== END ================================\n`,
)
expect(result.success).toBe(true)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the test fails when diff application is unsuccessful by adding an assertion or calling fail() in the else branch. Currently, the else branch only logs output without failing the test.

process.stdout.write(
`\n\n====================================================================\n`,
)
process.stdout.write(`== Failed to apply diff ==\n`)
process.stdout.write(
`====================================================================\n\n\n`,
)
console.error(result)
process.stdout.write(
`=============================== END ================================\n\n\n`,
)
}
} catch (err) {
console.error("Error processing files:", err.message)
console.error("Stack trace:", err.stack)
}
})
})
})

it("should not strip content that starts with pipe but no line number", async () => {
Expand Down
Loading