@@ -3,7 +3,7 @@ import { z } from 'zod';
33import * as trpc from '@trpc/server' ;
44import { observable } from '@trpc/server/observable' ;
55import { EventEmitter } from 'events' ;
6- import { handleIPCOperation } from '../handleIPCOperation ' ;
6+ import { handleIPCMessage } from '../handleIPCMessage ' ;
77import { IpcMainInvokeEvent } from 'electron' ;
88
99interface MockEvent {
@@ -51,11 +51,22 @@ describe('api', () => {
5151 } ,
5252 } ) ;
5353
54- await handleIPCOperation ( {
54+ await handleIPCMessage ( {
5555 createContext : async ( ) => ( { } ) ,
56- operation : { context : { } , id : 1 , input : { id : 'test-id' } , path : 'testQuery' , type : 'query' } ,
57- router : testRouter ,
5856 event,
57+ internalId : '1-1:1' ,
58+ message : {
59+ method : 'request' ,
60+ operation : {
61+ context : { } ,
62+ id : 1 ,
63+ input : { id : 'test-id' } ,
64+ path : 'testQuery' ,
65+ type : 'query' ,
66+ } ,
67+ } ,
68+ router : testRouter ,
69+ subscriptions : new Map ( ) ,
5970 } ) ;
6071
6172 expect ( event . sender . send ) . toHaveBeenCalledOnce ( ) ;
@@ -79,11 +90,22 @@ describe('api', () => {
7990 } ,
8091 } ) ;
8192
82- await handleIPCOperation ( {
93+ await handleIPCMessage ( {
8394 createContext : async ( ) => ( { } ) ,
84- operation : { context : { } , id : 1 , input : { id : 'test-id' } , path : 'testQuery' , type : 'query' } ,
85- router : testRouter ,
8695 event,
96+ internalId : '1-1:1' ,
97+ message : {
98+ method : 'request' ,
99+ operation : {
100+ context : { } ,
101+ id : 1 ,
102+ input : { id : 'test-id' } ,
103+ path : 'testQuery' ,
104+ type : 'query' ,
105+ } ,
106+ } ,
107+ router : testRouter ,
108+ subscriptions : new Map ( ) ,
87109 } ) ;
88110
89111 expect ( event . sender . send ) . not . toHaveBeenCalled ( ) ;
@@ -98,15 +120,20 @@ describe('api', () => {
98120 } ,
99121 } ) ;
100122
101- await handleIPCOperation ( {
123+ await handleIPCMessage ( {
102124 createContext : async ( ) => ( { } ) ,
103- operation : {
104- context : { } ,
105- id : 1 ,
106- input : undefined ,
107- path : 'testSubscription' ,
108- type : 'subscription' ,
125+ message : {
126+ method : 'request' ,
127+ operation : {
128+ context : { } ,
129+ id : 1 ,
130+ input : undefined ,
131+ path : 'testSubscription' ,
132+ type : 'subscription' ,
133+ } ,
109134 } ,
135+ internalId : '1-1:1' ,
136+ subscriptions : new Map ( ) ,
110137 router : testRouter ,
111138 event,
112139 } ) ;
@@ -124,41 +151,6 @@ describe('api', () => {
124151 } ) ;
125152 } ) ;
126153
127- test ( 'cancels subscriptions when webcontents are closed' , async ( ) => {
128- let isDestroyed = false ;
129- let onDestroyed : ( ) => void ;
130- const event = makeEvent ( {
131- sender : {
132- isDestroyed : ( ) => isDestroyed ,
133- send : vi . fn ( ) ,
134- on : ( _event : string , cb : ( ) => void ) => {
135- onDestroyed = cb ;
136- } ,
137- } ,
138- } ) ;
139-
140- await handleIPCOperation ( {
141- createContext : async ( ) => ( { } ) ,
142- operation : {
143- context : { } ,
144- id : 1 ,
145- input : undefined ,
146- path : 'testSubscription' ,
147- type : 'subscription' ,
148- } ,
149- router : testRouter ,
150- event,
151- } ) ;
152-
153- expect ( event . sender . send ) . not . toHaveBeenCalled ( ) ;
154-
155- onDestroyed ( ) ;
156-
157- ee . emit ( 'test' ) ;
158-
159- expect ( event . sender . send ) . not . toHaveBeenCalled ( ) ;
160- } ) ;
161-
162154 test ( 'subscription responds using custom serializer' , async ( ) => {
163155 const event = makeEvent ( {
164156 sender : {
@@ -193,15 +185,20 @@ describe('api', () => {
193185 } ) ,
194186 } ) ;
195187
196- await handleIPCOperation ( {
188+ await handleIPCMessage ( {
197189 createContext : async ( ) => ( { } ) ,
198- operation : {
199- context : { } ,
200- id : 1 ,
201- input : undefined ,
202- path : 'testSubscription' ,
203- type : 'subscription' ,
190+ message : {
191+ method : 'request' ,
192+ operation : {
193+ context : { } ,
194+ id : 1 ,
195+ input : undefined ,
196+ path : 'testSubscription' ,
197+ type : 'subscription' ,
198+ } ,
204199 } ,
200+ internalId : '1-1:1' ,
201+ subscriptions : new Map ( ) ,
205202 router : testRouter ,
206203 event,
207204 } ) ;
@@ -219,64 +216,4 @@ describe('api', () => {
219216 } ,
220217 } ) ;
221218 } ) ;
222-
223- test ( "doesn't crash when canceling subscriptions when custom deserializer doesn't allow undefined" , async ( ) => {
224- const t = trpc . initTRPC . create ( {
225- transformer : {
226- deserialize : ( input : unknown ) => {
227- if ( ! input ) throw new Error ( "Can't parse empty input" ) ;
228- return JSON . parse ( input as string ) ;
229- } ,
230- serialize : ( input ) => {
231- return JSON . stringify ( input ) ;
232- } ,
233- } ,
234- } ) ;
235-
236- const testRouter = t . router ( {
237- testSubscription : t . procedure . subscription ( ( ) => {
238- return observable ( ( emit ) => {
239- function testResponse ( ) {
240- emit . next ( 'test response' ) ;
241- }
242-
243- ee . on ( 'test' , testResponse ) ;
244- return ( ) => ee . off ( 'test' , testResponse ) ;
245- } ) ;
246- } ) ,
247- } ) ;
248-
249- let isDestroyed = false ;
250- let onDestroyed : ( ) => void ;
251- const event = makeEvent ( {
252- sender : {
253- isDestroyed : ( ) => isDestroyed ,
254- send : vi . fn ( ) ,
255- on : ( _event : string , cb : ( ) => void ) => {
256- onDestroyed = cb ;
257- } ,
258- } ,
259- } ) ;
260-
261- await handleIPCOperation ( {
262- createContext : async ( ) => ( { } ) ,
263- operation : {
264- context : { } ,
265- id : 1 ,
266- input : undefined ,
267- path : 'testSubscription' ,
268- type : 'subscription' ,
269- } ,
270- router : testRouter ,
271- event,
272- } ) ;
273-
274- expect ( event . sender . send ) . not . toHaveBeenCalled ( ) ;
275-
276- onDestroyed ( ) ;
277-
278- ee . emit ( 'test' ) ;
279-
280- expect ( event . sender . send ) . not . toHaveBeenCalled ( ) ;
281- } ) ;
282219} ) ;
0 commit comments