@@ -3,6 +3,7 @@ import {Model} from '../../model';
33import { File } from '../File' ;
44import { JsonDecoder } from '../../../json-pack/json/JsonDecoder' ;
55import { CborDecoder } from '../../../json-pack/cbor/CborDecoder' ;
6+ import { FileEncodingParams } from '../types' ;
67
78const setup = ( view : unknown ) => {
89 const model = Model . withServerClock ( ) ;
@@ -68,4 +69,36 @@ describe('.toBinary()', () => {
6869 expect ( file . log . replayToEnd ( ) . view ( ) ) . toEqual ( { foo : 'bar' } ) ;
6970 } ) ;
7071 } ) ;
72+
73+ const assertEncoding = ( file : File , params : FileEncodingParams ) => {
74+ const blob = file . toBinary ( params ) ;
75+ // if (params.format === 'ndjson') console.log(Buffer.from(blob).toString('utf8'))
76+ const file2 = params . format === 'seq.cbor' ? File . fromSeqCbor ( blob ) : File . fromNdjson ( blob ) ;
77+ expect ( file2 . model . view ( ) ) . toEqual ( file . model . view ( ) ) ;
78+ expect ( file2 . model !== file . model ) . toBe ( true ) ;
79+ expect ( file2 . log . start . view ( ) ) . toEqual ( undefined ) ;
80+ expect ( file2 . log . replayToEnd ( ) . view ( ) ) . toEqual ( file . model . view ( ) ) ;
81+ expect ( file2 . log . patches . size ( ) ) . toBe ( file . log . patches . size ( ) ) ;
82+ } ;
83+
84+ describe ( 'can encode/decode all format combinations' , ( ) => {
85+ const formats : FileEncodingParams [ 'format' ] [ ] = [ 'ndjson' , 'seq.cbor' ] ;
86+ const modelFormats : FileEncodingParams [ 'model' ] [ ] = [ 'sidecar' , 'binary' , 'compact' , 'verbose' ] ;
87+ const historyFormats : FileEncodingParams [ 'history' ] [ ] = [ 'binary' , 'compact' , 'verbose' ] ;
88+ const noViews = [ true , false ] ;
89+ for ( const format of formats ) {
90+ for ( const model of modelFormats ) {
91+ for ( const history of historyFormats ) {
92+ for ( const noView of noViews ) {
93+ if ( noView && ( model === 'sidecar' ) ) continue ;
94+ const params = { format, model, history, noView} ;
95+ test ( JSON . stringify ( params ) , ( ) => {
96+ const { file} = setup ( { foo : 'bar' } ) ;
97+ assertEncoding ( file , params ) ;
98+ } ) ;
99+ }
100+ }
101+ }
102+ }
103+ } ) ;
71104} ) ;
0 commit comments