@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
22import * as Electron from "electron" ;
33import { MacPermissionHelper , macAppBundlePath } from "./MacPermissionHelper.ts" ;
44import type { SettingsWindow } from "./MacSettingsWindow.ts" ;
5- import { SNAP_SHOT_PERMISSION_HELPER_CHANNEL } from "../ipc/channels.ts" ;
5+ import { MAC_PERMISSION_HELPER_CHANNEL } from "../ipc/channels.ts" ;
66
77const mocks = vi . hoisted ( ( ) => ( {
88 granted : false ,
@@ -109,11 +109,11 @@ afterEach(() => {
109109} ) ;
110110const iconPaths = [ "/bundle/prod-resources/icon.png" ] ;
111111const open = ( ) =>
112- helper . show ( "accessibility" , "/bundle/snapshot -permission-preload.cjs" , null , iconPaths ) ;
112+ helper . show ( "accessibility" , "/bundle/mac -permission-preload.cjs" , null , iconPaths ) ;
113113function send ( action : string , trusted = true ) {
114114 const window = windows . at ( - 1 ) ! ;
115115 Electron . ipcMain . emit (
116- SNAP_SHOT_PERMISSION_HELPER_CHANNEL ,
116+ MAC_PERMISSION_HELPER_CHANNEL ,
117117 {
118118 sender : trusted ? window . webContents : { } ,
119119 senderFrame : window . webContents . mainFrame ,
@@ -147,16 +147,16 @@ it("drags the running app bundle only for the helper's own renderer", async () =
147147it ( "rechecks permissions and releases resources when granted" , async ( ) => {
148148 await open ( ) ;
149149 mocks . granted = true ;
150- vi . advanceTimersByTime ( 1000 ) ;
150+ await vi . advanceTimersByTimeAsync ( 1000 ) ;
151151 expect ( windows [ 0 ] ! . destroyed ) . toBe ( true ) ;
152- expect ( Electron . ipcMain . listenerCount ( SNAP_SHOT_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
152+ expect ( Electron . ipcMain . listenerCount ( MAC_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
153153 expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
154154} ) ;
155155it ( "keeps only one helper and cleans up on dismissal" , async ( ) => {
156156 await open ( ) ;
157157 await helper . show ( "screen-recording" , "/preload.cjs" , null , iconPaths ) ;
158158 expect ( windows [ 0 ] ! . destroyed ) . toBe ( true ) ;
159- expect ( Electron . ipcMain . listenerCount ( SNAP_SHOT_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 1 ) ;
159+ expect ( Electron . ipcMain . listenerCount ( MAC_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 1 ) ;
160160 send ( "close" ) ;
161161 expect ( windows [ 1 ] ! . destroyed ) . toBe ( true ) ;
162162 expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
@@ -174,7 +174,7 @@ it("does not show a helper with a missing packaged icon", async () => {
174174it ( "cleans up when the helper page fails to load" , async ( ) => {
175175 mocks . loadURL . mockRejectedValueOnce ( new Error ( "load failed" ) ) ;
176176 await expect ( open ( ) ) . rejects . toThrow ( "load failed" ) ;
177- expect ( Electron . ipcMain . listenerCount ( SNAP_SHOT_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
177+ expect ( Electron . ipcMain . listenerCount ( MAC_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
178178 expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
179179} ) ;
180180
@@ -192,7 +192,7 @@ it("returns focus to onboarding when the permission is granted", async () => {
192192 const owner = new Electron . BrowserWindow ( { } ) ;
193193 await helper . show ( "screen-recording" , "/preload.cjs" , owner , iconPaths ) ;
194194 mocks . granted = true ;
195- vi . advanceTimersByTime ( 1000 ) ;
195+ await vi . advanceTimersByTimeAsync ( 1000 ) ;
196196 expect ( owner . show ) . toHaveBeenCalledOnce ( ) ;
197197 expect ( owner . focus ) . toHaveBeenCalledOnce ( ) ;
198198 expect ( windows [ 1 ] ! . destroyed ) . toBe ( true ) ;
@@ -203,7 +203,7 @@ it("closes the helper and stops checking when onboarding's window closes", async
203203 await helper . show ( "accessibility" , "/preload.cjs" , owner , iconPaths ) ;
204204 owner . destroy ( ) ;
205205 expect ( windows [ 1 ] ! . destroyed ) . toBe ( true ) ;
206- expect ( Electron . ipcMain . listenerCount ( SNAP_SHOT_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
206+ expect ( Electron . ipcMain . listenerCount ( MAC_PERMISSION_HELPER_CHANNEL ) ) . toBe ( 0 ) ;
207207 expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
208208} ) ;
209209
@@ -257,3 +257,58 @@ it("hides on tracking failure and resumes on a valid update", async () => {
257257 mocks . settingsChanged ! ( state ) ;
258258 expect ( windows [ 0 ] ! . showInactive ) . toHaveBeenCalledTimes ( 2 ) ;
259259} ) ;
260+
261+ it ( "waits for an asynchronous Full Disk Access check and returns to the owner" , async ( ) => {
262+ const owner = new Electron . BrowserWindow ( ) ;
263+ const probe = vi . fn < ( ) => Promise < boolean > > ( ) . mockResolvedValue ( false ) ;
264+ await helper . show (
265+ "full-disk-access" ,
266+ "/bundle/mac-permission-preload.cjs" ,
267+ owner ,
268+ iconPaths ,
269+ probe ,
270+ ) ;
271+ const pending = Promise . withResolvers < boolean > ( ) ;
272+ probe . mockReturnValue ( pending . promise ) ;
273+ await vi . advanceTimersByTimeAsync ( 3000 ) ;
274+ expect ( probe ) . toHaveBeenCalledTimes ( 2 ) ;
275+ expect ( windows [ 1 ] ! . destroyed ) . toBe ( false ) ;
276+ pending . resolve ( true ) ;
277+ await pending . promise ;
278+ expect ( windows [ 1 ] ! . destroyed ) . toBe ( true ) ;
279+ expect ( owner . focus ) . toHaveBeenCalledOnce ( ) ;
280+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
281+ } ) ;
282+
283+ it ( "keeps the helper open after a failed access check and retries" , async ( ) => {
284+ const probe = vi . fn < ( ) => Promise < boolean > > ( ) . mockResolvedValue ( false ) ;
285+ await helper . show (
286+ "full-disk-access" ,
287+ "/bundle/mac-permission-preload.cjs" ,
288+ null ,
289+ iconPaths ,
290+ probe ,
291+ ) ;
292+ probe . mockRejectedValueOnce ( new Error ( "temporarily unavailable" ) ) ;
293+ await vi . advanceTimersByTimeAsync ( 1000 ) ;
294+ expect ( windows [ 0 ] ! . destroyed ) . toBe ( false ) ;
295+ probe . mockResolvedValue ( true ) ;
296+ await vi . advanceTimersByTimeAsync ( 1000 ) ;
297+ expect ( windows [ 0 ] ! . destroyed ) . toBe ( true ) ;
298+ } ) ;
299+
300+ it ( "does not reopen a superseded helper when its initial probe completes" , async ( ) => {
301+ const pending = Promise . withResolvers < boolean > ( ) ;
302+ const first = helper . show (
303+ "full-disk-access" ,
304+ "/bundle/mac-permission-preload.cjs" ,
305+ null ,
306+ iconPaths ,
307+ ( ) => pending . promise ,
308+ ) ;
309+ await open ( ) ;
310+ pending . resolve ( false ) ;
311+ await first ;
312+ expect ( windows ) . toHaveLength ( 1 ) ;
313+ expect ( windows [ 0 ] ! . destroyed ) . toBe ( false ) ;
314+ } ) ;
0 commit comments