@@ -3,8 +3,9 @@ import { Cause, Effect, Exit } from 'effect';
33import { canonicalize , stringToUint8Array } from 'graph-framework-utils' ;
44import { expect , it } from 'vitest' ;
55import { applyEvent } from './apply-event.js' ;
6+ import { createInvitation } from './create-invitation.js' ;
67import { createSpace } from './create-space.js' ;
7- import { VerifySignatureError } from './types.js' ;
8+ import { InvalidEventError , VerifySignatureError } from './types.js' ;
89
910it ( 'should fail in case of an invalid signature' , async ( ) => {
1011 const author = {
@@ -34,3 +35,58 @@ it('should fail in case of an invalid signature', async () => {
3435 }
3536 }
3637} ) ;
38+
39+ it ( 'should fail in case state is not provided for an event other than createSpace' , async ( ) => {
40+ const author = {
41+ signaturePublicKey : '03594161eed61407084114a142d1ce05ef4c5a5279479fdd73a2b16944fbff003b' ,
42+ signaturePrivateKey : '76b78f644c19d6133018a97a3bc2d5038be0af5a2858b9e640ff3e2f2db63a0b' ,
43+ encryptionPublicKey : 'encryption' ,
44+ } ;
45+
46+ const result = await Effect . runPromiseExit (
47+ Effect . gen ( function * ( ) {
48+ const spaceEvent = yield * createSpace ( { author } ) ;
49+ const state = yield * applyEvent ( { event : spaceEvent } ) ;
50+
51+ const spaceEvent2 = yield * createInvitation ( { author, id : state . id , previousEventHash : state . lastEventHash } ) ;
52+ return yield * applyEvent ( { event : spaceEvent2 } ) ;
53+ } ) ,
54+ ) ;
55+
56+ expect ( Exit . isFailure ( result ) ) . toBe ( true ) ;
57+ if ( Exit . isFailure ( result ) ) {
58+ const cause = result . cause ;
59+ if ( Cause . isFailType ( cause ) ) {
60+ expect ( cause . error ) . toBeInstanceOf ( InvalidEventError ) ;
61+ }
62+ }
63+ } ) ;
64+
65+ it ( 'should fail in case of an event is applied that is not based on the previous event' , async ( ) => {
66+ const author = {
67+ signaturePublicKey : '03594161eed61407084114a142d1ce05ef4c5a5279479fdd73a2b16944fbff003b' ,
68+ signaturePrivateKey : '76b78f644c19d6133018a97a3bc2d5038be0af5a2858b9e640ff3e2f2db63a0b' ,
69+ encryptionPublicKey : 'encryption' ,
70+ } ;
71+
72+ const result = await Effect . runPromiseExit (
73+ Effect . gen ( function * ( ) {
74+ const spaceEvent = yield * createSpace ( { author } ) ;
75+ const state = yield * applyEvent ( { event : spaceEvent } ) ;
76+
77+ const spaceEvent2 = yield * createSpace ( { author } ) ;
78+ const state2 = yield * applyEvent ( { state, event : spaceEvent2 } ) ;
79+
80+ const spaceEvent3 = yield * createInvitation ( { author, id : state . id , previousEventHash : state . lastEventHash } ) ;
81+ return yield * applyEvent ( { state : state2 , event : spaceEvent3 } ) ;
82+ } ) ,
83+ ) ;
84+
85+ expect ( Exit . isFailure ( result ) ) . toBe ( true ) ;
86+ if ( Exit . isFailure ( result ) ) {
87+ const cause = result . cause ;
88+ if ( Cause . isFailType ( cause ) ) {
89+ expect ( cause . error ) . toBeInstanceOf ( InvalidEventError ) ;
90+ }
91+ }
92+ } ) ;
0 commit comments