binhong87_obsidian-note-agent/tests/utils/patch.test.ts
Bin-Home 9f8119934b feat: v1.1 follow-ups
- applyUnifiedPatch: replace stub with real unified-diff applier
  extracted to src/utils/patch.ts with 6 unit tests
- AgentLoop: enforce turnTimeoutMs via per-iteration AbortController
  chained to outer cancel signal
- ConversationList: replace <a href="#"> with <button> (a11y)
- Scheduler tests: strengthen with timezone-portable assertions

Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
2026-04-22 21:04:43 +08:00

65 lines
1.9 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { applyUnifiedPatch } from "../../src/utils/patch";
const original = ["line1", "line2", "line3", "line4", "line5"].join("\n");
describe("applyUnifiedPatch", () => {
it("returns original unchanged when patch is empty", () => {
expect(applyUnifiedPatch(original, "")).toBe(original);
expect(applyUnifiedPatch(original, " \n ")).toBe(original);
});
it("applies a simple hunk that replaces one line", () => {
const patch = [
"@@ -1,3 +1,3 @@",
" line1",
"-line2",
"+LINE2",
" line3",
].join("\n");
expect(applyUnifiedPatch(original, patch)).toBe(
["line1", "LINE2", "line3", "line4", "line5"].join("\n")
);
});
it("applies a hunk that inserts a line", () => {
const patch = ["@@ -1,2 +1,3 @@", " line1", "+inserted", " line2"].join("\n");
expect(applyUnifiedPatch(original, patch)).toBe(
["line1", "inserted", "line2", "line3", "line4", "line5"].join("\n")
);
});
it("applies a hunk that deletes a line", () => {
const patch = ["@@ -2,2 +2,1 @@", "-line2", " line3"].join("\n");
expect(applyUnifiedPatch(original, patch)).toBe(
["line1", "line3", "line4", "line5"].join("\n")
);
});
it("skips file header lines (--- +++)", () => {
const patch = [
"--- a/file.md",
"+++ b/file.md",
"@@ -3,1 +3,1 @@",
"-line3",
"+LINE3",
].join("\n");
expect(applyUnifiedPatch(original, patch)).toBe(
["line1", "line2", "LINE3", "line4", "line5"].join("\n")
);
});
it("applies two hunks in sequence", () => {
const patch = [
"@@ -1,1 +1,1 @@",
"-line1",
"+LINE1",
"@@ -5,1 +5,1 @@",
"-line5",
"+LINE5",
].join("\n");
expect(applyUnifiedPatch(original, patch)).toBe(
["LINE1", "line2", "line3", "line4", "LINE5"].join("\n")
);
});
});