11import { describe , it , expect } from "vitest" ;
22import { buildPromptContext } from "./context-builder.js" ;
33import type { PrData } from "./types.js" ;
4- import { MAX_DIFF_CHARS , MAX_FILE_CHARS } from "./constants.js" ;
4+ import { ANTHROPIC_CONTEXT_LIMITS } from "./constants.js" ;
5+
6+ const limits = ANTHROPIC_CONTEXT_LIMITS ;
57
68function makePrData ( overrides : Partial < PrData > = { } ) : PrData {
79 return {
@@ -16,7 +18,7 @@ function makePrData(overrides: Partial<PrData> = {}): PrData {
1618
1719describe ( "buildPromptContext" , ( ) => {
1820 it ( "includes PR title and description" , ( ) => {
19- const result = buildPromptContext ( makePrData ( ) , "" ) ;
21+ const result = buildPromptContext ( makePrData ( ) , "" , limits ) ;
2022 expect ( result ) . toContain ( "**Title:** Test PR" ) ;
2123 expect ( result ) . toContain ( "Test body" ) ;
2224 } ) ;
@@ -25,7 +27,7 @@ describe("buildPromptContext", () => {
2527 const data = makePrData ( {
2628 metadata : { title : "Test" , body : "" , headSha : "abc" } ,
2729 } ) ;
28- const result = buildPromptContext ( data , "" ) ;
30+ const result = buildPromptContext ( data , "" , limits ) ;
2931 expect ( result ) . not . toContain ( "**Description:**" ) ;
3032 } ) ;
3133
@@ -36,25 +38,29 @@ describe("buildPromptContext", () => {
3638 { sha : "1234567abcdef" , message : "Fix bug" } ,
3739 ] ,
3840 } ) ;
39- const result = buildPromptContext ( data , "" ) ;
41+ const result = buildPromptContext ( data , "" , limits ) ;
4042 expect ( result ) . toContain ( "## Commits" ) ;
4143 expect ( result ) . toContain ( "- abcdef1 Initial commit" ) ;
4244 expect ( result ) . toContain ( "- 1234567 Fix bug" ) ;
4345 } ) ;
4446
4547 it ( "includes diff content" , ( ) => {
4648 const data = makePrData ( { diff : "+added line\n-removed line" } ) ;
47- const result = buildPromptContext ( data , "" ) ;
49+ const result = buildPromptContext ( data , "" , limits ) ;
4850 expect ( result ) . toContain ( "## Diff" ) ;
4951 expect ( result ) . toContain ( "+added line\n-removed line" ) ;
5052 } ) ;
5153
5254 it ( "truncates long diffs at line boundaries" , ( ) => {
5355 const lines = Array . from ( { length : 10000 } , ( _ , i ) => `+line ${ i } ` ) ;
5456 const longDiff = lines . join ( "\n" ) ;
55- expect ( longDiff . length ) . toBeGreaterThan ( MAX_DIFF_CHARS ) ;
57+ expect ( longDiff . length ) . toBeGreaterThan ( limits . maxDiffChars ) ;
5658
57- const result = buildPromptContext ( makePrData ( { diff : longDiff } ) , "" ) ;
59+ const result = buildPromptContext (
60+ makePrData ( { diff : longDiff } ) ,
61+ "" ,
62+ limits ,
63+ ) ;
5864 expect ( result ) . toContain ( "[Content truncated]" ) ;
5965 } ) ;
6066
@@ -65,15 +71,15 @@ describe("buildPromptContext", () => {
6571 { filename : "big.ts" , content : "x" . repeat ( 500 ) } ,
6672 ] ,
6773 } ) ;
68- const result = buildPromptContext ( data , "" ) ;
74+ const result = buildPromptContext ( data , "" , limits ) ;
6975 expect ( result ) . toContain ( "## Changed File Contents" ) ;
7076 // Big file should appear before small file
7177 const bigIdx = result . indexOf ( "big.ts" ) ;
7278 const smallIdx = result . indexOf ( "small.ts" ) ;
7379 expect ( bigIdx ) . toBeLessThan ( smallIdx ) ;
7480 } ) ;
7581
76- it ( "truncates individual files exceeding MAX_FILE_CHARS " , ( ) => {
82+ it ( "truncates individual files exceeding maxFileChars " , ( ) => {
7783 const data = makePrData ( {
7884 changedFiles : [
7985 {
@@ -84,21 +90,22 @@ describe("buildPromptContext", () => {
8490 } ,
8591 ] ,
8692 } ) ;
87- // The content is larger than MAX_FILE_CHARS
88- expect ( data . changedFiles [ 0 ] . content . length ) . toBeGreaterThan ( MAX_FILE_CHARS ) ;
93+ expect ( data . changedFiles [ 0 ] . content . length ) . toBeGreaterThan (
94+ limits . maxFileChars ,
95+ ) ;
8996
90- const result = buildPromptContext ( data , "" ) ;
97+ const result = buildPromptContext ( data , "" , limits ) ;
9198 expect ( result ) . toContain ( "[Content truncated]" ) ;
9299 } ) ;
93100
94- it ( "stops adding files when total exceeds MAX_CHANGED_FILES_CHARS " , ( ) => {
101+ it ( "stops adding files when total exceeds maxChangedFilesChars " , ( ) => {
95102 const files = Array . from ( { length : 20 } , ( _ , i ) => ( {
96103 filename : `file${ i } .ts` ,
97- content : "x" . repeat ( MAX_FILE_CHARS - 100 ) ,
104+ content : "x" . repeat ( limits . maxFileChars - 100 ) ,
98105 } ) ) ;
99106 const data = makePrData ( { changedFiles : files } ) ;
100107
101- const result = buildPromptContext ( data , "" ) ;
108+ const result = buildPromptContext ( data , "" , limits ) ;
102109
103110 // Not all 20 files should be included
104111 const includedCount = ( result . match ( / # # # f i l e \d + \. t s / g) || [ ] ) . length ;
@@ -110,7 +117,7 @@ describe("buildPromptContext", () => {
110117 const data = makePrData ( {
111118 fileTree : [ "src/index.ts" , "src/utils.ts" , "package.json" ] ,
112119 } ) ;
113- const result = buildPromptContext ( data , "" ) ;
120+ const result = buildPromptContext ( data , "" , limits ) ;
114121 expect ( result ) . toContain ( "## Repository File Tree" ) ;
115122 expect ( result ) . toContain ( "src/index.ts" ) ;
116123 } ) ;
@@ -119,26 +126,25 @@ describe("buildPromptContext", () => {
119126 const result = buildPromptContext (
120127 makePrData ( ) ,
121128 "Focus on accessibility testing" ,
129+ limits ,
122130 ) ;
123131 expect ( result ) . toContain ( "## Additional Instructions" ) ;
124132 expect ( result ) . toContain ( "Focus on accessibility testing" ) ;
125133 } ) ;
126134
127135 it ( "omits additional instructions when custom prompt is empty" , ( ) => {
128- const result = buildPromptContext ( makePrData ( ) , "" ) ;
136+ const result = buildPromptContext ( makePrData ( ) , "" , limits ) ;
129137 expect ( result ) . not . toContain ( "## Additional Instructions" ) ;
130138 } ) ;
131139
132140 it ( "respects overall character cap" , ( ) => {
133- // Create data that would exceed MAX_TOTAL_CHARS
141+ // Create data that would exceed maxTotalChars
134142 const data = makePrData ( {
135143 diff : "x" . repeat ( 80_000 ) ,
136144 changedFiles : [ { filename : "big.ts" , content : "y" . repeat ( 60_000 ) } ] ,
137145 fileTree : Array . from ( { length : 5000 } , ( _ , i ) => `path/${ i } .ts` ) ,
138146 } ) ;
139- const result = buildPromptContext ( data , "" ) ;
140- // The result includes truncation markers, so it will be slightly over
141- // but the core content should be capped
147+ const result = buildPromptContext ( data , "" , limits ) ;
142148 expect ( result ) . toContain ( "[Content truncated]" ) ;
143149 } ) ;
144150} ) ;
0 commit comments