2026-04-07 23:48:23 +00:00
import { describe , expect , test } from "bun:test"
2026-05-25 02:05:27 +00:00
import { normalize , resolveFileDiff , text } from "./session-diff"
2026-04-07 23:48:23 +00:00
describe ( "session diff" , ( ) = > {
test ( "keeps unified patch content" , ( ) = > {
const diff = {
file : "a.ts" ,
patch :
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n@@ -1,2 +1,2 @@\n one\n-two\n+three\n" ,
additions : 1 ,
deletions : 1 ,
status : "modified" as const ,
}
const view = normalize ( diff )
expect ( view . fileDiff . name ) . toBe ( "a.ts" )
2026-05-25 02:05:27 +00:00
expect ( view . fileDiff . isPartial ) . toBe ( true )
2026-04-07 23:48:23 +00:00
expect ( text ( view , "deletions" ) ) . toBe ( "one\ntwo\n" )
expect ( text ( view , "additions" ) ) . toBe ( "one\nthree\n" )
} )
2026-05-05 01:05:53 +00:00
test ( "keeps missing final newlines from unified patches" , ( ) = > {
const diff = {
file : "a.ts" ,
patch :
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n@@ -1,2 +1,2 @@\n one\n-two\n\\ No newline at end of file\n+three\n\\ No newline at end of file\n" ,
additions : 1 ,
deletions : 1 ,
status : "modified" as const ,
}
const view = normalize ( diff )
expect ( text ( view , "deletions" ) ) . toBe ( "one\ntwo" )
expect ( text ( view , "additions" ) ) . toBe ( "one\nthree" )
} )
2026-05-25 02:05:27 +00:00
test ( "keeps separated patch hunks partial without complete file contents" , ( ) = > {
const fileDiff = resolveFileDiff ( {
file : "project.ts" ,
patch :
'Index: project.ts\n===================================================================\n--- project.ts\t\n+++ project.ts\t\n@@ -1,3 +1,2 @@\n import { and } from "drizzle-orm"\n-import { sql } from "drizzle-orm"\n import { ProjectTable } from "./project.sql"\n@@ -346,3 +345,3 @@\n import { Database } from "@/storage/db"\n-import { ProjectTable } from "./project.sql"\n+import { ProjectTable } from "../project/project.sql"\n import { SessionTable } from "../session/session.sql"\n' ,
} )
expect ( fileDiff . isPartial ) . toBe ( true )
expect ( fileDiff . hunks ) . toHaveLength ( 2 )
expect ( fileDiff . hunks [ 1 ] ? . collapsedBefore ) . toBeGreaterThan ( 0 )
} )
test ( "renders headerless persisted patches" , ( ) = > {
const view = normalize ( {
file : "a.ts" ,
patch : "@@ -1 +1 @@\n-old\n+new\n" ,
additions : 1 ,
deletions : 1 ,
status : "modified" as const ,
} )
expect ( view . fileDiff . name ) . toBe ( "a.ts" )
expect ( view . fileDiff . isPartial ) . toBe ( true )
expect ( text ( view , "deletions" ) ) . toBe ( "old\n" )
expect ( text ( view , "additions" ) ) . toBe ( "new\n" )
} )
test ( "does not share headerless patch metadata between files" , ( ) = > {
const patch = "@@ -1 +1 @@\n-old\n+new\n"
expect ( resolveFileDiff ( { file : "a.ts" , patch } ) . name ) . toBe ( "a.ts" )
expect ( resolveFileDiff ( { file : "b.ts" , patch } ) . name ) . toBe ( "b.ts" )
} )
test ( "keeps capped header-only patches partial" , ( ) = > {
const fileDiff = resolveFileDiff ( {
file : "a.ts" ,
2026-05-25 02:06:42 +00:00
patch :
"Index: a.ts\n===================================================================\n--- a.ts\t\n+++ a.ts\t\n" ,
2026-05-25 02:05:27 +00:00
} )
expect ( fileDiff . name ) . toBe ( "a.ts" )
expect ( fileDiff . isPartial ) . toBe ( true )
expect ( fileDiff . hunks ) . toEqual ( [ ] )
} )
test ( "keeps full legacy content as a complete diff" , ( ) = > {
2026-04-07 23:48:23 +00:00
const diff = {
file : "a.ts" ,
before : "one\n" ,
after : "two\n" ,
additions : 1 ,
deletions : 1 ,
status : "modified" as const ,
}
const view = normalize ( diff )
2026-05-25 02:05:27 +00:00
expect ( view . fileDiff . isPartial ) . toBe ( false )
2026-04-07 23:48:23 +00:00
expect ( text ( view , "deletions" ) ) . toBe ( "one\n" )
expect ( text ( view , "additions" ) ) . toBe ( "two\n" )
} )
2026-05-05 00:34:06 +00:00
test ( "ignores malformed persisted patches" , ( ) = > {
const diff = {
file : "a.ts" ,
patch :
"diff --git a/a.ts b/a.ts\nindex ff4ceb2..65a1de0 100644\n--- a/a.ts\n+++ b/a.ts\n@@ -1,3 +1,3 @@\n keep\n+add\n same\r" ,
additions : 1 ,
deletions : 1 ,
status : "modified" as const ,
}
const view = normalize ( diff )
expect ( text ( view , "deletions" ) ) . toBe ( "" )
expect ( text ( view , "additions" ) ) . toBe ( "" )
} )
2026-04-07 23:48:23 +00:00
} )