@@ -14,6 +14,27 @@ function readTranscriptLines(transcriptPath: string): string[] {
1414 return lines ;
1515}
1616
17+ async function appendHelloAndRequireId ( transcriptPath : string ) : Promise < string > {
18+ const appended = await appendInjectedAssistantMessageToTranscript ( {
19+ transcriptPath,
20+ message : "hello" ,
21+ } ) ;
22+ expect ( appended . ok ) . toBe ( true ) ;
23+ expect ( appended . messageId ) . toBeTypeOf ( "string" ) ;
24+ const messageId = appended . messageId ;
25+ if ( ! messageId ) {
26+ throw new Error ( "expected appended message id" ) ;
27+ }
28+ expect ( messageId . length ) . toBeGreaterThan ( 0 ) ;
29+ return messageId ;
30+ }
31+
32+ function readLastTranscriptRecord ( transcriptPath : string ) : Record < string , unknown > {
33+ const lines = readTranscriptLines ( transcriptPath ) ;
34+ expect ( lines . length ) . toBeGreaterThanOrEqual ( 2 ) ;
35+ return JSON . parse ( lines . at ( - 1 ) as string ) as Record < string , unknown > ;
36+ }
37+
1738// Guardrail: Gateway-injected assistant transcript messages must attach to the
1839// current leaf with a `parentId` and must not sever compaction history.
1940describe ( "gateway chat.inject transcript writes" , ( ) => {
@@ -24,22 +45,8 @@ describe("gateway chat.inject transcript writes", () => {
2445 } ) ;
2546
2647 try {
27- const appended = await appendInjectedAssistantMessageToTranscript ( {
28- transcriptPath,
29- message : "hello" ,
30- } ) ;
31- expect ( appended . ok ) . toBe ( true ) ;
32- expect ( appended . messageId ) . toBeTypeOf ( "string" ) ;
33- const messageId = appended . messageId ;
34- if ( ! messageId ) {
35- throw new Error ( "expected appended message id" ) ;
36- }
37- expect ( messageId . length ) . toBeGreaterThan ( 0 ) ;
38-
39- const lines = readTranscriptLines ( transcriptPath ) ;
40- expect ( lines . length ) . toBeGreaterThanOrEqual ( 2 ) ;
41-
42- const last = JSON . parse ( lines . at ( - 1 ) as string ) as Record < string , unknown > ;
48+ await appendHelloAndRequireId ( transcriptPath ) ;
49+ const last = readLastTranscriptRecord ( transcriptPath ) ;
4350 expect ( last . type ) . toBe ( "message" ) ;
4451
4552 // The regression we saw: raw jsonl appends omitted this field entirely.
@@ -71,20 +78,8 @@ describe("gateway chat.inject transcript writes", () => {
7178 "utf-8" ,
7279 ) ;
7380
74- const appended = await appendInjectedAssistantMessageToTranscript ( {
75- transcriptPath,
76- message : "hello" ,
77- } ) ;
78- expect ( appended . ok ) . toBe ( true ) ;
79- expect ( appended . messageId ) . toBeTypeOf ( "string" ) ;
80- const messageId = appended . messageId ;
81- if ( ! messageId ) {
82- throw new Error ( "expected appended message id" ) ;
83- }
84- expect ( messageId . length ) . toBeGreaterThan ( 0 ) ;
85-
86- const lines = readTranscriptLines ( transcriptPath ) ;
87- const last = JSON . parse ( lines . at ( - 1 ) as string ) as Record < string , unknown > ;
81+ const messageId = await appendHelloAndRequireId ( transcriptPath ) ;
82+ const last = readLastTranscriptRecord ( transcriptPath ) ;
8883
8984 expect ( last . type ) . toBe ( "message" ) ;
9085 expect ( last ) . toHaveProperty ( "id" , messageId ) ;
0 commit comments