11import { assert , describe , it } from "@effect/vitest" ;
22import * as Cause from "effect/Cause" ;
3- import * as Deferred from "effect/Deferred" ;
43import * as Effect from "effect/Effect" ;
54import * as ErrorReporter from "effect/ErrorReporter" ;
5+ import * as Exit from "effect/Exit" ;
6+ import * as Fiber from "effect/Fiber" ;
67import * as Layer from "effect/Layer" ;
78import * as Logger from "effect/Logger" ;
89import * as Queue from "effect/Queue" ;
910import * as Schema from "effect/Schema" ;
1011import * as Stream from "effect/Stream" ;
11- import { Rpc , RpcGroup , RpcMessage , RpcSerialization , RpcServer } from "effect/unstable/rpc" ;
12+ import { Rpc , RpcClient , RpcGroup , RpcServer } from "effect/unstable/rpc" ;
1213
1314import { WS_RPC_SERVER_OPTIONS } from "../ws.ts" ;
1415import * as DefectReporter from "./DefectReporter.ts" ;
@@ -17,8 +18,9 @@ class TestRpcs extends RpcGroup.make(
1718 Rpc . make ( "subscribe" , { success : Schema . Number , stream : true } ) ,
1819 Rpc . make ( "boom" , { success : Schema . Void } ) ,
1920) { }
21+ type TestRpc = RpcGroup . Rpcs < typeof TestRpcs > ;
2022
21- /** Runs `body` with the defect reporter installed and every error log captured. */
23+ /** Runs `body` with every error log captured. */
2224const withErrorLogs = < A , E , R > (
2325 body : ( logs : Queue . Queue < Cause . Cause < unknown > > ) => Effect . Effect < A , E , R > ,
2426) =>
@@ -28,9 +30,7 @@ const withErrorLogs = <A, E, R>(
2830 if ( logLevel === "Error" ) Queue . offerUnsafe ( logs , cause ) ;
2931 } ) ;
3032 return yield * body ( logs ) . pipe (
31- Effect . provide (
32- Layer . merge ( DefectReporter . layer , Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ,
33- ) ,
33+ Effect . provide ( Logger . layer ( [ logger ] , { mergeWithExisting : false } ) ) ,
3434 ) ;
3535 } ) ;
3636
@@ -41,83 +41,63 @@ describe("DefectReporter", () => {
4141 withErrorLogs ( ( logs ) =>
4242 Effect . gen ( function * ( ) {
4343 const subscription = yield * Queue . unbounded < number > ( ) ;
44- const responses = yield * Queue . unbounded < RpcMessage . FromServerEncoded > ( ) ;
45- const receive = yield * Deferred . make < Parameters < RpcServer . Protocol [ "Service" ] [ "run" ] > [ 0 ] > ( ) ;
46- const protocol = yield * RpcServer . Protocol . make ( ( write ) =>
47- Effect . gen ( function * ( ) {
48- yield * Deferred . succeed ( receive , write ) ;
49- const serialization = yield * RpcSerialization . RpcSerialization ;
50- return {
51- disconnects : yield * Queue . unbounded < number > ( ) ,
52- send : ( _clientId , response ) => Queue . offer ( responses , response ) ,
53- end : ( ) => Effect . void ,
54- clientIds : Effect . succeed ( new Set ( [ 0 ] ) ) ,
55- initialMessage : Effect . succeedNone ,
56- supportsAck : true ,
57- supportsTransferables : false ,
58- supportsSpanPropagation : false ,
59- supportsNotifications : true ,
60- codecFor : serialization . codecFor ,
61- } ;
62- } ) ,
63- ) ;
64- yield * RpcServer . make ( TestRpcs , WS_RPC_SERVER_OPTIONS ) . pipe (
44+ // The same client/server pairing as RpcTest.makeClient, with ws.ts's options.
45+ // oxlint-disable-next-line prefer-const
46+ let client ! : Effect . Success <
47+ ReturnType < typeof RpcClient . makeNoSerialization < TestRpc , never > >
48+ > ;
49+ const server = yield * RpcServer . makeNoSerialization ( TestRpcs , {
50+ ...WS_RPC_SERVER_OPTIONS ,
51+ onFromServer : ( response ) => client . write ( response ) ,
52+ } ) . pipe (
53+ // Provided to the handlers, as ws.ts does.
6554 Effect . provide (
6655 TestRpcs . toLayer ( {
6756 subscribe : ( ) => Stream . fromQueue ( subscription ) ,
6857 boom : ( ) => Effect . die ( new Error ( "handler bug" ) ) ,
69- } ) ,
58+ } ) . pipe ( Layer . provide ( DefectReporter . layer ) ) ,
7059 ) ,
71- Effect . provideService ( RpcServer . Protocol , protocol ) ,
72- Effect . forkScoped ,
7360 ) ;
74- const write = yield * Deferred . await ( receive ) ;
61+ client = yield * RpcClient . makeNoSerialization ( TestRpcs , {
62+ supportsAck : true ,
63+ onFromClient : ( { message } ) => server . write ( 0 , message ) ,
64+ } ) ;
7565
76- yield * write ( 0 , { _tag : "Request" , id : "1" , tag : "subscribe" , payload : null , headers : [ ] } ) ;
66+ const received = yield * Queue . unbounded < number > ( ) ;
67+ const sibling = yield * client . client . subscribe ( ) . pipe (
68+ Stream . runForEach ( ( value ) => Queue . offer ( received , value ) ) ,
69+ Effect . forkScoped ,
70+ ) ;
7771 yield * Queue . offer ( subscription , 1 ) ;
78- assert . deepEqual ( yield * Queue . take ( responses ) , {
79- _tag : "Chunk" ,
80- requestId : "1" ,
81- values : [ 1 ] ,
82- } ) ;
83- yield * write ( 0 , { _tag : "Ack" , requestId : "1" } ) ;
72+ assert . equal ( yield * Queue . take ( received ) , 1 ) ;
8473
85- yield * write ( 0 , { _tag : "Request" , id : "2" , tag : "boom" , payload : null , headers : [ ] } ) ;
86- const boom = yield * Queue . take ( responses ) ;
87- assert . equal ( boom . _tag , "Exit" ) ;
88- if ( boom . _tag === "Exit" ) {
89- assert . equal ( boom . requestId , "2" ) ;
90- assert . equal ( boom . exit . _tag , "Failure" ) ;
91- }
92- const logged = yield * Queue . take ( logs ) ;
93- assert . isTrue ( Cause . hasDies ( logged ) ) ;
94- assert . equal ( errorMessage ( logged ) , "handler bug" ) ;
74+ const boom = yield * Effect . exit ( client . client . boom ( ) ) ;
75+ assert . isTrue ( Exit . hasDies ( boom ) ) ;
76+ // The server reports before it answers, so the log is already written.
77+ const logged = yield * Queue . clear ( logs ) ;
78+ assert . deepEqual ( logged . map ( errorMessage ) , [ "handler bug" ] ) ;
9579
9680 // The sibling subscription on the same client keeps delivering.
9781 yield * Queue . offer ( subscription , 2 ) ;
98- assert . deepEqual ( yield * Queue . take ( responses ) , {
99- _tag : "Chunk" ,
100- requestId : "1" ,
101- values : [ 2 ] ,
102- } ) ;
82+ const next = yield * Queue . take ( received ) . pipe (
83+ Effect . raceFirst ( Fiber . await ( sibling ) . pipe ( Effect . as ( "subscription ended" ) ) ) ,
84+ ) ;
85+ assert . equal ( next , 2 ) ;
10386 assert . equal ( yield * Queue . size ( logs ) , 0 ) ;
10487 } ) ,
105- ) . pipe ( Effect . provide ( RpcSerialization . layerJson ) , Effect . scoped ) ,
88+ ) . pipe ( Effect . scoped ) ,
10689 ) ;
10790
108- it . effect ( "does not log typed failures, or a typed failure rethrown as a defect " , ( ) =>
91+ it . effect ( "logs defects, not typed failures or interrupts " , ( ) =>
10992 withErrorLogs ( ( logs ) =>
11093 Effect . gen ( function * ( ) {
111- // HttpApi reports a schema failure, then rethrows the same error as a defect.
112- const badRequest = new Error ( "bad request" ) ;
113- yield * ErrorReporter . report ( Cause . fail ( badRequest ) ) ;
114- yield * ErrorReporter . report ( Cause . die ( badRequest ) ) ;
11594 yield * ErrorReporter . report ( Cause . fail ( new Error ( "expected" ) ) ) ;
95+ yield * ErrorReporter . report ( Cause . interrupt ( ) ) ;
11696 yield * ErrorReporter . report ( Cause . die ( new Error ( "bug" ) ) ) ;
11797
11898 assert . equal ( errorMessage ( yield * Queue . take ( logs ) ) , "bug" ) ;
11999 assert . equal ( yield * Queue . size ( logs ) , 0 ) ;
120- } ) ,
100+ } ) . pipe ( Effect . provide ( DefectReporter . layer ) ) ,
121101 ) ,
122102 ) ;
123103} ) ;
0 commit comments