@@ -14,6 +14,7 @@ import * as Layer from "effect/Layer";
1414import * as Schema from "effect/Schema" ;
1515import * as HttpClient from "effect/http/HttpClient" ;
1616import * as HttpClientResponse from "effect/http/HttpClientResponse" ;
17+ import * as TestClock from "effect/testing/TestClock" ;
1718import { AppImageUpdater } from "electron-updater" ;
1819import * as DesktopConfig from "../app/DesktopConfig.ts" ;
1920import * as DesktopEnvironment from "../app/DesktopEnvironment.ts" ;
@@ -130,7 +131,8 @@ function realUpdaterFixture() {
130131}
131132
132133// Passing `logExports` turns on OTLP logs and keeps each exported body there.
133- function traceLayer ( root : string , logExports ?: Array < string > ) {
134+ // The collector takes every export, and never replies while `answering` is false.
135+ function traceLayer ( root : string , logExports ?: Array < string > , answering = ( ) => true ) {
134136 const otlpLogs = logExports !== undefined ;
135137 const environment = DesktopEnvironment . layer ( {
136138 dirname : "/repo/apps/desktop/dist-electron" ,
@@ -161,11 +163,15 @@ function traceLayer(root: string, logExports?: Array<string>) {
161163 ? Layer . succeed (
162164 HttpClient . HttpClient ,
163165 HttpClient . make ( ( request ) =>
164- Effect . sync ( ( ) => {
166+ Effect . suspend ( ( ) => {
165167 if ( request . body . _tag === "Uint8Array" ) {
166168 logExports . push ( new TextDecoder ( ) . decode ( request . body . body ) ) ;
167169 }
168- return HttpClientResponse . fromWeb ( request , new Response ( null , { status : 200 } ) ) ;
170+ return answering ( )
171+ ? Effect . succeed (
172+ HttpClientResponse . fromWeb ( request , new Response ( null , { status : 200 } ) ) ,
173+ )
174+ : Effect . never ;
169175 } ) ,
170176 ) ,
171177 )
@@ -249,13 +255,19 @@ describe("ElectronUpdater", () => {
249255
250256 // Runs the real install with the explicit-quit flags, then the adapter quit.
251257 // The dependency's intervening Electron event needs a packaged app.
252- // Returns the trace file as it was when the original quit ran.
253- const traceAtQuit = ( input : ReturnType < typeof realUpdaterFixture > ) =>
258+ // Returns the trace file as it was when the original quit ran, and the
259+ // OTLP log exports as they were when the update launched. Production
260+ // closes no scope before either, so neither does this.
261+ const traceAtQuit = (
262+ input : ReturnType < typeof realUpdaterFixture > ,
263+ logExports : ReadonlyArray < string > = [ ] ,
264+ ) =>
254265 Effect . gen ( function * ( ) {
255266 const updater = yield * ElectronUpdater . ElectronUpdater ;
256267 const environment = yield * DesktopEnvironment . DesktopEnvironment ;
257268 const originalQuit = input . adapter . quit ;
258269 let trace = "" ;
270+ let exported : ReadonlyArray < string > = [ ] ;
259271 let finish = ( ) => { } ;
260272 const quit = new Promise < void > ( ( resolve ) => {
261273 finish = resolve ;
@@ -265,13 +277,16 @@ describe("ElectronUpdater", () => {
265277 trace = NodeFS . existsSync ( tracePath ) ? NodeFS . readFileSync ( tracePath , "utf8" ) : "" ;
266278 finish ( ) ;
267279 } ) ;
268- vi . spyOn ( input . updater , "spawnLog" ) . mockResolvedValue ( true ) ;
280+ vi . spyOn ( input . updater , "spawnLog" ) . mockImplementation ( ( ) => {
281+ exported = [ ...logExports ] ;
282+ return Promise . resolve ( true ) ;
283+ } ) ;
269284 yield * updater . setAutoDownload ( false ) ;
270285 assert . isTrue ( input . updater . install ( true , true ) ) ;
271286 input . adapter . quit ( ) ;
272287 yield * Effect . promise ( ( ) => quit ) ;
273288 assert . equal ( originalQuit . mock . calls . length , 1 ) ;
274- return trace ;
289+ return { trace, exported } ;
275290 } ) ;
276291
277292 it . effect . each ( [ false , true ] ) (
@@ -280,8 +295,7 @@ describe("ElectronUpdater", () => {
280295 const input = realUpdaterFixture ( ) ;
281296 const logExports : Array < string > = [ ] ;
282297 return Effect . gen ( function * ( ) {
283- // Closing the layer scope drains the OTLP log exporter.
284- const persisted = yield * traceAtQuit ( input ) . pipe (
298+ const { trace : persisted , exported } = yield * traceAtQuit ( input , logExports ) . pipe (
285299 Effect . provide ( traceLayer ( input . root , otlpLogs ? logExports : undefined ) ) ,
286300 ) ;
287301 const spans = updaterSpans ( persisted ) ;
@@ -306,7 +320,7 @@ describe("ElectronUpdater", () => {
306320 ) ;
307321 assert . equal ( persisted . includes ( record ) , ! otlpLogs ) ;
308322 assert . equal (
309- logExports . some ( ( body ) => body . includes ( record ) ) ,
323+ exported . some ( ( body ) => body . includes ( record ) ) ,
310324 otlpLogs ,
311325 ) ;
312326 }
@@ -430,6 +444,54 @@ describe("ElectronUpdater", () => {
430444 ) ,
431445 ) ;
432446 } ) ;
447+
448+ it . effect ( "waits a bounded time for a log collector that never answers" , ( ) => {
449+ const input = realUpdaterFixture ( ) ;
450+ const logExports : Array < string > = [ ] ;
451+ const steps : Array < string > = [ ] ;
452+ let answering = false ;
453+ vi . spyOn ( input . updater , "spawnLog" ) . mockImplementation ( ( ) => {
454+ steps . push ( "launch" ) ;
455+ return Promise . resolve ( true ) ;
456+ } ) ;
457+ input . adapter . quit . mockImplementation ( ( ) => {
458+ steps . push ( "quit" ) ;
459+ } ) ;
460+ return Effect . gen ( function * ( ) {
461+ const updater = yield * ElectronUpdater . ElectronUpdater ;
462+ const environment = yield * DesktopEnvironment . DesktopEnvironment ;
463+ const tracePath = NodePath . join ( environment . logDir , "desktop.trace.ndjson" ) ;
464+ yield * updater . setAutoDownload ( false ) ;
465+ assert . isTrue ( input . updater . install ( true , true ) ) ;
466+ input . adapter . quit ( ) ;
467+ // The test clock holds the bound, so the file write and the export
468+ // are awaited in real time.
469+ yield * Effect . promise ( ( ) =>
470+ vi . waitFor (
471+ ( ) => {
472+ assert . include ( NodeFS . readFileSync ( tracePath , "utf8" ) , "desktop.updater" ) ;
473+ assert . lengthOf ( logExports , 1 ) ;
474+ } ,
475+ { timeout : 5_000 } ,
476+ ) ,
477+ ) ;
478+ yield * Effect . promise ( ( ) => new Promise ( ( resolve ) => setImmediate ( resolve ) ) ) ;
479+ assert . include ( logExports [ 0 ] , "AppImage update committed" ) ;
480+ assert . deepEqual ( steps , [ ] ) ;
481+ yield * TestClock . adjust ( "2 seconds" ) ;
482+ yield * Effect . promise ( ( ) =>
483+ vi . waitFor ( ( ) => assert . deepEqual ( steps , [ "launch" , "quit" ] ) , { timeout : 5_000 } ) ,
484+ ) ;
485+ } ) . pipe (
486+ // The scope's own final export must not wait on the test clock.
487+ Effect . ensuring (
488+ Effect . sync ( ( ) => {
489+ answering = true ;
490+ } ) ,
491+ ) ,
492+ Effect . provide ( traceLayer ( input . root , logExports , ( ) => answering ) ) ,
493+ ) ;
494+ } ) ;
433495 } ) ;
434496
435497 it . effect ( "scopes updater event listeners" , ( ) =>
0 commit comments