11/** @jest -environment ./src/utils/enableVersionCheck.ts */
22
33import {
4+ binaryTestEvents ,
5+ collect ,
46 createTestNode ,
57 jsonTestEvents ,
68 matchServerVersion ,
@@ -10,13 +12,15 @@ import {
1012import {
1113 KurrentDBClient ,
1214 ANY ,
13- AppendStreamRequest ,
1415 UnsupportedError ,
16+ AppendStreamRequest ,
17+ AppendStreamFailure ,
18+ STREAM_EXISTS ,
1519} from "@kurrent/kurrentdb-client" ;
1620
1721import { v4 } from "uuid" ;
1822
19- describe ( "MultiAppendStream " , ( ) => {
23+ describe ( "multiAppend " , ( ) => {
2024 const supported = matchServerVersion `>=25.0` ;
2125 const node = createTestNode ( ) ;
2226 let client ! : KurrentDBClient ;
@@ -30,33 +34,103 @@ describe("MultiAppendStream", () => {
3034 await node . down ( ) ;
3135 } ) ;
3236
33- optionalDescribe ( supported ) (
34- "should successfully append to multiple streams" ,
35- ( ) => {
36- test ( "json events" , async ( ) => {
37- const STREAM_NAME_1 = v4 ( ) . toString ( ) ;
38- const STREAM_NAME_2 = v4 ( ) . toString ( ) ;
37+ test ( "json events" , async ( ) => {
38+ const STREAM_NAME = v4 ( ) . toString ( ) ;
3939
40- const requests : AppendStreamRequest [ ] = [ ] ;
40+ const requests : AppendStreamRequest [ ] = [ ] ;
4141
42- requests . push ( {
43- streamName : STREAM_NAME_1 ,
44- events : jsonTestEvents ( ) ,
45- expectedState : ANY ,
46- } ) ;
42+ requests . push ( {
43+ streamName : STREAM_NAME ,
44+ events : binaryTestEvents ( ) ,
45+ expectedState : ANY ,
46+ } ) ;
47+
48+ try {
49+ await client . multiStreamAppend ( requests ) ;
50+ } catch ( error ) {
51+ expect ( error ) . toBeInstanceOf ( Error ) ;
52+ expect ( error . message ) . toBe (
53+ "multiStreamAppend requires server version 25.10 or higher."
54+ ) ;
55+ }
56+ } ) ;
57+
58+ optionalDescribe ( supported ) ( "Supported (>=25.1)" , ( ) => {
59+ test ( "json events" , async ( ) => {
60+ const STREAM_NAME_1 = v4 ( ) . toString ( ) ;
61+ const STREAM_NAME_2 = v4 ( ) . toString ( ) ;
62+ const expectedMetadata = {
63+ timestamp : new Date ( ) . toISOString ( ) ,
64+ int : 1 ,
65+ float : 1.1 ,
66+ string : "test" ,
67+ } ;
68+
69+ const requests : AppendStreamRequest [ ] = [ ] ;
70+
71+ requests . push ( {
72+ streamName : STREAM_NAME_1 ,
73+ events : jsonTestEvents ( 4 , "test" , expectedMetadata ) ,
74+ expectedState : ANY ,
75+ } ) ;
76+
77+ requests . push ( {
78+ streamName : STREAM_NAME_2 ,
79+ events : jsonTestEvents ( 4 , "test" , expectedMetadata ) ,
80+ expectedState : ANY ,
81+ } ) ;
82+
83+ const result = await client . multiStreamAppend ( requests ) ;
84+ expect ( result ) . toBeDefined ( ) ;
85+ expect ( result . success ) . toBeTruthy ( ) ;
86+
87+ const stream1Events = await collect ( client . readStream ( STREAM_NAME_1 ) ) ;
88+ const stream2Events = await collect ( client . readStream ( STREAM_NAME_2 ) ) ;
89+
90+ expect ( stream1Events . length ) . toBe ( 4 ) ;
91+ expect ( stream2Events . length ) . toBe ( 4 ) ;
4792
48- requests . push ( {
49- streamName : STREAM_NAME_2 ,
50- events : jsonTestEvents ( ) ,
51- expectedState : ANY ,
93+ for ( const event of [ ...stream1Events , ...stream2Events ] ) {
94+ expect ( event . event ) . toBeDefined ( ) ;
95+ expect ( event . event ?. metadata ) . toEqual ( {
96+ "$schema.data-format" : "Json" ,
97+ "$schema.name" : "test" ,
98+ ...expectedMetadata
5299 } ) ;
100+ }
101+ } ) ;
102+ } ) ;
103+
104+ optionalDescribe ( supported ) ( "Supported (>=25.1)" , ( ) => {
105+ test ( "stream revision conflict" , async ( ) => {
106+ const STREAM_NAME = v4 ( ) . toString ( ) ;
107+
108+ const requests : AppendStreamRequest [ ] = [ ] ;
53109
54- const result = await client . multiAppend ( requests ) ;
55- expect ( result ) . toBeDefined ( ) ;
56- expect ( result . success ) . toBeTruthy ( ) ;
110+ requests . push ( {
111+ streamName : STREAM_NAME ,
112+ events : jsonTestEvents ( ) ,
113+ expectedState : STREAM_EXISTS ,
57114 } ) ;
58- }
59- ) ;
115+
116+ const result = await client . multiStreamAppend ( requests ) ;
117+ expect ( result ) . toBeDefined ( ) ;
118+ expect ( result . success ) . toBeFalsy ( ) ;
119+ expect ( result . output ) . toBeDefined ( ) ;
120+ expect ( result . output . length ) . toBe ( 1 ) ;
121+
122+ const failures = result . output as AppendStreamFailure [ ] ;
123+ expect ( failures [ 0 ] . streamName ) . toBe ( STREAM_NAME ) ;
124+
125+ expect ( failures [ 0 ] ) . toMatchObject ( {
126+ streamName : expect . any ( String ) ,
127+ details : {
128+ type : "wrong_expected_revision" ,
129+ revision : BigInt ( - 1 ) ,
130+ } ,
131+ } ) ;
132+ } ) ;
133+ } ) ;
60134
61135 optionalDescribe ( ! supported ) ( "not supported (<25.0)" , ( ) => {
62136 test ( "throw unsupported error" , async ( ) => {
@@ -77,7 +151,7 @@ describe("MultiAppendStream", () => {
77151 } ) ;
78152
79153 try {
80- await client . multiAppend ( requests ) ;
154+ await client . multiStreamAppend ( requests ) ;
81155 } catch ( error ) {
82156 expect ( error ) . toBeInstanceOf ( UnsupportedError ) ;
83157 }
0 commit comments