1+ // @effect -diagnostics nodeBuiltinImport:off -- Build real electron-updater disk fixtures with its SHA-512 encoding.
2+ import * as NodeServices from "@effect/platform-node/NodeServices" ;
3+ import * as NodeCrypto from "node:crypto" ;
4+ import * as NodeEvents from "node:events" ;
5+ import * as NodeModule from "node:module" ;
6+ import * as NodePath from "node:path" ;
7+ import * as Deferred from "effect/Deferred" ;
8+ import * as FileSystem from "effect/FileSystem" ;
9+ import * as Option from "effect/Option" ;
10+ import { AppImageUpdater } from "electron-updater/out/AppImageUpdater.js" ;
11+ import { DebUpdater } from "electron-updater/out/DebUpdater.js" ;
12+ import { MacUpdater } from "electron-updater/out/MacUpdater.js" ;
13+ import { GenericProvider } from "electron-updater/out/providers/GenericProvider.js" ;
14+ import type { UpdateDownloadedEvent } from "electron-updater/out/types.js" ;
15+ import { NsisUpdater } from "electron-updater/out/NsisUpdater.js" ;
16+ import * as DesktopUpdates from "../updates/DesktopUpdates.ts" ;
17+ import { flushCallbacks , makeHarness } from "../updates/updatesTestHarness.ts" ;
118import { assert , describe , it } from "@effect/vitest" ;
219import * as Effect from "effect/Effect" ;
320import type { VerifyUpdateCodeSignature } from "electron-updater" ;
421import { beforeEach , vi } from "vite-plus/test" ;
522
6- const { autoUpdaterMock } = vi . hoisted ( ( ) => ( {
23+ const { autoUpdaterMock, updaterOverride } = vi . hoisted ( ( ) => ( {
24+ updaterOverride : { current : undefined as import ( "electron-updater" ) . AppUpdater | undefined } ,
725 autoUpdaterMock : {
826 allowDowngrade : false ,
927 allowPrerelease : false ,
@@ -24,7 +42,9 @@ const { autoUpdaterMock } = vi.hoisted(() => ({
2442} ) ) ;
2543
2644vi . mock ( "electron-updater" , ( ) => ( {
27- autoUpdater : autoUpdaterMock ,
45+ get autoUpdater ( ) {
46+ return updaterOverride . current ?? autoUpdaterMock ;
47+ } ,
2848} ) ) ;
2949
3050import * as ElectronUpdater from "./ElectronUpdater.ts" ;
@@ -221,3 +241,205 @@ describe("ElectronUpdater", () => {
221241 ) ;
222242 } ) ;
223243} ) ;
244+
245+ describe ( "installed electron-updater cache recovery" , ( ) => {
246+ it . effect . each ( [
247+ {
248+ name : "Intel Mac" ,
249+ platform : "darwin" ,
250+ Updater : MacUpdater ,
251+ selected : "x64.zip" ,
252+ other : "arm64.zip" ,
253+ rosetta : false ,
254+ } ,
255+ {
256+ name : "Apple Silicon running Intel" ,
257+ platform : "darwin" ,
258+ Updater : MacUpdater ,
259+ selected : "arm64.zip" ,
260+ other : "x64.zip" ,
261+ rosetta : true ,
262+ } ,
263+ {
264+ name : "Windows" ,
265+ platform : "win32" ,
266+ Updater : NsisUpdater ,
267+ selected : "x64.exe" ,
268+ other : "arm64.exe" ,
269+ rosetta : false ,
270+ } ,
271+ {
272+ name : "Debian" ,
273+ platform : "linux" ,
274+ Updater : DebUpdater ,
275+ selected : "x64.deb" ,
276+ other : "x64.AppImage" ,
277+ rosetta : false ,
278+ } ,
279+ {
280+ name : "AppImage" ,
281+ platform : "linux" ,
282+ Updater : AppImageUpdater ,
283+ selected : "x64.AppImage" ,
284+ other : "x64.deb" ,
285+ rosetta : false ,
286+ } ,
287+ ] as const ) ( "restores only the package selected by $name" , ( scenario ) =>
288+ Effect . scoped (
289+ Effect . gen ( function * ( ) {
290+ const fs = yield * FileSystem . FileSystem ;
291+ const directory = yield * fs . makeTempDirectoryScoped ( ) ;
292+ const cacheDirectory = NodePath . join ( directory , "custom-updater-cache" , "pending" ) ;
293+ const configPath = NodePath . join ( directory , "app-update.yml" ) ;
294+ yield * fs . writeFileString ( configPath , "updaterCacheDirName: custom-updater-cache\n" ) ;
295+ yield * fs . makeDirectory ( cacheDirectory , { recursive : true } ) ;
296+
297+ const require = NodeModule . createRequire ( import . meta. url ) ;
298+ require ( "electron" ) ;
299+ const electronModule = require . cache [ require . resolve ( "electron" ) ] ! ;
300+ const childProcess = require ( "node:child_process" ) as typeof import ( "node:child_process" ) ;
301+ const originalElectron = electronModule . exports ;
302+ const originalArch = Object . getOwnPropertyDescriptor ( process , "arch" ) ! ;
303+ yield * Effect . acquireRelease (
304+ Effect . sync ( ( ) => {
305+ // MacUpdater's native Squirrel bridge and host probes are OS boundaries.
306+ electronModule . exports = { autoUpdater : new NodeEvents . EventEmitter ( ) } ;
307+ Object . defineProperty ( process , "arch" , { value : "x64" , configurable : true } ) ;
308+ return vi
309+ . spyOn ( childProcess , "execFileSync" )
310+ . mockReturnValue ( scenario . rosetta ? "sysctl.proc_translated: 1" : "x86_64" ) ;
311+ } ) ,
312+ ( probe ) =>
313+ Effect . sync ( ( ) => {
314+ probe . mockRestore ( ) ;
315+ Object . defineProperty ( process , "arch" , originalArch ) ;
316+ electronModule . exports = originalElectron ;
317+ updaterOverride . current = undefined ;
318+ } ) ,
319+ ) ;
320+
321+ const extensions =
322+ scenario . platform === "darwin"
323+ ? [ "arm64.dmg" , "x64.dmg" , "arm64.zip" , "x64.zip" ]
324+ : scenario . platform === "win32"
325+ ? [ "arm64.exe" , "x64.exe" ]
326+ : [ "arm64.AppImage" , "x64.AppImage" , "arm64.deb" , "x64.deb" ] ;
327+ const files = extensions . map ( ( extension ) => {
328+ const url = `T3-Code-1.2.4-${ extension } ` ;
329+ return { url, sha512 : NodeCrypto . createHash ( "sha512" ) . update ( url ) . digest ( "base64" ) } ;
330+ } ) ;
331+ const info = {
332+ version : "1.2.4" ,
333+ releaseDate : "2026-10-01T00:00:00Z" ,
334+ files,
335+ path : files [ 0 ] ! . url ,
336+ sha512 : files [ 0 ] ! . sha512 ,
337+ } ;
338+ yield * Effect . acquireRelease (
339+ Effect . sync ( ( ) =>
340+ vi . spyOn ( GenericProvider . prototype , "getLatestVersion" ) . mockResolvedValue ( info ) ,
341+ ) ,
342+ ( feed ) => Effect . sync ( ( ) => feed . mockRestore ( ) ) ,
343+ ) ;
344+
345+ // A fresh instance for each launch forces disk validation rather than in-memory reuse.
346+ yield * Effect . forEach ( [ scenario . other , scenario . selected ] , ( cachedExtension ) =>
347+ Effect . scoped (
348+ Effect . gen ( function * ( ) {
349+ const cached = files . find ( ( file ) => file . url . endsWith ( `-${ cachedExtension } ` ) ) ! ;
350+ yield * fs . writeFileString ( NodePath . join ( cacheDirectory , cached . url ) , cached . url ) ;
351+ yield * fs . writeFileString (
352+ NodePath . join ( cacheDirectory , "update-info.json" ) ,
353+ JSON . stringify ( {
354+ fileName : cached . url ,
355+ sha512 : cached . sha512 ,
356+ isAdminRightsRequired : false ,
357+ } ) ,
358+ ) ;
359+ const updater = new scenario . Updater ( undefined , {
360+ version : "1.2.3" ,
361+ name : "T3 Code" ,
362+ isPackaged : true ,
363+ appUpdateConfigPath : configPath ,
364+ userDataPath : directory ,
365+ baseCachePath : directory ,
366+ whenReady : ( ) => Promise . resolve ( ) ,
367+ relaunch : vi . fn ( ) ,
368+ quit : vi . fn ( ) ,
369+ onQuit : vi . fn ( ) ,
370+ } ) ;
371+ updater . logger = null ;
372+ const networkDownload = vi . fn ( ( ) =>
373+ Promise . reject ( new Error ( "unexpected network download" ) ) ,
374+ ) ;
375+ Object . assign ( updater , { httpExecutor : { download : networkDownload } } ) ;
376+ if ( updater instanceof MacUpdater ) {
377+ // Keep real selection, executeDownload, and hash validation; stop at native handoff.
378+ Object . assign ( updater , {
379+ updateDownloaded : async ( _file : unknown , event : UpdateDownloadedEvent ) => {
380+ updater . emit ( "update-downloaded" , event ) ;
381+ return [ ] ;
382+ } ,
383+ } ) ;
384+ }
385+ updaterOverride . current = updater ;
386+ assert . deepEqual (
387+ yield * ElectronUpdater . make . pendingDownloadDirectory ,
388+ Option . some ( cacheDirectory ) ,
389+ ) ;
390+
391+ const inspected = Deferred . makeUnsafe < void > ( ) ;
392+ const downloadFinished = Deferred . makeUnsafe < void > ( ) ;
393+ const download = vi . spyOn ( updater , "downloadUpdate" ) ;
394+ const harness = makeHarness ( {
395+ platform : scenario . platform ,
396+ runningUnderArm64Translation : scenario . rosetta ,
397+ env : scenario . selected . endsWith ( "AppImage" )
398+ ? { APPIMAGE : "/test/T3-Code.AppImage" }
399+ : { } ,
400+ updater : {
401+ ...ElectronUpdater . make ,
402+ downloadUpdate : ElectronUpdater . make . downloadUpdate . pipe (
403+ Effect . ensuring ( Deferred . succeed ( downloadFinished , undefined ) ) ,
404+ ) ,
405+ } ,
406+ fileSystem : {
407+ ...fs ,
408+ readFileString : ( path , encoding ) =>
409+ path === "/missing/resources/package-type" && scenario . selected . endsWith ( "deb" )
410+ ? Effect . succeed ( "deb" )
411+ : fs . readFileString ( path , encoding ) ,
412+ exists : ( path ) =>
413+ fs . exists ( path ) . pipe ( Effect . tap ( ( ) => Deferred . succeed ( inspected , undefined ) ) ) ,
414+ } ,
415+ } ) ;
416+ yield * Effect . scoped (
417+ Effect . gen ( function * ( ) {
418+ const updates = yield * DesktopUpdates . DesktopUpdates ;
419+ yield * updates . configure ;
420+ // AppImage activation reads the host environment; the fixture is intentionally not installed.
421+ vi . spyOn ( updater , "isUpdaterActive" ) . mockReturnValue ( true ) ;
422+ assert . isTrue ( ( yield * updates . check ( "startup" ) ) . checked ) ;
423+ yield * Deferred . await ( inspected ) ;
424+ yield * flushCallbacks ;
425+
426+ if ( cachedExtension === scenario . other ) {
427+ assert . equal ( download . mock . calls . length , 0 ) ;
428+ assert . equal ( ( yield * updates . getState ) . status , "available" ) ;
429+ } else {
430+ yield * Deferred . await ( downloadFinished ) ;
431+ yield * flushCallbacks ;
432+ assert . equal ( download . mock . calls . length , 1 ) ;
433+ assert . equal ( ( yield * updates . getState ) . status , "downloaded" ) ;
434+ assert . equal ( ( yield * updates . getState ) . downloadedVersion , "1.2.4" ) ;
435+ }
436+ assert . equal ( networkDownload . mock . calls . length , 0 ) ;
437+ } ) ,
438+ ) . pipe ( Effect . provide ( harness . layer ) ) ;
439+ } ) ,
440+ ) ,
441+ ) ;
442+ } ) ,
443+ ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
444+ ) ;
445+ } ) ;
0 commit comments