@@ -12,7 +12,7 @@ describe('GroupContextInjector', () => {
1212 } ) ;
1313
1414 describe ( 'Basic Scenarios' , ( ) => {
15- it ( 'should inject group context into system message' , async ( ) => {
15+ it ( 'should inject group context before first user message' , async ( ) => {
1616 const injector = new GroupContextInjector ( {
1717 currentAgentId : 'agt_editor' ,
1818 currentAgentName : 'Editor' ,
@@ -35,31 +35,39 @@ describe('GroupContextInjector', () => {
3535 const context = createContext ( input ) ;
3636 const result = await injector . process ( context ) ;
3737
38- const systemContent = result . messages [ 0 ] . content ;
38+ // System message should be unchanged
39+ expect ( result . messages [ 0 ] . content ) . toBe ( 'You are a helpful editor.' ) ;
40+
41+ // Should have 3 messages now (system, injected, user)
42+ expect ( result . messages ) . toHaveLength ( 3 ) ;
3943
40- // Original content should be preserved
41- expect ( systemContent ) . toContain ( 'You are a helpful editor.' ) ;
44+ // Check injected message (second message)
45+ const injectedContent = result . messages [ 1 ] . content ;
46+ expect ( result . messages [ 1 ] . role ) . toBe ( 'user' ) ;
4247
4348 // Agent identity (plain text, no wrapper)
44- expect ( systemContent ) . toContain ( 'You are "Editor"' ) ;
45- expect ( systemContent ) . toContain ( 'acting as a participant' ) ;
46- expect ( systemContent ) . toContain ( '"Writing Team"' ) ;
47- expect ( systemContent ) . toContain ( 'agt_editor' ) ;
48- expect ( systemContent ) . not . toContain ( '<agent_identity>' ) ;
49+ expect ( injectedContent ) . toContain ( 'You are "Editor"' ) ;
50+ expect ( injectedContent ) . toContain ( 'acting as a participant' ) ;
51+ expect ( injectedContent ) . toContain ( '"Writing Team"' ) ;
52+ expect ( injectedContent ) . toContain ( 'agt_editor' ) ;
53+ expect ( injectedContent ) . not . toContain ( '<agent_identity>' ) ;
4954
5055 // Group context section with system prompt
51- expect ( systemContent ) . toContain ( '<group_context>' ) ;
52- expect ( systemContent ) . toContain ( 'A team for collaborative writing' ) ;
56+ expect ( injectedContent ) . toContain ( '<group_context>' ) ;
57+ expect ( injectedContent ) . toContain ( 'A team for collaborative writing' ) ;
5358
5459 // Participants section with XML format
55- expect ( systemContent ) . toContain ( '<group_participants>' ) ;
56- expect ( systemContent ) . toContain ( '<member name="Supervisor" id="agt_supervisor" />' ) ;
57- expect ( systemContent ) . toContain ( '<member name="Writer" id="agt_writer" />' ) ;
58- expect ( systemContent ) . toContain ( '<member name="Editor" id="agt_editor" you="true" />' ) ;
60+ expect ( injectedContent ) . toContain ( '<group_participants>' ) ;
61+ expect ( injectedContent ) . toContain ( '<member name="Supervisor" id="agt_supervisor" />' ) ;
62+ expect ( injectedContent ) . toContain ( '<member name="Writer" id="agt_writer" />' ) ;
63+ expect ( injectedContent ) . toContain ( '<member name="Editor" id="agt_editor" you="true" />' ) ;
5964
6065 // Identity rules
61- expect ( systemContent ) . toContain ( '<identity_rules>' ) ;
62- expect ( systemContent ) . toContain ( 'NEVER expose or display agent IDs' ) ;
66+ expect ( injectedContent ) . toContain ( '<identity_rules>' ) ;
67+ expect ( injectedContent ) . toContain ( 'NEVER expose or display agent IDs' ) ;
68+
69+ // Original user message should be third
70+ expect ( result . messages [ 2 ] . content ) . toBe ( 'Please review this.' ) ;
6371
6472 // Metadata should be updated
6573 expect ( result . metadata . groupContextInjected ) . toBe ( true ) ;
@@ -72,35 +80,37 @@ describe('GroupContextInjector', () => {
7280 enabled : false , // Disabled
7381 } ) ;
7482
75- const input : any [ ] = [ { role : 'system' , content : 'You are a helpful editor.' } ] ;
83+ const input : any [ ] = [
84+ { role : 'system' , content : 'You are a helpful editor.' } ,
85+ { role : 'user' , content : 'Hello' } ,
86+ ] ;
7687
7788 const context = createContext ( input ) ;
7889 const result = await injector . process ( context ) ;
7990
80- // Should be unchanged
91+ // Should be unchanged - no injection
92+ expect ( result . messages ) . toHaveLength ( 2 ) ;
8193 expect ( result . messages [ 0 ] . content ) . toBe ( 'You are a helpful editor.' ) ;
94+ expect ( result . messages [ 1 ] . content ) . toBe ( 'Hello' ) ;
8295 expect ( result . metadata . groupContextInjected ) . toBeUndefined ( ) ;
8396 } ) ;
8497
85- it ( 'should skip injection when no system message exists' , async ( ) => {
98+ it ( 'should skip injection when no user message exists' , async ( ) => {
8699 const injector = new GroupContextInjector ( {
87100 currentAgentId : 'agt_editor' ,
88101 currentAgentName : 'Editor' ,
89102 enabled : true ,
90103 } ) ;
91104
92- const input : any [ ] = [
93- { role : 'user' , content : 'Hello' } ,
94- { role : 'assistant' , content : 'Hi there!' } ,
95- ] ;
105+ const input : any [ ] = [ { role : 'system' , content : 'You are a helpful editor.' } ] ;
96106
97107 const context = createContext ( input ) ;
98108 const result = await injector . process ( context ) ;
99109
100- // Messages should be unchanged
101- expect ( result . messages [ 0 ] . content ) . toBe ( 'Hello' ) ;
102- expect ( result . messages [ 1 ] . content ) . toBe ( 'Hi there! ' ) ;
103- expect ( result . metadata . groupContextInjected ) . toBeUndefined ( ) ;
110+ // Messages should be unchanged - no user message to inject before
111+ expect ( result . messages ) . toHaveLength ( 1 ) ;
112+ expect ( result . messages [ 0 ] . content ) . toBe ( 'You are a helpful editor. ' ) ;
113+ expect ( result . metadata . groupContextInjected ) . toBe ( true ) ;
104114 } ) ;
105115 } ) ;
106116
@@ -113,12 +123,16 @@ describe('GroupContextInjector', () => {
113123 enabled : true ,
114124 } ) ;
115125
116- const input : any [ ] = [ { content : 'You are an editor.' , role : 'system' } ] ;
126+ const input : any [ ] = [
127+ { content : 'You are an editor.' , role : 'system' } ,
128+ { content : 'Hello' , role : 'user' } ,
129+ ] ;
117130
118131 const context = createContext ( input ) ;
119132 const result = await injector . process ( context ) ;
120133
121- expect ( result . messages [ 0 ] . content ) . toMatchSnapshot ( ) ;
134+ // Check injected message content
135+ expect ( result . messages [ 1 ] . content ) . toMatchSnapshot ( ) ;
122136 } ) ;
123137
124138 it ( 'should handle config with only group info' , async ( ) => {
@@ -129,25 +143,33 @@ describe('GroupContextInjector', () => {
129143 systemPrompt : 'Test group description' ,
130144 } ) ;
131145
132- const input : any [ ] = [ { content : 'System prompt.' , role : 'system' } ] ;
146+ const input : any [ ] = [
147+ { content : 'System prompt.' , role : 'system' } ,
148+ { content : 'Hello' , role : 'user' } ,
149+ ] ;
133150
134151 const context = createContext ( input ) ;
135152 const result = await injector . process ( context ) ;
136153
137- expect ( result . messages [ 0 ] . content ) . toMatchSnapshot ( ) ;
154+ // Check injected message content
155+ expect ( result . messages [ 1 ] . content ) . toMatchSnapshot ( ) ;
138156 } ) ;
139157
140158 it ( 'should handle empty config' , async ( ) => {
141159 const injector = new GroupContextInjector ( {
142160 enabled : true ,
143161 } ) ;
144162
145- const input : any [ ] = [ { content : 'Base prompt.' , role : 'system' } ] ;
163+ const input : any [ ] = [
164+ { content : 'Base prompt.' , role : 'system' } ,
165+ { content : 'Hello' , role : 'user' } ,
166+ ] ;
146167
147168 const context = createContext ( input ) ;
148169 const result = await injector . process ( context ) ;
149170
150- expect ( result . messages [ 0 ] . content ) . toMatchSnapshot ( ) ;
171+ // Check injected message content
172+ expect ( result . messages [ 1 ] . content ) . toMatchSnapshot ( ) ;
151173 } ) ;
152174 } ) ;
153175
@@ -158,15 +180,19 @@ describe('GroupContextInjector', () => {
158180 // Minimal config
159181 } ) ;
160182
161- const input : any [ ] = [ { role : 'system' , content : 'Base prompt.' } ] ;
183+ const input : any [ ] = [
184+ { role : 'system' , content : 'Base prompt.' } ,
185+ { role : 'user' , content : 'Hello' } ,
186+ ] ;
162187
163188 const context = createContext ( input ) ;
164189 const result = await injector . process ( context ) ;
165190
166- const systemContent = result . messages [ 0 ] . content ;
191+ // Check injected message content
192+ const injectedContent = result . messages [ 1 ] . content ;
167193
168194 // Even with minimal config, identity rules should be present
169- expect ( systemContent ) . toMatchSnapshot ( ) ;
195+ expect ( injectedContent ) . toMatchSnapshot ( ) ;
170196 } ) ;
171197 } ) ;
172198
@@ -179,12 +205,16 @@ describe('GroupContextInjector', () => {
179205 systemPrompt : 'Empty group description' ,
180206 } ) ;
181207
182- const input : any [ ] = [ { content : 'Prompt.' , role : 'system' } ] ;
208+ const input : any [ ] = [
209+ { content : 'Prompt.' , role : 'system' } ,
210+ { content : 'Hello' , role : 'user' } ,
211+ ] ;
183212
184213 const context = createContext ( input ) ;
185214 const result = await injector . process ( context ) ;
186215
187- expect ( result . messages [ 0 ] . content ) . toMatchSnapshot ( ) ;
216+ // Check injected message content
217+ expect ( result . messages [ 1 ] . content ) . toMatchSnapshot ( ) ;
188218 } ) ;
189219
190220 it ( 'should preserve other messages unchanged' , async ( ) => {
@@ -204,10 +234,16 @@ describe('GroupContextInjector', () => {
204234 const context = createContext ( input ) ;
205235 const result = await injector . process ( context ) ;
206236
207- // Only system message should be modified
208- expect ( result . messages [ 0 ] . content ) . toContain ( '<group_context>' ) ;
209- expect ( result . messages [ 1 ] . content ) . toBe ( 'User message.' ) ;
210- expect ( result . messages [ 2 ] . content ) . toBe ( 'Assistant response.' ) ;
237+ // System message should be unchanged
238+ expect ( result . messages [ 0 ] . content ) . toBe ( 'System prompt.' ) ;
239+
240+ // Injected message should be second
241+ expect ( result . messages [ 1 ] . role ) . toBe ( 'user' ) ;
242+ expect ( result . messages [ 1 ] . content ) . toContain ( '<group_context>' ) ;
243+
244+ // Original messages should be preserved
245+ expect ( result . messages [ 2 ] . content ) . toBe ( 'User message.' ) ;
246+ expect ( result . messages [ 3 ] . content ) . toBe ( 'Assistant response.' ) ;
211247 } ) ;
212248 } ) ;
213249} ) ;
0 commit comments